#include <iostream>
#include <cstdlib>
#include <ctime>

int generateRandomNumber(int max) {
    return rand() % max + 1;
}

char generateOperator() {
    char operators[] = {'+', '-', '*'};
    return operators[rand() % 3];
}
// 根据符号进行计算
int count1(char op1,int num1,int num2){
    int result1;
        // 左边的计算
    switch (op1) {
        case '+':
            result1 = num1 + num2;
            break;
        case '-':
            result1 = num1 - num2;
            break;
        case '*':
            result1 = num1 * num2;
            break;
    }
    return result1;
}
char getOp2(int num1,int num2,int num3,int num4,char op1,char op3){
    int result1 = count1(op1,num1,num2);
    int result2 = count1(op3,num3,num4);
     if(result1 - result2 >0){
        return '>';
    }else if(result1 - result2 == 0){
          return '=';
    }else {
       return '<';
    }
   
}
int randomNum(std::string &strNum1,std::string &strNum2,std::string &strNum3,std::string &strNum4,int postion){
    if(postion == 1){
        strNum1 = "(    )";
    }else if(postion == 2){
        strNum2 = "(    )";
    }
    else if(postion == 3){
        strNum3 = "(    )";
    }
    else{
        strNum4 = "(    )";
    }
    return postion;
}

bool checkfun3(int num1,int num2,int num3,int num4,char op1,char op3,int postion){
    int result1 = count1(op1,num1,num2);
    int result2 = count1(op3,num3,num4);
    if(num1 == num3 && num2 == num4){
    	 return true;
   }
  if(num1 == num4 && num2 == num3){
    	 return true;
   }
  //  std::cout<<num1<<op1<<num2<<","<<num3<<op3<<num4<<","<<"result1 - result2"<<result1 - result2<<std::endl;
    if(result1 <= 0 || result2 <=0){
        return true;
    }else if(abs(result1 - result2) > 3){
        return true;
    }
    else{
        //  随机填空左边
        if(postion>2 && op3=='*' && (result2 < num1 ||  result2 <num2) ){
            return true;
        }
        if(postion>2 && op1=='*' && (result1 < num3 ||  result1 <num4) ){
            return true;
        }

        return false;
    }
    return true;
}


void fun3(){
    
    int num1, num2, num3, num4;
    char op1, op2, op3;
    int result1, result2;
    bool res = true;
    int position;   // 随机填空的位置
    std::string strNum1, strNum2, strNum3, strNum4;
    do {
        
        res = true;
        
        op1 = generateOperator();
        op3 = generateOperator();
        if(op1 == '*'){
             num1 = generateRandomNumber(6);
             num2 = generateRandomNumber(6);
        }
        else{
             num1 = generateRandomNumber(30);
             num2 = generateRandomNumber(30);
        }
        if(op3 == '*'){
             num3 = generateRandomNumber(6);
             num4 = generateRandomNumber(6);
        }
        else{
             num3 = generateRandomNumber(30);
             num4 = generateRandomNumber(30);
        }
      
        position = generateRandomNumber(4);
        
        strNum1 = std::to_string(num1);
        strNum2 = std::to_string(num2);
        strNum3 = std::to_string(num3);
        strNum4 = std::to_string(num4);
        randomNum(strNum1,strNum2,strNum3,strNum4,position);
        res = checkfun3(num1,num2,num3,num4,op1,op3,position);
    } while (res); // 差值在3以内

    op2 = getOp2(num1,num2,num3,num4,op1,op3);
    std::cout  << strNum1 << " " << op1 << " " << strNum2 << op2 << strNum3 << " " << op3 << " " << strNum4 ;

}
int main() {
    srand(static_cast<unsigned int>(time(0))); // 初始化随机数种子
    // 代码需要更新因为出现(    ) + 22>5 * 4， 这样的情况
    for(int i = 0 ; i < 210;i++){
        if(i%21 ==0 || i ==0){
              std::cout<<"填空题"<<std::endl;
        }
        for(int j = 0 ;j < 2 ; j ++){
            fun3();
            if(j != 1)
            std::cout<<"\t\t\t\t";
        }
        std::cout<<std::endl;
    }
    return 0;
}
