3122004629 杨森
3122006504 钟礼骏

作业要求 https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230
作业目标 用c++代码实现小学四则运算,记录PSP表格
GitHub链接 https://github.com/sunshyys/sizeyunsuan/tree/main

函数如下:

1.random():给定最大数范围后用来生成一个随机数并返回;

2.ran_str():生成四个运算符中的一个随机符号并返回;

3.as():用来进行结果运算的函数,返回结果值;

主函数如下:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>

//#define MaxSize 100//定义栈中元素的最大个数 
//typedef struct SqStack
//{
//	string data[MaxSize];//存放栈中的元素 
//	int top;//栈顶指针 
//}SqStack;
//
////typedef struct LinkNode
////{
////	int data;//数据域
////	struct LinkNode *next;//指针域 
////}*LiStack;//栈类型定义 
//
//void InitStack(SqStack &S)
//{
//	S.top=-1;
//}
//
//void Push(SqStack &S,string x)//入栈 
//{
//	if(S.top==MaxSize-1)
//	{
//		cout<<"栈满"<<endl;
//		return; 
//	}
//	S.data[++S.top]=x;
//}
//
//void Pop(SqStack &S,string x)//出栈 
//{
//	if(S.top==-1)
//	{
//		cout<<"栈空"<<endl;
//		return;
//	}
//	x=S.data[S.top--];
//}
//
//int GetTop(SqStack S)//读取顶元素 
//{
//	if(S.top==-1)
//	{
//		cout<<"栈空"<<endl;
//		return 0;
//	}
//	else
//	{
//		return S.data[S.top];
//	}
//}
//
//void PrintStack(SqStack S)//遍历栈 
//{
//	while(S.top!=-1)
//	{
//		cout<<S.data[S.top--]<<" ";
//	}
//	cout<<endl;
//}
//
//
//


int random(int n,int m)//随机数 
{
	return rand()%(m-n+1)+n;
}

string rand_str()//运算符
{
	int pp;
	pp=rand()%4;
	if(pp==0)return "+";
	else if(pp==1)return "-";
	else if(pp==2)return "×";
	else if(pp==3)return "÷";
}




double as(string t,double aaa,double bbb)//结果运算 
{
	double ccc;
	if(t=="+")
	{
		ccc=aaa+bbb;
	}
	else if(t=="-")
	{
		ccc=aaa-bbb;
	}
	else if(t=="×")
	{
		ccc=aaa*bbb;
	}
	else if(t=="÷")
	{
		ccc=aaa/bbb;
	}
	return ccc;
}





int main()
{
	
	
	
	FILE *fp;
	fp=fopen("exc.txt","w");
	fclose(fp);
	fp=fopen("ans.txt","w");
	fclose(fp);
//	fp=fopen("exc.txt","a");
//	fprintf(fp,"aaa");
//  fclose(fp);

//	string S[20];
//	S[6]="÷";
//	S[7]="×";
//	S[8]=4+'0';
//	S[13]="*";
//	cout<<S[6]<<endl<<S[13]<<endl<<S[8]<<endl<<S[7]<<endl;
	int a,b,N,R;
	int yc;
	srand(time(0));
	string t;
	//t=rand_str();
	string geshu,fanwei;
	string jieguo;
	cout<<"请输入需要的操作:"<<endl<<"题目个数:-n"<<endl<<"数值范围:-r" <<endl;
	cin>>geshu;
	if(geshu!="-n"){cout<<"输入错误,拜拜了您嘞";return 0;}
	else 
	cin>>N;
	cin>>fanwei;
	if(fanwei!="-r"){cout<<"输入错误,拜拜了您嘞";return 0;}
	else 
	cin>>R;
//	yc=random(1,3);
//	cout<<yc;
//	for(int i=1;i<=N;i++)
//	{
//		a=random(1,R);
//		cout<<a<<endl;
//	}
	
	for(int j=1;j<=N;j++)
	{
		string SS[10];
		int aa[10];
		int io=0,ioo=0;
		yc=random(1,3);
		int yccc=yc;
		b=random(1,R);
		cout<<yc<<endl;
		cout<<b;
		aa[0]=b;
		
		while(yc>0)
		{
			a=random(1,R);
			t=rand_str();
			SS[io++]=t;
			aa[++ioo]=a;
			cout<<t<<a;
			yc--;
		}
		cout<<"="<<endl;
		
		double san;
		double bb[10];
		bb[0]=aa[0];
		bb[1]=aa[1];
		bb[2]=aa[2];
		bb[3]=aa[3];
		
		if(yccc==1)
		{
			san=as(SS[0],aa[0],aa[1]);
			
			
			string s;
			if(SS[0]=="×")SS[0]="*";
			else if(SS[0]=="÷")SS[0]="/";
			
			fp=fopen("exc.txt","a");
			fprintf(fp,"question%d :     %d  %s  %d  =    \n",j,aa[0],SS[0].c_str(),aa[1]);
			fclose(fp);
			
			
			fp=fopen("ans.txt","a");
			fprintf(fp,"question%d :     %d  %s  %d  =  %.3f  \n",j,aa[0],SS[0].c_str(),aa[1],san);
			fclose(fp);
			
		}
		else if(yccc==2)
		{
			if((SS[1]=="×"||SS[1]=="÷")&&(SS[0]=="+"||SS[0]=="-"))
			{
				san=as(SS[1],aa[1],aa[2]);
				san=as(SS[0],aa[0],san);
			}
			else
			{
				san=as(SS[0],aa[0],aa[1]);
				san=as(SS[1],san,aa[2]);
			}
			
			if(SS[0]=="×")SS[0]="*";
			else if(SS[0]=="÷")SS[0]="/";
			if(SS[1]=="×")SS[1]="*";
			else if(SS[1]=="÷")SS[1]="/";
			
			fp=fopen("exc.txt","a");
			fprintf(fp,"question%d :     %d  %s  %d  %s  %d  =   \n",j,aa[0],SS[0].c_str(),aa[1],SS[1].c_str(),aa[2]);
			fclose(fp);
			fp=fopen("ans.txt","a");
			fprintf(fp,"question%d :     %d  %s  %d  %s  %d  =  %.3f  \n",j,aa[0],SS[0].c_str(),aa[1],SS[1].c_str(),aa[2],san);
			fclose(fp);
			
			
		}
		else if(yccc==3)
		{
//			if(((SS[0]=="×")||(SS[0]=="÷"))&&((SS[1]=="+")||(SS[1]=="-"))&&((SS[0]=="×")||(SS[0]=="÷")))
//			{
//				san=as(SS[0],aa[0],aa[1]);
//				int pl;
//				pl=as(SS[2],aa[2],aa[3]);
//				san=as(SS[1],as,pl);	
//			}
//			else if(((SS[0]=="+")||(SS[0]=="-"))&&((SS[1]=="×")||(SS[1]=="÷"))&&((SS[0]=="+")||(SS[0]=="-")))
//			{
//				san=as(SS[1])
//			}
			if(((SS[0]=="+")||(SS[0]=="-"))&&((SS[1]=="×")||(SS[1]=="÷"))&&((SS[2]=="+")||(SS[2]=="-")))
			{
				san=as(SS[1],aa[1],aa[2]);
				san=as(SS[0],aa[0],san);
				san=as(SS[2],san,aa[3]);
			}
			else
			{
			
			int key=0;
			for(int i=0;i<3;i++)
			{
				if(SS[i]=="×"||SS[i]=="÷")
				{
					key++;
					san=as(SS[i],bb[i],bb[i+1]);
					bb[i]=san;
					bb[i+1]=san;
					if(SS[i-1]=="×"||SS[i-1]=="÷")
					{
						bb[i-1]=bb[i];
					}
				}
			}
			if(key==0)
			{
				for(int i=0;i<3;i++)aa[i+1]=as(SS[i],aa[i],aa[i+1]);
				san=aa[3];
			}
			else if(key>0&&key<3)
			{
				for(int i=0;i<3;i++)
				{
					if(SS[i]=="+"||SS[i]=="-")
					{
						san=as(SS[i],bb[i],bb[i+1]);
						bb[i]=san;
						bb[i+1]=san;
						if(SS[i+1]=="×"||SS[i+1]=="÷")
						{
							bb[i+2]=san;
							if(SS[i+1]=="×"||SS[i+1]=="÷")
							{
								bb[i+3]=san;
							}
						}
					}
				}
			}
			else if(key==3)
			{
				san=bb[3];
			}
			
			
			
			if(SS[0]=="×")SS[0]="*";
			else if(SS[0]=="÷")SS[0]="/";
			
			if(SS[1]=="×")SS[1]="*";
			else if(SS[1]=="÷")SS[1]="/";
			
			if(SS[2]=="×")SS[2]="*";
			else if(SS[2]=="÷")SS[2]="/";
			
			fp=fopen("exc.txt","a");
			fprintf(fp,"question%d :     %d  %s  %d  %s  %d  %s  %d  =    \n",j,aa[0],SS[0].c_str(),aa[1],SS[1].c_str(),aa[2],SS[2].c_str(),aa[3]);
			fclose(fp);
			fp=fopen("ans.txt","a");
			fprintf(fp,"question%d :     %d  %s  %d  %s  %d  %s  %d  =  %.3f  \n",j,aa[0],SS[0].c_str(),aa[1],SS[1].c_str(),aa[2],SS[2].c_str(),aa[3],san);
			fclose(fp);
		}
		
	}
		cout<<san<<endl<<endl;
		
		
//		×";
//	else if(pp==3)return "÷
		
//		int ri=0;
//		while(yccc>0)
//		{
//			cout<<aa[ri]<<endl;
//			cout<<SS[ri++]<<endl;
//			yccc--;
//		}
		
		
//		while(ycc>=0)
//		{
//			cout<<SS[ioo++]<<endl;
//			ycc--;
//		}
	}
	
	
	return 0;
}


运行n为10,r为10:

结果在exc.txt和ans.txt中生成:

PSP表格如下:

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 150 180
Estimate 估计这个任务需要多少时间 250 300
Development 开发 300 320
Analysis 需求分析 (包括学习新技术) 100 100
Design Spec 生成设计文档 30 30
Design Review 设计复审 30 20
Coding Standard 代码规范 (为目前的开发制定合适的规范) 20 10
Design 具体设计 140 150
Coding 具体编码 30 50
Code Review 代码复审 20 20
Test 测试(自我测试,修改代码,提交修改) 10 30
Reporting 报告 100 200
Test Repor 测试报告 30 30
Size Measurement 计算工作量 5 10
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 5 20
合计 1220 1470
posted on 2024-09-28 17:44  y-sunshy  阅读(14)  评论(0)    收藏  举报