Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include "Arithmetic.h"
  2. //==============================================================================
  3. Answer::Answer(int ans):ans(ans),Left(0),Right(0),operation(0){
  4.    ostringstream out;
  5.    if(ans>=0){
  6.       out<<ans;
  7.    }
  8.    else out<<'('<<ans<<')';
  9.    primer=out.str();
  10.    prior=5;
  11. }
  12. //==============================================================================
  13. Answer::Answer(Answer& Left, char operation, Answer& Right):operation(operation),Left(&Left), Right(&Right){
  14.    if(operation=='+'){
  15.       prior=1;
  16.       ans=Left.ans+Right.ans;
  17.    }
  18.    if(operation=='-'){
  19.       prior=2;
  20.       ans=Left.ans-Right.ans;
  21.    }
  22.    if(operation=='*'){
  23.       prior=3;
  24.       ans=Left.ans*Right.ans;
  25.    }
  26.    if(operation==':'){
  27.       prior=4;
  28.       ans=Left.ans/Right.ans;
  29.    }
  30.    if(operation=='('){
  31.       prior=0;
  32.       primer="-";
  33.       if (ans<0)primer+="(";
  34.       primer+=Right.primer;
  35.       if (ans<0) primer+=")";
  36.       ans=-Right.ans;
  37.       return;
  38.    }
  39.    if(prior>Left.prior && Left.prior){
  40.       primer+='(';
  41.       primer+=Left.primer;
  42.       primer+=')';
  43.    }
  44.    else primer+=Left.primer;
  45.    primer+=' ';
  46.    primer+=operation;
  47.    primer+=' ';
  48.    if (prior>Right.prior || Right.prior==2 || Right.prior==4){
  49.       primer+='(';
  50.       primer+=Right.primer;
  51.       primer+=')';
  52.    }
  53.    else primer+=Right.primer;
  54. }
  55. //==============================================================================
  56. Answer::~Answer(){
  57.    if(Left) delete Left;
  58.    if(Right) delete Right;
  59. }
  60. //==============================================================================
  61. Generator::Generator(int kolP, int kolD, int left, int right, int *boxOperation){
  62.    if (kolP<0 || kolD<0 || left>right) throw PossibleErrors(incorrect_atribute);
  63.    this->kolP=kolP;
  64.    this->kolD=kolD;
  65.    this->left=left;
  66.    this->right=right;
  67.    for(int i=0; i<5; i++)
  68.       this->boxOperation[i]=boxOperation[i];
  69.    srand((int)time(0));
  70. }
  71. //==============================================================================
  72. void Generator::inFile(string fileName){
  73.    Answer *AS;
  74.    ofstream fine(fileName.c_str());
  75.    for(int i=0; i<kolP; i++){
  76.       AS=this->generateOneAnswer();
  77.       fine<<AS->Primer()<<" = \n"<<endl;
  78.       fine<<"Ответ: "<<AS->answer()<<"\n\n";
  79.       delete AS;
  80.    }
  81. }
  82. //==============================================================================
  83. void Generator::inVector(vector<string> &h){
  84.    string primer;
  85.    Answer *AS;
  86.    for(int i=0; i<kolP; i++){
  87.       AS=this->generateOneAnswer();
  88.       primer=AS->Primer();
  89.       primer+=" = ";
  90.       ostringstream out;
  91.       out<<AS->answer();
  92.       primer+=out.str();
  93.       h.push_back(primer);
  94.       delete AS;
  95.    }
  96. }
  97. //==============================================================================
  98. Answer* Generator::recurs(int num,int kolD){
  99.    int operandL, operandR;
  100.    int N, M, index, cnt(0);
  101.    char operation;
  102.    string operations="+-*:(";
  103.    ostringstream out;
  104.    if (kolD==0)
  105.       return new Answer(num);
  106.    while (1){
  107.       cnt++;
  108.       if(cnt>=10000)
  109.          throw PossibleErrors(not_successful);
  110.       index=rand()%5;
  111.       if(!boxOperation[index]) continue;
  112.       operation=operations[index];
  113.       operandL=rand()%(right-left+1)+left;
  114.       if(operation=='+')
  115.          operandR=num-operandL;
  116.       if(operation=='-')
  117.          operandR=operandL-num;
  118.       if(operation=='*'){
  119.          if (operandL==0) continue;
  120.          if (num%operandL) continue;
  121.          operandR=num/operandL;
  122.       }
  123.       if(operation==':'){
  124.          if(num==0) continue;
  125.          if(operandL%num )continue;
  126.          operandR=operandL/num;
  127.          if(operandR==0) continue;
  128.       }
  129.       if(operation=='('){
  130.          if(-num>right || -num<left || num==0 )continue;
  131.          return new Answer(*(new Answer(0)),operation,*recurs(-num,kolD-1));
  132.       }
  133.       if(operandR>right || operandR<left)continue;
  134.          break;
  135.    }
  136.    N=rand()%kolD;
  137.    M=kolD-N-1;
  138.    return new Answer(*recurs(operandL,N),operation,*recurs(operandR,M));
  139. }
  140. //==============================================================================
  141. Answer* Generator::generateOneAnswer(){
  142.    int num=rand()%(right-left+1)+left;
  143.    return recurs(num,kolD);
  144. }
  145. //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement