C学习备忘录

时间:2021-01-26 15:25:59
1. system("pause");
2.long long a = 0LL;
3.printf("%lld",a);
4.fflush(stdin);
5.for (; (clock()-now) < (CLOCKS_PER_SEC*2) ;)
6.连接字符串
    char a[40] = "this is a big ";
    char b[] = "game show! WO~";
    int count1 = 0;
    int count2 = 0;
    while(a[count1])
	count1++;
    while(b[count2])
	count2++;
    count2 = 0;
    while(a[count1++]=b[count2++]);
7.数组字符串
char string[2][30] = {"hello ","C.You know~"};
    int count[] = {0,0};
    int i =0;
    
    for(i=0;i<2;i++)
    {
        while(string[i][count[i]])
            count[i]++;    
    } 
    
    char new[count[0]+count[1]+1];
    count[0] = 0;
    while(string[0][count[0]])
    {
        new[count[0]] = string[0][count[0]];
        count[0]++;    
    }
    count[1] = 0;
    while(new[count[0]++]=string[1][count[1]++]);
    printf("new:%s\n",new);

8. scanf_s()
9. strchr 从前往后寻找 strrchr 反向寻找  返回地址
10.strstr 寻找字符串
11.输入字符串
char buffer[90];
gets(buffer);
12.更改字符串大小写
char a[] = "AbCdEfG";
for(int i=0;(a[i] = tolower(a[i]))!='\0';i++);
13.宽字符串wchar_t  a[] = L"abc"; 输出 %S  <wchar.h> wcsXXX
14.fgetws() iswlower() ...
15.int* p = (int*) calloc(50,sizeof(int));
16.代码片段
const size_t MAX_LENGTH = 100;

int main()
{
    
    char word[MAX_LENGTH];
    char* p = word;
    int index;
    char* words[3] = {NULL};
    
    for (int i=0;i<3;i++)
    {
        index=0;
        fflush(stdin);
        printf("请输入字符串:\n");
        while(1)
        {
            *(p+index++) = getchar();
            if(*(p+index-1)=='\n')
            {
                *(p+index-1) = '\0';
                words[i] = (char*)malloc(sizeof(char)*index);
                strcpy(words[i],word);
                break;    
            }
            
            if(index==MAX_LENGTH-1)
            {
                *(p+index) = '\0';
                //words[i] = p;
                words[i] = (char*)malloc(sizeof(char)*(index+1));
                strcpy(words[i],p);
                break;    
            }
            
        }          
    }
17.C语言真心牛X  int any_function(int(*pfun)(int,int),int x,int y);
#include <stdio.h>
#include <stdlib.h>
int add(int,int);
int sub(int,int);
int calc(int (*p)(int,int),int,int);


int main(void)
{
    printf("%d\n",calc(add,1,2));
    printf("%d\n",calc(sub,2,1));
    system("pause");
    return 0;    
}

int add(int a,int b)
{
    return a+b;    
}

int sub(int a, int b)
{
    return a-b;    
}

int calc(int (*p)(int,int),int a,int b)
{
    return p(a,b);
}
18.可变参数
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int func(int a,int b,...)
{
    int sum = a+b;
    int value = 0;
    va_list parg;
    va_start(parg,b);
    while((value = va_arg(parg,int)) != 0)
        sum += value;
    va_end(parg);
    return sum;     
}


int main(void)
{
    printf("func = %d\n",func(1,2,3,0));
    
    system("pause");
    return 0;    
}
19.wscanf(L"%lf",&variable); 
20.读取
    printf("输入身份证号码:\n");   
    scanf("%*6d%4d%2d%2d",&year,&month,&day);
    printf("出生信息:%d年%d月%d日",year,month,day);
21. getchar ungetc
22.
#include <stdio.h>
#include <limits.h>
#include <wchar.h>
#include <ctype.h>
#include <wctype.h>
#include <stdlib.h>

int main()
{
    int count = 0;
    char ch = 0;
    for(int code=0;code<=CHAR_MAX;code++)
    {
        ch = (char)code;
        if(isprint(ch))
        {
            if(++count % 32 == 0)
                printf("\n");
            printf("%c",ch);    
        }    
    }
    
    printf("\n");
    
    count = 0;
    wchar_t wch = 0;
    
    for(wchar_t wch=L'a';wch<=L'z';wch++)
    {
        if(count++ % 3 == 0)
            wprintf(L"\n");
        
        wprintf(L" %lc %#x %lc %#x",wch,(long)wch,towupper(wch),(long)towupper(wch));    
    }
    printf("\n");
    system("pause");
    return 0;    
}
23.gets puts getchar putchar
24.double->%lf float->%f
25.单向链表
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    
    struct linkList
    {
      int num;
      struct linkList* next;  
    };
    
    struct linkList* current=NULL;
    struct linkList* previous=NULL;
    struct linkList* first=NULL;
    char input = 0;
    
    while(1)
    {
        fflush(stdin);
        puts("是否输入:(y/n)");
        scanf("%c",&input);
        if(tolower(input)=='n')
            break;
        
        current = (struct linkList*)malloc(sizeof(struct linkList));
        
        if(first==NULL)
            first = current;
        
        if(previous!=NULL)
            previous->next = current;
            
        puts("输入链表值:");
        scanf("%d",&current->num);
        current->next = NULL;
        previous = current;
    }
    
    current = first;
    
    while(current!=NULL)
    {
        printf("当前链表值为:%d\n",current->num);
        previous = current;
        current = current->next;
        free(previous);
    }
    
    system("pause");
    return 0;    
}
26.二叉树
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int value;
    int count;
    struct Node* pLeft;
    struct Node* pRight;    
};

