7月26日 学习日志

  1. 一、变量的存储类型
  2. 1. register int a = 1;   //定义一个寄存器变量, a存放在寄存器中, 寄存器变量不能取地址
  3. 2. extern int a = 1;     //声明一个外部变量, 声明不用分配空间 (定义变量要分配空间)
  4. 3. static   (1) 在函数外部 static int a = 1;   // static 修饰全局变量, 改变变量的作用域, 只能在当前文件中被调用
  5.                   (函数外部 int a = 1;   // a是全局变量, 其他文件也可调用)
  6.                (2) static void print( );    // static 修饰函数, 改变函数的作用域, 只能在当前文件中被调用
  7.                (3) 在函数内部 static int a = 0;   // static 修饰局部变量, 改变变量生命周期, 直到程序结束才被释放
  8.                     例:
  9.  
  10. #include <stdio.h>
  11.  
  12. void add()
  13. {
  14. static int a = 0;
  15. a++;
  16. printf(" %d", a);
  17. }
  18.  
  19. int main()
  20. {
  21. int i, a;
  22.  
  23. for(i = 0; i < 5; i++)
  24. {
  25. add();
  26. }
  27. return 0;
  28. }
  29. 运行结果为:1   2   3   4   5
  30. 若不加static, 运行结果为:1   1   1   1   1
  31.  
  32. 二、指针练习
  33. 1.输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
  34. (帧头和帧尾分别是head和tail  字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧)
  35. 方法:参考上一篇比较字符串子串的方法 ,帧头和帧尾分别做比较 ,比较成功一次 ,cmp+1,两次都比较成功时 ,cmp = 2 ,此时输出帧头和帧尾以及中间的字符。
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40.  
  41. char *find(char *str, char *head, char *tail)
  42. {
  43. int len_str, len_head, len_tail;
  44. int i, j, k, n, cmp = 0;
  45. len_str = strlen(str);
  46. len_head = strlen(head);
  47. len_tail = strlen(tail);
  48. n = len_str - (len_head + len_tail);
  49.  
  50. for(i = 0; i <= n; i++)
  51. {
  52. if(strncmp(str + i, head, len_head) == 0)
  53. {
  54. cmp++;
  55. break;
  56. }
  57. }
  58. for(j = i + len_head; j <= len_str - len_tail; j++)
  59. {
  60. if(strncmp(str + j, tail, len_tail) == 0)
  61. {
  62. cmp++;
  63. break;
  64. }
  65. }
  66.  
  67. if(2 == cmp)
  68. {
  69. for(k = i; k <= j + len_tail; k++)
  70. {
  71. printf("%c", *(str + k));
  72. }
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. char *str = NULL;
  79. char *head = NULL;
  80. char *tail = NULL;
  81. str = (char*)malloc(sizeof(char)*20);
  82. head = (char*)malloc(sizeof(char)*10);
  83. tail = (char*)malloc(sizeof(char)*10);
  84.  
  85. printf("Please input a string, head string, tail string:\n");
  86. scanf("%s%s%s", str, head, tail);
  87.  
  88. find(str, head, tail);
  89.  
  90. printf("\n");
  91.  
  92. return 0;
  93. }
  94. 2.使用指针将字符串排序 
  95. 方法:使用指针数组,每输入一个字符串就向操作系统申请一个空间 ,然后使用strcmp,逐个字符串比较,先让第一个字符串与后面所有的字符串比较 ,交换位置,再让第二个字符串与后面所有的字符串比较,交换位置......以此类推,直到比较结束。
  96.  
  97. #include <stdio.h>
  98. #include <string.h>
  99. #include <stdlib.h>
  100.  
  101. void sort(char *str[], int n)
  102. {
  103. char *temp;
  104. temp = (char *)malloc(sizeof(char) * 50);
  105. int i, j;
  106.  
  107. for (i = 0; i < n - 1; i++)
  108. {
  109. for (j = i + 1; j < n; j++)
  110. {
  111. if (strcmp(str[i], str[j]) > 0)
  112. {
  113. strcpy(temp, str[j]);
  114. strcpy(str[j], str[i]);
  115. strcpy(str[i], temp);
  116. }
  117. }
  118. }
  119.  
  120. for (i = 0; i < n; i++)
  121. {
  122. printf(" %s", str[i]);
  123. }
  124. }
  125.  
  126. int main()
  127. {
  128. char *str[50];
  129. int n, i;
  130.  
  131. printf("Please input n:");
  132. scanf("%d", &n);
  133. printf("Please input n string:");
  134.  
  135. for (i = 0; i < n; i++)
  136. {
  137. str[i] = (char *)malloc(sizeof(char) * 20);
  138. scanf("%s", str[i]);
  139. }
  140.  
  141. sort(str, n);
  142.  
  143. printf("\n");
  144. return 0;
  145. }
  146.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值