面向对象程序设计寒假作业3


这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/2020OOP
这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/2020OOP/homework/10288
这个作业的目标 1.继续完成作业二的编程题。2.优化架构,思考代码的拓展性,比如我需要增加其他功能,如选择,循环语句怎么办。3.思考:可以参考现有的编程语言,把这些语言的内容加入。如选择、循环语句、函数、或者扩大数字范围,支持负数等。
作业正文 如下
其他参考文献 让你的代码有更好的扩展性 程序的可维护性和可扩展性

编程题要求:

读题,提取出题目的要求。
分解需求,把需求分解为几个你觉得不太相关的模块。
思考每个模块怎么写,可以从简单的模块开始写。
对于不会的问题进行查阅资料。
对于每一个模块设计测试用例。
详细记录下以上每一步,并写在博客中。
不要求完全做出来,但要求记录详细。
建议博客长度不少于1000字(不包含代码)。

解释:

单元测试:对每一个函数进行测试,这代表了你需要把代码分到不同的文件,使用不同的主函数切换测试/运行。
编译脚本:运行该脚本后无需任何输入就能自动编译全部代码,并输出编译结束的代码。
测试脚本:运行该脚本后可以自动的编译并运行所有测试样例,并输出测试结果。

功能添加:以前写的代码只能进行10以内的非常简单的计算,这次尝试把范围扩大到两位数。

大佬们已经开始算四位数五位数了,我太菜了

对于将汉字转化为数字的函数,添加了部分代码使得函数的转化功能由0-10扩大到了0-99

(但这里存在的问题是输入的时候需要每一位上的汉字和数字都一一对应,例如五十八要以五八的形式输入,九十四要以九四的形式输入,而整十数如六十要输入六零,才能正常运行得到结果,这不符合我们的使用习惯,所以还有待改进):

int changeInHun(char s[10])
{        
    int i,a,b,c;
    char x[10],y[10];
    if(strcmp(s,"零")==0)    return 0;
    else if(strcmp(s,"一")==0)   return 1; 
    else if(strcmp(s,"二")==0)   return 2;
    else if(strcmp(s,"三")==0)   return 3;
    else if(strcmp(s,"四")==0)   return 4;  
    else if(strcmp(s,"五")==0)   return 5;
    else if(strcmp(s,"六")==0)   return 6;
    else if(strcmp(s,"七")==0)   return 7;
    else if(strcmp(s,"八")==0)   return 8;
    else if(strcmp(s,"九")==0)   return 9;
    else if(strcmp(s,"十")==0)   return 10;
    else if
	{
        for(i=0;i<2;i++)
		{
            x[i]=s[i];
            y[i]=s[i+2];
        }
        if(strcmp(x,"十")==0)
		{
            a=10;
            b=changeInHun(y);
            c=a+b;
        }
        else 
		{
            a=changeInHun(x);
            b=changeInHun(y);
            c=a*10+b;
        }
        return c;
    }
}

下面对该函数进行测试:

  • 测试代码如下:
#include<stdio.h>
#include<string.h> 
int changeInHun(char s[10]);
int main()
{
    char p[10]={'\0'};
    int k;
    while(1)
	{    
		scanf("%s",p);
    	k=changeInHun(p);
    	printf("%d\n",k);
	} 
    return 0;
}
int changeInHun(char s[10])
{        
	int i,a,b,c;
    char x[10],y[10];
    if(strcmp(s,"零")==0)    return 0;
    else if(strcmp(s,"一")==0)   return 1; 
    else if(strcmp(s,"二")==0)   return 2;
    else if(strcmp(s,"三")==0)   return 3;
    else if(strcmp(s,"四")==0)   return 4;  
    else if(strcmp(s,"五")==0)   return 5;
    else if(strcmp(s,"六")==0)   return 6;
    else if(strcmp(s,"七")==0)   return 7;
    else if(strcmp(s,"八")==0)   return 8;
    else if(strcmp(s,"九")==0)   return 9;
    else if(strcmp(s,"十")==0)   return 10;
    else 
	{
        for(i=0;i<2;i++)
		{
            x[i]=s[i];
            y[i]=s[i+2];
        }
        if(strcmp(x,"十")==0)
		{
            a=10;
            b=changeInHun(y);
            c=a+b;
        }
        else 
		{
            a=changeInHun(x);
            b=changeInHun(y);
            c=a*10+b;
        }
        return c;
    }
}
  • 测试结果:

对于将数字转化回汉字的函数:

(因为输出的结果要从10以内扩大到100以内,需要讨论的情况变多了,我把一个函数分成两个,一个用以讨论不同情况,一个则单纯用作输出,同时添加了计算结果是负数的输出)

  • exchangeInHun函数用于讨论不同的数字:
void exchangeInHun(int t)
{
	int m;
    if(t>0)
	{	
    	if(t<=10)	output(t);
    	else if(t<20&&t>10)
    	{	
			printf("十");
			t=t%10;
			output(t); 
		}
    	else if(t>=20&&t<100&&t%10==0)
    	{
    		t=t/10;
    		output(t);
    		printf("十"); 
		}
		else
		{
			m=t/10;
			output(m);
			printf("十");
			t=t%10;
			output(t);
		}
    }
    else if(t<0)    
	{
		t=-t;
		printf("负");
		exchangeInHun(t); 
	}
}
  • output函数用于输出:
void output(int x) 
{ 
	if(x==0)    printf("零");
   	else if(x==1)    printf("一");
   	else if(x==2)    printf("二");
   	else if(x==3)    printf("三");
   	else if(x==4)    printf("四");
   	else if(x==5)    printf("五");
   	else if(x==6)    printf("六");
   	else if(x==7)    printf("七");
   	else if(x==8)    printf("八");
   	else if(x==9)    printf("九");
   	else if(x=10)	 printf("十");
}  
  • 测试结果如下:

完整代码如下:

#include<stdio.h>
#include<string.h>
int changeInHun(char s[10]);
int calculate(char s[10],int m,int n);
void exchangeInHun(int t);
void output(int x);
int main()
{	
    char a[10],b[10],c[10],d[10];
    int x,y;    
    scanf("%s %s %s %s",a,b,c,d);
    x=changeInHun(d);
    while(1)
    {
        scanf("%s",a);
        if(strcmp(a,"看看")==0)
        {
            scanf("%s",b);
            break;
        } 
        else
        { 
            scanf("%s %s",b,c);
            y=changeInHun(c);
            x=calculate(b,x,y); 
        }  
    }
    exchangeInHun(x);
    return 0;
} 
 
int changeInHun(char s[10])
{        
	int i,a,b,c;
    char x[10],y[10];
    if(strcmp(s,"零")==0)    return 0;
    else if(strcmp(s,"一")==0)   return 1; 
    else if(strcmp(s,"二")==0)   return 2;
    else if(strcmp(s,"三")==0)   return 3;
    else if(strcmp(s,"四")==0)   return 4;  
    else if(strcmp(s,"五")==0)   return 5;
    else if(strcmp(s,"六")==0)   return 6;
    else if(strcmp(s,"七")==0)   return 7;
    else if(strcmp(s,"八")==0)   return 8;
    else if(strcmp(s,"九")==0)   return 9;
    else if(strcmp(s,"十")==0)   return 10;
    else 
	{
        for(i=0;i<2;i++)
		{
            x[i]=s[i];
            y[i]=s[i+2];
        }
        if(strcmp(x,"十")==0)
		{
            a=10;
            b=changeInHun(y);
            c=a+b;
        }
        else 
		{
            a=changeInHun(x);
            b=changeInHun(y);
            c=a*10+b;
        }
        return c;
    }
}

int calculate(char s[10],int m,int n)
{   
    int k;
    if(strcmp(s,"增加")==0)   k=m+n;
    if(strcmp(s,"减少")==0)   k=m-n;
    if(strcmp(s,"乘以")==0)   k=m*n;
    if(strcmp(s,"除以")==0)   k=m/n;//新增了乘除运算
    return k;
}

void exchangeInHun(int t)
{
	int m;
    if(t>0)
	{	
    	if(t<=10)	output(t);
    	else if(t<20&&t>10)
    	{	
			printf("十");
			t=t%10;
			output(t); 
		}
    	else if(t>=20&&t<100&&t%10==0)
    	{
    		t=t/10;
    		output(t);
    		printf("十"); 
		}
		else
		{
			m=t/10;
			output(m);
			printf("十");
			t=t%10;
			output(t);
		}
    }
    else if(t<0)    
	{
		t=-t;
		printf("负");
		exchangeInHun(t); 
	}
}

void output(int x) 
{ 
	if(x==0)    printf("零");
   	else if(x==1)    printf("一");
   	else if(x==2)    printf("二");
   	else if(x==3)    printf("三");
   	else if(x==4)    printf("四");
   	else if(x==5)    printf("五");
   	else if(x==6)    printf("六");
   	else if(x==7)    printf("七");
   	else if(x==8)    printf("八");
   	else if(x==9)    printf("九");
   	else if(x=10)	 printf("十");
} 
  • 测试结果如下:

反思:代码的功能有限且复杂,而且我用了很多else/if,所以拓展性也是比较差的,但我水平有限(枯了),只能先参考大佬们的再自己慢慢磨,希望在以后的学习中能让自己提高!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM