(2012年西南赛区初试)
第一题:请找出给定字符串中的大写字母,并按顺序存在给定的输出数组里
- #include <stdio.h>
- #include <stdlib.h>
- int get_word(char * word_front);
- void my_find(char input[], char output[], char des_str[]);
- int
- main(int argc, char *argv[])
- {
- char input[] = "ab aa aaa aaaa b ";
- char des_str[] = "ab";
- char output[sizeof(input)];
- my_find(input, output, des_str);
- puts(output);
- return 0;
- }
- void
- my_find(char input[], char output[], char des_str[])
- {
- char *source_word_p = input;
- char *des_word_p = des_str;
- int str_size = 0;
- int i = 0;
- while(input[i] != '\0'){
- str_size++;
- i++;
- }
- printf("The input[]'s length is %d\n",str_size+1);
- int des_size = 0;
- i = 0;
- while(des_str[i] != '\0'){
- des_size++;
- i++;
- }
- printf("The des_str[]'s length is %d\n", des_size+1);
- /*bad~*/
- while(*source_word_p != '\0'){
- int word_length = 0;
- word_length = get_word(source_word_p);
- printf("word_length is %d\n",word_length);
- int match = 0;
- int flag_start = 0;
- for(i=0; i<(word_length+1); i++){
- if((*des_word_p == '\0') && (*source_word_p ==' ')){
- match == 1;
- printf("The address is %d\n", (source_word_p - input));
- }else if((*des_word_p != '\0') && (*source_word_p !=' ')){
- //if(flag){}
- if((*des_word_p == *source_word_p) || (*des_word_p == '?') ){
- source_word_p++;
- des_word_p++;
- continue;
- }else if(*des_word_p != '*'){
- break;
- }else if(*des_word_p == '*'){
- flag_start = 1;
- des_word_p++;
- continue;
- }else{
- break;
- }
- }else{
- match =0;
- break;
- }
- }
- des_word_p = des_str;
- if(*(source_word_p + word_length) != '\0'){
- source_word_p += (word_length + 1);
- }else{
- break;
- }
- }
- }
- int
- get_word(char *word_front)
- {
- int word_length = 0;
- while((*word_front != ' ') && (*word_front != '\0')){
- word_front++;
- word_length++;
- }
- return word_length;
- }
第二题:请重新排列字符数组中的数字,按由大到小排序,其他字符位置不变,保存到输出数组中。
- #include <stdio.h>
- #include <stdlib.h>
- int my_sort(char input[], char output[]);
- int
- main(int argc, char *argv[])
- {
- char input[] = "ab#9ab%8ks*7sf@6hf!5";
- printf("%d\n",sizeof(input));
- char output[sizeof(input)];
- my_sort(input, output);
- puts(input);
- return 0;
- }
- int
- my_sort(char input[], char output[])
- {
- /**/
- int size = 0;
- int i = 0;
- char *input_str = input;
- while(input_str[size] != '\0'){
- size++;
- }
- printf("%d\n", size+1);
- char min_value = '0';
- char tmp_char;
- char *min_p;
- char *front_p = input;
- /*bad beaf~*/
- int count = 0;
- while(*front_p != '\0'){
-
- /*search all string one time*/
- for(i=(front_p-input_str); i<size; i++){
- /*digit*/
- if(('0' <= input_str[i])&&(input_str[i] <= '9')){
- count++;
- /*get front_p*/
- if(count == 1){
- front_p = &input_str[i];
- min_value = input_str[i];
- }
- /*min value*/
- if(input_str[i] <= min_value){
- min_value = input_str[i];
- min_p = &input_str[i];
- }
- }
- }
-
- /*switch*/
- if(count > 0){
- tmp_char = *front_p;
- *front_p = *min_p;
- *min_p = tmp_char;
- }
- /*control varluable*/
- count = 0;
- front_p++;
- }
- return 0;
- }
第三题:通配符应用,请从给定字符串中找出匹配的单词,*表示多个字符匹配,
?表一个字符匹配。我表示只会做第一题。
- /*
- *还存在很多bug,但是基本像个样子了。花了很多时间~,特别是用GDB调试。GDB?说什么好呢……
- */
- #include <stdio.h>
- #include <stdlib.h>
- int get_word(const char *word_start);
- void print_word_info(char *word, int len);
- int
- main(int argc, char *argv[])
- {
- int len;
- char source_str[] = "This wil wel is great. I will make it.";
- char *aim_str = "i?";
- my_match(source_str, aim_str);
-
- }
- /*
- *匹配函数
- */
- void
- my_match(const char *input_str, const char *input_key)
- {
-
- char *word_start;
- char *word_ptr, *key_ptr;
- int word_len,key_len;
- int flag = 0;
-
- word_start = input_str;
- key_ptr = input_key;
- word_ptr = word_start;
- word_len = get_word(word_start);
- key_len = get_word(key_ptr);
- do{
- if(*word_ptr != ' ' && *word_ptr != '\0' \
- && *word_ptr != ',' && *word_ptr != '.'){
- if(*key_ptr == '*' && *(key_ptr+1) =='\0'){
- word_ptr++;
- flag = 1;
- print_word_info(word_start, word_len);
- }else if(*key_ptr == '*' && *(key_ptr+1) !='\0'){
- if(*word_ptr == *(key_ptr+1)){
- key_ptr += 2;
- word_ptr++;
- }else{
- word_ptr++;
- }
- }else if(*key_ptr == '?'){
- key_ptr++;
- word_ptr++;
- }else if(*key_ptr == *word_ptr){
- key_ptr++;
- word_ptr++;
- }else{
- flag = 1;
- }
- }else if(*key_ptr == '*' && *(key_ptr+1) == '\0' || *key_ptr == '\0'){
- flag = 1;
- print_word_info(word_start, word_len);
- }else{
- flag = 1;
- }
-
- if(flag){
- key_ptr = input_key;
- word_start += (word_len + 1);
- word_ptr = word_start;
- word_len = get_word(word_start);
- flag = 0;
- }
- }while(*(word_start - 1) != '\0');
-
- }
- /*
- *得到一个单词末尾位置(from 1)
- */
- int
- get_word(const char *word_start)
- {
- int i;
- char c;
- for(i = 0; (word_start[i] != '\0') && (word_start[i] != ' '); i++)
- ;
- return i;
- }
- /*
- *输出位置和匹配的单词
- */
- void
- print_word_info(char *word, int len)
- {
- int i;
- for(i = 0; i < len; i++){
- putchar(word[i]);
- }
- printf(" ");
- }
只搞定第一道,反思……
1)开始没有在编程框架内编程,还耗费了比较多的时间在设计输入输出交的互式上。
2)VS6.0还就都没有碰了,有点小影响。现在发现,GDB还是很好用的。
3)一个低级错误,耗费了我很多时间。没有在问题出单步执行,调试。
4)结构,思路,再细节。
5)平时动手太少……practice, practice, and practice!
6)逻辑关系理不清,划分模块做得不好。
7)我需要对我使用的编辑器,编译器更加的熟悉。
按照我现在这个水平,进了决赛很很难胜出的。编程,还是可以感觉到成就感的。敢不敢再给我一次机会!
################################基本完成三道题后的感触################################
8)我编程的时候对问题的分析不够,因而脑子里很乱。
9)之下而上的模块可以写出测试代码。我使用GDB调试,花了很多时间,是我没用好呢?还是字符见面限定了速度。后来尝试了下使用KDbg调试,好点了,毕竟可以调出很多窗口,同时显示。watch的使用要方便些。
10)调试花了我很多时间。掌握好调试技巧会更有效率。
阅读(1074) | 评论(0) | 转发(0) |