[C语言] C 语言实例 呕心沥血之作 快速提升C能力

[复制链接]
3031|27
 楼主| 一路向北lm 发表于 2018-10-12 20:06 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2018-10-12 20:15 编辑

C 语言实例1—— 打印0~100之间所有质数
  1. #include <stdlib.h>
  2. int main()
  3. {
  4.   int number;
  5.   int divisor;
  6. /*
  7. ** One and two are easy.
  8. */
  9. printf( "1\n2\n" );
  10. /*
  11. ** No other even numbers are prime; look at the remaining odd ones.
  12. */
  13. for( number = 3; number <= 100; number = number + 2 )
  14. {
  15.   for( divisor = 3; divisor < number; divisor = divisor + 2 )
  16.   {
  17.     if( number % divisor == 0 )
  18.     break;
  19.   }
  20. /*
  21. ** If the loop above stopped because the divisor got too big,
  22. ** we’ve got a prime.
  23. */
  24. if( divisor >= number )
  25. printf( "%d\n", number );
  26. }
  27. }


 楼主| 一路向北lm 发表于 2018-10-12 20:06 | 显示全部楼层
本帖最后由 一路向北lm 于 2018-10-12 20:09 编辑

C 语言实例2—— 输入三角形三边长度,判断是什么类型三角形(等边、等腰、不能构成三角形)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5. float a;
  6. float b;
  7. float c;
  8. float temp;
  9. /*
  10. ** Prompt for and read the data.
  11. */
  12. printf( "Enter the lengths of the three sides of the triangle: " );
  13. scanf( "%f %f %f", &a, &b, &c );
  14. /*
  15. ** Rearrange the values so that a is the longest and c is the shortest.
  16. */
  17. if( a < b )
  18. {
  19. temp = a;
  20. a = b;
  21. b = temp;
  22. }
  23. if( a < c )
  24. {
  25. temp = a;
  26. a = c;
  27. c = temp;
  28. }
  29. if( b < c )
  30. {
  31. temp = b;
  32. b = c;
  33. c = temp;
  34. }
  35. /*
  36. ** Now see what kind of triangle it is. Note that if any of the sides
  37. ** is <= 0 (and we really only have to check the shortest one for this),
  38. ** or if the two shorter sides together are shorter than the longest
  39. ** side, it isn’t a triangle at all.
  40. */
  41. if( c <= 0 || b + c < a )
  42. printf( "Not a triangle.\n" );
  43. else if( a == b && b == c )
  44. printf( "Equilateral.\n" );
  45. else if( a == b || b == c )
  46. printf( "Isosceles.\n" );
  47. else
  48. printf( "Scalene.\n" );
  49. return EXIT_SUCCESS;
  50. }


 楼主| 一路向北lm 发表于 2018-10-12 20:10 | 显示全部楼层
C 语言实例3——C与指针快速上手例子程序读取一串列标号,这些列标号成对出现,表示输入行的列范围。这串列标号以一个负值结尾,作为结束标志。剩余输入行被程序读入并打印,然后输入行中被选中范围的字符串被提出出来并打印。说的有点模糊,来个实例,输入:

4  9  12  20  -1

abcdefghijklmnopqrstuvwxyz

Original input:  abcdefghijklmnopqrstuvwxyz

Rearrange line: efghijmnopqrstu

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>

  4. #define MAX_COLS       20
  5. #define MAX_INPUT      1000


  6. int read_column_numbers( int columns[], int max);
  7. void rearrange( char *output, char const *input,int n_columns,int const columns[]);

  8. int main()
  9. {
  10.    int n_columns;           //进行处理的列标号
  11.    int columns[MAX_COLS];   //需要处理的列数
  12.    char input[MAX_INPUT];   //容纳输入行的数组
  13.    char output[MAX_INPUT];  //容纳输出行的数组

  14.    //读取该串列标号
  15.    n_columns = read_column_numbers(columns,MAX_COLS);

  16.    //读取 处理和打印剩余的输入行
  17.    while(gets(input)!=NULL)
  18.    {
  19.      printf("Original input : %s\n",input);
  20.          rearrange(output,input,n_columns,columns);
  21.          printf("Rearranged line: %s\n",output);
  22.    
  23.    }
  24.    return EXIT_SUCCESS;

  25. }


  26. // 读取列标号,超出规定范围则不会理睬
  27. int read_column_numbers( int columns[], int max)
  28. {
  29.     int num=0;
  30.         int ch;
  31.    
  32.     // 取得列标号 如果读取的数小于0 则停止
  33.         while( num<max && scanf("%d",&columns[num])==1 && columns[num]>=0 )
  34.          num +=1;
  35.         if(num%2!=0)
  36.         {
  37.           puts("Last column number is not paired");
  38.           exit(EXIT_FAILURE);
  39.         }
  40.     // 丢弃改行中包含最后一个数字的那部分内容
  41.         while( (ch=getchar())!=EOF && ch!='\n');
  42.         return num;
  43. }

  44. // 处理输入行,将制定的字符链接在一起,输出行以NUL结尾
  45. void rearrange( char *output, char const *input,int n_columns,int const columns[])
  46. {
  47.      
  48.      int col ;        //columns数组的下标
  49.          int output_col; //输出列计数器
  50.      int len;        //输入行的长度
  51.          len=strlen(input);
  52.      output_col =0;
  53.   //处理每对列标号
  54.   for( col =0;col<n_columns;col+=2)
  55.   {
  56.       int nchars = columns[col+1]-columns[col]+1;

  57.       //如果输入行结束或输出行数已满,就结束任务
  58.           if(columns[col]>=len || output_col==MAX_INPUT-1)
  59.                   break;
  60.           //如果输出行数空间不够,只复制可以容纳的数据
  61.           if(output_col+nchars>MAX_INPUT-1)
  62.                   nchars = MAX_INPUT -output_col-1;
  63.           //复制相关数据
  64.           strncpy(output+output_col,input+columns[col],nchars);
  65.           output_col+=nchars;
  66.    
  67.   }
  68.   output[output_col]='\0';

  69. }




 楼主| 一路向北lm 发表于 2018-10-12 20:11 | 显示全部楼层
C 语言实例4——建立简单的静态链表为了建立链表 使head指向a节点,a.next指向b节点,b.next指向c节点这就构成了链表关系。
  1. #include<stdio.h>
  2. struct Student
  3. {
  4.    int num;
  5.    float score;
  6.    struct Student *next;
  7. };

  8. int main()
  9. {
  10.      struct Student a,b,c,*head, *p;
  11.      a.num=10101;
  12.          a.score=89.5;
  13.          b.num=10103;
  14.          b.score=90;
  15.          c.num=10107;
  16.          c.score=85;

  17.          head=&a;
  18.          a.next=&b;
  19.          b.next=&c;
  20.          c.next=NULL;
  21.          p=head;


  22.          do  
  23.          {
  24.            printf("%ld,%5.1f\n",p->num,p->score);
  25.            p=p->next;

  26.          }
  27.          while(p!=NULL);
  28.          return 0;
  29. }



 楼主| 一路向北lm 发表于 2018-10-12 20:12 | 显示全部楼层
C 语言实例5——单链表 头插法
  1. // 链表的头插入
  2. #include<stdio.h>
  3. #include<stdlib.h>


  4. // 单链表声明
  5. struct Book
  6. {
  7.    //信息域
  8.         char title[128];
  9.         char author[40];
  10.         //指针域
  11.    struct Book * next;
  12. };


  13. void getInput(struct Book *book);
  14. void addBook(struct Book **library) ;
  15. void printlibrary(struct Book *library);
  16. void relsaselibrary(struct Book **library);



  17. void getInput(struct Book *book)
  18. {
  19.   printf("请输入书名:");
  20.   scanf("%s",book->title);
  21.   printf("请输入作者:");
  22.   scanf("%s",book->author);
  23. }

  24. void addBook(struct Book **library)  //参数library (head指针)是head指针的值。
  25. {
  26.   struct  Book  *book, *temp;

  27.   // 在堆里面申请新的节点
  28.   book =(struct Book *)malloc(sizeof(struct Book));
  29.   if(book==NULL)
  30.   {
  31.     printf("内存分配失败");
  32.         exit(1);
  33.   }

  34.   // 为新节点填充信息域内容
  35.   getInput(book);

  36.   if(*library != NULL)
  37.   {
  38.      // 把新的节点插入到链表的头部
  39.           temp = *library;   //保存原来head指针指向的地址
  40.           *library = book;   //把head指针指向新节点
  41.           book->next=temp;   //把新节点的指针域指向原来head指针指向的地址
  42.   
  43.   }
  44.   else   // 如果为NULL 空的单链表
  45.   {
  46.     *library = book;
  47.         book->next=NULL;  
  48.   }
  49. }

  50. void printlibrary(struct Book *library)
  51. {
  52.    struct Book *book;
  53.    int count =1;

  54.    book = library;
  55.    while(book != NULL)
  56.    {
  57.      printf("Book%d: " ,count);
  58.          printf("书名:%s",book->title);
  59.          printf("作者:%s",book->author);
  60.         // printf('\n');
  61.          book = book->next;
  62.          count++;
  63.    
  64.    }

  65. }

  66. void relsaselibrary(struct Book **library)
  67. {
  68.     struct Book *temp; // 临时变量

  69.         while(*library !=NULL)    //直到library头指针指向NULL
  70.         {
  71.             temp = *library;
  72.                 *library =(*library)->next;
  73.                 free(temp);
  74.         }

  75. }
  76. int main()
  77. {
  78.   
  79.           struct Book *library = NULL;
  80.           int ch;
  81.       while(1)
  82.           {
  83.               printf("请问是否需要录入书籍信息(Y/N)");
  84.                   do
  85.                   {
  86.                        ch = getchar();
  87.                  
  88.                   }
  89.               while( ch !='Y' && ch!='N');

  90.               if(ch == 'Y')
  91.                   {
  92.                   addBook(&library);
  93.                   }
  94.               else
  95.                   {
  96.                   break;
  97.                   }
  98.          
  99.           }
  100.            printlibrary(library);
  101.        relsaselibrary(&library);
  102.            return 0;
  103. }



 楼主| 一路向北lm 发表于 2018-10-12 20:13 | 显示全部楼层
