飞道的博客

c现代方法 13章程序设计题 自己编写答案

372人阅读  评论(0)

1.第一题错在哪里了?


  
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 100
  4. #define LENGTH 20
  5. int main()
  6. {
  7. char word[LENGTH];
  8. char words[LEN + 1][LENGTH + 1];
  9. scanf( "%s",word);
  10. char (*p)[LENGTH + 1];
  11. p = words;
  12. char *smallest = "";
  13. char *biggest = "";
  14. while( strlen(word) != 4)
  15. {
  16. strcpy(*p ++,word);
  17. if( strcmp(smallest,word) > 0)
  18. smallest = word;
  19. if( strcmp(biggest,word) < 0)
  20. biggest = word;
  21. scanf( "%s",word);
  22. }
  23. printf( "%s\n",biggest);
  24. printf( "%s\n",smallest);
  25. return 0;
  26. }

改正:


  
  1. //方法1:smallest和biggest定义为char *
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. /*LEN是words的单词个数,LENGTH是words每个单词的预留长度*/
  6. #define LEN 100
  7. #define LENGTH 20
  8. int init(char (*)[LENGTH + 1],int );
  9. void find_big_small(char (*)[LENGTH + 1]);
  10. void print(char (*p)[LENGTH + 1]);
  11. int main()
  12. {
  13. char words[LEN + 1][LENGTH + 1];
  14. init(words,LEN + 1);
  15. print(words);
  16. find_big_small(words);
  17. return 0;
  18. }
  19. /* initial the words */
  20. int init(char (*p)[LENGTH + 1],int n)
  21. {
  22. char word[LENGTH + 1];
  23. scanf( "%s",word);
  24. int cnt = 0;
  25. while( strlen(word) != 4 && (cnt < n))
  26. {
  27. strcpy(*p ++,word);
  28. scanf( "%s",word);
  29. ++ cnt;
  30. }
  31. if(cnt == n)
  32. strcpy(*p, "");
  33. else
  34. {
  35. strcpy(*p ++ ,word);
  36. strcpy(*p, "");
  37. ++ cnt;
  38. }
  39. return cnt;
  40. }
  41. /* find smallest and biggest word */
  42. void find_big_small( char (*p)[LENGTH + 1])
  43. {
  44. //small和big是拷贝,这两个赋值都得拷贝
  45. char small[LENGTH + 1];
  46. char big[LENGTH + 1];
  47. strcpy(small,*p);
  48. strcpy(big,*p);
  49. for(; strcmp(*p, "") != 0;++p)
  50. {
  51. if( strcmp(small,*p) > 0)
  52. strcpy(small,*p);
  53. if( strcmp(big,*p) < 0)
  54. strcpy(big,*p);
  55. }
  56. printf( "small:%s\n",small);
  57. printf( "big :%s\n",big);
  58. }
  59. /* print the words */
  60. void print(char (*p)[LENGTH + 1])
  61. {
  62. while( strcmp(*p, "") != 0)
  63. {
  64. printf( "%s\n",*p ++);
  65. }
  66. }

自己评注:什么都好,就是在find_big_small中small和big都执行拷贝,下面这个程序不执行字符串拷贝,

下面是执行结果:


  
  1. dog
  2. zebra
  3. rabbit
  4. catfish
  5. walrus
  6. cat
  7. fish
  8. ---------------------- begin print -----------------------
  9. dog
  10. zebra
  11. rabbit
  12. catfish
  13. walrus
  14. cat
  15. fish
  16. -------------------------- end-----------------------------
  17. small:cat
  18. big :zebra

把程序中查找中的small和big换成指针后,程序如下:


  
  1. //方法1:smallest和biggest定义为char *
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #define LEN 100
  6. #define LENGTH 20
  7. int init(char (*)[LENGTH + 1],int );
  8. void find_big_small(char (*)[LENGTH + 1]);
  9. void print(char (*p)[LENGTH + 1]);
  10. int main()
  11. {
  12. char words[LEN + 1][LENGTH + 1];
  13. init(words,LEN + 1);
  14. print(words);
  15. find_big_small(words);
  16. return 0;
  17. }
  18. int init(char (*p)[LENGTH + 1],int n)
  19. {
  20. char word[LENGTH + 1];
  21. scanf( "%s",word);
  22. int cnt = 0;
  23. while( strlen(word) != 4 && (cnt < n))
  24. {
  25. strcpy(*p ++,word);
  26. scanf( "%s",word);
  27. ++ cnt;
  28. }
  29. if(cnt == n)
  30. strcpy(*p, "");
  31. else
  32. {
  33. strcpy(*p ++ ,word);
  34. strcpy(*p, "");
  35. ++ cnt;
  36. }
  37. return cnt;
  38. }
  39. void find_big_small( char (*p)[LENGTH + 1])
  40. {
  41. char (*small)[LENGTH + 1];
  42. char (*big)[LENGTH + 1];
  43. small = p;
  44. big = p;
  45. for(; strcmp(*p, "") != 0;++p)
  46. {
  47. //small big is pointer which pointed to array
  48. if( strcmp(*small,*p) > 0)
  49. small = p;
  50. if( strcmp(*big,*p) < 0)
  51. big = p;
  52. }
  53. printf( "small:%s\n",*small);
  54. printf( "big :%s\n",*big);
  55. }
  56. void print(char (*p)[LENGTH + 1])
  57. {
  58. while( strcmp(*p, "") != 0)
  59. {
  60. printf( "%s\n",*p ++);
  61. }
  62. }

 


转载:https://blog.csdn.net/digitalkee/article/details/113796572
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场