| 這個作業屬于哪個課程 | AHPU-軟體工程導論-計算機18級 |
|---|---|
| 這個作業要求在哪里 | 個人作業-四則運算題目生成程式 |
| 這個作業的目標 | 熟練使用markdown語法攥寫博客,對題目需求進行分析并實作 |
| 學號 | 3181002122 |
一、題目要求
寫一個能自動生成小學四則運算題目的程式,然后在此基礎上擴展:
1)除了整數以外,還要支持真分數的四則運算,例如:1/6+1/8=7/24
2)程式要求能處理用戶的輸入,判斷對錯,累積分數
3)程式支持可以由用戶自行選擇加、減、乘、除運算
4)使用-n引數控制生成題目的個數,例如Myapp.exe -n 10,將生成10個題目
二、代碼提交
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
// 定義一個分式結構體
typedef struct{
int top;
int bottom;
} fraction;
int maxDivisor(int m, int n);
void reductionFraction(int &m, int &n);
void add(int num,fraction computerFracAns[],int computerIntAns[]);
void subtract(int num, fraction computerFracAns[], int computerIntAns[]);
void multiply(int num, fraction computerFracAns[], int computerIntAns[]);
void divide(int num, fraction computerFracAns[]);
int main(){
// 分數情況下,計算機的答案以及用戶的答案
fraction computerFracAns[1000], userFracAns[1000];
// 整數情況下,計算機的答案以及用戶的答案
int computerIntAns[1000] = { 0 }, userIntAns[1000] = { 0 };
int check;
char c;
int num, score = 0;
cout<<"四則運算題目生成程式"<<endl;
cout<<"請選擇:"<<endl;
cout<<"1:加法"<<endl;
cout<<"2:減法"<<endl;
cout<<"3:乘法"<<endl;
cout<<"4:除法"<<endl;
cin>>check; // 選擇運算方式
cout<<"請輸入題目數量:";
cin>>num;
switch (check){
case 1:add(num, computerFracAns, computerIntAns); break;
case 2:subtract(num, computerFracAns, computerIntAns); break;
case 3:multiply(num, computerFracAns, computerIntAns); break;
case 4:divide(num, computerFracAns); break;
}
if (check == 4){ // 題目是否為除法分類討論,分別有不同的處理方案
for (int i = 0; i < num; i++){
cout<<"第"<<i+1<<"道題的答案為:";
if (computerFracAns[i].bottom == 1){
cin>>userFracAns[i].top;
userFracAns[i].bottom = 1;
}
else
cin>>userFracAns[i].top>>c>>userFracAns[i].bottom;
}
}
else{
for (int i = 0; i <= num / 2; i++){
cout<<"第"<<i+1<<"道題的答案為:";
cin>>userIntAns[i];
}
for (int i = num / 2 + 1; i < num; i++){
cout<<"第"<<i+1<<"道題的答案為:";
if (computerFracAns[i].bottom == 1){
cin>>userFracAns[i].top;
userFracAns[i].bottom = 1;
}
else
cin>>userFracAns[i].top>>c>>userFracAns[i].bottom;
}
}
// 比較運算結果,正確則得分加1
for (int i = 0; i <= num / 2; i++)
if (userIntAns[i] == computerIntAns[i])
score++;
for (int i = num / 2 + 1; i < num; i++)
if (userFracAns[i].top == computerFracAns[i].top && userFracAns[i].bottom == computerFracAns[i].bottom)
score++;
cout<<"成績為:"<<score<<endl;
return 0;
}
// 求最大公約數
int maxDivisor(int m, int n){
if (n == 0)
return m;
return
maxDivisor(n, m % n);
}
// 對分數進行約分,不輸出引數
void reductionFraction(int &m, int &n){
int p = maxDivisor(abs(m), abs(n));
m /= p;
n /= p;
}
// 對分數進行約分,輸出引數
void reductionFractionOutput(int &m, int &n){
int p = maxDivisor(abs(m), abs(n));
m /= p;
n /= p;
if (n == 1)
cout<<m;
else
cout<<m<<"/"<<n;
}
// 生成加法題目
void add(int num,fraction computerFracAns[],int computerIntAns[]){
int m, n = 0;
int top1, bottom1 = 0;
int top2, bottom2 = 0;
// 一半題目為整數運算,一半題目為分數運算
for (int i = 0; i <=num / 2; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
m = rand() % 10 + 1;
n = rand() % 10 + 1;
computerIntAns[i] = m + n;
cout<<m<<"+"<<n<<endl; //輸出整數題目
}
for (int i = num/2+1; i < num; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
top1 = rand() % 10 + 1;
bottom1 = rand() % 10 + 1;
top2 = rand() % 10 + 1;
bottom2 = rand() % 10 + 1;
reductionFractionOutput(top1, bottom1);
cout<<"+";
reductionFractionOutput(top2, bottom2);
cout<<endl;
computerFracAns[i].top = top1 * bottom2 + top2 * bottom1;
computerFracAns[i].bottom = bottom1 * bottom2;
reductionFraction(computerFracAns[i].top, computerFracAns[i].bottom);
}
}
// 生成減法題目
void subtract(int num, fraction computerFracAns[], int computerIntAns[]){
int m, n = 0;
int top1, bottom1 = 0;
int top2, bottom2 = 0;
// 一半題目為整數運算,一半題目為分數運算
for (int i = 0; i <= num / 2; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
m = rand() % 10 + 1;
n = rand() % 10 + 1;
computerIntAns[i] = m - n;
cout<<m<<"-"<<n<<endl;
}
for (int i = num / 2 + 1; i < num; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
top1 = rand() % 10 + 1;
bottom1 = rand() % 10 + 1;
top2 = rand() % 10 + 1;
bottom2 = rand() % 10 + 1;
reductionFractionOutput(top1, bottom1);
cout<<"-";
reductionFractionOutput(top2, bottom2);
cout<<endl;
computerFracAns[i].top = top1 * bottom2 - top2 * bottom1;
computerFracAns[i].bottom = bottom1 * bottom2;
reductionFraction(computerFracAns[i].top, computerFracAns[i].bottom);
}
}
// 生成乘法題目
void multiply(int num, fraction computerFracAns[], int computerIntAns[]){
int m, n = 0;
int top1, bottom1 = 0;
int top2, bottom2 = 0;
// 一半題目為整數運算,一半題目為分數運算
for (int i = 0; i <= num / 2; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
m = rand() % 10 + 1;
n = rand() % 10 + 1;
computerIntAns[i] = m * n;
cout<<m<<"*"<<n<<endl;
}
for (int i = num / 2 + 1; i < num; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
top1 = rand() % 10 + 1;
bottom1 = rand() % 10 + 1;
top2 = rand() % 10 + 1;
bottom2 = rand() % 10 + 1;
reductionFractionOutput(top1, bottom1);
cout<<"*";
reductionFractionOutput(top2, bottom2);
cout<<endl;
computerFracAns[i].top = top1 * top2;
computerFracAns[i].bottom = bottom1 * bottom2;
reductionFraction(computerFracAns[i].top, computerFracAns[i].bottom);
}
}
// 生成除法題目
void divide(int num, fraction computerFracAns[]){
int top1, bottom1 = 0;
int top2, bottom2 = 0;
// 在除法下,題目無需考慮整數情況,因為此情況下他就會有除法存在就是分數了
for (int i = 0; i < num; i++){
unsigned int times = (unsigned int)time(0); // 使用time生成亂數
srand(times * (i + 1));
top1 = rand() % 10 + 1;
bottom1 = rand() % 10 + 1;
top2 = rand() % 10 + 1;
bottom2 = rand() % 10 + 1;
reductionFractionOutput(top1, bottom1);
cout<<"/";
reductionFractionOutput(top2, bottom2);
cout<<endl;
computerFracAns[i].top = top1 * bottom2;
computerFracAns[i].bottom = bottom1 * top2;
reductionFraction(computerFracAns[i].top, computerFracAns[i].bottom);
}
}
三、運行截圖
加法算式生成:

