PIXNET Logo登入

痞客興的部落格

跳到主文

歡迎光臨痞客興(Charles Lin)在痞客邦的小天地,這裡留下的是我做過,我看過,我感興趣的一些文章,記錄備忘也和大家分享.

部落格全站分類:數位生活

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 8月 24 週四 201709:11
  • [C++] 浮點數精度問題~四捨五入函數

資料來自於 http://jashliao.pixnet.net/blog/post/223492566-c-c++-%E6%B5%AE%E9%BB%9E%E6%95%B8%5B%E7%B2%BE%E5%BA%A6%E5%95%8F%E9%A1%8C%5D~%E5%9B%9B%E6%8D%A8%E4%BA%94%E5%85%A5%E5%87%BD%E6%95%B8
// 四捨五入 取到 小數點第 X 位
double CLib::rounding(double num, int index)
{
    //https://dotblogs.com.tw/forloop/2016/07/31/rounding
    bool isNegative = false; // whether is negative number or not
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(785)

  • 個人分類:C語言的嫩咖行
▲top
  • 8月 18 週五 201709:42
  • [C++] Windows 純 C/C++ 使用 SOCKET 撰寫 Server [ thread(執行緒) ] / Client

原文來自 http://jashliao.pixnet.net/blog/post/223451341
 
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(1,808)

  • 個人分類:C語言的嫩咖行
▲top
  • 8月 18 週五 201709:39
  • [C++] Windows 純 C/C++ 使用 pthread 函式庫實作thread(執行緒)

原文章來自 http://jashliao.pixnet.net/blog/post/219032153
#include <iostream>
#include "pthread.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>//#include <unistd.h>//Sleep(WINDOWS) vs sleep(LINUX)
using namespace std;
#define MAX 20
/* LINUX C的 jash_thread 轉WINDOWS */
pthread_t thread[4];
pthread_mutex_t mut;
int number;
int i;
void *thread1(void *p)
{
    printf ("thread1 : I'm thread 1\n");
    for (i = 0; i < MAX; i++)
    {
        printf("thread1 : %d\n",i);
        pthread_mutex_lock(&mut);
            if(i<MAX)
            {
                number++;
                printf("thread1 : number = %d\n",number);
            }
        pthread_mutex_unlock(&mut);
        Sleep( 30 );//sleep(1);
    }
    printf("thread1 :主函數在等我完成任務嗎?\n");
    pthread_exit(NULL);
}
void *thread2(void *p)
{
    printf("thread2 : I'm thread 2\n");
    for (i = 0; i < MAX; i++)
    {
        printf("thread2 : %d\n",i);
        pthread_mutex_lock(&mut);
            if(i<MAX)
            {
                number++;
                printf("thread2 : number = %d\n",number);
            }
        pthread_mutex_unlock(&mut);
        Sleep( 30 );//sleep(1);
    }
    printf("thread2 :主函數在等我完成任務嗎?\n");
    pthread_exit(NULL);
}
void *thread3(void *p)
{
    printf("thread3 : I'm thread 3\n");
    for (i = 0; i < MAX; i++)
    {
        printf("thread3 : %d\n",i);
        pthread_mutex_lock(&mut);
            if(i<MAX)
            {
                number++;
                printf("thread3 : number = %d\n",number);
            }
        pthread_mutex_unlock(&mut);
        Sleep( 30 );//sleep(1);
    }
    printf("thread3 :主函數在等我完成任務嗎?\n");
    pthread_exit(NULL);
}
void *thread4(void *p)
{
    printf("thread4 : I'm thread 4\n");
    for (i = 0; i < MAX; i++)
    {
        printf("thread4 : %d\n",i);
        pthread_mutex_lock(&mut);
            if(i<MAX)
            {
                number++;
                printf("thread4 : number = %d\n",number);
            }
        pthread_mutex_unlock(&mut);
        Sleep( 30 );//sleep(1);
    }
    printf("thread4 :主函數在等我完成任務嗎?\n");
    pthread_exit(NULL);
}
void thread_create(void)
{
    int temp;
    memset(&thread, 0, sizeof(thread));          //comment1
    /*創建線程*/
    if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2
    {
        printf("線程1創建失敗!\n");
    }
    else
    {
        printf("線程1被創建\n");
    }
    if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3
    {
        printf("線程2創建失敗");
    }
    else
    {
        printf("線程2被創建\n");
    }
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(2,081)

  • 個人分類:C語言的嫩咖行
▲top
  • 11月 01 週二 201614:12
  • BCB6(Borland C++ Builder6)中設置程式中斷點除錯方法

資料來源: http://jashliao.pixnet.net/blog/post/220855763
資料來源: http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=60265
資料來源: http://bbs.csdn.net/topics/30122621
 
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(1,388)

  • 個人分類:C語言的嫩咖行
▲top
  • 10月 27 週四 201615:02
  • [C] AES-128 coding sample


出處 http://www.codedata.com.tw/social-coding/aes/
// 本程式由陳鍾誠將 jsaes: AES in JavaScript (作者: B. Poettering) 翻譯為 C 語言版,
// 來源為 http://point-at-infinity.org/jsaes/,授權為 GNU GPL 授權。
 
#include <stdio.h>
 
#define BYTE unsigned char
 
void printBytes(BYTE b[], int len) {
  int i;
  for (i=0; i<len; i++)
    printf("%d ", b[i]);
  printf("\n");
}
 
/******************************************************************************/
 
// The following lookup tables and functions are for internal use only!
BYTE AES_Sbox[] = {99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,
  118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,
  147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,
  7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,
  47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,
  251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,
  188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,
  100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,
  50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,
  78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,
  116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,
  158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,
  137,13,191,230,66,104,65,153,45,15,176,84,187,22};
 
BYTE AES_ShiftRowTab[] = {0,5,10,15,4,9,14,3,8,13,2,7,12,1,6,11};
 
BYTE AES_Sbox_Inv[256];
BYTE AES_ShiftRowTab_Inv[16];
BYTE AES_xtime[256];
 
void AES_SubBytes(BYTE state[], BYTE sbox[]) {
  int i;
  for(i = 0; i < 16; i++)
    state[i] = sbox[state[i]];
}
 
void AES_AddRoundKey(BYTE state[], BYTE rkey[]) {
  int i;
  for(i = 0; i < 16; i++)
    state[i] ^= rkey[i];
}
 
void AES_ShiftRows(BYTE state[], BYTE shifttab[]) {
  BYTE h[16];
  memcpy(h, state, 16);
  int i;
  for(i = 0; i < 16; i++)
    state[i] = h[shifttab[i]];
}
 
void AES_MixColumns(BYTE state[]) {
  int i;
  for(i = 0; i < 16; i += 4) {
    BYTE s0 = state[i + 0], s1 = state[i + 1];
    BYTE s2 = state[i + 2], s3 = state[i + 3];
    BYTE h = s0 ^ s1 ^ s2 ^ s3;
    state[i + 0] ^= h ^ AES_xtime[s0 ^ s1];
    state[i + 1] ^= h ^ AES_xtime[s1 ^ s2];
    state[i + 2] ^= h ^ AES_xtime[s2 ^ s3];
    state[i + 3] ^= h ^ AES_xtime[s3 ^ s0];
  }
}
 
void AES_MixColumns_Inv(BYTE state[]) {
  int i;
  for(i = 0; i < 16; i += 4) {
    BYTE s0 = state[i + 0], s1 = state[i + 1];
    BYTE s2 = state[i + 2], s3 = state[i + 3];
    BYTE h = s0 ^ s1 ^ s2 ^ s3;
    BYTE xh = AES_xtime[h];
    BYTE h1 = AES_xtime[AES_xtime[xh ^ s0 ^ s2]] ^ h;
    BYTE h2 = AES_xtime[AES_xtime[xh ^ s1 ^ s3]] ^ h;
    state[i + 0] ^= h1 ^ AES_xtime[s0 ^ s1];
    state[i + 1] ^= h2 ^ AES_xtime[s1 ^ s2];
    state[i + 2] ^= h1 ^ AES_xtime[s2 ^ s3];
    state[i + 3] ^= h2 ^ AES_xtime[s3 ^ s0];
  }
}
 
// AES_Init: initialize the tables needed at runtime.
// Call this function before the (first) key expansion.
void AES_Init() {
  int i;
  for(i = 0; i < 256; i++)
    AES_Sbox_Inv[AES_Sbox[i]] = i;
   
  for(i = 0; i < 16; i++)
    AES_ShiftRowTab_Inv[AES_ShiftRowTab[i]] = i;
 
  for(i = 0; i < 128; i++) {
    AES_xtime[i] = i << 1;
    AES_xtime[128 + i] = (i << 1) ^ 0x1b;
  }
}
 
// AES_Done: release memory reserved by AES_Init.
// Call this function after the last encryption/decryption operation.
void AES_Done() {}
 
/* AES_ExpandKey: expand a cipher key. Depending on the desired encryption
   strength of 128, 192 or 256 bits 'key' has to be a byte array of length
   16, 24 or 32, respectively. The key expansion is done "in place", meaning
   that the array 'key' is modified.
*/  
int AES_ExpandKey(BYTE key[], int keyLen) {
  int kl = keyLen, ks, Rcon = 1, i, j;
  BYTE temp[4], temp2[4];
  switch (kl) {
    case 16: ks = 16 * (10 + 1); break;
    case 24: ks = 16 * (12 + 1); break;
    case 32: ks = 16 * (14 + 1); break;
    default:
      printf("AES_ExpandKey: Only key lengths of 16, 24 or 32 bytes allowed!");
  }
  for(i = kl; i < ks; i += 4) {
    memcpy(temp, &key[i-4], 4);
    if (i % kl == 0) {
      temp2[0] = AES_Sbox[temp[1]] ^ Rcon;
      temp2[1] = AES_Sbox[temp[2]];
      temp2[2] = AES_Sbox[temp[3]];
      temp2[3] = AES_Sbox[temp[0]];
      memcpy(temp, temp2, 4);
      if ((Rcon <<= 1) >= 256)
        Rcon ^= 0x11b;
    }
    else if ((kl > 24) && (i % kl == 16)) {
      temp2[0] = AES_Sbox[temp[0]];
      temp2[1] = AES_Sbox[temp[1]];
      temp2[2] = AES_Sbox[temp[2]];
      temp2[3] = AES_Sbox[temp[3]];
      memcpy(temp, temp2, 4);
    }
    for(j = 0; j < 4; j++)
      key[i + j] = key[i + j - kl] ^ temp[j];
  }
  return ks;
}
 
// AES_Encrypt: encrypt the 16 byte array 'block' with the previously expanded key 'key'.
void AES_Encrypt(BYTE block[], BYTE key[], int keyLen) {
  int l = keyLen, i;
  printBytes(block, 16);
  AES_AddRoundKey(block, &key[0]);
  for(i = 16; i < l - 16; i += 16) {
    AES_SubBytes(block, AES_Sbox);
    AES_ShiftRows(block, AES_ShiftRowTab);
    AES_MixColumns(block);
    AES_AddRoundKey(block, &key[i]);
  }
  AES_SubBytes(block, AES_Sbox);
  AES_ShiftRows(block, AES_ShiftRowTab);
  AES_AddRoundKey(block, &key[i]);
}
 
// AES_Decrypt: decrypt the 16 byte array 'block' with the previously expanded key 'key'.
void AES_Decrypt(BYTE block[], BYTE key[], int keyLen) {
  int l = keyLen, i;
  AES_AddRoundKey(block, &key[l - 16]);
  AES_ShiftRows(block, AES_ShiftRowTab_Inv);
  AES_SubBytes(block, AES_Sbox_Inv);
  for(i = l - 32; i >= 16; i -= 16) {
    AES_AddRoundKey(block, &key[i]);
    AES_MixColumns_Inv(block);
    AES_ShiftRows(block, AES_ShiftRowTab_Inv);
    AES_SubBytes(block, AES_Sbox_Inv);
  }
  AES_AddRoundKey(block, &key[0]);
}
 
// ===================== test ============================================
int main() {
  int i;
  AES_Init();
   
  BYTE block[16];
  for(i = 0; i < 16; i++)
    block[i] = 0x11 * i;
 
  printf("原始訊息:"); printBytes(block, 16);
   
  BYTE key[16 * (14 + 1)];
  int keyLen = 32, maxKeyLen=16 * (14 + 1), blockLen = 16;
  for(i = 0; i < keyLen; i++)
    key[i] = i;
 
  printf("原始金鑰:"); printBytes(key, keyLen);
 
  int expandKeyLen = AES_ExpandKey(key, keyLen);
 
  printf("展開金鑰:"); printBytes(key, expandKeyLen);
 
  AES_Encrypt(block, key, expandKeyLen);
 
  printf("加密完後:"); printBytes(block, blockLen);
 
  AES_Decrypt(block, key, expandKeyLen);
 
  printf("解密完後:"); printBytes(block, blockLen);
 
  AES_Done();
}
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(700)

  • 個人分類:C語言的嫩咖行
▲top
  • 1月 19 週二 201618:53
  • [C語言] sizeof()及strlen()

今天寫程式時卡位了,最主要是沒弄清楚sizeof()與strlen()的使用時機,後來問了jashliao,點通了我這小嫩咖,所以稍加記錄
而今天的主要情形是我使用C開檔,並寫入一段文字,儲存後離開,但去開檔看文字有寫入但後面多了一些亂碼,像是^@
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(1,311)

  • 個人分類:C語言的嫩咖行
▲top
  • 1月 19 週二 201617:29
  • [C語言] 編譯報錯warning: control reaches end of non-void function

為什麼會有這個錯誤呢???
是因為你答應人家的事沒做到,怎麼說呢???
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(1) 人氣(16,142)

  • 個人分類:C語言的嫩咖行
▲top
  • 1月 15 週五 201609:10
  • [C語言] 編譯錯誤warning: implicit declaration of function ‘exit’

請檢查你有沒有加這個header
#include <stdlib.h>
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(3,226)

  • 個人分類:C語言的嫩咖行
▲top
  • 1月 15 週五 201608:04
  • [C語言] 使用C語言存取MySQL的範例

以下的範例是使用C語言來達成MySQL的連結,本文是參考http://codex.wiki/post/106065-844
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/usr/include/mysql/mysql.h"
int main(){
MYSQL mysql,*sock;
MYSQL_RES *res;
MYSQL_ROW row;
char qbuf[160] = "select afield,bfield,cfield,dfield from tablename where tran = 1 limit 0,1";
mysql_init(&mysql);
if (!(sock = mysql_real_connect(&mysql,"localhost","dbuser","dbpassword","dbname",0,NULL,0))){
fprintf(stderr,"Couldnt connect to engine!n%snn",mysql_error(&mysql));
perror("");
exit(1);
}
if(mysql_query(sock,qbuf)) {
fprintf(stderr,"Query failed (%s)n",mysql_error(sock));
exit(1);
}
if (!(res=mysql_store_result(sock))) {
fprintf(stderr,"Couldnt get result from %sn", mysql_error(sock));
exit(1);
}
printf("number of fields returned: %dn",mysql_num_fields(res));
while((row = mysql_fetch_row(res))) {
printf("afield:%s ,bfield:%s, cfield:%s, dfield:%s",row[0],row[1],row[2],row[3]) ;
puts( "\nquery ok !n" ) ;
}
mysql_free_result(res);
mysql_close(sock);
exit(0);
return 0;
}
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(1) 人氣(1,209)

  • 個人分類:C語言的嫩咖行
▲top
  • 1月 14 週四 201619:07
  • [C語言] 使用MySQL時,編譯時出現"未定義參考到"錯誤

就是像這樣
mysql.c:(.text+0x25): 未定義參考到「mysql_init」
(繼續閱讀...)
文章標籤

痞客興 發表在 痞客邦 留言(0) 人氣(685)

  • 個人分類:C語言的嫩咖行
▲top
12»

google本站搜尋

文章分類

toggle 綜合知識 (3)
  • 鄉土靈異 (1)
  • 電的知識 (7)
  • 綜合知識 (143)
toggle WEB應用 (14)
  • ASP.NET (2)
  • 來玩Flash (1)
  • CGI (1)
  • EasyUI (5)
  • 來玩JQM (3)
  • 來玩CSS (5)
  • 來玩HTML (10)
  • 來玩drupal (29)
  • 來玩Nginx (8)
  • 來玩JavaScript (50)
  • WEB應用 (13)
  • 來玩Node.js (5)
  • 來玩PHP (31)
  • 來玩Apache (3)
toggle 資料庫應用 (5)
  • 來玩MSSQL (11)
  • 來玩SQLite (1)
  • 來玩MongoDB (10)
  • 來玩MySQL (21)
  • 來玩SQL Relay (2)
toggle 影音多媒體應用 (5)
  • 影音多媒體應用 (0)
  • 來玩vlc (4)
  • 來玩RED5 (1)
  • 來玩Wowza (1)
  • FFmpeg (1)
toggle 來玩Linux (5)
  • 來玩DHCP (1)
  • 來玩iptables (5)
  • 來玩Memcache (1)
  • 來玩Linux (147)
  • shell (12)
  • 來玩docker (22)
  • 股市知識 (2)
  • 程式設計 (2)
  • 邦邦星3C共和國 (2)
  • 時事記評 (0)
  • 奇人奇事 (3)
  • 手機測試 (1)
  • 個人感想 (3)
  • 外行看文學 (1)
  • 來玩Arduino (1)
  • 健身減肥 (46)
  • 免費資源 (10)
  • 來玩C# (92)
  • C語言的嫩咖行 (15)
  • 雲端應用 (13)
  • 趣味人生 (29)
  • 來起七桃 (6)
  • 來玩英文 (7)
  • 電腦軟體 (9)
  • 來玩JAVA (17)
  • 來玩Android (56)
  • 公司會計 (2)
  • 手機應用 (3)
  • 電腦硬體 (2)
  • 來玩Winodws (48)
  • 來玩Banana pi (9)
  • 網管應用 (21)
  • 來玩git (1)
  • 未分類文章 (1)

FlagCounter

C 組廣告版面

參觀人氣

  • 本日人氣:
  • 累積人氣:

bloggerads

誰來我家

最新文章

  • [docker] 指令docker login及docker logout - 登入登出registry
  • [docker] 指令docker diff - 看目前容器讀寫層做了那些變化
  • [docker] 指令docker commit 或 docker container commit - 使用運行中的容器製作印象檔
  • [docker] 為容器建立 init process,方便回收zombie
  • [docker] 指令 docker inspect 或 docker container inspect - 獲取容器的詳細配置資料
  • [docker] 指令 docker exec - 進入到已啟動的容器操作介面
  • [docker] 設定容器的重啟策略 --restart
  • [docker] 指令 docker container prune - 刪除所有停止的容器
  • [docker] 指令 docker rm 或 docker container rm - 刪除容器
  • [docker] 指令 docker stop 或 docker container stop - 停止容器的運行

shinystat