Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434030
  • 博文数量: 67
  • 博客积分: 2468
  • 博客等级: 上尉
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-05 01:21
文章分类

全部博文(67)

文章存档

2013年(1)

2012年(65)

2011年(1)

分类: C/C++

2012-04-22 18:42:16

(2012年西南赛区初试)
第一题:请找出给定字符串中的大写字母,并按顺序存在给定的输出数组里

点击(此处)折叠或打开

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

  3. int get_word(char * word_front);
  4. void my_find(char input[], char output[], char des_str[]);

  5. int
  6. main(int argc, char *argv[])
  7. {
  8.     char input[] = "ab aa aaa aaaa b ";
  9.     char des_str[] = "ab";
  10.     char output[sizeof(input)];

  11.     my_find(input, output, des_str);
  12.     puts(output);

  13.     return 0;
  14. }

  15. void
  16. my_find(char input[], char output[], char des_str[])
  17. {
  18.     char *source_word_p = input;
  19.     char *des_word_p = des_str;

  20.     int str_size = 0;
  21.     int i = 0;
  22.     while(input[i] != '\0'){
  23.         str_size++;
  24.         i++;
  25.     }
  26.     printf("The input[]'s length is %d\n",str_size+1);

  27.     int des_size = 0;
  28.     i = 0;
  29.     while(des_str[i] != '\0'){
  30.         des_size++;
  31.         i++;
  32.     }
  33.     printf("The des_str[]'s length is %d\n", des_size+1);

  34.     /*bad~*/
  35.     while(*source_word_p != '\0'){

  36.     int word_length = 0;
  37.     word_length = get_word(source_word_p);
  38.     printf("word_length is %d\n",word_length);

  39.     int match = 0;
  40.     int flag_start = 0;
  41.     for(i=0; i<(word_length+1); i++){
  42.         if((*des_word_p == '\0') && (*source_word_p ==' ')){
  43.             match == 1;
  44.             printf("The address is %d\n", (source_word_p - input));
  45.         }else if((*des_word_p != '\0') && (*source_word_p !=' ')){
  46.             //if(flag){}
  47.             if((*des_word_p == *source_word_p) || (*des_word_p == '?') ){
  48.                 source_word_p++;
  49.                 des_word_p++;
  50.                 continue;
  51.             }else if(*des_word_p != '*'){
  52.                 break;
  53.             }else if(*des_word_p == '*'){
  54.                 flag_start = 1;
  55.                 des_word_p++;
  56.                 continue;
  57.             }else{
  58.                 break;
  59.             }
  60.         }else{
  61.             match =0;
  62.             break;
  63.         }
  64.     }

  65.     des_word_p = des_str;

  66.     if(*(source_word_p + word_length) != '\0'){
  67.         source_word_p += (word_length + 1);
  68.     }else{
  69.         break;
  70.     }

  71.     }
  72. }

  73. int
  74. get_word(char *word_front)
  75. {
  76.     int word_length = 0;
  77.     while((*word_front != ' ') && (*word_front != '\0')){
  78.         word_front++;
  79.         word_length++;
  80.     }

  81.     return word_length;
  82. }

第二题:请重新排列字符数组中的数字,按由大到小排序,其他字符位置不变,保存到输出数组中。

点击(此处)折叠或打开

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

  3. int my_sort(char input[], char output[]);

  4. int
  5. main(int argc, char *argv[])
  6. {
  7.     char input[] = "ab#9ab%8ks*7sf@6hf!5";
  8.     printf("%d\n",sizeof(input));
  9.     char output[sizeof(input)];
  10.     my_sort(input, output);
  11.     puts(input);

  12.     return 0;

  13. }

  14. int
  15. my_sort(char input[], char output[])
  16. {
  17.     /**/
  18.     int size = 0;
  19.     int i = 0;

  20.     char *input_str = input;
  21.     while(input_str[size] != '\0'){
  22.         size++;
  23.     }
  24.     printf("%d\n", size+1);

  25.     char min_value = '0';
  26.     char tmp_char;
  27.     char *min_p;
  28.     char *front_p = input;
  29.     /*bad beaf~*/
  30.     int count = 0;

  31.     while(*front_p != '\0'){
  32.         
  33.         /*search all string one time*/
  34.         for(i=(front_p-input_str); i<size; i++){
  35.             /*digit*/
  36.             if(('0' <= input_str[i])&&(input_str[i] <= '9')){
  37.                 count++;
  38.                 /*get front_p*/
  39.                 if(count == 1){
  40.                     front_p = &input_str[i];
  41.                     min_value = input_str[i];
  42.                 }
  43.                 /*min value*/
  44.                 if(input_str[i] <= min_value){
  45.                     min_value = input_str[i];
  46.                     min_p = &input_str[i];
  47.                 }
  48.             }
  49.         }
  50.         
  51.         /*switch*/
  52.         if(count > 0){
  53.             tmp_char = *front_p;
  54.             *front_p = *min_p;
  55.             *min_p = tmp_char;
  56.         }

  57.         /*control varluable*/
  58.         count = 0;
  59.         front_p++;
  60.     }

  61.     return 0;
  62. }


