第四次作业:个人项目-小学四则运算 “软件”之初版

一、题目要求:

像《构建之法》的人物阿超那样,写一个能自动生成小学四则运算题目的命令行 “软件”。


具体要求:任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 30, 那就输出 30 道题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。 如:23 - 3 * 4 = 11

二、代码提交

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<windows.h>

float algorithm(float x,char op,float y);
float algorithm2(float x,char op2,float y);
char createnumberop();
void test1(int n);

main()

{
int chooce,n;


printf("\n\t1.100以内四则运算 (小数点后保留两位)\n\n");

printf("\n请输入四则运算的数目:");


scanf("%d",&n);
if(n<=0)
printf("【数目有误,请重新输入!】\n");


test1(n);
}

 

//第一个运算符计算
float algorithm(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 algorithm2(float x,char op2,float y)
{
float result;
switch(op2)
{
case'+':result = x + y;break;
case'-':result = x - y;break;
case'*':result = x * y;break;
case'/':result = x / y;break;
}
return result;
}

//随机产生运算符
char mark()
{
int op;
op=rand() % 4+1;
switch(op)
{
case 1:return'+';
case 2:return'-';
case 3:return'*';
case 4:return'/';
}
return 0;
}

//100以内四则运算
void test1(int n)
{
int i,rightnum=0,wrongnum=0;//rightnumber正确的个数,wrongnumber错误的个数
float a,b,c,answer,result;
char op,op2;//运算符
srand(time(NULL));
for(i=0;i<n;i++)
{
a=(float)(rand() % 100+1);//获取随机数
b=(float)(rand() % 100+1);
c=(float)(rand() % 100+1);
op=mark();//获取随机运算符
op2=mark();
if((op=='+' || op=='-') && (op2=='*' || op2=='/')){
result=algorithm(a,op,algorithm2(b,op2,c));
}
else{
result=algorithm2(algorithm(a,op,b),op2,c);//根据运算符优先级得出结果
}
if(result< 0)//如果结果为非正数,则重新获取题目
{
i--;
continue;
}
else{
printf("%.f %c %.f %c %.f = ",a,op,b,op2,c);
scanf("%f",&answer);

if((int)(100.0*answer+0.5)/100.0==(int)(100.0*result+0.5)/100.0 && result>=0)//四舍五入精确到小数点后两位
{
printf("【回答正确!】\n\n");
rightnum++;
}
else
{
printf("【回答错误!答案是:%.2f】\n\n",(int)(100.0*result+0.5)/100.0);
wrongnum++;
}
}
}
printf("你总共答对了%d题,准确率为%.2f%!\n",n,rightnum,(float)rightnum/(float)n*100);
system("pause");
system("CLS");
printf("\n\n");
}

三、运算结果

 

四、个人软件过程耗时估计与统计表

PSP2.1 Personal Software Process Stages Time Senior Student Time
Planning 计划 8 6
· Estimate 估计这个任务需要多少时间 8 6
Development 开发 82 88
· Analysis 需求分析 (包括学习新技术) 6 10
· Design Spec 生成设计文档 5 6
· Design Review 设计复审 4 6
· Coding Standard 代码规范 3 3
· Design 具体设计 10 12
· Coding 具体编码 36 21
· Code Review 代码复审 7 9
· Test 测试(自我测试,修改代码,提交修改) 13 21
Reporting 报告 9 6
· 测试报告 3 2
· 计算工作量 2 1
· 并提出过程改进计划 3 3

 

posted @ 2018-10-15 13:56  廖世豪  阅读(181)  评论(2编辑  收藏  举报