減法算式生成:

乘法算式生成:

除法算式生成:

四、個人小結
| psp2.1 | 任務內容 | 計劃完成需要的時間(min) | 實際完成需要的時間(min) |
|---|---|---|---|
| Planning | 計劃 | 30 | 50 |
| Estimate | 估計這個任務需要多少時間,并規劃大致作業步驟 | 10 | 5 |
| Development | 開發 | 355 | 300 |
| Analysis | 需求分析(包括學習新技術) | 20 | 25 |
| Design Spec | 生成設計檔案 | 15 | 10 |
| Design Review | 設計復審 | 10 | 10 |
| Coding Standard | 代碼規范 | 20 | 15 |
| Design | 具體設計 | 30 | 25 |
| Coding | 具體編碼 | 180 | 150 |
| Code Review | 代碼復審 | 15 | 10 |
| Test | 測驗(自我測驗,修改代碼,提交修改) | 10 | 15 |
| Reporting | 報告 | 20 | 15 |
| Test Report | 測驗報告 | 15 | 10 |
| Size Measurement | 計算作業量 | 10 | 10 |
| Postmortem & Process Improvement Plan | 事后總結,并提出程序改進計劃 | 10 | 15 |
識訓
這是我第一次在博客園書寫我的第一篇博客,之前都是經常使用csdn,兩者的markdown編輯器比較起來,csdn上有更多的可視化選項
,而博客園更多是要自己撰寫語法,本次博客是軟體工程課程一次題目作業,根據題目的要求進行對應的編程,整個程序也是對之前學習的知識的一次鞏固,還是很值得的,后面還第一次接觸到了psp表,然后我也根據要求對我的整個開發歷程進行了一個大概統計,也對我自己整個開發歷程做一個簡單的回顧,還是比較好的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/207456.html
標籤:其他
上一篇:沒有爬過的壁紙網站越來越少了,找到一個不錯的(附原始碼)
下一篇:Java注解(入門級)