struct Node* createNode(int value);
void addNode(struct Node* pNode,int value); 
void listNode(struct Node* pNode);

int main()
{
    struct Node* root = createNode(100);
    addNode(root,90);
    addNode(root,95);
    listNode(root);
    system("pause");
    return 0;    
}

struct Node* createNode(int value)
{
    struct Node* pNode = (struct Node*)malloc(sizeof(struct Node));
    pNode->value = value;
    pNode->count = 1;
    pNode->pLeft = NULL;
    pNode->pRight = NULL;
    return pNode;       
}

void addNode(struct Node* pNode,int value)
{
    //struct Node* pNode = (struct Node*)malloc(sizeof(struct Node));
    if(value==pNode->value)
    {
        pNode->count += 1;
        return;    
    }
    
    if(value>pNode->value)
    {
        if(pNode->pRight==NULL)
        {
            pNode->pRight = createNode(value);
            return;    
        }
        else
        {
            return addNode(pNode->pRight,value);    
        }
    }
    else
    {
        if(pNode->pLeft==NULL)
        {
            pNode->pLeft = createNode(value);
            return;    
        }
        else
        {
            return addNode(pNode->pLeft,value);
        }       
    }    
    
    
}

void listNode(struct Node* pNode)
{
    if(pNode->pRight!=NULL)
        listNode(pNode->pRight);
    printf("value=%d\n",pNode->value);
    if(pNode->pLeft!=NULL)
        listNode(pNode->pLeft);    
}
27.typedef用法
==>  typedef struct pts* pPoint;
==>  typedef int(*function_pointer)(int,int);
==>  typedef struct pts Point;
==>  typedef struct pts{int x;int y;int z;}Point; 
28.文件操作
#include <stdio.h>
#include <stdlib.h>


int main()
{
    
    FILE* pfile = fopen("d:\\text2.txt","w");
    if(!pfile)
    {
        printf("wrong...\n");
        system("pause");
        return 1;    
    }
    
    fclose(pfile);
    
    int result = rename("d:\\text1.txt","d:\\text2.txt");
    
    remove("d:\\text2.txt");
    
    system("pause");
    return 0;    
}
29. fputc(mystr[i],pfile);  while((c = fgetc(pfile)) != EOF)
30. sprintf(newstr,"%d.%s",count,mystr);
31. rewind(pfile);
32. //fprintf(stderr,"%s打开出现错误\n",path);  perror(path);
33. feof(pfile)
34. size_t count = fwrite(a,sizeof(a[0]),num_a,pfile);  count = fread(b,sizeof(b[0]),num_a,pfile);
35. remove  成功则返回0,失败则返回-1,错误原因存于errno
36. long ftell(FILE *pfile); fpos_t here = 0; fgetpos(pfile,&here);
37. int fseek(FILE *pfile ,long offset, int origin); //SEEK_SET 文件开头 
38. int fsetpos(FILE *pfile,fpos_t *position);
39. 临时文件  FILE* pfile = tmpfile();  FILE *pfile = tmpnam(NULL);
40. 
FILE *pfile = NULL;
    char name[30];
    int name_len = 0;
    char tel[12];
    int tel_len = 0;
    char answer = 0;
    pfile = fopen("d:\\data.bin","ab+");
    if(pfile==NULL)
        exit(1);
    //输入信息
    do
    {
        fflush(stdin);
        puts("输入姓名:");
        gets(name);
        puts("输入电话:");
        gets(tel);
        name_len = strlen(name);
        tel_len = strlen(tel);
        fwrite(&name_len,sizeof(int),1,pfile);
        fwrite(name,sizeof(char),(name_len+1),pfile);
        fwrite(&tel_len,sizeof(int),1,pfile);
        fwrite(tel,sizeof(char),(tel_len+1),pfile);
        puts("是否继续输入:");
        answer = getchar();   
    }while(answer=='y');
    fclose(pfile);
    //读取信息 
    pfile = fopen("d:\\data.bin","rb");
    if(pfile==NULL)
        exit(1);
    while(1)
    {
        
        fread(&name_len,sizeof(int),1,pfile);
        if(feof(pfile))
            break;
        fread(name,sizeof(char),(name_len+1),pfile);
        fread(&tel_len,sizeof(int),1,pfile);
        fread(tel,sizeof(char),(tel_len+1),pfile);
        printf("姓名:%-10s 电话:%s\n",name,tel);    
    }
    fclose(pfile);
41. #ifdef #ifndef #elif
42. #include <time.h> srand((unsigned int)time(NULL));   int k=x+rand()%(y-x+1); //生成[x,y]范围整数 
43. #define log(var) printf("log: %s=%d\n",#var,var)
44. #include <assert.h> //断言,参数为int,值为0或false时,程序中断
45. cup_time = ((double)(end-start))/CLOCKS_PER_SEC; 
46. 自定义delay函数
void delay(int sec)
{
    clock_t start_time;
    start_time = clock();
    while((clock()-start_time)<(CLOCKS_PER_SEC/1000*sec)){}
}
47. 打印时间 time_t c = time(NULL);printf("%s\n",ctime(&c));
48. 获取时间信息
time_t c = time(NULL);
struct tm* time_data = localtime(&c);
printf("%d\n",time_data->tm_mon);
49. 二进制输出的一些方法
void myfunc(int a)
{
    int arr[100];
    itoa(a,arr,2);    
    printf("a=%d a=%s\n",a,arr);
}
char *change(int val, int base, char *retbuf )
{
    static char    *str = "0123456789ABCDEF";
    char *p;
    char buf[15];

    p = buf+14;
    *p = 0;

    do { *--p = str[val % base]; } while( val /= base );
    strcpy(retbuf,p);
    return retbuf;
}
50. 格式化输出
for(int i=0;i<100;i++)
       printf("%2d%c",i,(i%10==9||i==99)?'\n':' ');
51. 
double myfunc(char s[])
{
    int index = 0;
    int sign = 1;
    double result = 0.0;
    double dot = 1.0;
    int state = 0;
    while(s[index]!='\0')
    {
        if(state==1&&s[index]==' ')
            break;
        while(s[index]==' ')
            index++;
        if(s[index]=='+'||s[index]=='-')
        {
            sign = (s[index]=='+')?1:-1;
            index++;
            state = 1;
        }
        while(s[index]>='0'&&s[index]<='9')
        {
            state = 1;
            result = result*10 + s[index]-'0';
            index++;
        }
        if(s[index]=='.')
        {
            index++;
            state = 1;
        }    
        while(s[index]>='0'&&s[index]<='9')
        { 
            dot *= 0.1;
            result = result + (s[index++]-'0')*dot;
        }
    }
    return sign*result;   
}
52.