欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2024/4/18 编程问答 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 c现代方法 13章程序设计题 自己编写答案 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1题,下面程序错在哪啦?(改正在后面) 

#include <stdio.h> #include <string.h> #define LEN 100 #define LENGTH 20 int main() { char word[LENGTH + 1]; char words[LEN +1][LENGTH + 1];char (*p)[LENGTH + 1]; p = words; char *smallest; char *biggest; scanf("%s",word);smallest = ""; biggest = "";while(strlen(word) != 4){strcpy(*p ++ ,word);if(strcmp(smallest,word) > 0) smallest = p; if(strcmp(biggest,word) < 0)biggest = p; scanf("%s",word);}printf("%s\n",biggest); printf("%s\n",smallest);return 0; }~

下面有2~3个改正,每个改正都可以运行

改正1:

#include <stdio.h> #include <string.h> #define LEN 100 #define LENGTH 20 int main() { char word[LENGTH + 1]; char words[LEN +1][LENGTH + 1];char (*p)[LENGTH + 1]; p = words; //错误1,smallest和biggest是要指向数组words的元素(元素是维数是LENGTH + 1的数组)的 ,也就是指向 //数组的指针,所以必须定义成数组的指针,下面两句是错误的 char (*smallest)[LENGTH +1];//应该改成 char (*smallest)[LENGTH + 1];//当然也不能赋值为 "" char (*biggest)[LENGTH + 1]; //应该改成 char(*biggest)[LENGTH + 1];//当然也不能赋值为 "" scanf("%s",word);smallest = words; biggest = words;while(strlen(word) != 4){ //smallest 和 biggest一会不能指向word(因为word得内容一直在变化,指向word等于指>向变化量) //所以要一会或许把p值给smallest或者biggest,p值while循环最后面再自增strcpy(*p,word);//去掉了自增,后面再自增if(strcmp(*smallest,*p) > 0) //word改正*psmallest = p; //改成=p if(strcmp(*biggest,word) < 0)biggest = p; //改成=p ++ p;scanf("%s",word);}printf("%s\n",*biggest); printf("%s\n",*smallest);return 0; }~

上面程序错误说明:

#include <stdio.h> #include <string.h> // LEN is the number of elements in the array words #define LEN 100 #define LENGTH 20 int main() { char word[LENGTH + 1]; char words[LEN +1][LENGTH + 1];//p一会要指向数组words,所以需要数组指针,所以如下定义 char (*p)[LENGTH + 1]; p = words; //存放words的元素的指针,因为words是字符串数组(就是数组的数组),所以small和bigge//st必须字符串指针的指针或者数组指针,即使char**也不行char (*smallest)[LENGTH + 1] = words; char (*biggest)[LENGTH + 1] = words;scanf("%s",word); while(strlen(word) != 4){ //word是在不断改变的,所以word不能传给smallest和biggest, //把words的某个地址传给smallest和biggest strcpy(*p,word);if(strcmp(*smallest,*p) > 0)smallest = p;if(strcmp(*biggest,*p) < 0)biggest = p;++ p;scanf("%s",word);}printf("%s\n",*biggest); printf("%s\n",*smallest);return 0; }

改正2:

//方法1:smallest和biggest定义为char * #include <stdio.h> #include <string.h> #include <stdbool.h>/*LEN是words的单词个数,LENGTH是words每个单词的预留长度*/ #define LEN 100 #define LENGTH 20int init(char (*)[LENGTH + 1],int ); void find_big_small(char (*)[LENGTH + 1]); void print(char (*p)[LENGTH + 1]); int main() { char words[LEN + 1][LENGTH + 1]; init(words,LEN + 1); print(words); find_big_small(words);return 0; }/* initial the words */ int init(char (*p)[LENGTH + 1],int n) { char word[LENGTH + 1]; scanf("%s",word); int cnt = 0; while(strlen(word) != 4 && (cnt < n)){strcpy(*p ++,word); scanf("%s",word);++ cnt;} if(cnt == n)strcpy(*p,""); else{strcpy(*p ++ ,word);strcpy(*p,"");++ cnt;}return cnt; } /* find smallest and biggest word */ void find_big_small( char (*p)[LENGTH + 1]) {//small和big是拷贝,这两个赋值都得拷贝char small[LENGTH + 1];char big[LENGTH + 1];strcpy(small,*p);strcpy(big,*p);for(; strcmp(*p,"") != 0;++p){if(strcmp(small,*p) > 0)strcpy(small,*p);if(strcmp(big,*p) < 0)strcpy(big,*p);}printf("small:%s\n",small);printf("big :%s\n",big); }/* print the words */ void print(char (*p)[LENGTH + 1]) { while(strcmp(*p,"") != 0){printf("%s\n",*p ++);} }

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

下面是执行结果:

dog zebra rabbit catfish walrus cat fish ----------------------begin print ----------------------- dog zebra rabbit catfish walrus cat fish --------------------------end----------------------------- small:cat big :zebra

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

//方法1:smallest和biggest定义为char * #include <stdio.h> #include <string.h> #include <stdbool.h> #define LEN 100 #define LENGTH 20int init(char (*)[LENGTH + 1],int ); void find_big_small(char (*)[LENGTH + 1]); void print(char (*p)[LENGTH + 1]); int main() { char words[LEN + 1][LENGTH + 1]; init(words,LEN + 1); print(words); find_big_small(words);return 0; }int init(char (*p)[LENGTH + 1],int n) { char word[LENGTH + 1]; scanf("%s",word); int cnt = 0; while(strlen(word) != 4 && (cnt < n)){strcpy(*p ++,word); scanf("%s",word);++ cnt;} if(cnt == n)strcpy(*p,""); else{strcpy(*p ++ ,word);strcpy(*p,"");++ cnt;}return cnt; }void find_big_small( char (*p)[LENGTH + 1]) {char (*small)[LENGTH + 1];char (*big)[LENGTH + 1];small = p;big = p;for(; strcmp(*p,"") != 0;++p){//small big is pointer which pointed to arrayif(strcmp(*small,*p) > 0)small = p;if(strcmp(*big,*p) < 0)big = p;}printf("small:%s\n",*small);printf("big :%s\n",*big); }void print(char (*p)[LENGTH + 1]) { while(strcmp(*p,"") != 0){printf("%s\n",*p ++);} }

4.

#include <stdio.h>int main(int argc,char **argv) { int i; for(i = 3;i > 0;--i)printf("%s ",argv[i]); printf("\n");return 0; }

运行:

r@r:~/coml/c/13/program/4$ ./reverse void and null null and void

5.

#include <stdio.h> #include <stdlib.h> int main(int argc,char **argv) { int i,sum = 0; for(i = argc - 1;i > 0; --i)sum += atoi(argv[i]); printf("sum = %d\n",sum);return 0; }

注:以上程序使用了atoi函数,原型是   int atoi(const char *str)。定义在stdlib头文件中,作用是把字符串转换成整数。

6.

#include <stdio.h> #include <string.h> #include <ctype.h> #define LEN 20 #define NUM_PLA 100 void trans(char *); int main(int argc,char **argv) { char *planets[] = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus" ,"Neptune","Pluto"}; char temp[LEN + 1]; int i,j; for(i = 1; i != argc; ++ i){strcpy(temp,argv[i]);trans(temp);for(j = 0; j != sizeof(planets)/sizeof(planets[0]);++ j){if( strcmp(planets[j],temp) == 0)break;}if(j == 9)printf("%s is not a planet\n",argv[i]);elseprintf("%s is planet %d\n",argv[i],j + 1);}return 0; } void trans(char *p) { *p ++ = toupper(*p); int i = 0; while(*p != '\0'){*p = tolower(*p);++ p;} }

注:tolower,toupper都定义在ctype头文件中

8.

#include <stdio.h> #include <string.h> #define LEN 20char *decade[] = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninty"}; char *unit[] = {"one","two","three","four","five","six","seven","eight","nine"}; void print_str(int n); int main() { int n; int decade_num,unit_num;printf("Enter a two-digit number:"); scanf("%2d",&n); decade_num = n / 10; unit_num = n % 10; if(decade_num == 1)print_str(n); else {printf("%s ",decade[decade_num -2]);if(unit_num == 0)printf("\n");else printf("%s\n",unit[unit_num - 1]);}return 0; }void print_str(int n) { char str_num[LEN + 1]; switch(n){case 10:strcpy(str_num,"ten");break;case 11:strcpy(str_num,"eleven");break; case 12:strcpy(str_num,"twelve");break; case 13:strcpy(str_num,"thirteen");break; case 14:strcpy(str_num,"fourteen");break; case 15:strcpy(str_num,"fifteen");break; case 16:strcpy(str_num,"sixteen");break; case 17:strcpy(str_num,"seventeen");break; case 18:strcpy(str_num,"eighteen");break; case 19:strcpy(str_num,"nineteen");break; default:printf("wrong number.\n");} printf("%s",str_num); }

运行结果:

r@r:~/coml/c/13/program/7$ gcc 1.c -o 123 r@r:~/coml/c/13/program/7$ ./123 Enter a two-digit number:23 twenty three r@r:~/coml/c/13/program/7$ ./123 Enter a two-digit number:12 twelver@r:~/coml/c/13/program/7$ ./123 Enter a two-digit number:89 eighty nine r@r:~/coml/c/13/program/7$ ./123 Enter a two-digit number:50 fifty

8.

#include <stdio.h> #include <ctype.h> int compute_scrabble_value(const char *); #define LEN 100 int main() { char num_str[LEN + 1],value; scanf("%s",num_str); value = compute_scrabble_value(num_str); printf("%d",value); return 0; }int compute_scrabble_value(const char *s) { int sum = 0,value; char ch; for(;*s != '\0';++ s){ch = tolower(*s);if(ch == 'a' ||ch == 'e' ||ch == 'i' ||ch == 'l' ||ch == 'n' ||ch == 'o' || ch == 'r' ||ch == 's' ||ch == 't' ||ch == 'u')value = 1;else if( ch == 'd' || ch == 'g')value = 2;else if( ch == 'b' || ch == 'm' || ch == 'p'|| ch == 'c' )value = 3;else if(ch == 'f' || ch == 'h' || ch == 'v' || ch == 'w' || ch == 'y')value = 4;else if(ch == 'k')value = 5;else if(ch == 'j'|| ch == 'x')value = 8; else if (ch == 'q' || ch == 'z')value = 10; else { value = 0;printf("%c is is not english word.\n",*s);printf("value is : %d\n",value);}sum += value;}return sum; } pitfall 12

9.

#include <stdio.h> #include <ctype.h> #define LEN 100 int compute_vowel_count(const char*); int main() { int sum; char arr[LEN + 1]; scanf("%s",arr); sum = compute_vowel_count(arr); printf("total have %d vowels.\n",sum);return 0; }int compute_vowel_count(const char *s) { char ch; int sum = 0,cnt; for(;*s != '\0';++ s){ch = tolower(*s);if(ch == 'a' || ch == 'e' || ch =='i' || ch == 'o' || ch == 'u')cnt = 1;else cnt = 0;sum += cnt;}return sum;}

10.

#include <stdio.h> #include <string.h> #include <ctype.h> #define LEN 50 void reverse_name(const char*); int readline(char *,int ); int main() { char name[LEN + 1]; readline(name,LEN+1); reverse_name(name); return 0; } void reverse_name(const char *name) { char surname[LEN + 1]; char name_head; const char *p; char *ptr; int i; for(p = name; *p != '\0';++p){if(*p >= 'A' && *p <= 'Z' || *p >='a' && *p<='z'){ name_head = toupper(*p);break;} }for(i = strlen(name);i > 0;-- i){if(p[i] >= 'A' && p[i] <= 'Z'){break;}} strcpy(surname,name + i); for(ptr = surname;*ptr != '\0';++ ptr){if(*ptr == ' ')*ptr = '\0';} printf("%s, %c\n",surname,name_head);}int readline(char *p,int n) { char ch; int i = 0; while((ch = getchar()) != '\n' && i < n){*p++ = ch;++ i;} *p = '\0'; return i; }

运行结果:

Lloyd Fosdick Fosdick, L

 12.