C 语言实例6——单链表 尾插法
1. 遍历方式(不推荐,效率比较低。需要遍历整个链表。)
  1. // 链表的尾插入
  2. #include<stdio.h>
  3. #include<stdlib.h>


  4. // 单链表声明
  5. struct Book
  6. {
  7.    //信息域
  8.         char title[128];
  9.         char author[40];
  10.         //指针域
  11.    struct Book * next;
  12. };


  13. void getInput(struct Book *book);
  14. void addBook(struct Book **library) ;
  15. void printlibrary(struct Book *library);
  16. void relsaselibrary(struct Book **library);



  17. void getInput(struct Book *book)
  18. {
  19.   printf("请输入书名:");
  20.   scanf("%s",book->title);
  21.   printf("请输入作者:");
  22.   scanf("%s",book->author);
  23. }

  24. void addBook(struct Book **library)  //参数library (head指针)是head指针的值。
  25. {
  26.   struct  Book  *book, *temp;

  27.   // 在堆里面申请新的节点
  28.   book =(struct Book *)malloc(sizeof(struct Book));
  29.   if(book==NULL)
  30.   {
  31.     printf("内存分配失败");
  32.         exit(1);
  33.   }

  34.   // 为新节点填充信息域内容
  35.   getInput(book);

  36.   if(*library != NULL)
  37.   {
  38.      // 把新的节点插入到链表的尾部
  39.           temp = *library;   //保存原来head指针指向的地址
  40.           while(temp->next !=NULL)   // 定义到链表尾部
  41.           {
  42.              temp=temp->next;
  43.           }
  44.           // 插入数据
  45.           temp->next = book;  
  46.           book->next=NULL;   
  47.   
  48.   }
  49.   else   // 如果为NULL 空的单链表
  50.   {
  51.     *library = book;
  52.         book->next=NULL;  
  53.   }
  54. }

  55. void printlibrary(struct Book *library)
  56. {
  57.    struct Book *book;
  58.    int count =1;

  59.    book = library;
  60.    while(book != NULL)
  61.    {
  62.      printf("Book%d: " ,count);
  63.          printf("书名:%s",book->title);
  64.          printf("作者:%s\n",book->author);
  65.        
  66.          book = book->next;
  67.          count++;
  68.    
  69.    }

  70. }

  71. void relsaselibrary(struct Book **library)
  72. {
  73.     struct Book *temp; // 临时变量

  74.         while(*library !=NULL)    //直到library头指针指向NULL
  75.         {
  76.             temp = *library;
  77.                 *library =(*library)->next;
  78.                 free(temp);
  79.         }

  80. }
  81. int main()
  82. {
  83.   
  84.           struct Book *library = NULL;
  85.           int ch;
  86.       while(1)
  87.           {
  88.               printf("请问是否需要录入书籍信息(Y/N):");
  89.                   do
  90.                   {
  91.                        ch = getchar();
  92.                  
  93.                   }
  94.               while( ch !='Y' && ch!='N');

  95.               if(ch == 'Y')
  96.                   {
  97.                   addBook(&library);
  98.                   }
  99.               else
  100.                   {
  101.                   break;
  102.                   }
  103.          
  104.           }
  105.            printlibrary(library);
  106.        relsaselibrary(&library);
  107.            return 0;
  108. }

2. 不需要遍历,添加指向链表尾的指针(推荐使用,效率高)。
  1. // 链表的尾插入(不需要遍历)
  2. #include<stdio.h>
  3. #include<stdlib.h>


  4. // 单链表声明
  5. struct Book
  6. {
  7.    //信息域
  8.         char title[128];
  9.         char author[40];
  10.         //指针域
  11.    struct Book * next;
  12. };


  13. void getInput(struct Book *book);
  14. void addBook(struct Book **library) ;
  15. void printlibrary(struct Book *library);
  16. void relsaselibrary(struct Book **library);



  17. void getInput(struct Book *book)
  18. {
  19.   printf("请输入书名:");
  20.   scanf("%s",book->title);
  21.   printf("请输入作者:");
  22.   scanf("%s",book->author);
  23. }

  24. void addBook(struct Book **library)  //参数library (head指针)是head指针的值。
  25. {
  26.   struct  Book  *book;
  27.   static struct  Book  *tail;   //指向链表尾部
  28.   // 在堆里面申请新的节点
  29.   book =(struct Book *)malloc(sizeof(struct Book));
  30.   if(book==NULL)
  31.   {
  32.     printf("内存分配失败");
  33.         exit(1);
  34.   }

  35.   // 为新节点填充信息域内容
  36.   getInput(book);

  37.   if(*library != NULL)
  38.   {
  39.           // 插入数据
  40.           tail->next = book;  
  41.           book->next=NULL;   
  42.   
  43.   }
  44.   else   // 如果为NULL 空的单链表
  45.   {
  46.     *library = book;
  47.         book->next=NULL;  
  48.   }
  49.   tail =book;
  50. }

  51. void printlibrary(struct Book *library)
  52. {
  53.    struct Book *book;
  54.    int count =1;

  55.    book = library;
  56.    while(book != NULL)
  57.    {
  58.      printf("Book%d: " ,count);
  59.          printf("书名:%s",book->title);
  60.          printf("作者:%s\n",book->author);
  61.        
  62.          book = book->next;
  63.          count++;
  64.    
  65.    }

  66. }

  67. void relsaselibrary(struct Book **library)
  68. {
  69.     struct Book *temp; // 临时变量

  70.         while(*library !=NULL)    //直到library头指针指向NULL
  71.         {
  72.             temp = *library;
  73.                 *library =(*library)->next;
  74.                 free(temp);
  75.         }

  76. }
  77. int main()
  78. {
  79.   
  80.           struct Book *library = NULL;
  81.           int ch;
  82.       while(1)
  83.           {
  84.               printf("请问是否需要录入书籍信息(Y/N):");
  85.                   do
  86.                   {
  87.                        ch = getchar();
  88.                  
  89.                   }
  90.               while( ch !='Y' && ch!='N');

  91.               if(ch == 'Y')
  92.                   {
  93.                   addBook(&library);
  94.                   }
  95.               else
  96.                   {
  97.                   break;
  98.                   }
  99.          
  100.           }
  101.            printlibrary(library);
  102.        relsaselibrary(&library);
  103.            return 0;
  104. }





 楼主| 一路向北lm 发表于 2018-10-12 20:16 | 显示全部楼层
C 语言实例7——单链表 的搜索
  1. // 单链表的搜索
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>

  5. // 单链表声明
  6. struct Book
  7. {
  8.    //信息域
  9.         char title[128];
  10.         char author[40];
  11.         //指针域
  12.    struct Book * next;
  13. };


  14. void getInput(struct Book *book);
  15. void addBook(struct Book **library) ;
  16. void printlibrary(struct Book *library);
  17. void relsaselibrary(struct Book **library);
  18. struct Book  *searchBook(struct Book *library,char *target);
  19. void printBook(struct Book *book);



  20. void getInput(struct Book *book)
  21. {
  22.   printf("请输入书名:");
  23.   scanf("%s",book->title);
  24.   printf("请输入作者:");
  25.   scanf("%s",book->author);
  26. }

  27. void addBook(struct Book **library)  //参数library (head指针)是head指针的值。
  28. {
  29.   struct  Book  *book;
  30.   static struct  Book  *tail;   //指向链表尾部
  31.   // 在堆里面申请新的节点
  32.   book =(struct Book *)malloc(sizeof(struct Book));
  33.   if(book==NULL)
  34.   {
  35.     printf("内存分配失败");
  36.         exit(1);
  37.   }

  38.   // 为新节点填充信息域内容
  39.   getInput(book);

  40.   if(*library != NULL)
  41.   {
  42.           // 插入数据
  43.           tail->next = book;  
  44.           book->next=NULL;   
  45.   
  46.   }
  47.   else   // 如果为NULL 空的单链表
  48.   {
  49.     *library = book;
  50.         book->next=NULL;  
  51.   }
  52.   tail =book;
  53. }

  54. void printlibrary(struct Book *library)
  55. {
  56.    struct Book *book;
  57.    int count =1;

  58.    book = library;
  59.    while(book != NULL)
  60.    {
  61.      printf("Book%d: " ,count);
  62.          printf("书名:%s",book->title);
  63.          printf("作者:%s\n",book->author);
  64.        
  65.          book = book->next;
  66.          count++;
  67.    
  68.    }

  69. }

  70. void relsaselibrary(struct Book **library)
  71. {
  72.     struct Book *temp; // 临时变量

  73.         while(*library !=NULL)    //直到library头指针指向NULL
  74.         {
  75.             temp = *library;
  76.                 *library =(*library)->next;
  77.                 free(temp);
  78.         }
  79. }

  80. struct Book  *searchBook(struct Book *library,char *target)
  81. {
  82.   struct Book *book;
  83.   book = library;
  84.   while(book != NULL)
  85.   {
  86.           if(!strcmp(book->author,target)|| !strcmp(book->title,target))
  87.           {
  88.             break;
  89.           }
  90.           book = book->next;
  91.   }
  92.   return book;   //返回节点的指针
  93. }

  94. void printBook(struct Book *book)
  95. {
  96.   printf("书名:%s",book->title);
  97.   printf("作者:%s",book->author);
  98. }

  99. int main()
  100. {
  101.   
  102.           struct Book *library = NULL;
  103.           struct Book *book;
  104.           int ch;
  105.           char input[128];
  106.       while(1)
  107.           {
  108.               printf("请问是否需要录入书籍信息(Y/N):");
  109.                   do
  110.                   {
  111.                        ch = getchar();
  112.                  
  113.                   }
  114.               while( ch !='Y' && ch!='N');

  115.               if(ch == 'Y')
  116.                   {
  117.                   addBook(&library);
  118.                   }
  119.               else
  120.                   {
  121.                   break;
  122.                   }
  123.          
  124.           }
  125.            printlibrary(library);

  126.            printf("请输入书名或作者:");
  127.            scanf("%s",input);
  128.            book =searchBook(library,input);
  129.            if(book ==NULL)
  130.            {
  131.              printf("没有找到相关书籍\n");
  132.            }
  133.            else
  134.            {
  135.             do
  136.                 {
  137.                   printf("已找到符合书籍...\n");
  138.                   printBook(book);
  139.                 }
  140.                 while((book=searchBook(book->next,input))!= NULL);
  141.            }
  142.        relsaselibrary(&library);
  143.            return 0;
  144. }



smartpower 发表于 2018-10-15 09:02 | 显示全部楼层
在MCU一般不太用到这么高深的技术。用得最多的就一个FOR。
877049204 发表于 2018-10-15 10:10 | 显示全部楼层
顶一个,有参考书或者电子文档分享一下更好
eydj2008 发表于 2018-10-15 11:23 | 显示全部楼层
是的在 MCU中 我还没使用过链表 ,    不知道哪些地方有用,   感觉是数据库用的.

评论

内存管理  发表于 2018-10-16 13:35
1949zbh 发表于 2018-10-15 14:01 | 显示全部楼层
顶一下
Vansm 发表于 2018-10-15 17:51 | 显示全部楼层
记得上大学的时候我们C语言老师自己做了个题库  里面有一千个题,整个大一都在做题中度过,现在想想都心有余悸
yangzhi1023 发表于 2018-10-16 09:43 | 显示全部楼层
就这么点了吗? 直接整理好打包发出来不是更好吗?
caijie001 发表于 2018-10-16 14:08 | 显示全部楼层
一言不合放代码
 楼主| 一路向北lm 发表于 2018-10-16 23:07 | 显示全部楼层
eydj2008 发表于 2018-10-15 11:23
是的在 MCU中 我还没使用过链表 ,    不知道哪些地方有用,   感觉是数据库用的. ...

RTT 就用到了啊
 楼主| 一路向北lm 发表于 2018-10-16 23:07 | 显示全部楼层

吓唬他们
 楼主| 一路向北lm 发表于 2018-10-16 23:07 | 显示全部楼层
yangzhi1023 发表于 2018-10-16 09:43
就这么点了吗? 直接整理好打包发出来不是更好吗?

还没整理好呢
 楼主| 一路向北lm 发表于 2018-10-16 23:08 | 显示全部楼层
Vansm 发表于 2018-10-15 17:51
记得上大学的时候我们C语言老师自己做了个题库  里面有一千个题,整个大一都在做题中度过,现在想想都心有 ...

唉,自己一天练习点,很有用吧
 楼主| 一路向北lm 发表于 2018-10-16 23:08 | 显示全部楼层
 楼主| 一路向北lm 发表于 2018-10-16 23:08 | 显示全部楼层
877049204 发表于 2018-10-15 10:10
顶一个,有参考书或者电子文档分享一下更好

自己写的,没有什么参考书呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部