首页 > 技术文章 > 四则运算生成器

zgq0 2017-09-15 16:04 原文

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;
}

 

推荐阅读