2020面向对象寒假作业(三)
2020面向对象寒假作业(三)
这个作业属于哪里 | 2020面向对象程序设计 |
---|---|
这个作业在哪里 | 2020面向对象寒假作业三 |
这个作业目标 | 1.继续完成编程题 2.发布博客 |
作业正文 | 2020面向对象寒假作业(三) |
参考文献 | - |
1.代码优化
(1)num中文转数字计算函数
原函数
int num(char d[])
{
if(!strcmp(d,"零")) return 0;
else if(!strcmp(d,"一")) return 1;
else if(!strcmp(d,"二")) return 2;
else if(!strcmp(d,"三")) return 3;
else if(!strcmp(d,"四")) return 4;
else if(!strcmp(d,"五")) return 5;
else if(!strcmp(d,"六")) return 6;
else if(!strcmp(d,"七")) return 7;
else if(!strcmp(d,"八")) return 8;
else if(!strcmp(d,"九")) return 9;
else if(!strcmp(d,"十")) return 10;
else return -1;
}
优化函数
int num(char d[])
{
char nums[11][4]={"零","一","二","三","四","五","六","七","八","九","十"};
char b[0];
int n,i,j,sum=0,judge=0;
n=strlen(d);
for(i=0;i<=n-2;i=i+2)
{
strncpy(b,d+i,2);
if(!strcmp(b,"负")) judge=1;
for(j=0;j<11;j++)
{
if(!strcmp(b,nums[j]))
{
if(i==0&&j==10) sum+=j;
else if(i!=0&&j==10) sum=sum*10;
else if(j!=10) sum+=j;
}
}
}
if(judge==0) return sum;
else return sum*(-1);
}
优化思考
【1】.使用strlen,strncpy,strcmp函数分别进行得到总字符数,复制指定字符到另一个数组,判断这个字符数组与相应字符是否相等。
【2】.把原函数选择模式改写成循环模式,大量减少代码,并得到与对应字符对应的数字j,方便后续计算总数sum。
【3】.通过对负数的判断和循环,将原函数零到十的范围改写成负一百到一百
(2).sum数字转化成中文函数
原函数
void sum(int n)
{
char nums[10][4]={"零","一","二","三","四","五","六","七","八","九"};
if(n<10) printf("%s",nums[n]);
else
{
int m=n%10;
n=n/10;
if(!strcmp(nums[n],"一")) printf("十%s",nums[m]);
else printf("%s十%s",nums[n],nums[m]);
}
}
优化函数
void sum(int n)
{
char nums[10][4]={"零","一","二","三","四","五","六","七","八","九"};
if(n<0)
{
printf("欠债:");
n=fabs(n);
}
if(n<10) printf("%s",nums[n]);
else if(n<100)
{
int m=n%10;
n=n/10;
if(!strcmp(nums[n],"一")) printf("十%s",nums[m]);
else printf("%s十%s",nums[n],nums[m]);
}
else
{
int m=n%10,k=n/100;
n=n%100/10;
if(!strcmp(nums[n],"零")) printf("%s百零%s",nums[k],nums[m]);
else if(!strcmp(nums[m],"零")) printf("%s百%s十",nums[k],nums[n]);
else printf("%s百%s十%s",nums[k],nums[n],nums[m]);
}
}
优化思路
【1】.增加一个选择分支,转化超过一百的总数
【2】.通过增加一个分支,将原函数总数范围从零到一百改为负一千到一千
【3】.对于负的总数,声明为负债:总数
(3).judge中文转化成符号函数
原函数
int judge(int n ,char c[],char d[])
{
if(!strcmp(c,"增加")) return n+num(d);
else if(!strcmp(c,"减少")) return n-num(d);
else error(4);
}
优化函数
int judge(int n ,char c[],char d[])
{
if(!strcmp(c,"增加")) return n+num(d);
else if(!strcmp(c,"减少")) return n-num(d);
else if(!strcmp(c,"乘以")) return n*num(d);
else if(!strcmp(c,"除以")) return n/num(d);
}
优化思路
【1】.增加两个选择分支,支持乘除运算
【2】.定义为int函数,不会有小数
2.原代码函数
calculate循环计算函数:通过循环重复运算,无需变化
void calculate(int coin)
{
char b[10],c[10],d[10];
while(2)
{
scanf("%s",b);
if(!strcmp(b,"看看"))
{
sum(coin);
break;
}
scanf("%s%s",c,d);
coin = judge(coin,c,d);
}
}
3.编译脚本
脚本展示
结果展示
4.单元测试
单元测试代码
#include<stdio.h>
#include<string.h>
int num(char d[])
{
char nums[11][4]={"零","一","二","三","四","五","六","七","八","九","十"};
char b[0];
int n,i,j,sum=0,judge=0,judge1=0;
n=strlen(d);
for(i=0;i<=n-2;i=i+2)
{
strncpy(b,d+i,2);
if(!strcmp(b,"负"))
{
judge=1;continue;
}
for(j=0;j<11;j++)
{
if(!strcmp(b,nums[j]))
{
if(i==0&&j==10) sum+=j;
else if(i!=0&&j==10) sum=sum*10;
else if(j!=10) sum+=j;
judge1=1;
}
}
if(judge1==0) return -1;
}
if(judge==0) return sum;
else return sum*(-1);
}
int judge(char c[])
{
if(!strcmp(c,"增加")) return 1;
else if(!strcmp(c,"减少")) return 2;
else if(!strcmp(c,"乘以")) return 3;
else if(!strcmp(c,"除以")) return 4;
else return -1;
}
int main()
{
int a,i;
char d[13][10]={"九十九","二十三","负三","三","四","二十三","六","七","八","九","十","增加","减少"};
for(i=0;i<13;i++)
{
if(i<11)
{
a=num(d[i]);
if(a!=-1) printf("pass\n");
else printf("error\n");
}
else
{
a=judge(d[i]);
if(a!=-1) printf("pass\n");
else printf("error\n");
}
}
}
测试脚本
结果展示
5.测试脚本
测试文本代码
测试结果
测试脚本
6.完整代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int num(char d[])
{
char nums[11][4]={"零","一","二","三","四","五","六","七","八","九","十"};
char b[0];
int n,i,j,sum=0,judge=0;
n=strlen(d);
for(i=0;i<=n-2;i=i+2)
{
strncpy(b,d+i,2);
if(!strcmp(b,"负")) judge=1;
for(j=0;j<11;j++)
{
if(!strcmp(b,nums[j]))
{
if(i==0&&j==10) sum+=j;
else if(i!=0&&j==10) sum=sum*10;
else if(j!=10) sum+=j;
}
}
}
if(judge==0) return sum;
else return sum*(-1);
}
int judge(int n ,char c[],char d[])
{
if(!strcmp(c,"增加")) return n+num(d);
else if(!strcmp(c,"减少")) return n-num(d);
else if(!strcmp(c,"乘以")) return n*num(d);
else if(!strcmp(c,"除以")) return n/num(d);
}
void sum(int n)
{
char nums[10][4]={"零","一","二","三","四","五","六","七","八","九"};
if(n<0)
{
printf("欠债:");
n=fabs(n);
}
if(n<10) printf("%s",nums[n]);
else if(n<100)
{
int m=n%10;
n=n/10;
if(!strcmp(nums[n],"一")) printf("十%s",nums[m]);
else printf("%s十%s",nums[n],nums[m]);
}
else
{
int m=n%10,k=n/100;
n=n%100/10;
if(!strcmp(nums[n],"零")) printf("%s百零%s",nums[k],nums[m]);
else if(!strcmp(nums[m],"零")) printf("%s百%s十",nums[k],nums[n]);
else printf("%s百%s十%s",nums[k],nums[n],nums[m]);
}
printf("\n");
}
void calculate(int coin)
{
char b[10],c[10],d[10];
while(2)
{
scanf("%s",b);
if(!strcmp(b,"看看"))
{
sum(coin);
break;
}
scanf("%s%s",c,d);
coin = judge(coin,c,d);
}
}
int main()
{
char file[100];
printf("请输入文件地址\n");
scanf("%s",file);
freopen(file,"r",stdin);
char a[10], b[10], c[10], d[10];
int coin;
scanf("%s%s%s%s",a,b,c,d);
getchar();
if(!strcmp(a,"整数"))
{
coin += num(d);
}
calculate(coin);
}