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

一、题目要求:

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


具体要求:任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 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.1Personal Software Process StagesTime Senior StudentTime
Planning计划86
· Estimate估计这个任务需要多少时间86
Development开发8288
· Analysis需求分析 (包括学习新技术)610
· Design Spec生成设计文档56
· Design Review设计复审46
· Coding Standard代码规范33
· Design具体设计1012
· Coding具体编码3621
· Code Review代码复审79
· Test测试(自我测试,修改代码,提交修改)1321
Reporting报告96
·测试报告32
·计算工作量21
·并提出过程改进计划33

 

转载于:https://www.cnblogs.com/l-sh/p/9790621.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值