Wiki内検索
最近更新したページ
2011-08-24
2010-05-18
2010-05-03
2010-02-18
2010-02-10
2010-02-09
2010-02-08
2010-02-07
2010-02-02
2009-12-24
2009-12-05
2009-07-05
2009-07-03
最新コメント
MenuBar1 by stunning seo guys
MenuBar1 by stunning seo guys
MenuBar1 by stunning seo guys
MenuBar1 by awesome things!
MenuBar1 by awesome things!
MenuBar1 by awesome things!
MenuBar1 by check it out
MenuBar1 by awesome things!
MenuBar1 by check it out
MenuBar1 by stunning seo guys
Menu
Programming Tips
タグ

cprograming

C言語 テクニック集



  • getcharによる文字列操作

memset(str, 0x00, sizeof(str));

while ((c = getchar( )) != EOF) {
str[i++] = tolower(c);
if (c == '\n') break;
}

  • 文字列を小文字に
memset(str, 0x00, sizeof(str));
scanf("%s", str);
j = 0;
while (1) {
str[j] = tolower(str[j]);
if (str[j] == '\n') break;
j++;
}
printf("%s", str);

  • scanfで空白文字も
printf("input\n");
scanf("%[^\n]", str);
printf("%s", str);

  • strtokの使い方
char mojiretu[] = "aaa, bbb, ccccc";
char sdata[3][125];
char token[] = " ,";
char *work;
int i = 0;

strcpy(sdata[i++], strtok(mojiretu, token));
while(1)
{
if ( (work = strtok(NULL, token)) == NULL) break;
strcpy(sdata[i++], work);
}

for (i = 0; i < 3; ++i)
{
printf("%s\n", sdata[i]);
}

  • 一つ一つセーブロードしていく
void loadData(int *data, int size)
{
FILE *fp;
int i;
fp = fopen("data1.dat", "r+b");
for ( i = 0; i < size; ++i)
{
fread(&data[i], sizeof(int), 1, fp);
}
fclose(fp);
}

void dispData(int *data, int size)
{
int i;
for (i = 0; i < size; ++i)
{
printf("%d\n", data[i]);
}
}

  • input処理 ループを使う
void input1(char *data)
{
char work[128];
while(1)
{
memset(work, 0x00, sizeof(work));
printf("%s", "\n input 3 num: \n");
scanf("%s", work);
if (strspn(work, "0123456789") < strlen(work))
{
printf("%s", "input only num\n");
continue;
}
if(strlen(work) != 3)
{
printf("%s", "input 3 number");
continue;
}
break;
}
printf("input num is %s\n", work);
strcpy(data, work);
}

  • ある範囲の乱数
rnd = rand() % (num + 1);
  • srand使い方
#include <time.h>
time_t nt;
srand("time(&nt)");
2009年06月28日(日) 03:39:26 Modified by eruvasu




スマートフォン版で見る