这个作业属于哪个课程 | 2020面向对象程序设计 |
---|---|
这个作业要求在哪里 | 面向对象程序设计寒假作业3 |
这个作业的目标 |
1.优化第二次作业的代码 2.实现数字范围的扩大 |
作业正文 | https://www.cnblogs.com/smallgrape/p/12313179.html |
其他参考文献 |
一.优化代码
与上次的代码相比,将数字的范围延伸到了负数,和最终数据结果实现三位数。
完整代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h>//支持负数,输出可达到百位 int main() { char type_of_variable[5],variable_name[25],initial_value[6],operating[3],a[3]; scanf("%s%s%s%s",type_of_variable,variable_name,operating,initial_value);//初始化的输入 int sum,s=0; void last(int sum); int set_initial_value1(char in[]); int set_initial_value2(char in[]); int set_changing_value(int sum,char variable_name[]);//函数声明 a[0]=initial_value[0];a[1]=initial_value[1];a[2]='\0'; if(strcmp(a,"负")==0)s=-set_initial_value2(initial_value);//初始值 else s=set_initial_value1(initial_value); sum=set_changing_value(s,variable_name); //最终值 last(sum); system("PAUSE"); return 0; } int transform_initial_value1(char initial_value[])//函数.1. 文字与数字的转换 { int s; if(strcmp("零",initial_value)==0) s=0; else if(strcmp("一",initial_value)==0)s=1; else if(strcmp("二",initial_value)==0)s=2; else if(strcmp("三",initial_value)==0)s=3; else if(strcmp("四",initial_value)==0)s=4; else if(strcmp("五",initial_value)==0)s=5; else if(strcmp("六",initial_value)==0)s=6; else if(strcmp("七",initial_value)==0)s=7; else if(strcmp("八",initial_value)==0)s=8; else if(strcmp("九",initial_value)==0)s=9; else if(strcmp("十",initial_value)==0)s=10; return s;} int transform_initial_value2(char initial_value[])//函数.2. 文字与数字的转换 { int s; if(strcmp("十一",initial_value)==0)s=11; else if(strcmp("十二",initial_value)==0)s=12; else if(strcmp("十三",initial_value)==0)s=13; else if(strcmp("十四",initial_value)==0)s=14; else if(strcmp("十五",initial_value)==0)s=15; else if(strcmp("十六",initial_value)==0)s=16; else if(strcmp("十七",initial_value)==0)s=17; else if(strcmp("十八",initial_value)==0)s=18; else if(strcmp("十九",initial_value)==0)s=19; return s; } int transform_initial_value3(char initial_value[])//函数.3. 文字与数字的转换 { int s; if (strcmp("一十",initial_value)==0) s=10; else if(strcmp("二十",initial_value)==0) s=20; else if(strcmp("三十",initial_value)==0)s=30; else if(strcmp("四十",initial_value)==0)s=40; else if(strcmp("五十",initial_value)==0)s=50; else if(strcmp("六十",initial_value)==0)s=60; else if(strcmp("七十",initial_value)==0)s=70; else if(strcmp("八十",initial_value)==0)s=80; else if(strcmp("九十",initial_value)==0)s=90; return s; } void retransform_ultimate_value1(int sum)//函数。4.转换最终值 { switch(sum) { case 0:printf("零");break; case 1:printf("一");break; case 2:printf("二");break; case 3:printf("三");break; case 4:printf("四");break; case 5:printf("五");break; case 6:printf("六");break; case 7:printf("七");break; case 8:printf("八");break; case 9:printf("九");break; } } void retransform_ultimate_value2(int sum)//函数。5.转换最终值 { switch(sum) { case 0:printf("十");break; case 1:printf("十一");break; case 2:printf("十二");break; case 3:printf("十三");break; case 4:printf("十四");break; case 5:printf("十五");break; case 6:printf("十六");break; case 7:printf("十七");break; case 8:printf("十八");break; case 9:printf("十九");break; } } int set_initial_value1(char in[])//函数。6.设置初始值 { int length=strlen(in),sum;char b[3],c[3]; int transform_initial_value1(char initial_value[]); int transform_initial_value2(char initial_value[]); int transform_initial_value3(char initial_value[]); if(length==2) sum=transform_initial_value1(in); else if(length==4) { sum=transform_initial_value3(in); } else if(length==6) { b[0]=in[0];b[1]=in[1]; b[2]=in[2];b[3]=in[3]; b[4]='\0'; c[0]=in[4];c[1]=in[5]; c[2]='\0'; sum=transform_initial_value3(b)+transform_initial_value1(c); } return sum; } int set_initial_value2(char in[])//函数。6.设置初始值 { char b[3],c[3],d[8];int i; for(i=0;in[i]!='\0';i++) d[i]=in[i+2]; int length=strlen(d),sum; int transform_initial_value1(char initial_value[]); int transform_initial_value2(char initial_value[]); int transform_initial_value3(char initial_value[]); if(length==2) sum=transform_initial_value1(d); else if(length==4) { sum=transform_initial_value3(d); } else if(length==6) { b[0]=d[0];b[1]=d[1]; b[2]=d[2];b[3]=d[3]; b[4]='\0'; c[0]=d[4];c[1]=d[5]; c[2]='\0'; sum=transform_initial_value3(b)+transform_initial_value1(c); } return sum; } int set_changing_value(int sum,char variable_name[])//函数.7.设定中间值 { int i,s;char variable_name2[25],operating[3],value[3],a[3]; int set_initial_value1(char in[]);int set_initial_value2(char in[]); scanf("%s%s%s",variable_name2,operating,value); for(i=0;strcmp(variable_name,variable_name2)==0;i++) { if(i!=0) scanf("%s",value); a[0]=value[0];a[1]=value[1];a[2]='\0'; if(strcmp(a,"负")==0) s=-set_initial_value2(value); else s=set_initial_value1(value); if(strcmp("增加",operating)==0)//判断 sum+=s; else if(strcmp("减少",operating)==0) sum-=s; scanf("%s%s",variable_name2,operating); } return sum; } void last(int sum) { int i,a[5],w=sum;void result (int i,int a[]); for(i=0;;i++) { a[i]=(int)fabs(sum)%10; sum=sum/10; if(sum==0)break; } if(w>=0) result(i,a); else { printf("负"); result(i,a); } } void result (int i,int a[]) { void retransform_ultimate_value1(int sum); void retransform_ultimate_value2(int sum);//函数声明 if(i==0) retransform_ultimate_value1(a[0]); else if(i==1) { retransform_ultimate_value1(a[1]); retransform_ultimate_value2(a[0]); } else if(i==2&&a[1]!=0) { retransform_ultimate_value1(a[2]); printf("百"); retransform_ultimate_value1(a[1]); retransform_ultimate_value2(a[0]); } else if(i==2&&a[1]==0&&a[0]==0) { retransform_ultimate_value1(a[2]); printf("百"); } else if(i==2&&a[1]==0&&a[0]!=0) { retransform_ultimate_value1(a[2]); printf("百零"); retransform_ultimate_value1(a[0]); } else printf("error!"); }
测试样例 三位数的相对特殊的情况
负数的三位数:
正数的三位数:
二.单元测试
1)代码:汉字转化数字
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int set_initial_value1(char in[]); int set_initial_value2(char in[]); int test(int i,char initial_value[]);int s,n,i,flag=0;char initial_value[6],a[3]; FILE *freopen( const char *path, const char *mode, FILE *stream ); freopen("c:\\users\\蒲桃见\\desktop\\out.txt","w",stdout); for(i=0;i<10;i++) { n=test(i,initial_value); a[0]=initial_value[0];a[1]=initial_value[1];a[2]='\0'; if(strcmp(a,"负")==0)s=-set_initial_value2(initial_value);//初始值 else s=set_initial_value1(initial_value); if(n==s) printf("Pass\n"); else printf("Fail\n"); } fclose(stdout);//关闭文件 system("PAUSE"); return 0; } int test(int i,char initial_value[]) { int n; if(i==0){strcpy(initial_value,"零"); n=0;} if(i==1){strcpy(initial_value,"负七"); n=-7;} if(i==2){strcpy(initial_value,"负一十四"); n=-14;} if(i==3){strcpy(initial_value,"二十一"); n=21;} if(i==4){strcpy(initial_value,"一十九"); n=19;} if(i==5){strcpy(initial_value,"负三十五"); n=-35;} if(i==6){strcpy(initial_value,"四十七"); n=47;} if(i==7){strcpy(initial_value,"五十二"); n=52;} if(i==8){strcpy(initial_value,"负七十八"); n=-78;} if(i==9){strcpy(initial_value,"八十九"); n=89;} return n; } int transform_initial_value1(char initial_value[])//函数.1. { int s; if(strcmp("零",initial_value)==0) s=0; else if(strcmp("一",initial_value)==0)s=1; else if(strcmp("二",initial_value)==0)s=2; else if(strcmp("三",initial_value)==0)s=3; else if(strcmp("四",initial_value)==0)s=4; else if(strcmp("五",initial_value)==0)s=5; else if(strcmp("六",initial_value)==0)s=6; else if(strcmp("七",initial_value)==0)s=7; else if(strcmp("八",initial_value)==0)s=8; else if(strcmp("九",initial_value)==0)s=9; else if(strcmp("十",initial_value)==0)s=10; return s;} int transform_initial_value2(char initial_value[])//函数.2. { int s; if(strcmp("十一",initial_value)==0)s=11; else if(strcmp("十二",initial_value)==0)s=12; else if(strcmp("十三",initial_value)==0)s=13; else if(strcmp("十四",initial_value)==0)s=14; else if(strcmp("十五",initial_value)==0)s=15; else if(strcmp("十六",initial_value)==0)s=16; else if(strcmp("十七",initial_value)==0)s=17; else if(strcmp("十八",initial_value)==0)s=18; else if(strcmp("十九",initial_value)==0)s=19; return s;} int transform_initial_value3(char initial_value[])//函数.3. { int s; if (strcmp("一十",initial_value)==0) s=10; else if(strcmp("二十",initial_value)==0) s=20; else if(strcmp("三十",initial_value)==0)s=30; else if(strcmp("四十",initial_value)==0)s=40; else if(strcmp("五十",initial_value)==0)s=50; else if(strcmp("六十",initial_value)==0)s=60; else if(strcmp("七十",initial_value)==0)s=70; else if(strcmp("八十",initial_value)==0)s=80; else if(strcmp("九十",initial_value)==0)s=90; return s; } int set_initial_value1(char in[])//函数。6.设置初始值 { int length=strlen(in),sum;char b[3],c[3]; int transform_initial_value1(char initial_value[]); int transform_initial_value2(char initial_value[]); int transform_initial_value3(char initial_value[]); if(length==2) sum=transform_initial_value1(in); else if(length==4) { sum=transform_initial_value3(in); } else if(length==6) { b[0]=in[0];b[1]=in[1]; b[2]=in[2];b[3]=in[3]; b[4]='\0'; c[0]=in[4];c[1]=in[5]; c[2]='\0'; sum=transform_initial_value3(b)+transform_initial_value1(c); } return sum; } int set_initial_value2(char in[])//函数。6.设置初始值 { char b[3],c[3],d[8];int i; for(i=0;in[i]!='\0';i++) d[i]=in[i+2]; int length=strlen(d),sum; int transform_initial_value1(char initial_value[]); int transform_initial_value2(char initial_value[]); int transform_initial_value3(char initial_value[]); if(length==2) sum=transform_initial_value1(d); else if(length==4) { sum=transform_initial_value3(d); } else if(length==6) { b[0]=d[0];b[1]=d[1]; b[2]=d[2];b[3]=d[3]; b[4]='\0'; c[0]=d[4];c[1]=d[5]; c[2]='\0'; sum=transform_initial_value3(b)+transform_initial_value1(c); } return sum; }
2)数字转化汉字
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> int main() { void mainly(int i,int a[],char n[]); FILE *freopen( const char *path, const char *mode, FILE *stream ); int test(int j,char x[]); int i,sum,j,a[3],w;char e[12],n[12],x[6]; freopen("c:\\users\\蒲桃见\\desktop\\reout.txt","w",stdout); for(j=0;j<10;j++) { sum=test(j,x); w=sum; for(i=0;;i++) { a[i]=(int)fabs(sum)%10; sum=sum/10; if(sum==0)break; } mainly(i,a,n); if(w<0) { strcpy(e,"负"); strcat(e,n); } else strcpy(e,n); if(strcmp(e,x)==0)printf("Pass\n"); else printf("Fail\n"); } fclose(stdout);//关闭文件 system("PAUSE"); return 0; } void mainly(int i,int a[],char n[]) { char m[6],l[6];void retransform_ultimate_value1(int sum,char n[]); void retransform_ultimate_value2(int sum,char m[]); if(i==0) retransform_ultimate_value1(a[0],n); else if(i==1) { retransform_ultimate_value1(a[1],n); retransform_ultimate_value2(a[0],m); strcat(n,m); } else if(i==2&&a[1]!=0) { strcpy(n,"一百"); retransform_ultimate_value1(a[1],m); retransform_ultimate_value2(a[0],l); strcat(n,m);strcat(n,l); } else if(i==2&&a[1]==0&&a[0]==0) strcpy(n,"一百"); else if(i==2&&a[1]==0&&a[0]!=0) { strcpy(n,"一百零"); retransform_ultimate_value1(a[0],m); strcat(n,m); } else strcpy(n,"error!"); } int test(int j,char x[]) { int sum; switch(j) {case 0:strcpy(x,"零");sum=0;break; case 1:strcpy(x,"负一十七");sum=-17;break; case 2:strcpy(x,"二十八");sum=28;break; case 3:strcpy(x,"负三十九");sum=-39;break; case 4:strcpy(x,"四十五");sum=45;break; case 5:strcpy(x,"负五十");sum=-50;break; case 6:strcpy(x,"六十一");sum=61;break; case 7:strcpy(x,"一百八十九");sum=189;break; case 8:strcpy(x,"负一百一十九");sum=-119;break; case 9:strcpy(x,"七十八");sum=78;break; } return sum; } void retransform_ultimate_value1(int sum,char n[])//函数。4.转换最终值 { switch(sum) { case 0:strcpy(n,"零");break; case 1:strcpy(n,"一");break; case 2:strcpy(n,"二");break; case 3:strcpy(n,"三");break; case 4:strcpy(n,"四");break; case 5:strcpy(n,"五");break; case 6:strcpy(n,"六");break; case 7:strcpy(n,"七");break; case 8:strcpy(n,"八");break; case 9:strcpy(n,"九");break; } } void retransform_ultimate_value2(int sum,char m[])//函数。5.转换最终值 { switch(sum) { case 0:strcpy(m,"十");break; case 1:strcpy(m,"十一");break; case 2:strcpy(m,"十二");break; case 3:strcpy(m,"十三");break; case 4:strcpy(m,"十四");break; case 5:strcpy(m,"十五");break; case 6:strcpy(m,"十六");break; case 7:strcpy(m,"十七");break; case 8:strcpy(m,"十八");break; case 9:strcpy(m,"十九");break; } }
测试脚本:
测试脚本:代码的主函数
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h>//支持负数,输出可达到百位 int main() { char type_of_variable[5],variable_name[25],initial_value[6],operating[3],a[3]; FILE *freopen( const char *path, const char *mode, FILE *stream ); freopen("c:\\users\\蒲桃见\\desktop\\bigin.txt","r",stdin); freopen("c:\\users\\蒲桃见\\desktop\\bigout.txt","w",stdout); scanf("%s%s%s%s",type_of_variable,variable_name,operating,initial_value);//初始化的输入 int sum,s=0; void last(int sum); int set_initial_value1(char in[]); int set_initial_value2(char in[]); int set_changing_value(int sum,char variable_name[]);//函数声明 a[0]=initial_value[0]; a[1]=initial_value[1]; a[2]='\0'; if(strcmp(a,"负")==0) s=-set_initial_value2(initial_value);//初始值 else s=set_initial_value1(initial_value); sum=set_changing_value(s,variable_name); //最终值 last(sum); fclose(stdin); fclose(stdout); system("PAUSE"); return 0; }
三.编译脚本: