• 四则运算生成器


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

  • 相关阅读:
    js对象Array —— 使用.操作符和用['xxx']访问对象的区别
    JavaScript为字符串提供的一些常用方法
    ES6新增属性——,模板字符串`` 中可使用${ }代替‘+’字符串拼接
    【vue】使用vue+element搭建项目,Tree树形控件使用
    vue循环遍历List,Map,Array
    解决分页查询只能查到本页信息的问题
    vue之 js字符串驼峰和下划线互相转换
    PHP中的对象遍历技巧
    php类的复制(克隆)
    php类的自动加载
  • 原文地址:https://www.cnblogs.com/zgq0/p/7526933.html
Copyright © 2020-2023  润新知