天天看點

結對項目-四則運算 “軟體”之更新版

本作業要求來自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2213 

我的githup位址為:https://github.com/lianglina98/ruanjian02

、基本要求

從個人項目出發,将程式改造成一個單機帶使用者界面(不是控制台)的程式,這個程式最基本要達到:

  • 自動生成題目,單個題目最多不能超過4個運算符,操作數小于100。
  • 使用者可以輸入答案
  • 若使用者輸入答案正确,則提示正确;若答案錯誤,則提示錯誤,并要提示正确答案是多少。

二、擴充方向

  1. 程式可以出帶括号的正整數四則運算,支援分數,除法保留兩位小數,如:(1/3+1)*2 = 2.67,特别注意:這裡是2.67而非2.66(保留兩位小數功能實作,不支援出帶括号題目)
  2. 使用者可以選擇出題的個數(最多不能超過5個題目),答題結束可以顯示使用者答錯的題目個數和答對的題目個數(實作)

三、開發環境調試:Visual C++

四、代碼呈現

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<math.h>

#include<windows.h>

float calculate(float x,char op,float y)

{

float result;

switch(op)

{

case'+':result = x + y;break;

case'-':result = x - y;break;

case'*':result = x * y;break;

case'/':result = x / y;break;

}

return result;

}

float calculate2(float x,char op2,float y)

switch(op2)

int createnumber()

return rand() % 100+1;

int createnumber2()

return rand() % 20+1;

char createnumberop()

int op;

op=rand() % 4+1;

case 1:return'+';

case 2:return'-';

case 3:return'*';

case 4:return'/';

return 0;

void exercises(int n)

int i,right=0,wrong=0;

float a,b,c,answer,result1,result2;

char op,op2;

srand(time(NULL));

for(i=0;i<n;i++)

a=(float)createnumber();

b=(float)createnumber();

c=(float)createnumber();

op=createnumberop();

op2=createnumberop();

result1=calculate2(calculate(a,op,b),op2,c);

result2=calculate(a,op,calculate2(b,op2,c));

if(result1 < 0 || result2 <0)

{

i--;

continue;

}

else{

printf("%.f %c %.f %c %.f = ",a,op,b,op2,c);

scanf("%f",&answer);

if((op=='+' || op=='-') && (op2=='*' || op2=='/'))

{

if((int)(100.0*answer+0.5)/100.0==(int)(100.0*result2+0.5)/100.0 && result2 >=0)

{

printf("\n【回答正确!】\n\n");

right++;

}

else

printf("\n【回答錯誤!答案是:%.2f】\n\n",(int)(100.0*result2+0.5)/100.0);

wrong++;

}

else

if((int)(100.0*answer+0.5)/100.0==(int)(100.0*result1+0.5)/100.0 && result1 >=0)

{printf("\n【回答錯誤!答案是:%.2f】\n\n",(int)(100.0*result1+0.5)/100.0);

printf("\n你答對了%d題,答錯了%d題\n\n",right,wrong);

double gongbeishu(double b,double d)

double i,max;

max=b>d?b:d;

for(i=max;;i++)

if((int)(i)%(int)(b)==0 && (int)(i)%(int)(d)==0)

break;

return i;

double gongyueshu(double a,double c)

double i,min;

min=a<c?a:c;

for(i=2;i<=min;i++)

if((int)(a)%(int)(i)==0 && (int)(c)%(int)(i)==0)

if(i>min)

i=1;

int main()

int n;

printf("\n——————————————四則運算-------------------------------------------\n");

printf("\n\n| 歡迎進入四則運算小程式! |\n");

printf("\n——————————————---------------------------------------------------\n");

while(1)

do{

printf("\n請輸入四則運算的數目:");

scanf("%d",&n);

if(n<=0)

printf("數目有誤,請重新輸入!\n");

if(n>5)

printf("最多不能超過5個題目,請重新輸入!\n");

}while(n<=0 || n>5);

exercises(n);

結對項目-四則運算 “軟體”之更新版

五、運作結果截圖

結對項目-四則運算 “軟體”之更新版

六、感想

我并沒有結對,自己的基礎不好,不想拖累别人。這個作業我做得很簡單,主要的問題就在于計算上,其他基本沒有什麼問題。