第三题:通配符应用,请从给定字符串中找出匹配的单词,*表示多个字符匹配,
        ?表一个字符匹配。我表示只会做第一题。

点击(此处)折叠或打开

  1. /*
  2.  *还存在很多bug,但是基本像个样子了。花了很多时间~,特别是用GDB调试。GDB?说什么好呢……
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. int get_word(const char *word_start);
  7. void print_word_info(char *word, int len);

  8. int
  9. main(int argc, char *argv[])
  10. {
  11.     int len;
  12.     char source_str[] = "This wil wel is great. I will make it.";
  13.     char *aim_str = "i?";
  14.     my_match(source_str, aim_str);
  15.     
  16. }

  17. /*
  18.  *匹配函数
  19.  */
  20. void
  21. my_match(const char *input_str, const char *input_key)
  22. {
  23.     
  24.     char *word_start;
  25.     char *word_ptr, *key_ptr;
  26.     int word_len,key_len;
  27.     int flag = 0;
  28.     
  29.     word_start = input_str;
  30.     key_ptr = input_key;
  31.     word_ptr = word_start;
  32.     word_len = get_word(word_start);
  33.     key_len = get_word(key_ptr);

  34.     do{
  35.         if(*word_ptr != ' ' && *word_ptr != '\0' \
  36.             && *word_ptr != ',' && *word_ptr != '.'){
  37.             if(*key_ptr == '*' && *(key_ptr+1) =='\0'){
  38.                 word_ptr++;
  39.                 flag = 1;
  40.                 print_word_info(word_start, word_len);
  41.             }else if(*key_ptr == '*' && *(key_ptr+1) !='\0'){
  42.                 if(*word_ptr == *(key_ptr+1)){
  43.                     key_ptr += 2;
  44.                     word_ptr++;
  45.                 }else{
  46.                     word_ptr++;
  47.                 }
  48.             }else if(*key_ptr == '?'){
  49.                 key_ptr++;
  50.                 word_ptr++;
  51.             }else if(*key_ptr == *word_ptr){
  52.                 key_ptr++;
  53.                 word_ptr++;
  54.             }else{
  55.                 flag = 1;
  56.             }
  57.         }else if(*key_ptr == '*' && *(key_ptr+1) == '\0' || *key_ptr == '\0'){
  58.             flag = 1;
  59.             print_word_info(word_start, word_len);
  60.         }else{
  61.             flag = 1;
  62.         }
  63.         
  64.         if(flag){
  65.             key_ptr = input_key;
  66.             word_start += (word_len + 1);
  67.             word_ptr = word_start;
  68.             word_len = get_word(word_start);
  69.             flag = 0;
  70.         }
  71.     }while(*(word_start - 1) != '\0');
  72.     
  73. }

  74. /*
  75.  *得到一个单词末尾位置(from 1)
  76.  */
  77. int
  78. get_word(const char *word_start)
  79. {
  80.     int i;
  81.     char c;
  82.     for(i = 0; (word_start[i] != '\0') && (word_start[i] != ' '); i++)
  83.     ;
  84.     return i;
  85. }

  86. /*
  87.  *输出位置和匹配的单词
  88.  */
  89. void
  90. print_word_info(char *word, int len)
  91. {
  92.     int i;
  93.     for(i = 0; i < len; i++){
  94.         putchar(word[i]);
  95.     }
  96.     printf(" ");
  97. }



只搞定第一道,反思……
1)开始没有在编程框架内编程,还耗费了比较多的时间在设计输入输出交的互式上。
2)VS6.0还就都没有碰了,有点小影响。现在发现,GDB还是很好用的。
3)一个低级错误,耗费了我很多时间。没有在问题出单步执行,调试。
4)结构,思路,再细节。
5)平时动手太少……practice, practice, and practice!
6)逻辑关系理不清,划分模块做得不好。
7)我需要对我使用的编辑器,编译器更加的熟悉。

按照我现在这个水平,进了决赛很很难胜出的。编程,还是可以感觉到成就感的。敢不敢再给我一次机会!

################################基本完成三道题后的感触################################
8)我编程的时候对问题的分析不够,因而脑子里很乱。
9)之下而上的模块可以写出测试代码。我使用GDB调试,花了很多时间,是我没用好呢?还是字符见面限定了速度。后来尝试了下使用KDbg调试,好点了,毕竟可以调出很多窗口,同时显示。watch的使用要方便些。
10)调试花了我很多时间。掌握好调试技巧会更有效率。



阅读(999) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~