面向對象程序設計寒假作業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