软件工程第二次作业

软工第二次作业

内容

预习
阅读讲义( https://www.cnblogs.com/xinz/archive/2011/11/27/2265425.html )第二部分内容 2. 个人开发技术

个人程序
(本作业内容来源于网址:https://www.cnblogs.com/xinz/p/7417960.html,
https://www.cnblogs.com/jiel/p/4810756.html
像《构建之法》的人物阿超那样,写一个能自动生成小学四则运算题目的命令行 “软件”。 可以使用C,C++,或者C#等语言实现。

要求

分别能够实现小学一、二、三、四、五年级的四则运算要求, 逐步实现各个年级的难度
要求能够通过输入来选择不同年级,每个年级还得区分难,中,易三个等级
对于三、四、五年级需要支持括号与多个运算符
程序支持判断对错及累计得分与时间
一次可以出100道题目,而且不能重复(比如2+3 与 3+2 算重复的)
充分发挥想象增加满足小学生数学检测需要的功能
作业2
git学习与码云代码托管平台gitee
阅读网址如下关于git的使用
https://www.liaoxuefeng.com/wiki/896043488029600

https://gitee.com/help/categories/43
通过之前已经注册的码云账号,学会简单的使用
将作业1所写代码托管到gitee平台上个人仓库

在博客园给出作业并包括所写的代码,同时给出托管于gitee上的链接

代码链接

https://gitee.com/tao_tao_chen1/soft_work/blob/master/Math.cpp

感想

抓狂,原本打算根据PAT算法题里面的简单计算器进行改良从而实现所有功能,但是因为数据结构功底不扎实,那个代码怎么改都改不对,只能用最基础的循环分支语句来写,代码写了近600行,但有价值的东西较少,总算实现了分数的计算,由于时间关系,计时和没有重复的功能没有实现,下次有机会再回来修改。#

代码

/*假定四则运算
一年级简单程度是10以内2个数加减法,中等程度是10以内3个数加减法,困难程度是20以内3个数加减法
二年级简单程度是100以内2个数加减法,中等程度是100以内3个数加减法,困难程度是1000以内3个数加减法
三年级简单程度是2位数乘1位数乘法,中等是2位数乘2位数乘法。困难是1位数乘法和加减法的混合运算
四年级简单程度是2位数除1位数除法,中等是2位数乘除法混合运算,困难是3位数乘除法的混合运算(答案以整数加余数的形式展现)(a/b/c类型的答案以3位小数展现且abc可为小数)
五年级简单程度是四个分数加减运算,中等是四个分数的的乘除运算,困难是4个分数的混合运算(答案以带分数形式给出)
*/
#include<stdio.h>
#include<iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//下面的代码根据算法笔记A1081 分数的四则运算修改而得
typedef long long ll;
int ans4,ans5,ans6;//带分数
ll gcd(ll a,ll b){//求最大公约数的辗转相除法递归写法
 return b==0?a:gcd(b,a%b);
}
struct Fraction{//分数
	ll up,down;//分子,分母
};
Fraction reduction(Fraction result){//分数的化简
	if(result.down<0){
		result.down=-result.down;
		result.up=-result.up;
		
	}
	if(result.up==0){
		result.down=1;
	}else{
		int d=gcd(abs(result.up),abs(result.down));
		result.down/=d;
		result.up/=d;
	}
	return result;
}
//分数的加减乘除
Fraction add(Fraction f1,Fraction f2){
	Fraction result;
	result.up=f1.up*f2.down+f2.up *f1.down;
	result.down=f1.down*f2.down;
	return reduction(result);
}
Fraction minu(Fraction f1,Fraction f2){
	Fraction result;
	result.up=f1.up*f2.down-f2.up *f1.down;
	result.down=f1.down*f2.down;
	return reduction(result);
}
Fraction multi(Fraction f1,Fraction f2){
	Fraction result;
	result.up=f1.up*f2.up;
	result.down=f1.down*f2.down;
	return reduction(result);
}
Fraction divide(Fraction f1,Fraction f2){
	Fraction result;
	result.up=f1.up*f2.down;
	result.down=f1.down*f2.up;
	return reduction(result);
}
//分数的输出
void showResult(Fraction r){
	reduction(r);
	if(r.down==1) printf("%lld\n",r.up);
	else if(abs(r.up)>r.down){//假分数
		ans4=r.up/r.down;
		ans5=abs(r.up)%r.down;
		ans6=r.down;
	}else{//真分数
		ans4=0;
		ans5=r.up;
		ans6=r.down;
	}
}
int ans;
int ans1;//除法所得结果整数部分
int ans2;//除法所得结果余数部分
double ans3;//(a/b/c类型的答案以3位小数展现)

int a1[100],b1[100],c1[100];//10以内整数
int main(){
	Fraction sum,sum2,temp,sum3,sum4;
	sum.up=0;
	sum.down=1;
	sum2.up=0;
	sum2.down=1;
	sum3.up=1;
	sum3.down=1;
	sum4.up=1;
	sum4.down=1;
	int right=0;
    int wrong=0;
	int grade;
	int degree;//3 Difficulty level,respectively are difficult=3 medium=2 easy=1 
	int num;//题目数量
	printf("请输入你的年级,一年级输入1,二年级输入2,三年级输入3,四年级输入4,五年级输入5\n");
	scanf("%d",&grade);
	printf("请选择难易程度,选择简单输入1,选择中等输入2,选择困难输入3\n");
	scanf("%d",&degree);
	switch(grade){
	case 1:
		if(degree==1){
			printf("这是一年级运算简单程度\n");
			printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
			
		    int n1=rand()%10;
			int n2=rand()%10;
			if(n1>=n2){
				a1[i]=n1;
				b1[i]=n2;
			}
			else{
				a1[i]=n2;
				b1[i]=n1;
			}
			}
			for(int i=0;i<num/2;i++){
				printf(" %d + %d = \n",a1[i],b1[i]);
				scanf("%d",&ans);
					if(a1[i]+b1[i]==ans){
                    printf("回答正确!\n");
                    right++;
                                         }
            else{
               printf("回答错误!\n");
               wrong++;
                 }
			                         }
			for(int i=num/2;i<num;i++){
				printf("\n %d -%d = ",a1[i],b1[i]);
				scanf("%d",&ans);
					if(a1[i]-b1[i]==ans){
                    printf("回答正确!\n");
                    right++;
                                         }
            else{
               printf("回答错误!\n");
               wrong++;
                 }
			                         }
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
		}
		
		else if(degree==2){printf("这是一年级运算中等程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			for(int i=0;i<num;){
			int n1=rand()%10;
			int n2=rand()%10;
			int n3=rand()%10;
			if(n1+n2-n3>=0&&n1+n3-n2>=0){
				a1[i]=n1;
				b1[i]=n2;
				c1[i]=n3;
				i++;}			
			}
			for(int i=0;i<num/2;i++){
				printf(" %d + %d -%d= \n",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]+b1[i]-c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			                         }
			for(int i=num/2;i<num;i++){
				printf("\n %d -%d +%d= ",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]-b1[i]+c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++; }
			                         }
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
		}
		else{
			printf("这是一年级运算困难程度");
		    printf("请输入题目数量\n");
			scanf("%d",&num);
			for(int i=0;i<num;){
			int n1=rand()%10;
			int n2=rand()%10;
			int n3=rand()%10;
			if(n1+n2-n3>=0&&n1+n3-n2>=0){
				a1[i]=n1+10;
				b1[i]=n2+10;
				c1[i]=n3+10;
				i++;}			
			}
			for(int i=0;i<num/2;i++){
				printf(" %d + %d -%d= \n",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]+b1[i]-c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			                         }
			for(int i=num/2;i<num;i++){
				printf("\n %d -%d +%d= ",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]-b1[i]+c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++; }
			                         }
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
		}

		break;
	case 2:
		if(degree==1){printf("这是二年级运算简单程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
			
		    int n1=rand()%100;
			int n2=rand()%100;
			if(n1>=n2){
				a1[i]=n1;
				b1[i]=n2;
			}
			else{
				a1[i]=n2;
				b1[i]=n1;
			}
			}
			for(int i=0;i<num/2;i++){
				printf(" %d + %d = \n",a1[i],b1[i]);
				scanf("%d",&ans);
					if(a1[i]+b1[i]==ans){
                    printf("回答正确!\n");
                    right++;
                                         }
            else{
               printf("回答错误!\n");
               wrong++;
                 }
			                         }
			for(int i=num/2;i<num;i++){
				printf("\n %d -%d = ",a1[i],b1[i]);
				scanf("%d",&ans);
					if(a1[i]-b1[i]==ans){
                    printf("回答正确!\n");
                    right++;
                                         }
            else{
               printf("回答错误!\n");
               wrong++;
                 }
			                         }
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);}
		else if(degree==2){printf("这是二年级运算中等程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			for(int i=0;i<num;){
			int n1=rand()%100;
			int n2=rand()%100;
			int n3=rand()%100;
			if(n1+n2-n3>=0&&n1+n3-n2>=0){
				a1[i]=n1;
				b1[i]=n2;
				c1[i]=n3;
				i++;}			
			}
			for(int i=0;i<num/2;i++){
				printf(" %d + %d -%d= \n",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]+b1[i]-c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			                         }
			for(int i=num/2;i<num;i++){
				printf("\n %d -%d +%d= ",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]-b1[i]+c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++; }
			                         }
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);}
		else{printf("这是二年级运算困难程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			for(int i=0;i<num;){
			int n1=rand()%1000;
			int n2=rand()%1000;
			int n3=rand()%1000;
			if(n1+n2-n3>=0&&n1+n3-n2>=0){
				a1[i]=n1;
				b1[i]=n2;
				c1[i]=n3;
				i++;}			
			}
			for(int i=0;i<num/2;i++){
				printf(" %d + %d -%d= \n",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]+b1[i]-c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			                         }
			for(int i=num/2;i<num;i++){
				printf("\n %d -%d +%d= ",a1[i],b1[i],c1[i]);
				scanf("%d",&ans);
					if(a1[i]-b1[i]+c1[i]==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++; }
			                         }
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);}
		break;
	case 3:
		if(degree==1){printf("这是三年级运算简单程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
		    int n1=rand()%100;
			int n2=rand()%10;
				printf(" %d * %d = \n",n1,n2);
				scanf("%d",&ans);
					if(n1*n2==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}	                                  
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
		}
		else if(degree==2){printf("这是三年级运算中等程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
		    int n1=rand()%100;
			int n2=rand()%100;
				printf(" %d * %d = \n",n1,n2);
				scanf("%d",&ans);
					if(n1*n2==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}	                                  
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);}
		else{printf("这是三年级运算困难程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;){
		    int n1=rand()%10;
			int n2=rand()%10;
			int n3=rand()%100;
			if(n1*n2-n3>0){
				i++;
				printf(" %d * %d-%d = \n",n1,n2,n3);
				scanf("%d",&ans);
					if(n1*n2-n3==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}				
			else{
				i++;
					printf(" %d * %d+%d = \n",n1,n2,n3);
				scanf("%d",&ans);
					if(n1*n2+n3==ans){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}
			}
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);}
		break;
	case 4:
		if(degree==1){
			printf("这是四年级运算简单程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
		    int n1=rand()%100;
			int n2=rand()%10;
				printf(" %d / %d = \n",n1,n2);
				scanf("%d %d",&ans1,&ans2);
					if(n1/n2==ans1&&n1%n2==ans2){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}	                                  
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
		}
		else if(degree==2){printf("这是四年级运算中等程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num/2;i++){
		    int n1=rand()%100;
			int n2=rand()%100;
			int n3=rand()%100;			
				printf(" %d * %d/%d = \n",n1,n2,n3);
				scanf("%d %d",&ans1,&ans2);
					if(n1*n2/n3==ans1&&n1*n2%n3==ans2){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}				
			for(int i=num/2;i<num;i++){
		    double n1=rand()%100;
			double n2=rand()%100;
			double n3=rand()%10;			
				printf(" %.f /%.f/%.f = \n",n1,n2,n3);
				scanf("%lf",&ans3);//注意 scanf("%.3f",&ans3); 这种写法是错误的
				double c=n1/(n2*n3);
				c=(int)(1000.0*c+0.5)/1000.0;//将计算结果保留为三位小数
					if(c==ans3){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
			}
		else{printf("这是四年级运算困难程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num/2;i++){
		    int n1=rand()%1000;
			int n2=rand()%1000;
			int n3=rand()%1000;			
				printf(" %d * %d/%d = \n",n1,n2,n3);
				scanf("%d %d",&ans1,&ans2);
					if(n1*n2/n3==ans1&&n1*n2%n3==ans2){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}				
			for(int i=num/2;i<num;i++){
		    double n1=rand()%1000;
			double n2=rand()%1000;
			double n3=rand()%100;			
				printf(" %.f /%.f/%.f = \n",n1,n2,n3);
				scanf("%lf",&ans3);//注意 scanf("%.3f",&ans3); 这种写法是错误的
				double c=n1/(n2*n3);
				c=(int)(1000.0*c+0.5)/1000.0;//将计算结果保留为三位小数
					if(c==ans3){
                    printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}
			printf("你一共答对了%d道题,答错了%d道题\n",right,wrong);
		}
		break;
	default://五年级
		if(degree==1){printf("这是五年级运算简单程度");
			printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
				sum.up=0;
	            sum.down=1;
			for(int i=0;i<2;i++){	
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld+",temp.up,temp.down);
		sum=add(sum,temp);
	}
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld-",temp.up,temp.down);
		sum=add(sum,temp);
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld=",temp.up,temp.down);
		sum=minu(sum,temp);
			showResult(sum);
			int o=0,p=0,q=0;
			scanf("%d %d/%d",&o,&p,&q);
			if(o==ans4&&p==ans5&&q==ans6){ printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}
		}	
		else if(degree==2){printf("这是五年级运算中等程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
				sum3.up=1;
	            sum3.down=1;
			for(int i=0;i<2;i++){	
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld*",temp.up,temp.down);
		sum3=multi(sum3,temp);
	}
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld/",temp.up,temp.down);
		sum3=multi(sum3,temp);
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld=",temp.up,temp.down);
		sum3=divide(sum3,temp);
			showResult(sum3);
			int o=0,p=0,q=0;
			scanf("%d %d/%d",&o,&p,&q);
			if(o==ans4&&p==ans5&&q==ans6){ printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}
		}
		else{printf("这是五年级运算困难程度");
		printf("请输入题目数量\n");
			scanf("%d",&num);
			srand((unsigned) (time(NULL)));
			for(int i=0;i<num;i++){
				sum3.up=1;
	            sum3.down=1;
			for(int i=0;i<2;i++){	
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld*",temp.up,temp.down);
		sum3=multi(sum3,temp);
	}
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld/",temp.up,temp.down);
		sum3=multi(sum3,temp);
		temp.up=rand()%10;
		temp.down=rand()%10+1;
		printf("%lld/%lld=",temp.up,temp.down);
		sum3=divide(sum3,temp);
			showResult(sum3);
			int o=0,p=0,q=0;
			scanf("%d %d/%d",&o,&p,&q);
			if(o==ans4&&p==ans5&&q==ans6){ printf("回答正确!\n");
                    right++;}
            else{
               printf("回答错误!\n");
               wrong++;}
			}
		}
}	
	system("pause");
return 0;
}
posted @ 2020-03-04 20:49  西泽尔1  阅读(126)  评论(0编辑  收藏  举报