#include <stdio.h> #include <string.h> #include <ctype.h> #define LEN 20 #define NUM 30 #define MAX_LENGTH 600 int init_(char (*p)[LEN + 1],int n,char *); int readline(char *,int ); void reverse(); int main() {char terminate; char arr[NUM + 1][LEN + 1]; int cnt =init_(arr,NUM +1,&terminate); printf("There is total %d words:\n",cnt); int i; for(i = 0;i != cnt; ++i){printf("%s ",arr[i]); } printf("\n"); reverse(arr,cnt); printf("%c",terminate);return 0; }//return how many words int init_(char (*p)[LEN +1],int n,char *terminate) { char words[MAX_LENGTH + 1]; char *ptr; char word[LEN + 1]; int i = 0; int cnt = 0; readline(words,MAX_LENGTH + 1); *terminate = words[strlen(words) - 1]; words[strlen(words) - 1] = '\0';for(ptr = words;*ptr != '\0';){if(*ptr != ' '){word[i ++] = *ptr ++;}if(*ptr == ' '){word[i ++] = '\0';strcpy(*p ++,word);i = 0;++ cnt;while(*ptr == ' ')++ ptr; }if(*(ptr + 1) == '\0'){word[i ++] = *ptr ++;word[i] = '\0';strcpy(*p ++,word);++ cnt; } } return cnt;}//change the words void reverse(char (*p)[LEN +1],int n) { int i; for(i = n -1;i >= 0; --i){if(i != 0)printf("%s ",p[i]);elseprintf("%s",p[i]);} } int readline(char *p,int n) { char ch; int i = 0; while((ch = getchar()) != '\n'){//读入的时候跳过开始的空白字符if(isspace(ch) && i ==0)continue;if(i < n){ *p ++= ch;++ i;} } *p = '\0'; return i; }

运行结果是: 

i am a good and you are a good too? There is total 10 words: i am a good and you are a good too too good a are you and good a am i?

13 

#include <stdio.h> #include <ctype.h> #include <string.h> void encrypt(char *message,int shift) {int i;for(i = 0;i != strlen(message);++ i){if(message[i] >= 'A' && message[i] <= 'Z'){message[i] += shift;if(message[i] > 'Z')message[i] -= 'Z'-'A'+1; }else if(message[i] >= 'a' && message[i] <= 'z') {message[i] += shift;if(message[i] > 'z')message[i] -= 'z' - 'a' + 1; }else ;} } //本题需要输入时候跳过空格,所以必须自己定义一个字符串输入函数 int read_line(char *s,int n) { int cnt = 0; char ch; while((ch = getchar()) != '\n'){if(cnt == n - 2){printf("stack over flow.");break;}{*s ++ = ch; cnt ++; }*s = '\0'; }} int main(){char test[100];printf("enter a message:");read_line(test,100);printf("enter shift:");int n;scanf("%d",&n);encrypt(test,n); printf("%s",test);return 0;}

运行:

enter a message:Go ahead,make my day. enter shift:3 Jr dkhdg,pdnh pb gdb.

14题. 

#include <stdio.h> #include <ctype.h> #include <stdlib.h> typedef int bool; #define true 1 #define false 0 int alpha_nums[26] = {0}; bool are_anagrams(const char *word1,const char * word2) {int i = 0;int j = 0;char ch1,ch2;while(word1[i] != '\0'){if(isalpha(word1[i])){ch1 = tolower(word1[i]);alpha_nums[ch1 - 'a'] ++;}++ i;}while(word2[j] != '\0'){if(isalpha(word2[j])){ch2 = tolower(word2[j]);alpha_nums[ch2 - 'a']--;}++ j; } int *p = alpha_nums; bool sign = true; while(p != alpha_nums + 26){if(*p ++ != 0){sign = false; break;}} return sign; }int main() { char word1[100]; char word2[100];printf("Enter first word:");scanf("%s",word1);printf("Enter second word:");scanf("%s",word2);bool result = are_anagrams(word1,word2);if(result == true)printf("The two words are anagrams.");elseprintf("The two words are not anagrams"); return 0; }

在ubuntu20.04上运行结果是: 

r@r:~/coml/c/13/program/14$ ./123 Enter first word:smartest Enter second word:mattress The two words are anagrams.r@r:~/coml/c/13/program/14$ ./123 Enter first word:dumbest Enter second word:stumble The two words are not anagrams

15题

