• 四则运算的代码的改进(三)


    题目:在原基础上能够输入答案判断对错,并且输出正确答案与错误答案的数目:

    源代码:

    #include<iostream>
    #include<time.h>
    using namespace std;
    float suan(float o1, char oc, float o2)
    {
    	float r;
    	switch (oc)
    	{
    	case '+':
    		r = o1 + o2;
    		break;
    	case '-':
    		r = o1 - o2;
    		break;
    	case '*':
    		r = o1 * o2;
    		break;
    	case '/':
    		r = o1 / o2;
    		r = int((r + 0.5) * 100) / 100.0;
    		break;
    	}
    	return r;
    }
    void main()
    {
    	int i, k, q, j, v, h, l, hl, d = 0, c = 0; //定义几个变量
    	float *o1, *o2, *o3, *r1, *r2;
    	char *oc1, *oc2;
    	char n1, n2;
    	int a1, a2;
    	char g[4] = { '+', '-', '*', '/' };
    	char y[5][5] = {
    		' ', '+', '-', '*', '/',
    		'+', '=', '=', '<', '<',
    		'-', '=', '=', '<', '<',
    		'*', '>', '>', '=', '=',
    		'/', '>', '>', '=', '=' };
    	srand((unsigned)time(NULL)); //本地时间
    b:	cout << "请问您想输出多少道题目?" << endl;
    	cin >> q;
    	cout << "请输入操作数值的最大范围:" << endl;
    	cin >> j;
    	cout << "请问是否有无乘除法?(Y/N)" << endl;
    	cin >> n1;
    	cout << "请问是否有无括号?(Y/N)" << endl;
    	cin >> n2;
    	cout << "请输入每行运算式的个数:" << endl;
    	cin >> v;
    	cout << "请输入每行运算式之间的间隔:" << endl;
    	cin >> a2;
    	o1 = new float[q];
    	o2 = new float[q];
    	o3 = new float[q];
    	r1 = new float[q];
    	r2 = new float[q];
    	oc1 = new char[q];
    	oc2 = new char[q];
    	cout << "以下为随机选取的题目:" << endl << endl;
    	for (i = 1; i <= q; i++)          //定义一个可以改变题目数量的q
    	{
    		if (n1 == 'Y')
    		{
    
    			oc1[i] = g[rand() % 4];
    			oc2[i] = g[rand() % 4];
    			o1[i] = rand() % j;
    			o2[i] = rand() % j;
    			o3[i] = rand() % j;
    			if (i == 1)              //直接输出第一题
    			{
    				if (n2 == 'N')       //无括号   
    				{
    					cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    					for (hl = 1; hl < 5; hl++)
    					{
    						if (oc1[i] == y[hl][0])
    						{
    							h = hl;
    						}
    						if (oc2[i] == y[0][hl])
    						{
    							l = hl;
    						}
    					}
    					switch (y[h][l])
    					{
    					case '>':
    						r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    						break;
    					case '=':
    						r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    						break;
    					case '<':
    						r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    						break;
    					}
    				}
    				else                 //有括号
    				{
    					if (rand() % 2 == 0)          //括号在前
    					{
    						if (oc1[i] == '+' || oc1[i] == '-')
    						{
    							if (oc2[i] == '*' || oc2[i] == '/')
    							{
    								cout << '(' << o1[i] << oc1[i] << o2[i] << ')' << oc2[i] << o3[i] << "=	";
    								r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    							}
    							else
    							{
    								cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    								r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    							}
    						}
    						if (oc1[i] == '*' || oc1[i] == '/')
    						{
    							cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    							r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    						}
    					}
    					else                          //括号在后
    					{
    						if (oc1[i] == '+' || oc1[i] == '-')
    						{
    							if (oc2[i] == '+' || oc2[i] == '-')
    							{
    								cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=	";
    								r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    							}
    							else
    							{
    								cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    								r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    							}
    						}
    						if (oc1[i] == '*' || oc1[i] == '/')
    						{
    							cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=	";
    							r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    						}
    					}
    				}
    			}
    			else
    			{
    				for (k = 0; k < i; k++)                        //避免重复start
    				{
    					if (o1[k] == o1[i] && o2[k] == o2[i] && o3[k] == o3[i] && oc1[k] == oc1[i] && oc2[k] == oc2[i])
    					{
    						i--;
    						break;
    					}
    					else
    					{
    						if (k == i - 1)  //比较到最后一个数时
    						{
    							//循环输出/*q*/道运算题
    							if (n2 == 'N')       //无括号   
    							{
    								cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    								for (hl = 1; hl < 5; hl++)
    								{
    									if (oc1[i] == y[hl][0])
    									{
    										h = hl;
    									}
    									if (oc2[i] == y[0][hl])
    									{
    										l = hl;
    									}
    								}
    								switch (y[h][l])
    								{
    								case '>':
    									r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    									break;
    								case '=':
    									r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    									break;
    								case '<':
    									r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    									break;
    								}
    							}
    							else                 //有括号
    							{
    								if (rand() % 2 == 0)          //括号在前
    								{
    									if (oc1[i] == '+' || oc1[i] == '-')
    									{
    										if (oc2[i] == '*' || oc2[i] == '/')
    										{
    											cout << '(' << o1[i] << oc1[i] << o2[i] << ')' << oc2[i] << o3[i] << "=	";
    											r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    										}
    										else
    										{
    											cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    											r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    										}
    									}
    									if (oc1[i] == '*' || oc1[i] == '/')
    									{
    										cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    										r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    									}
    								}
    								else                          //括号在后
    								{
    									if (oc1[i] == '+' || oc1[i] == '-')
    									{
    										if (oc2[i] == '+' || oc2[i] == '-')
    										{
    											cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=	";
    											r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    										}
    										else
    										{
    											cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    											r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    										}
    									}
    									if (oc1[i] == '*' || oc1[i] == '/')
    									{
    										cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=	";
    										r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    									}
    								}
    							}
    						}
    					}
    				}                                              //避免重复end
    			}
    		}
    		else if (n1 == 'N')
    		{
    			oc1[i] = g[rand() % 2];
    			oc2[i] = g[rand() % 2];
    			o1[i] = rand() % j;
    			o2[i] = rand() % j;
    			o3[i] = rand() % j;
    			if (i == 1)
    			{
    				if (n2 == 'N')
    				{
    					cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    					r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    				}
    				else
    				{
    					if (rand() % 2 == 0)
    					{
    						cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    						r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    					}
    					else
    					{
    						cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=	";
    						r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    					}
    				}
    			}
    			else
    			{
    				for (k = 0; k < i; k++)                                  //避免重复start
    				{
    					if (o1[k] == o1[i] && o2[k] == o2[i] && o3[k] == o3[i] && oc1[k] == oc1[i] && oc2[k] == oc2[i])
    					{
    						i--;
    						break;
    					}
    					else
    					{
    						if (k == i - 1)  //比较到最后一个数时
    						{
    							//循环输出/*q*/道运算题
    							if (n2 == 'N')
    							{
    								cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    								r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    							}
    							else
    							{
    								if (rand() % 2 == 0)
    								{
    									cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=	";
    									r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]);
    								}
    								else
    								{
    									cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=	";
    									r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i]));
    								}
    							}
    						}
    					}
    				}                                                         //避免  重复end
    			}
    		}
    		if (i % v == 0)
    		{
    			for (a1 = 1; a1 <= a2; a1++)
    			{
    				cout << endl;
    			}
    		}
    	}
    	cout << endl << "请输入答案:" << endl;
    	for (i = 1; i <= q; i++)
    	{
    		cin >> r2[i];
    	}
    	cout << endl << "以下是正确答案:" << endl;
    	for (i = 1; i <= q; i++)
    	{
    		cout << r1[i] << "	";
    		if (i % v == 0)
    		{
    			cout << endl;
    		}
    	}
    	for (i = 1; i <= q; i++)
    	{
    		if (r1[i] == r2[i])
    		{
    			d++;
    		}
    		else
    		{
    			c++;
    		}
    	}
    	cout << endl << "您答对了" << d << "道题,答错了" << c << "道题!" << endl;
    	cout << endl << "请重新测试:" << endl;
    	goto b;
    }
    

     截图:

    总结:此次实验内容对我有一定难度,因为以前学的不好所以好多函数调用不太懂,是我和同学合作完成,以后一定要多加练习。

  • 相关阅读:
    HDU1530 Maximum Clique 最大团裸题
    关于ASP.NET"未能映射路径"问题
    [sql] like in 实现参数化查询的问题
    解决vs 2010复制汉字到Word出现乱码
    向HtmlAgilityPack道歉:解析HTML还是你好用
    根据firefox,ie等不同的浏览器跳转到对应页面
    asp.net怎样将整个目录上传?
    <%= %> 和<%# %>的区别
    .net C# cookie清除问题
    未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\website7\53477a4d\df66d972\App_Web_sytxol
  • 原文地址:https://www.cnblogs.com/TSbj/p/4357232.html
Copyright © 2020-2023  润新知