结对编程-四则运算

发布时间 2023-04-10 22:33:13作者: ran_ran

  本次结对编程作业由我和2152714完成

  • 源代码:
/*
 *Created by zouran on 2023/4/10.
 *Description:四则运算
 *prams:
*/
#include <ctime>
#include <random>
#include "iostream"
using namespace std;
//存储运算符
class Operations{
private:
    char operation[4]{};
    char randomOperation[2]{};
    int randParams[3]{};
public:
    Operations();
    [[nodiscard]] const char *getOperation() const;
    void initialize(auto u,auto e);
    bool judge();
    void print();
};
//无参构造函数
Operations::Operations() {
    operation[0]='+';
    operation[1]='-';
    operation[2]='*';
    operation[3]='/';
}
//获取运算数组
const char *Operations::getOperation() const {
    return operation;
}
//生成随机题的数据与运算符
void  Operations::initialize(auto u,auto e){
    for(int & randParam : this->randParams)
    {randParam=u(e);}
    randomOperation[0]=this->operation[u(e)%4];
    randomOperation[1]=this->operation[u(e)%4];
}
//俩者间运算
int operation(int param1,int param2,char option){
    switch (option) {
        case '+': return param1+param2;
        case '-': return param1-param2;
        case '*': return param1*param2;
        case '/': return param1/param2;
    }
}
//判断题目运算结果是否在100内
bool Operations::judge(){
    int sum=0;
    int params[3];
    for(int i=0;i<3;i++){
        params[i]=this->randParams[i];
    }
    char operation1=this->randomOperation[0];
    char operation2=this->randomOperation[1];
    if   (operation1=='+'&&operation2=='+')
    {
        sum=params[0]+params[1]+params[2];
    }
    else if(operation1=='+'&&operation2=='-'){
        sum=params[0]+params[1]-params[2];
    }
    else if(operation1=='+'&&operation2=='*'){
        sum=params[0]+params[1]*params[2];
    }
    else if(operation1=='+'&&operation2=='/'){
        sum=params[0]+params[1]/params[2];
    }
    else if(operation1=='-'&&operation2=='+'){
        sum=params[0]-params[1]+params[2];
    }
    else if(operation1=='-'&&operation2=='-'){
        sum=params[0]-params[1]-params[2];
    }
    else if(operation1=='-'&&operation2=='*'){
        sum=params[0]-params[1]*params[2];
    }
    else if(operation1=='-'&&operation2=='/'){
        sum=params[0]-params[1]/params[2];
    }
    else if(operation1=='*'&&operation2=='+'){
        sum=params[0]*params[1]+params[2];
    }
    else if(operation1=='*'&&operation2=='-'){
        sum=params[0]*params[1]-params[2];
    }
    else if(operation1=='*'&&operation2=='*'){
        sum=params[0]*params[1]*params[2];
    }
    else if(operation1=='*'&&operation2=='/'){
        sum=params[0]*params[1]/params[2];
    }
    else if(operation1=='/'&&operation2=='+'){
        sum=params[0]/params[1]+params[2];
    }
    else if(operation1=='/'&&operation2=='-'){
        sum=params[0]/params[1]-params[2];
    }
    else if(operation1=='/'&&operation2=='*'){
        sum=params[0]/params[1]*params[2];
    }
    else if(operation1=='/'&&operation2=='/'){
        sum=params[0]/params[1]/params[2];
    }
    if(sum>=0&&sum<=100)
    return true;
    return false;
}
//打印题目
void Operations::print() {
    printf("%-2d %c %-2d %c %-2d =\n",randParams[0],randomOperation[0],randParams[1],randomOperation[1],randParams[2]);
}
//主执行文件
void exe(){
    auto count=0;
    system("chcp 65001");
    cout<<"请输入要生成的题目数量:\n";
    cin>>count;
    int i=0;
    default_random_engine e;
    uniform_int_distribution<int> u(1,10);
    e.seed(time(nullptr));
    while(i<count) {
        u(e);
        auto *operations = new Operations();
        operations->initialize(u,e);
        if (operations->judge()) {
            i++;
            operations->print();
        }
    }
}
int main(){
    exe();
    return 0;
}
  • 运行结果:

 

 

  • 总结
  • 本次结对编程可以帮助学习别人的代码风格以及编码思路,在本次编码过程中主要的俩个问题如下
  • C++中随机数生成的函数种子问题
  • 多次运行结果中的随机生成数一样的导致程序受阻,在查阅C++11标准的发现可以使用
  • uniform_int_distribution:产生均匀分布的整数
  • 用法如下:

 

#include <iostream>
#include <ctime>
#include <random>
 
int main() {
 
    std::default_random_engine e;
    std::uniform_int_distribution<int> u(2,20); // 左闭右闭区间
    e.seed(time(0));
 
    for (int i=0; i<10; i++) {
        std::cout << u(e) << std::endl;
    }
 
    return 0;
}

 

  • 控制台中文输出乱码问题,这是原来用Devc++开发C++未遇到的问题
  • 解决方法便是在代码中加入下面的语句
system("chcp 65001");