#2020面向對象寒假做業(二)html
這個做業屬於哪裏 | 2020面向對象程序設計 |
---|---|
這個做業在哪裏 | 2020面向對象寒假做業二 |
- | 1.繼續完成編程題 |
這個做業目標 | 2.新建一個github倉庫,並把做業推送到該倉庫 |
- | 3.發佈博客 |
做業正文 | 2020面向對象寒假做業(二) |
參考文獻 | 2020面向對象程序設計寒假做業2 題解 |
- | Windows批處理(cmd/bat)經常使用命令教程 |
##1.使用github desktop新建一個github倉庫 | |
###(1)下載github desktop | |
###(2)註冊信息,進入以下頁面 | |
![]() |
|
###(3)點在右上角file,點擊new repository,填入信息創建新倉庫 | |
![]() |
|
###(4)進入倉庫所在文件夾,把做業相關文件複製到該文件夾,再上傳到倉庫 | |
![]() |
##2.編程題 ·繼續完成做業一的編程題。 ·優化架構,通常要求每一個函數長度不超過15行。 ·優化規範,尤爲是命名規範。 ·製做一個編譯腳本,運行該腳本能夠編譯你的代碼,可選的腳本語言,python(2.7),windows批處理,powershell,shell。 ·進行單元測試,即測試每個函數,並製做一個測試腳本,運行該腳本能夠進行測試,並顯示測試結果。 ·在做業一編程題的基礎上添加如下功能 ·經過命令行讀取一個文件 ###(1)代碼分塊 ####error塊:提示錯誤信息python
對輸入error函數的數據進行判斷,若是數據錯誤,輸出錯誤提示信息若是數據正確,不執行任何操做。git
void error(int judge) { if(judge==1) printf("數據類型輸入錯誤\n"); else if(judge==2) printf("數值輸入錯誤,應屬於零到十範圍\n"); else if(judge==3) printf("數值總和必須在0~99範圍\n"); else if(judge==4) printf("符號輸入錯誤\n"); }
####num塊:執行字符轉爲數字github
將輸入num函數的字符進行判斷,若是能轉化成數字,則返回對應數字;若是不能轉化成數字,則返回錯誤信息。shell
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 error(2); }
####judge塊:符號判斷編程
對輸入judge函數的數據進行判斷,若是是指定符號,則進行指定操做;若是不是指定符號,則返回錯誤信息。windows
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); }
####sum塊:將最終數據轉化成字符輸出架構
將輸入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]); } }
####calculate塊:針對每一次的運算調用judge、sum、num塊單元測試
對輸入calculate函數的運算操做,調用相關函數,執行運算操做
void calculate(int coin) { char b[10],c[10],d[10]; while(2) { scanf("%s",b); if(!strcmp(b,"看看")) { if(coin>99) error(3); else sum(coin); break; } scanf("%s%s",c,d); coin = judge(coin,c,d); } }
####main塊:打開輸入信息文件,鏈接函數
負責打開包含輸入信息的文件,做爲主函數鏈接其餘函數。
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,"整數")) { if(num(d)==-1) error(2); else { coin += num(d); } } else error(1); if(coin>99||coin<0) error(3); else { calculate(coin); } }
###(2)製做編譯腳本
題目信息理解錯,分析其餘同窗做業(感謝大佬)以後,改用Windows批處理。
@echo off echo 請輸入文件所在磁盤 set /p a= echo 正在轉移到該磁盤 cd "%a%" echo 請輸入文件所在路徑 set /p b= echo 轉移到文件路徑 cd "%b%" echo 請輸入文件名 set /p c= echo 編譯開始 gcc "%c%" -o try.exe if exist "try.exe" echo 編譯成功 if not exist "try.exe" echo 編譯失敗 pause
####問題: >【1】用Notepad++編譯腳本,出現亂碼狀況 >【2】出現bat文件快速打開快速關閉狀況 >【3】出現沒法用變量a實現cd、if操做問題 ####解決方法: >【1】使用Windows自帶的記事本 >【2】在文件中末尾添加pause語句 >【3】使用"%a%"格式解決 ###(3)測試腳本
繼續使用Windows批處理製做簡單測試腳本 ####單元測試模塊選取 由於代碼中num函數與judge函數,承擔最重要的運算部分,故選取num函數與judge函數進行單元測試。 設計測試程序
#include<stdio.h> 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 judge(char c[]) { if(!strcmp(c,"增長")) return 1; else if(!strcmp(c,"減小")) return 2; else return -1; } int main() { int a,i,k; char d[13][10]={"零","一","二","三","四","五","六","七","八","九","十","增長","減小"}; char c[13][10]={"0","我","真","的","好","菜","啊","!","十一","1","2","加上","-"}; for(i=0,k=1;i<13;i++) { if(k==1) printf("輸入 %s ",d[i]); else printf("輸入 %s ",c[i]); if(i<11) { if(k==1) a=num(d[i]); else a=num(c[i]); if(a!=-1) printf("number pass\n"); else printf("number error\n"); } else { if(k==1) a=judge(d[i]); else a=num(c[i]); if(a!=-1) printf("symbol pass\n"); else printf("symbol error\n"); } if(i==12&&k==1) { k++; i=0; printf("正確輸入測試完畢,進行錯誤輸入測試\n"); } if(k==2&&i==12) printf("全部輸入測試完畢"); } }
編寫測試腳本
@echo off cd d: echo 開始測試 text1.exe echo 測試結束 pause
測試結果展現 ###(3)經過命令行讀取一個文件
使用C語言中的freopen()函數傳入包含輸入信息的文件,再執行後續操做 ####首先編寫幾個輸入信息的文件
####運行C語言程序
####問題: 【1】使用fopen函數,顯示錯誤信息 【2】使用正確的函數,地址輸入錯誤 ####解決方法: 【1】使用freopen函數 【2】地址錯誤形式"E:\aa.txt",正確形式"E:\aa.txt"或者"E:/aa.txt" ###(4)完整代碼
#include<stdio.h> #include<string.h> void error(int judge) { if(judge==1) printf("數據類型輸入錯誤\n"); else if(judge==2) printf("數值輸入錯誤,應屬於零到十範圍\n"); else if(judge==3) printf("數值總和必須在0~99範圍\n"); else if(judge==4) printf("符號輸入錯誤\n"); } 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 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); } 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 calculate(int coin) { char b[10],c[10],d[10]; while(2) { scanf("%s",b); if(!strcmp(b,"看看")) { if(coin>99) error(3); else 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,"整數")) { if(num(d)==-1) error(2); else { coin += num(d); } } else error(1); if(coin>99||coin<0) error(3); else { calculate(coin); } }
##總結:此次的做業我作了很長時間,從最開始的理解錯題目,用Python在錯誤的方向上尋找解決辦法,遲遲沒有成果而放棄,再到如今的成功完成。我明白正確的方向比盲目的尋找更重要。 ##Windows批處理天下第一!