1. 程式人生 > >cJSON的使用介紹及例項分析

cJSON的使用介紹及例項分析

使用意圖:
用於方便實現包含充電資訊的結構體輸入到檔案中,然後需要的時候,從檔案中以充電資訊的結構體的形式輸出;

簡要介紹:
cJSON的使用,網上有很多的資料,主要就是兩個檔案,一個頭檔案cJSON.h,一個應用檔案cJSON.c,前者主要是對cJSON.c的一些函式的宣告,我們在自己的應用程式中,比如main.c中,包含cJSON.h,以及在編譯的時候,將cJSON.c和main.c一起編譯,即:

gcc main.c cJSON.c -g -lm -o main

這裡我用的是:

gcc cJSON.c test.c bx_deal.c -g -lm -o bx_deal

注:test.c中寫入的main函式(c語言編譯入口),其中用到的cJSON的應用函式都在bx_deal.c中,而bx_deal.c中的函式中用到的cJSON函式都在cJSON.c中,這樣做的目的是,用bx_deal.c作為中間檔案,能夠在其中傳參的時候,根據我們的需求以及實際使用,更具靈活性,像這裡,幾個函式:
SK_API int bx_deal_init(const char *p_fileName);

/// @brief 模組反初始化
SK_API int bx_deal_quit();

/// @brief 按下標讀取一條資訊
/// @param [in] seq: 序號
/// @param [out] p_dealInfo: 儲存交易記錄的結構體
/// @return 0:成功 非0:失敗
/// 將結果存到一個結構體中,即*p_dealInfo;
SK_API int bx_read_dealInfo(unsigned short seq,deal_info_t *p_dealInfo);

/// @brief 按下標讀取一條資訊
/// @param [in] seq: 序號
/// @param [in] p_dealInfo: 交易記錄的結構體
/// @return 0:成功 非0:失敗

SK_API int bx_write_dealInfo(deal_info_t *p_dealInfo);

/// @brief 獲取最新的交易記錄下標
/// @return 最新的交易記錄下標
SK_API unsigned short bx_get_max_index();

這幾個函式方便我們處理自己傳遞的資料資訊(這裡主要是以將充電資訊以結構體的資訊輸出),最後實現的就是將我們的充電資訊(結構體,在檔案中pub_type.h有定義),從傳進來的結構體讀入檔案中,然後在需要顯示的時候,從檔案提取出來,或者讀出來;

程式原始碼分析:
程式梳理:
所有程式
這裡主要是由:
cJSON的介面函式檔案 - cJSON.c和cJSON.h;
主要包含充電資訊結構體的標頭檔案 - pub_type.h
pub_type.h
主要包含充電資訊結構體等的標頭檔案baoxin_def.h:
baoxin_def.h
test.c就是我們的標頭檔案,裡面寫好了main函式,使用bx_deal.c中的一些使用者函式,然後這些使用者函式又套用了cJSON.c中的一些標頭檔案;
所以我們在編譯的時候需要:

gcc test.c cJSON.c bx_deal.c -g -lm -o bx_deal

注:會生成一個文字檔案,chargeinfo.txt;

程式聯絡:
test.c中用了bx_deal.c中的函式,bx_deal.c中又套用了cJSON.c中的函式;
test.c中用到了baoxin_def.h中充電資訊結構體,而這個結構體中用到了pub_type.h的SK_TIME結構體成員;
bx_deal.h和CJSON.h分別是bx_deal.c和CJSON.c裡面是對他們的標頭檔案中的函式的宣告;
chargeinfo.txt是這個程式執行生成的;

程式分析:
下面給出除了cJSON.h和cJSON.c以外的標頭檔案,這兩個檔案可以在網上找到,給出了bx_deal.c,bx_deal.h,test.c,pub_type.h,baoxin_def.h等五個檔案,共七個檔案;以及編譯,執行程式生成的文字檔案chargeinfo.txt;

bx_deal.c檔案:

#include "baoxin_def.h"
#include "bx_deal.h"
#include <stdio.h>
#include "cJSON.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>

//const  char *g_dealFile  = "usr/data/deal.json";
//const char *g_dealFile = "/mnt/hgfs/share_for_linux/cJSON_lib_use/chargeinfo.txt";


const char START_TIME[] =   "start_time";
const char STOP_TIME[]  =   "stop_time";
const char CHARGE_TIME[]    =   "charge_time";
const char TOTAL_KWH[]  =   "total_kwh";
const char SEQ[]        =   "seq";

typedef struct bx_dealInfo_t 
{
    char *          p_text;
    cJSON *         p_deal;

    int             buf_len;
    int             max_num;

    char                path[128];
}bx_dealInfo_t;



const int MAX_BUF_SIZE = 16 * 1024;
const int MAX_DEAL_NUM   = 100;
static bx_dealInfo_t *g_p_dealInfo = NULL;



static void event_write()
{
    assert(NULL != g_p_dealInfo->p_text);

    //
    while (g_p_dealInfo->buf_len < strlen(g_p_dealInfo->p_text))
    {
        free(g_p_dealInfo->p_text);
        g_p_dealInfo->p_text = NULL;

        //刪除最老的記錄
        int count = cJSON_GetArraySize(g_p_dealInfo->p_deal);
        if (g_p_dealInfo->max_num < count)
            cJSON_DeleteItemFromArray(g_p_dealInfo->p_deal, count - 1);

        g_p_dealInfo->p_text = cJSON_PrintUnformatted(g_p_dealInfo->p_deal);
    }

    assert(NULL != g_p_dealInfo->p_text);

    //重新整理到檔案
    FILE *p_file = fopen(g_p_dealInfo->path, "w");
    assert(NULL != p_file);

    int len = fwrite(g_p_dealInfo->p_text, 1, strlen(g_p_dealInfo->p_text), p_file);
    assert(len == strlen(g_p_dealInfo->p_text));

    fclose(p_file);
}


int  bx_dealInfo_init(const char *p_path)
{

    assert(NULL != p_path);
    assert(NULL == g_p_dealInfo);

    g_p_dealInfo = malloc(sizeof(bx_dealInfo_t));
    memset(g_p_dealInfo, 0, sizeof(bx_dealInfo_t));

    g_p_dealInfo->buf_len   =   MAX_BUF_SIZE ;
    g_p_dealInfo->max_num =  MAX_DEAL_NUM ;

    FILE *p_file = fopen(p_path, "r");
    if (NULL == p_file)
    {
        g_p_dealInfo->p_text = NULL;
        //若沒有檔案,則建立JSON控制代碼
        g_p_dealInfo->p_deal = cJSON_CreateArray();
    }
    else
    {
        g_p_dealInfo->p_text = (char*)malloc(g_p_dealInfo->buf_len + 2048);
        memset(g_p_dealInfo->p_text, 0, g_p_dealInfo->buf_len + 2048);

        //讀取檔案內容初始化JSON控制代碼
        int len = fread(g_p_dealInfo->p_text, 1, g_p_dealInfo->buf_len, p_file);
        fclose(p_file);
        assert(0 < len);
        //解析一個物件,並將其返回值給p_deal
        g_p_dealInfo->p_deal = cJSON_Parse(g_p_dealInfo->p_text);
        assert(cJSON_Array == g_p_dealInfo->p_deal->type);
    }

    //g_p_dealInfo->p_mutex = sys_mutex_create();

    //記錄檔案路徑

    strcpy(g_p_dealInfo->path, p_path);

   return 0;
   }

 int bx_deal_quit()
   {
    assert(NULL != g_p_dealInfo);
    assert(NULL != g_p_dealInfo->p_deal);

    g_p_dealInfo->p_deal = NULL;   //
    if (NULL != g_p_dealInfo->p_text)
    {
        //重新整理到檔案
        event_write();
        free(g_p_dealInfo->p_text);
    }
    cJSON_Delete(g_p_dealInfo->p_deal);

    free(g_p_dealInfo);
    g_p_dealInfo = NULL;

    return 0;
 }

  /// @brief   按下標讀取一條資訊
  /// @param [in]       seq: 序號
  /// @param [out]      p_dealInfo: 儲存交易記錄的結構體
  /// @return  0:成功 非0:失敗
  // int bx_read_dealInfo(unsigned  int seq,  deal_info_t  *p_dealInfo)

 int bx_read_dealInfo(unsigned  short seq,deal_info_t  *p_dealInfo)
  {
    printf("+++++bx_read_dealInfo++++\n");
    assert(NULL != g_p_dealInfo);
    char *out = NULL;
    cJSON *pArrayItem = NULL;
    //獲取陣列的節點數量size
    int count = cJSON_GetArraySize(g_p_dealInfo->p_deal);

    pArrayItem = cJSON_GetArrayItem(g_p_dealInfo->p_deal, count-seq);

    printf("***********seq = %d*********\n",seq);
        //將pArrayItem的值以字串的形式列印到char型buffer上,cJSON_Print()會自動分配記憶體空間,用完需要釋放記憶體。   
    out = cJSON_Print( pArrayItem );        

    printf("%s\n",out);

    //指定格式: 2016/08/30 18:19:30
    //start_time
    printf("++++++++++++++++++++\n");

    cJSON* start_time_temp = cJSON_GetObjectItem(pArrayItem, START_TIME);   
    sscanf(start_time_temp->valuestring, "%d/%d/%d %d:%d:%d\n",     \
        &(p_dealInfo->start_time.year),&(p_dealInfo->start_time.mon),&(p_dealInfo->start_time.mday),   \
        &(p_dealInfo->start_time.hour),&(p_dealInfo->start_time.min),&(p_dealInfo->start_time.sec));

    printf("++++p_dealInfo->start_time.year = %d++++\n",p_dealInfo->start_time.year);
    printf("++++p_dealInfo->start_time.mon = %d++++\n",p_dealInfo->start_time.mon);
    printf("++++p_dealInfo->start_time.mday = %d++++\n",p_dealInfo->start_time.mday);
    printf("++++p_dealInfo->start_time.hour = %d++++\n",p_dealInfo->start_time.hour);
    printf("++++p_dealInfo->start_time.min = %d++++\n",p_dealInfo->start_time.min);
    printf("++++p_dealInfo->start_time.sec = %d++++\n",p_dealInfo->start_time.sec); 
    printf("+++++++++++++++%s(%d)\n",__FILE__,__LINE__);

    //指定格式: 2016/08/30 18:19:30
    //stop_time
    printf("++++++++++++++++++++\n");

    cJSON* stop_time_temp = cJSON_GetObjectItem(pArrayItem, STOP_TIME); 
    sscanf(stop_time_temp->valuestring, "%d/%d/%d %d:%d:%d\n",     \
        &(p_dealInfo->stop_time.year),&(p_dealInfo->stop_time.mon),&(p_dealInfo->stop_time.mday),   \
        &(p_dealInfo->stop_time.hour),&(p_dealInfo->stop_time.min),&(p_dealInfo->stop_time.sec));

    printf("++++p_dealInfo->stop_time.year = %d++++\n",p_dealInfo->stop_time.year);
    printf("++++p_dealInfo->stop_time.mon = %d++++\n",p_dealInfo->stop_time.mon);
    printf("++++p_dealInfo->stop_time.mday = %d++++\n",p_dealInfo->stop_time.mday);
    printf("++++p_dealInfo->stop_time.hour = %d++++\n",p_dealInfo->stop_time.hour);
    printf("++++p_dealInfo->stop_time.min = %d++++\n",p_dealInfo->stop_time.min);
    printf("++++p_dealInfo->stop_time.sec = %d++++\n",p_dealInfo->stop_time.sec);   
    printf("+++++++++++++++%s(%d)\n",__FILE__,__LINE__);

    printf("++++++++++++++++++++\n");
    cJSON* charge_time_temp = cJSON_GetObjectItem(pArrayItem, CHARGE_TIME);
    if(charge_time_temp->valueint == 0)
    {
        printf("++++++++++no_charge++++++++++\n");
        return 0;
    }else{
    p_dealInfo->charge_time = charge_time_temp->valueint;
    printf("++++p_dealInfo->charge_time = %d++++\n",p_dealInfo->charge_time);
    }
    printf("++++++++++++++++++++\n");
    cJSON* total_kwh_temp = cJSON_GetObjectItem(pArrayItem, TOTAL_KWH);
    p_dealInfo->total_kwh = total_kwh_temp->valueint;
    printf("++++p_dealInfo->total_kwh = %d++++\n",p_dealInfo->total_kwh);

        // 釋放記憶體  
        cJSON_Delete(pArrayItem); 

    pArrayItem = NULL;      
    free(out);
    out = NULL;

    return 0;
   }

   /// @brief   獲取最新的交易記錄下標
   /// @return   最新的交易記錄下?
   unsigned short  bx_get_max_index()
   {
    #if 0
    printf("+++++bx_get_max_no++++\n");
    //獲取陣列的節點數量size
    unsigned short count = cJSON_GetArraySize(g_p_dealInfo->p_deal);
    printf("++++++++++count = %d+++++++++\n",count);
    #endif

    return  0;
   }


   int  bx_write_dealInfo(deal_info_t *p_dealInfo)
   {

    assert(NULL != g_p_dealInfo);
    //assert(NULL !=p_dealInfo);

    printf("+++++bx_write_dealInfo++++\n");

    cJSON* p_data = cJSON_CreateObject();

    //向陣列array中插入新的專案
    cJSON_InsertItemInArray(g_p_dealInfo->p_deal, 0, p_data);

    //獲取陣列的節點數量size
    int count = cJSON_GetArraySize(g_p_dealInfo->p_deal);
    //刪除最早的記錄
    if (g_p_dealInfo->max_num < count)
        cJSON_DeleteItemFromArray(g_p_dealInfo->p_deal, count - 1);

    //輸出該結構體的資訊到p_data內 
    char str_time[32] = { 0 };
    sprintf(str_time, "%4d/%02d/%02d %02d:%02d:%02d", p_dealInfo->start_time.year,p_dealInfo->start_time.mon, p_dealInfo->start_time.mday, p_dealInfo->start_time.hour,p_dealInfo->start_time.min,p_dealInfo->start_time.sec);
    cJSON_AddStringToObject(p_data, START_TIME, str_time);

    memset(str_time,0,32);


    sprintf(str_time, "%4d/%02d/%02d %02d:%02d:%02d", p_dealInfo->stop_time.year, p_dealInfo->stop_time.mon, p_dealInfo->stop_time.mday, p_dealInfo->stop_time.hour,p_dealInfo->stop_time.min, p_dealInfo->stop_time.sec);
    cJSON_AddStringToObject(p_data, STOP_TIME, str_time);

    cJSON_AddNumberToObject(p_data, CHARGE_TIME, p_dealInfo->charge_time);

    cJSON_AddNumberToObject(p_data, TOTAL_KWH, p_dealInfo->total_kwh);


    #if 0
    if (NULL != p_dealInfo->p_text)
    {
        free(p_dealInfo->p_text);
        p_dealInfo->p_text = NULL;
    }
    #endif

    if (NULL != g_p_dealInfo->p_text)
    {
        free(g_p_dealInfo->p_text);
        g_p_dealInfo->p_text = NULL;
    }

    //重新整理到檔案2016.8.25
    //p_dealInfo->p_text = cJSON_PrintUnformatted(p_dealInfo->p_event);
    g_p_dealInfo->p_text = cJSON_Print(g_p_dealInfo->p_deal);

    event_write();

   }

test.c檔案:

#include <stdio.h>
#include "cJSON.h"
#include "baoxin_def.h"
#include "bx_deal.h"

//struct deal_info_t *p_dealInfo = NULL; 

struct deal_info_t p_dealInfo;

struct deal_info_t p_dealInfo1 =  
{       
    {2016,8,21,7,15,10,30,0},   //2016/8/25 14:58:30"
    {2016,8,21,7,16,10,30,0},   //"2016/8/25 15:58:30"
    1,
    7
};
struct deal_info_t p_dealInfo2 =  
{       
    {2016,8,22,7,15,10,30,0},   //2016/8/25 14:58:30"
    {2016,8,22,7,16,10,30,0},   //"2016/8/25 15:58:30"
    1,
    7
};
struct deal_info_t p_dealInfo3 =  
{       
    {2016,8,23,7,15,10,30,0},   //2016/8/25 14:58:30"
    {2016,8,23,7,16,10,30,0},   //"2016/8/25 15:58:30"
    1,
    7
};
struct deal_info_t p_dealInfo4 =  
{       
    {2016,8,24,7,15,10,30,0},   //2016/8/25 14:58:30"
    {2016,8,24,7,16,10,30,0},   //"2016/8/25 15:58:30"
    1,
    7
};
struct deal_info_t p_dealInfo5 =  
{       
    {2016,8,25,7,15,10,30,0},   //2016/8/25 14:58:30"
    {2016,8,25,7,16,10,30,0},   //"2016/8/25 15:58:30"
    1,
    7
};
struct deal_info_t p_dealInfo6 =  
{       
    {2016,8,26,7,15,10,30,0},   //2016/8/25 14:58:30"
    {2016,8,26,7,16,10,30,0},   //"2016/8/25 15:58:30"
    1,
    7
};

const char *p_path = "chargeinfo.txt";

//const char *p_path = "/mnt/hgfs/share_for_linux/cJSON_lib_use/chargeinfo.txt";



int main()
{
    bx_dealInfo_init(p_path);
#if 0
    bx_write_dealInfo(&p_dealInfo1);
    bx_write_dealInfo(&p_dealInfo2);
    bx_write_dealInfo(&p_dealInfo3);
    bx_write_dealInfo(&p_dealInfo4);
    bx_write_dealInfo(&p_dealInfo5);    
    bx_write_dealInfo(&p_dealInfo6);
#endif

    bx_get_max_index();
    //bx_read_dealInfo(1,&p_dealInfo);
    bx_read_dealInfo(2,&p_dealInfo);
    //bx_read_dealInfo(3,&p_dealInfo);
    //bx_read_dealInfo(4,&p_dealInfo);
    //bx_read_dealInfo(5,&p_dealInfo);
    //bx_read_dealInfo(6,&p_dealInfo);

    bx_deal_quit();

    //system_pause();

    return 0;

pub_type.h檔案:

#ifndef __PUB_TYPE_H__
#define __PUB_TYPE_H__

#include<stdio.h>

#ifdef __cplusplus      
#define SK_API extern "C"   
#else       
#define SK_API extern   
#endif

#pragma pack(push) //
#pragma pack(4)

/// @name  _SK_TIME_
/// @brief 時間描述
#if 0
typedef struct SK_TIME
{
   unsigned short      year;       ///< 某年

   unsigned char       mon;        ///< 某月
   unsigned char       mday;       ///< 某日
   unsigned char       wday;       ///< 星期幾

    unsigned char       hour;       ///< 時
    unsigned char       min;        ///< 分
    unsigned char       sec;        ///< 秒

    int         msec;              ///< 豪秒
}SK_TIME;
#endif
typedef struct SK_TIME
{
   unsigned short     year;       ///< 某年

   unsigned char       mon;        ///< 某月
   unsigned char       mday;       ///< 某日
   unsigned char       wday;       ///< 星期幾

   unsigned char       hour;       ///< 時
   unsigned char       min;        ///< 分
   unsigned char       sec;        ///< 秒

     int       msec;               ///< 豪秒
}SK_TIME;


#pragma pack(pop)//

#endif // __PUB_TYPE_H__
//end

baoxin_def.h:

///////////////////////////////////////////////////////////////////////////
//
/// @file        baoxin.h
/// @brief   與寶信車聯網的通訊模組
/// @author  
/// @version 0.1
/// @history 修改歷史
/// @warning 沒有警告
///////////////////////////////////////////////////////////////////////////

#ifndef   __BAO_XIN_DEF_H__
#define   __BAO_XIN_DEF_H__


#include <stdio.h>
#include"pub_type.h"


#define  BX_LOGIN_CODE_LEN  32          //登入碼的最大長度
#define  BX_MSG_HEADER_LEN  12          //訊息頭的最大長度
#define  BX_CELL_PHONE_LEN  12          //終端手機號碼的最大長度
#define  BX_MSG_CRC_LEN  1          //校驗碼長度
#pragma pack(push) //
#pragma pack(1)



typedef struct bx_ser_info_t
{
    unsigned char server[4];   //4個位元組,從高位到低位,依次對應IP地址的4個段
    unsigned  short port;       //埠號
}bx_ser_info_t;

typedef  struct  cp_state_t
{
    unsigned char  elec_lock_state ;      
    //電磁鎖的狀態 0:關閉狀態 1:開啟狀態 2:無電磁鎖
    //充電槍的狀態 0:充電槍處於與樁連線狀態;(暫時無)1:充電槍處於與車連線狀態;2:充電槍處於懸空狀態;3:充電槍與車連線並且車已經準備好 
    unsigned char  cp_error;              
    //充電樁故障 0:無故障 1:有故障
    //總電量3位元組,高8位、中間8位、低8位。總電量,單位為W.H
    unsigned char  current;          
    //充電電流值單位為安培(A)
}cp_state_t;


typedef  struct  login_info_t
{
    unsigned char  login_code[32] ;      //登入碼
}login_info_t;


typedef  struct  result_reply_t
{
    unsigned char  answer ;      //充電狀態設定結果:0設定成功,1設定失敗
}result_reply_t;

typedef  struct  deal_info_t
{
    //unsigned short  seq ;             //查詢的交易記錄號
    SK_TIME  start_time;            //充電的開始時間
    SK_TIME  stop_time;         //充電的結束時間
    unsigned  int  charge_time; //充電時長
    unsigned  int  total_kwh;        //充電總電度
}deal_info_t;


#pragma pack(pop)//

bx_deal.h檔案:

#ifndef __BX_DEAL_H__
#define  __BX_DEAL_H__

#include "pub_type.h"



/// @brief   模組初始化
/// @param [in]       p_fileName: 檔案路徑
/// @return  0:成功 非0:失敗
SK_API  int  bx_deal_init(const char *p_fileName);



/// @brief   模組反初始化
SK_API  int  bx_deal_quit();


/// @brief   按下標讀取一條資訊
/// @param [in]       seq: 序號
/// @param [out]      p_dealInfo: 儲存交易記錄的結構體
/// @return  0:成功 非0:失敗
/// 將結果存到一個結構體中,即*p_dealInfo;
SK_API int bx_read_dealInfo(unsigned short seq,deal_info_t  *p_dealInfo);

/// @brief   按下標讀取一條資訊
/// @param [in]       seq: 序號
/// @param [in]       p_dealInfo: 交易記錄的結構體
/// @return  0:成功 非0:失敗

SK_API int  bx_write_dealInfo(deal_info_t  *p_dealInfo);

/// @brief   獲取最新的交易記錄下標
/// @return   最新的交易記錄下標
SK_API  unsigned short  bx_get_max_index();

#endif

chargeinfo.txt檔案:
chargeinfo.txt

小結論:在這個小工程中,運用了cJSON的介面函式,方便讓我從人為定義的結構體,這裡是包含了充電的資訊,將這些資訊(結構體)通過cJSON介面函式,以cJSON結構存入指定檔案中chargeinfo.txt,並從這個檔案以cJSON函式方便的讀取出裡面的相應序號的充電資訊,並以指定的充電資訊的結構體形式輸出;非常方便;

相關推薦

cJSON的使用介紹例項分析

使用意圖: 用於方便實現包含充電資訊的結構體輸入到檔案中,然後需要的時候,從檔案中以充電資訊的結構體的形式輸出; 簡要介紹: cJSON的使用,網上有很多的資料,主要就是兩個檔案,一個頭檔案cJSON.h,一個應用檔案cJSON.c,前者主要是對c

Python使用Ctypes與C/C++ DLL檔案通訊過程介紹例項分析

專案中可能會經常用到第三方庫,主要是出於程式效率考慮和節約開發時間避免重複造輪子。無論第三方庫開源與否,程式語言是否與當前專案一致,我們最終的目的是在當前程式設計環境中呼叫庫中的方法並得到結果或者藉助庫中的模組實現某種功能。這個過程會牽涉到很多東西,本篇文章將簡要的介紹一下該過程的一些問題。 1.背景 多

mybatis原理,配置介紹原始碼分析

前言 mybatis核心元件有哪些?它是工作原理是什麼? mybatis配置檔案各個引數是什麼含義? mybatis只添加了介面類,沒有實現類,為什麼可以直接查詢呢? mybatis的mapper對映檔案各個引數又是什麼含義? mybatis-spring提供哪些機制簡化了原生mybatis? m

android ANR、traces檔案獲取例項分析

前言:前段時間專案開發中遇到anr的問題,時間緊急,一時間又難以定位,通過臨時方法解決後,最近有時間對ANR的問題做一次份細的解決方案,本文中的解決方案是通過綜合其他部落格後自己再通過例項驗證後得出的可行方案,讀者如遇類似問題可做參考,歡迎評論交流。

PageRank 演算法例項分析

本文一部分是針對圖的PageRank 的實現,以及具體資料集的分析過程的記錄。 另一部分是BFS的實現,並記錄每一層的節點數。 資料集下載地址  soc-Slashdot0811 、 roadNet-CA 、 soc-LiveJournal1 1. java 實現程式碼 M

Spark入門實戰系列--9.Spark圖計算GraphX介紹例項

1、GraphX介紹 1.1 GraphX應用背景 Spark GraphX是一個分散式圖處理框架,它是基於Spark平臺提供對圖計算和圖挖掘簡潔易用的而豐富的介面,極大的方便了對分散式圖處理的需求。 眾所周知·,社交網路中人與人之間有很多關係鏈,例如Twitter、Faceb

java定時器類Timer和多執行緒介紹例項

任務要求: 完成一個java application應用程式,使用定時器程式設計,在實時顯示當前時間,每1秒時鐘內容更新一次。 完成一個java application應用程式,在應用程式主程序中新開一個執行緒,此執行緒進行死迴圈,每1秒被啟用一次,啟用時即在

WebView簡單介紹程式碼分析

Intent intent = getIntent(); String url = intent.getStringExtra("murl"); web_view.setWebViewClient(new WebViewClient()); WebSett

微信 小程式前端原始碼詳解例項分析

微信小程式前端原始碼邏輯和工作流 看完微信小程式的前端程式碼真的讓我熱血沸騰啊,程式碼邏輯和設計一目瞭然,沒有多餘的東西,真的是大道至簡。 廢話不多說,直接分析前端程式碼。個人觀點,難免有疏漏,僅供參考。 檔案基本結構: 先看入口app.js,app(obj)註冊一個小程式。接受一個 obje

ICE介紹例項

Ice-Internet Communications Engine,是一種面向物件、跨平臺、多語言的通訊中介軟體。支援C++、Java、C#、VB、Python等,客戶和伺服器可以用不同的語言,可以執行在不同的作業系統和機器架構上,支援TCP/UDP。 Ice應用組成:

Java記憶體資料庫-H2介紹例項(SpringBoot)

介紹 記憶體資料庫(Embedded database或in-momery database)具有配置簡單、啟動速度快、尤其是其可測試性等優點,使其成為開發過程中非常有用的輕量級資料庫。在spring中支援HSQL、H2和Derby三種資料庫。 總的感覺H2很厲害的樣子,既然是這樣的話,我們先介紹H2

圖的BFS和DFS原理例項分析(java)

BFS和DFS是圖的兩種遍歷方式,是最簡單的圖搜尋演算法。 本文將給出給出BFS和DFS的以下幾種實現方式: 1、使用佇列Queue實現圖的BFS遍歷 2、遞迴實現圖的DFS遍歷 3、使用棧Stack迭代實現圖的DFS遍歷 一、BFS(廣度優先搜尋

K最近鄰分類演算法原理例項分析

目錄 概述 原理 要點 例項 1、概述 K最近鄰(k-Nearest Neighbor,KNN),指導思想是“近朱者赤,近墨者黑”,由你的鄰居來推斷出你的類別,KNN分類演算法是最簡單的機器學習演算法。 2、原理 從訓練集中找到和新資料最接近的k條記錄

roc曲線簡單介紹例項程式碼

今日筆記:roc曲線 還是僅供自己參考學習。。 首先要注意的是roc曲線僅適用於二分類問題,不是二分類問題應首先通過各種手段轉為二分類問題。 roc橫座標為TPR,縱座標為TPR,若要知道TPR,FPR,就要從混淆矩陣說起... 漏掉了f1..f1 = 2*p*r

IOServer開源專案介紹原始碼分析

IOServer是國人寫的一個仿MINA的輕量級伺服器框架。最近因為使用MINA的緣故,閱讀了IOServer的原始碼,順便熟悉了J2SE NIO部分。 IOServer的設計目標是手機網遊伺服器框架。支援TCP協議。 這個框架的使用方法和MINA類似;它有一個Proto

Linux C Socket UDP程式設計介紹例項

1、UDP網路程式設計主要流程 UDP協議的程式設計框架,客戶端和伺服器之間的差別在於伺服器必須使用bind()函式來繫結偵聽的本地UDP埠,而客戶端則可以不進行繫結,直接傳送到伺服器地址的某個埠地址。框圖如圖1.3所示 UDP協議的伺服器端流程 伺服器流程主要分為下述6個

next陣列介紹例項

本文轉自他人。(http://blog.csdn.net/iamyvette/article/details/77433991) 提示:切記!!!是前一位的next對應的值 與其自身比較。而這裡的next 的值就是下標(next值為1就是下標為1的值) next陣列的求

python爬蟲入門--Beautiful Soup庫介紹例項

整理自:北理工嵩天老師的網路課程。 1、Beautiful Soup庫基礎知識 (1)Beautiful Soup庫的理解 Beautiful Soup庫是解析、遍歷、維護“標籤樹”的功能庫。 BeautifulSoup對應一個HTML/XML文件的全部內容。

C/S和B/S效能介紹優缺點分析

為了區別於傳統的C/S模式,才特意將其稱為B/S模式。認識到這些結構的特徵,對於系統的選型而言是很關鍵的。 一:系統的效能   在系統的效能方面,B/S佔有優勢的是其異地瀏覽和資訊採集的靈活性。任何時間、任何地點、任何系統,只要可以使用瀏覽器上網,就可以使用B/S系統的終端。   不過,採用B/S結構,客

最短路徑之Dijkstra演算法例項分析

Dijkstra演算法迪科斯徹演算法 Dijkstra演算法描述為:假設用帶權鄰接矩陣來表示帶權有向圖。首先引進一個輔助向量D,它的每個分量D[i]表示當前所找到的從始點v到每個終點Vi的最短路徑。它的初始狀態為:若兩頂點之間有弧,則D[i]為弧上的權值;否則置D[i]為無