c现代方法 13章程序设计题 自己编写答案
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                c现代方法 13章程序设计题 自己编写答案
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                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是要指向數(shù)組words的元素(元素是維數(shù)是LENGTH + 1的數(shù)組)的 ,也就是指向 //數(shù)組的指針,所以必須定義成數(shù)組的指針,下面兩句是錯誤的 char (*smallest)[LENGTH +1];//應(yīng)該改成 char (*smallest)[LENGTH + 1];//當(dāng)然也不能賦值為 "" char (*biggest)[LENGTH + 1]; //應(yīng)該改成 char(*biggest)[LENGTH + 1];//當(dāng)然也不能賦值為 "" scanf("%s",word);smallest = words; biggest = words;while(strlen(word) != 4){ //smallest 和 biggest一會不能指向word(因為word得內(nèi)容一直在變化,指向word等于指>向變化量) //所以要一會或許把p值給smallest或者biggest,p值while循環(huán)最后面再自增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一會要指向數(shù)組words,所以需要數(shù)組指針,所以如下定義 char (*p)[LENGTH + 1]; p = words; //存放words的元素的指針,因為words是字符串?dāng)?shù)組(就是數(shù)組的數(shù)組),所以small和bigge//st必須字符串指針的指針或者數(shù)組指針,即使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的單詞個數(shù),LENGTH是words每個單詞的預(yù)留長度*/ #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都執(zhí)行拷貝,下面這個程序不執(zhí)行字符串拷貝,
下面是執(zhí)行結(jié)果:
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 void5.
#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函數(shù),原型是? ?int atoi(const char *str)。定義在stdlib頭文件中,作用是把字符串轉(zhuǎn)換成整數(shù)。
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); }運行結(jié)果:
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 fifty8.
#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 129.
#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; }運行結(jié)果:
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; }運行結(jié)果是:?
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 ;} } //本題需要輸入時候跳過空格,所以必須自己定義一個字符串輸入函數(shù) 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上運行結(jié)果是:?
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 anagrams15題
#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表達式相除算出的結(jié)果不對的,sizeof()是計算數(shù)組的,不是字符串的 const char *ptr2 = message + strlen(message) - 1; while(ptr1 <= ptr2){//可能得連續(xù)跳過非字母的字符while(!isalpha(*ptr1))++ ptr1;//可能連續(xù)跳過非字母的字符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? Palindrome18.
#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 ++; }//將字符串格式轉(zhuǎn)換成整形,需要包含頭文件stdlib.h month_int = atoi(month); printf("%s %s,%s",month_sets[month_int-1],day,year);}運行?
2/23/2021 February 23,2021總結(jié)
以上是生活随笔為你收集整理的c现代方法 13章程序设计题 自己编写答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: c++ multimap的几个inse
 - 下一篇: pygame 学习check_event