#include <stdio.h> #define STACK_SIZE 100 typedef int bool; #define true 1 #define false 0 char contents[STACK_SIZE]; int top = 0;bool is_number(char ); bool is_operator(char ); double evaluate_RPN_expression(const char *); double calculate(double ,double ,char );char pop() {return contents[-- top]; }void push(char ch) {contents[top ++] = ch; }void print(char *p,int n){char *ptr = p;while(ptr != p + n){printf("%c ",*ptr);++ ptr;}} void print_num(int *p,int n) { int *ptr = p; while(ptr != (p + n)){printf("%d",*ptr);++ ptr;}}int main() { char ch; double result; while((ch = getchar()) != '\n'){if(is_operator(ch) || is_number(ch))push(ch);else if(ch == '=')push(ch);elsebreak;} result = evaluate_RPN_expression(contents); printf("%lf",result);return 0; }double evaluate_RPN_expression(const char *expression) { double num_arr[STACK_SIZE]; int index = 0; const char *ptr = expression; double temp;int cnt = 0; while(*ptr != '='){if(is_number(*ptr)){num_arr[index ++] = *ptr - 48;}else if(is_operator(*ptr)){temp = calculate(num_arr[index-2],num_arr[index-1],*ptr); -- index;num_arr[index - 1] = temp; }else {printf("error character:%c.\n\n",*ptr);return -1; } ptr ++;} return num_arr[index - 1]; }double calculate(double d1,double d2,char ope) { double result; if(ope == '-')result = d1 - d2; else if(ope == '+')result = d1 + d2; else if(ope == '*')result = d1 * d2; else if(ope == '/')result = d1 / d2; else{printf("error ope.");result = -1;} return result; }bool is_operator(char ch) { if(ch == '+' || ch == '-' || ch == '*'|| ch == '/')return true; else return false; } bool is_number(char ch) { if(ch >= '0' && ch <= '9')return true; else return false; }

16题 

#include <string.h> #include <stdio.h> #include <string.h> #define MAX_SIZE 100 void reverse(char *p); int readline(char *); int main() { char arr[MAX_SIZE]; readline(arr); reverse(arr); printf("\n"); printf("%s",arr);return 0; } void reverse(char *p) { char *ptr1 = p; char *ptr2 = p + strlen(p) - 1; char temp; while(ptr1 <= ptr2){temp = *ptr1;*ptr1 = *ptr2;*ptr2 = temp;++ ptr1;-- ptr2; } }int readline(char *p) { char ch; int cnt = 0;while((ch = getchar()) != '\n'){if(cnt <= MAX_SIZE - 2){*p = ch;++ p;++ cnt; }elsebreak; }*p = '\0';return cnt; }

17.

#include <stdio.h> #include <ctype.h> #include <string.h> #define MAX_SIZE 100 int readline(char *,int ); typedef int bool; #define true 1 #define false 0 bool is_palindrome(const char *message){ const char *ptr1 = message; //这里不能用sizeof(message) / sizeof(message[0] -1代替下面的一个) // 用sizeof表达式相除算出的结果不对的,sizeof()是计算数组的,不是字符串的 const char *ptr2 = message + strlen(message) - 1; while(ptr1 <= ptr2){//可能得连续跳过非字母的字符while(!isalpha(*ptr1))++ ptr1;//可能连续跳过非字母的字符while(!isalpha(*ptr2))-- ptr2;if(tolower(*ptr1) != tolower(*ptr2))return false;else{ptr1 ++;ptr2 --;}} return true; }int main() { char message[MAX_SIZE + 1]; readline(message,MAX_SIZE); bool result = is_palindrome(message); if(result == true)printf("Palindrome"); elseprintf("Not palindrome"); return 0; }int readline(char *p,int n) { char ch; int cnt = 0; while((ch = getchar()) != '\n'){if(cnt < n){*p ++= ch;++ cnt;}} *p = '\0'; return cnt; }

运行

hE LIVED AS A DEVIL,EH? Palindrome

18.

#include <stdio.h> #include <stdlib.h> #define MAX_SIZE 20 char *month_sets[12] = {"Januday","February","March","Apail","May","June","July","August","September","October","November","December"}; void conversion_format(const char *); int main() { char message[MAX_SIZE]; scanf("%s",message); conversion_format(message); return 0; }void conversion_format(const char *message) { char month[3]; char day[3]; char year[5]; int month_int; //处理月份 int i = 0; while(*message != '/') {month[i ++] = *message ++; } month[i] = '\0';//处理day ++ message; i = 0; while(*message != '/') {day[i ++] = *message ++; } day[i] = '\0';//处理年份 ++ message; i = 0; while(*message != '\0') { year[i ++] = *message ++; }//将字符串格式转换成整形,需要包含头文件stdlib.h month_int = atoi(month); printf("%s %s,%s",month_sets[month_int-1],day,year);}

运行 

2/23/2021 February 23,2021

总结

以上是生活随笔为你收集整理的c现代方法 13章程序设计题 自己编写答案的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。