面向對象程序設計寒假作業2題解


作業描述 詳情
這個作業屬於哪個課程 2020面向對象程序設計
這個作業要求在哪里 面向對象程序設計寒假作業2
這個作業的目標 1.實踐題:新建GitHub倉庫,並將作業上傳至該倉庫 2.編程題:優化代碼、編譯腳本、添加功能
作業正文 面向對象程序設計寒假作業2題解
其他參考文獻 desktop用法 [power shell的用法](https://www.cnblogs.com/lsdb/p/9531338.html"power shell的用法") Python中os.system的用法 C語言常見命名法

一、實踐題

新建GitHub倉庫

上傳編程題代碼至倉庫

二、編程題

首先是對作業一的編程題進行優化
題目要求:
①優化架構,一般要求每個函數長度不超過15行。
②優化規范,尤其是命名規范。
對於要求①,最先想到的是有沒有辦法優化一下函數算法,對着代碼發了一會兒呆發現這條路太難了,走不通,壓到20行左右的時候怎么也擠不出5行
於是想到了函數嵌套套娃,將原本的函數分為更多的函數,更加模塊化
對於要求②,參考了駱駝命名法和下划線命名法
除main函數外,共有5個函數

int In_Or_De(char* s, char * sp,int a);//判斷是增加還是減少
void output(int a);//輸出結果
int Ch_Switch_Num(char* s);//將漢字轉為數字
int changeSingle(char* s);//將單個漢字轉換為數字
char *extract(char* a, char* b);//提取“十”字前后的數字

Part1.輸入

int main()
{
	char s[10];char sp[10];
	int i, caibu;
	scanf("%s", s);
	scanf("%s", s);
	strcpy(sp, s);
	for (i = 0; i < 2; i++)
		scanf("%s", s);
	caibu = Ch_Switch_Num(s);
	scanf("%s", s);
	caibu = In_Or_De(s,sp,caibu);
	scanf("%s", s);
	output(caibu);
	return 0;
}
int In_Or_De(char* s, char* sp,int a)
{
	while (strcmp(s, sp) == 0)
	{
		scanf("%s", s);
		if (strcmp(s, "增加") == 0)
		{
			scanf("%s", s);
			a += Ch_Switch_Num(s);
		}
		else if (strcmp(s, "減少") == 0)
		{
			scanf("%s", s);
			a -= Ch_Switch_Num(s);
		}
		scanf("%s", s);
	}
	return a;
}

Part.2轉換

int Ch_Switch_Num(char* s)
{
	char buffer[5] = "";
	if (strlen(s) == 2)//處理0-9的漢字
		j=changeSingle(s);
	else if (strlen(s) == 4)//處理10-19以及十的倍數的漢字
	{
		strcpy(buffer,extract(buffer, s));
		if (strcmp(buffer, "十") == 0)//處理10-19的漢字
			j = 10 + changeSingle(extract(buffer,s+2));
		else//處理十的倍數
			j = changeSingle(buffer) * 10;
	}
	else//處理幾十幾的漢字
		j = changeSingle(extract(buffer, s)) * 10 + changeSingle(extract(buffer, s + 4));
	return j;
}
int changeSingle(char* s)
{
	int i;
	for (i = 0; i < 11; i++)
	{
		if (strcmp(s, chinese[i]) == 0)
			return i;
	}
}
char *extract(char* a, char* b)
{
	a[0] = b[0];
	a[1] = b[1];
	return a;
}

Part3.輸出

void output(int a)
{
	if (a <= 10)
		printf("%s", chinese[a]);
	else if (a > 10 && a < 20)
		printf("十%s", chinese[a - 10]);
	else if (a % 10 == 0)
		printf("%s十", chinese[a / 10]);
	else
		printf("%s十%s", chinese[a / 10], chinese[a % 10]);
}

完整代碼

#include<stdio.h>
#include<string.h>
int j;
int In_Or_De(char* s, char * sp,int a);//判斷是增加還是減少
void output(int a);//輸出結果
int Ch_Switch_Num(char* s);//將漢字轉為數字
int changeSingle(char* s);//將單個漢字轉換為數字
char *extract(char* a, char* b);//提取“十”字前后的數字
char chinese[11][3] = { "零","一","二","三","四","五","六","七","八","九","十" };
int main()
{
	char s[10];char sp[10];
	int i, caibu;
	scanf("%s", s);
	scanf("%s", s);
	strcpy(sp, s);
	for (i = 0; i < 2; i++)
		scanf("%s", s);
	caibu = Ch_Switch_Num(s);
	scanf("%s", s);
	caibu = In_Or_De(s,sp,caibu);
	scanf("%s", s);
	output(caibu);
	return 0;
}
int In_Or_De(char* s, char* sp,int a)
{
	while (strcmp(s, sp) == 0)
	{
		scanf("%s", s);
		if (strcmp(s, "增加") == 0)
		{
			scanf("%s", s);
			a += Ch_Switch_Num(s);
		}
		else if (strcmp(s, "減少") == 0)
		{
			scanf("%s", s);
			a -= Ch_Switch_Num(s);
		}
		scanf("%s", s);
	}
	return a;
}
void output(int a)
{
	if (a <= 10)
		printf("%s", chinese[a]);
	else if (a > 10 && a < 20)
		printf("十%s", chinese[a - 10]);
	else if (a % 10 == 0)
		printf("%s十", chinese[a / 10]);
	else
		printf("%s十%s", chinese[a / 10], chinese[a % 10]);
}
int Ch_Switch_Num(char* s)
{
	char buffer[5] = "";
	if (strlen(s) == 2)//處理0-9的漢字
		j=changeSingle(s);
	else if (strlen(s) == 4)//處理10-19以及十的倍數的漢字
	{
		strcpy(buffer,extract(buffer, s));
		if (strcmp(buffer, "十") == 0)//處理10-19的漢字
			j = 10 + changeSingle(extract(buffer,s+2));
		else//處理十的倍數
			j = changeSingle(buffer) * 10;
	}
	else//處理幾十幾的漢字
		j = changeSingle(extract(buffer, s)) * 10 + changeSingle(extract(buffer, s + 4));
	return j;
}
int changeSingle(char* s)
{
	int i;
	for (i = 0; i < 11; i++)
	{
		if (strcmp(s, chinese[i]) == 0)
			return i;
	}
}
char *extract(char* a, char* b)
{
	a[0] = b[0];
	a[1] = b[1];
	return a;
}

制作編譯腳本

剛看到這里的時候確實有點被嚇退了,完全沒有接觸過的內容,一點頭緒都沒有,在查閱了power shell、shell、Windows批處理的相關資料后后還是沒有什么頭緒
最近正好在自學Python,那天看到一位大佬(感謝)的腳本使用了Python,於是我選擇了PythonPython牛逼
引入os模塊,調用os.system方法

有exe文件出現,編譯成功

單元測試

一開始把問題想的太復雜,問了同學后才知道是自己想太多
所有函數中轉換函數Ch_Switch_Num函數最為重要,因此選擇她進行測試
測試代碼

int main(void)
{
	test("九十九", 99);
	test("零", 0);
	test("一", 1);
	test("十", 10);
	test("五十", 50);
	test("六十三", 63);
	test("七十七", 77);
	printf("以下為錯誤測試\n");
	test("sekiro", 99);
	test("真", 66);
	test("好", 1);
	test("玩", 2);
	return 0;
		
}
void test(char* s, int a)
{
	if (Ch_Switch_Num(s) == a)
		printf("pass\n");
	else
		printf("error\n");
}

添加功能

參考了同學的作業使用了freopen()函數,在代碼中添加freopen()便能實現功能的添加


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM