四则运算----C++版
一、设计思想
因java中已做过,就是简单的将java中的语句调换为C++的语句。
二、代码
#include<iostream.h>
#include<Stdlib.h>
void main()
{	
	int h;
	int g;
	int a;
	int f;
	cout<<"请输入要输出多少道运算题"<<endl;
	cin>>h;
	cout<<"请输入你要计算数的最大值"<<endl;
	cin>>g;
	cout<<"**************菜单选择程序****************"<<endl;
	cout<<"请选择要使用的功能:"<<endl;
	cout<<"1.   整数加减法运算!"<<endl;
	cout<<"2.   整数四则运算!"<<endl;
	cout<<"3.   分数的加减运算"<<endl;
	cout<<"4.   分数的四则运算"<<endl;
	cout<<" *********************************************"<<endl;
	char arr[] = {'+','-','*','/'};
	char arr2[]={'-',' '};
	cin>>a;
	switch(a)
	{	
	case 1:
		{
			cout<<"请选择减法是否有负数;0:无负数,1:有负数"<<endl;
			cin>>f;
			int i;
			switch(f)
			{
			case 0:
				{
					for(i=0;i<h;i++)
					{
						
						cout<<rand()%g<<arr[rand()%2]<<rand()%g<<"="<<endl;
					}
					break;
				}
			case 1: 
				{
					for(i=0;i<h;i++)
					{
						cout<<arr2[rand()%2]<<rand()%g<<arr[rand()%2]<<arr2[rand()%2]<<rand()%g<<"="<<endl;
					}
					break;
				}
			default:
				cout<<"输入的数有误,请输入0或1"<<endl;
			}
			break;
		}
	case 2:
		{
			cout<<"**************四则运算菜单选择程序****************"<<endl;
			cout<<"请选择要使用的功能:"<<endl;
			cout<<"1.   正整数无余数四则运算!"<<endl;
			cout<<"2.   可负整数无余数四则运算!"<<endl;
			cout<<"3.   正整数可有余数四则运算"<<endl;
			cout<<"4.   可负整数可有余数四则运算"<<endl;
			cout<<" *********************************************"<<endl;
			cin>>f;
			switch(f)
			{
			case 1:
				{
					for(int i=0;i<h;i++)
					{
						int u=rand()%g;
						int v=rand()%g+1;
						char w=arr[rand()%4];
						if(u%v==0 && w=='/'){
							cout<<u<<w<<v<<"="<<endl;
						}
						else
							cout<<u<<arr[rand()%3]<<v<<"="<<endl;
					}
					break;
				}
			case 2: 
				{
					for(int i=0;i<h;i++)
					{
						int u=rand()%g;
						int v=rand()%g;
						char w=arr[rand()%4];
						if(v!=0 && u%v==0 && w=='/' )
							cout<<arr2[rand()%2]<<u<<" "<<w<<" "<<arr2[rand()%2]<<v<<"="<<endl;
						else
							cout<<arr2[rand()%2]<<u<<" "<<arr[rand()%3]<<" "<<arr2[rand()%2]<<v<<"="<<endl;
					}
					break;
				}
			case 3:
				{
					for(int i=0;i<h;i++)
					{
						int u=rand()%g;
						int v=rand()%g+1;
						char w=arr[rand()%4];
						cout<<u<<" "<<w<<" "<<v<<"="<<endl;
					}
					break;
				}
			case 4:
				{
					for(int i=0;i<h;i++)
					{
						int u=rand()%g;
						int v=rand()%g;
						char w=arr[rand()%4];
						cout<<arr2[rand()%2]<<u<<" "<<w<<" "<<arr2[rand()%2]<<v<<"="<<endl;
					}
					break;
				}
			default:
				cout<<"输入的数有误,请输入1--4的整数"<<endl;
			}
			
		}
	case 3:
		{
			cout<<"请选择减法是否有负数;0:无负数,1:有负数"<<endl;
			cin>>f;
			switch(f)
			{
			case 0:
				{
					for(int i=0;i<h;i++)
					{
						cout<<rand()%g<<"/"<<rand()%g+1<<" "<<arr[rand()%2]<<" "<<rand()%g<<"/"<<rand()%g+1<<"="<<endl;
					}
					break;
				}
			case 1: 
				{
					for(int i=0;i<h;i++)
					{
						cout<<arr2[rand()%2]<<rand()%g<<"/"<<rand()%g+1<<" "<<arr[rand()%2]<<" "<<arr2[rand()%2]<<rand()%g<<"/"<<rand()%g+1<<"="<<endl;
					}
					break;
				}
			default:
				cout<<"输入的数有误,请输入0或1"<<endl;
			}
			break;
		}
	case 4:
		{
			cout<<"**************四则运算菜单选择程序****************"<<endl;
			cout<<"请选择要使用的功能:"<<endl;
			cout<<"1.   正分数四则运算!"<<endl;
			cout<<"2.   可负分数四则运算!"<<endl;
			cout<<" *********************************************"<<endl;
			cin>>f;
			switch(f)
			{
			case 1:
				{
					for(int i=0;i<h;i++)
					{
						int u=rand()%g;
						int v=rand()%g+1;
						int x=rand()%g;
						int y=rand()%g+1;
						char w=arr[rand()%4];
						cout<<u<<"/"<<v<<" "<<w<<" "<<x<<"/"<<y<<"="<<endl;
					}
					break;
				}
			case 2: 
				{
					for(int i=0;i<h;i++)
					{
						int u=rand()%g;
						int v=rand()%g;
						int x=rand()%g;
						int y=rand()%g;
						char w=arr[rand()%4];
						if(v!=0 && w=='/' )
							cout<<arr2[rand()%2]<<u<<"/"<<v<<" "<<w<<" "<<arr2[rand()%2]<<x<<"/"<<y<<"="<<endl;
						else
							cout<<arr2[rand()%2]<<u<<"/"<<v<<" "<<arr[rand()%3]<<" "<<arr2[rand()%2]<<x<<"/"<<y<<"="<<endl;
					}
					break;
				}
			default:
				cout<<"输入的数有误,请输入1或2"<<endl;
			}
			break;
		}
	default:
		cout<<"输入的数有误,请输入1--4的整数"<<endl;
		}  	
	}
	
三、实验截图










四、心得体会
不同的语言编程,可能提供不一样的思路;开阔编程的思路。
不同的语言编程算法一样,但一些函数调用不太一样。
五、psp0表格
一、项目计划总结
周活动总结表
姓名:杨广鑫 日期:2015.3.20
| 日期 任务 | 听课 | 编写程序 | 阅读课本 | 准备考试 | 
 | 
 | 日总计 | 
| 周日 | 
 | 120 | 
 | 
 | 
 | 
 | 120 | 
| 周一 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
| 周二 | 100 | 60 | 20 | 
 | 
 | 
 | 180 | 
| 周三 | 
 | 120 | 
 | 
 | 
 | 
 | 120 | 
| 周四 | 
 | 
 | 10 | 
 | 
 | 
 | 10 | 
| 周五 | 100 | 50 | 15 | 
 | 
 | 
 | 165 | 
| 周六 | 
 | 45 | 
 | 
 | 
 | 
 | 45 | 
| 周总结 | 200 | 395 | 45 | 
 | 
 | 
 | 640 | 
阶段时间和效率 周数(上一次周活动表的周数+1):
不包括上一周在内的累计时间
| 总计 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
| 平均 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
| 最大 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
| 最小 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
以前各周的累计时间
| 总计 | 200 | 395 | 45 | 
 | 
 | 
 | 640 | 
| 平均 | 200 | 395 | 45 | 
 | 
 | 
 | 640 | 
| 最大 | 200 | 395 | 45 | 
 | 
 | 
 | 640 | 
| 最小 | 200 | 395 | 45 | 
 | 
 | 
 | 640 | 
二、时间记录表:
学生 杨广鑫 日期 2015年3月13日
教师 王建民 课程 PSP
| 日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 | 
| 3.15 | 14.30 | 16.30 | 无 | 120 | 编程序 | 作业 | 
| 3.16 | 
 | 
 | 
 | 
 | 
 | 
 | 
| 3.17 | 19:15 | 20:15 | 无 | 60 | 编程序 | 作业 | 
| 3.18 | 14:30 | 21:14 | 有 | 120 | 编程序 | 作业 | 
| 3.19 | 
 | 
 | 
 | 
 | 
 | 
 | 
| 3.20 | 18:50 | 19:40 | 无 | 50 | 完善程序 | 作业 | 
| 3.21 | 9:10 | 9:55 | 无 | 45 | 完善程序 | 作业 | 
三、缺陷记录日志:
学生 杨广鑫
日期 2015年3月20日
教员 王建民
程序号
| 日期 | 编号 | 类型 | 引入阶段 | 排除阶段 | 修复时间 | 修复缺陷 | |||||
| 3.15 | 1 | a2 | 运行 | 运行之后马上 | 1min | 
 | |||||
| 出现逻辑混乱。 | |||||||||||
| 3.15 | 2 | a3 | 编译 | 编译 | 1min | 
 | |||||
| 定义在使用变量之后 | |||||||||||
| 3.17 | 3 | B1 | 运行 | 运行之后 | 3min | 
 | |||||
| 在运行时,输出的结果虽然正确,但格式却不正确,行间距混乱 | |||||||||||
| 3.18 | 4 | a1 | 编译 | 编译 | 1min | 
 | |||||
| 漏掉; | |||||||||||
| 3.20 | 5 | B2 | 运行 | 运行之后 | 15min | 
 | |||||
| 在运行时,输出的格式不是按要求的输出的,原因是if语句插入的位置不正确 | |||||||||||
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号