四则运算生成器

1.代码来源:自己编写

2.运行环境:linux终端

3.编程语言:c/c++语言

4.bug:未发现

5.当前功能:可以生成0-100的四则运算的题,题的数量可以在程序开始运行时输入,同时在每道题做完后判断正确性和给出正确答案。当所有的题都做完时,可以看到你做对的题的数目以及得分。

5.功能扩展的方向:增加真分数运算,把goahead集成到程序中做成网页版等。

6.gitbub代码地址:https://github.com/moonzhu/Computing

7.实现:

//============================================================================
// Name : Computing.cpp
// Author : 
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include "Computing.h"
using namespace std;

int main() {
Main m;

m.exec();
return 0;
}

 

/*
* Calculate.cpp
*
* Created on: Sep 8, 2017
* Author: moon
*/

#include "Calculate.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
using namespace std;

Calculate::Calculate() {
this->result_true = 0;
this->result_user = 0;
this->type = 0;
this->x = 0;
this->y = 0;
}

Calculate::~Calculate() {
}

int Calculate::calculate(void) {
int ret;

switch (this->type) {
case ADD:
result_true = x + y;
if (result_user == result_true)
ret = 0;
else
ret = -1;
break;

case SUB:
result_true = x - y;
if (result_user == result_true)
ret = 0;
else
ret = -1;
break;

case MUL:
result_true = x * y;
if (result_user == result_true)
ret = 0;
else
ret = -1;
break;

case DIV:
result_true = x / y;
if ((result_user - result_true)<0.01||(result_true-result_user)<0.01)
ret = 0;
else
ret = -1;
break;

default:
cout << "该版本只支持四则运算!" << endl;
ret = -2;
break;
}
cout<<"正确答案:"<< setprecision(2)<<result_true;
if(ret==0)
cout<<"\t正确"<<endl<<endl;
else if(ret==-1)
cout<<"\t错误"<<endl<<endl;
return ret;
}

 

 

/*
* Main.cpp
*
* Created on: Sep 8, 2017
* Author: moon
*/

#include "Main.h"
#include <iostream>
using namespace std;

Main::Main() {
this->finished_num = 0;
this->grade = 0;
this->right_num = 0;
this->topic_num = 0;
this->wrong_num = 0;
}

Main::~Main() {

}

void Main::exec(void) {
int num;
float x;
float y;
char type;
float result;

cout << "请输入题目数量:";
cin >> this->topic_num;

num = this->topic_num;
while (num > 0) {
//system("clear");
show();
srand(time(0));
x = rand() % 100 + 1;
y = rand() % 100 + 1;
type = rand() % 4 + 1;
calculate.setX(x);
calculate.setY(y);
calculate.setType(type);
show_topic(type,x,y);
cin >> result;

calculate.setResultUser(result);
if (calculate.calculate() == 0)
this->right_num++;
else
this->wrong_num++;

this->finished_num++;
num--;
}
show_grade();
}

void Main::show(void) {
cout << "总题数:" << this->topic_num << "\t" << this->finished_num + 1 << "/"
<< this->topic_num << "(已做/总数)" << endl;
}

void Main::show_topic(char type,int x,int y) {
switch (type) {
case ADD:
cout << x << "+" << y << "=";
break;

case SUB:
cout << x << "-" << y << "=";
break;

case MUL:
cout << x << "*" << y << "=";
break;

case DIV:
cout << x << "/" << y << "=";
break;

}
}

void Main::show_grade(void)
{
this->grade=this->right_num*2;
cout<<"正确数量:"<<this->right_num<<"\t总数:"<<this->topic_num<<"\t得分:"<<this->grade<<endl;
}

 

转载于:https://www.cnblogs.com/zgq0/p/7526933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值