• 四则运算生成器


    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<<" 正确"<<endl<<endl;
    else if(ret==-1)
    cout<<" 错误"<<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 << " " << 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<<" 总数:"<<this->topic_num<<" 得分:"<<this->grade<<endl;
    }

  • 相关阅读:
    状压DP
    string
    hdu3068
    HDU Stealing Harry Potter's Precious(状压BFS)
    状压BFS
    BFS+打印路径
    poj Meteor Shower
    C语言-无符号数与有符号数不为人知的秘密
    keras_实现cnn_手写数字识别
    python_plot画图参数设置
  • 原文地址:https://www.cnblogs.com/zgq0/p/7526933.html
Copyright © 2020-2023  润新知