• 个人作业1——四则运算题目生成程序(基于控制台)


    题目描述:

    从《构建之法》第一章的 “程序” 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 “软件”,满足以下需求:

    • 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
    • 运算符为 +,-,×,÷
    • 并且要求能处理用户的输入,并判断对错,打分统计正确率。
    • 要求能处理用户输入的真分数, 如 1/2, 5/12 等
    • 使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目Myapp.exe -n 10
    PSP2.1 Personal Software Process Stages Time (%) Senior Student(/hour) Time (%)(/hour)
    · Planning 计划 2 1.5
    · Estimate 估计这个任务需要多少时间 37 40
    · Analysis 需求分析 (包括学习新技术) 1 1
    · Coding Standard 代码规范 0.5 0.5
    · Design 具体设计 1.5 1
    · Coding 具体编码 30 35
    · Test 测试(自我测试,修改代码,提交修改) 1 1
    Reporting 报告 1 1

    源代码:

    #include<iostream>
    #include<stdlib.h>
    #include<time.h>
    using namespace std;
    int b = 0;
    int maxNum(int i, int j)
    {
    	int k;
    	if ((k = i % j) != 0)
    	{
    		maxNum(j, k);
    	}
    	else
    		return j;
    }
    int INT()
    {
    	int x, y, z, t, m, n, c, r1, r2;
    	char r3[10], r4[10];
    	srand(time(NULL));
    	memset(r3, 0, sizeof(r3));
    	memset(r4, 0, sizeof(r4));
    	x = rand() % 100;
    	y = rand() % 100;
    	z = rand() % 4;
    	r1 = 0;
    	r2 = 0;
    	switch (z)
    	{
    	case 0:
    		cout << x << "+" << y << "=";
    		cin >> r1;
    		cin.get();
    		r2 = x + y;
    		if (r1 == r2)
    		{
    			cout << "    " << "T" << endl;
    			b = b + 1;
    		}
    		else cout << "    " << "F" << endl;
    		break;
    	case 1:
    		if (x<y)
    		{
    			t = x;
    			x = y;
    			y = t;
    		}
    		else;
    		cout << x << "-" << y << "=";
    		cin >> r1;
    		cin.get();
    		r2 = x - y;
    		if (r1 == r2)
    		{
    			cout << "    " << "T" << endl;
    			b = b + 1;
    		}
    		else cout << "    " << "F" << endl;
    
    		break;
    	case 2:
    		cout << x << "*" << y << "=";
    		cin >> r1;
    		r2 = x * y;
    		if (r1 == r2)
    		{
    			cout << "    " << "T" << endl;
    			b = b + 1;
    		}
    		else cout << "    " << "F" << endl;
    		break;
    	case 3:
    		if (y != 0)
    		{
    			cout << x << "÷" << y << "=";
    			c = maxNum(x, y);
    			m = x / c;
    			n = y / c;
    			if (n != 1)
    			{
    				sprintf_s(r3, "%d/%d", m, n);
    				cin >> r4;
    				if (strcmp(r3, r4) == 0)
    				{
    					cout << "    " << "T" << endl;
    					b = b + 1;
    				}
    				else cout << "    " << "F" << endl;
    			}
    			else
    			{
    				cin >> r2;
    				if (r2 == m) {
    					cout << "    " << "T" << endl;
    					b = b + 1;
    				}
    				else cout << "    " << "F" << endl;
    			}
    
    		}
    		else {
    			cout << x << "÷" << "1" << "=";
    			cin >> r2;
    			if (r2 == x) {
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		break;
    	default:
    		cout << "wrong" << endl;
    		break;
    	}
    
    	return 0;
    }
    
    int fra() {
    	int x, y, z, t, m, n, c, r1, r2, i, j;
    	char r3[10], r4[10];
    	srand(time(NULL));
    	memset(r3, 0, sizeof(r3));
    	memset(r4, 0, sizeof(r4));
    	x = rand() % 100;
    	y = rand() % 100;
    	i = rand() % 100;
    	j = rand() % 100;
    	z = rand() % 4;
    	r1 = 0;
    	r2 = 0;
    	if (y == 0 || y == 1)
    		y = 2;
    	if (j == 0 || j == 1)
    		j = 2;
    	switch (z)
    	{
    	case 0:
    		cout << x << "/" << y << "+" << i << "/" << j << "=";
    		m = (x*j) + (i*y);
    		n = y*j;
    		c = maxNum(m, n);
    		m = m / c;
    		n = n / c;
    		if (n != 1) {
    			sprintf_s(r3, "%d/%d", m, n);
    			cin >> r4;
    			if (strcmp(r3, r4) == 0)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		else {
    			cin >> r2;
    			if (r2 == m)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		break;
    	case 1:
    		if ((x*j)<(i*y))
    		{
    			t = x;
    			x = i;
    			i = t;
    			t = y;
    			y = j;
    			j = t;
    		}
    		else;
    		cout << x << "/" << y << "-" << i << "/" << j << "=";
    		m = (x*j) - (i*y);
    		n = y*j;
    		c = maxNum(m, n);
    		m = m / c;
    		n = n / c;
    		if (n != 1) {
    			sprintf_s(r3, "%d/%d", m, n);
    			cin >> r4;
    			if (strcmp(r3, r4) == 0)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		else {
    			cin >> r2;
    			if (r2 == m)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		break;
    	case 2:
    		cout << x << "/" << y << "×" << i << "/" << j << "=";
    		m = x*i;
    		n = y*j;
    		c = maxNum(m, n);
    		m = m / c;
    		n = n / c;
    		if (n != 1) {
    			sprintf_s(r3, "%d/%d", m, n);
    			cin >> r4;
    			if (strcmp(r3, r4) == 0)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		else {
    			cin >> r2;
    			if (r2 == m)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		break;
    	case 3:
    		cout << x << "/" << y << "÷" << i << "/" << j << "=";
    		m = x*j;
    		n = y*i;
    		c = maxNum(m, n);
    		m = m / c;
    		n = n / c;
    		if (n != 1) {
    			sprintf_s(r3, "%d/%d", m, n);
    			cin >> r4;
    			if (strcmp(r3, r4) == 0)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		else {
    			cin >> r2;
    			if (r2 == m)
    			{
    				cout << "    " << "T" << endl;
    				b = b + 1;
    			}
    			else cout << "    " << "F" << endl;
    		}
    		break;
    	default:
    		cout << "wrong" << endl;
    		break;
    	}
    
    	return 0;
    }
    int main(int argc, char*argv[]) {
    	int a, c;
    	if (argc<2)
    		cin >> a;
    	else a = atoi(argv[2]);
    	srand(time(NULL));
    
    	for (int i = 0; i < a; i++) {
    		c = rand() % 2;
    		switch (c) {
    		case 0:INT(); break;
    		case 1:fra(); break;
    		default:
    			cout << "wrong" << endl;
    			break;
    		}
    	}
    
    	cout << "grade:" << b;
    
    	return 0;
    }
    
    

    运行截图:

    码云链接 https://git.oschina.net/lenmo/sizeyunsuan.git

  • 相关阅读:
    设计模式之迭代器与组合模式(三)
    设计模式之迭代器与组合模式(二)
    设计模式之迭代器与组合模式(一)
    设计模式之模板方法模式(一)
    设计模式之模板方法模式(三)
    设计模式之模板方法模式(二)
    Spring Cloud微服务初探
    设计模式之适配器模式与外观模式(二)
    设计模式之适配器模式与外观模式(一)
    设计模式之命令模式(三)
  • 原文地址:https://www.cnblogs.com/lenmo/p/6499027.html
Copyright © 2020-2023  润新知