Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arithmetic.h"
- //==============================================================================
- Answer::Answer(int ans):ans(ans),Left(0),Right(0),operation(0){
- ostringstream out;
- if(ans>=0){
- out<<ans;
- }
- else out<<'('<<ans<<')';
- primer=out.str();
- prior=5;
- }
- //==============================================================================
- Answer::Answer(Answer& Left, char operation, Answer& Right):operation(operation),Left(&Left), Right(&Right){
- if(operation=='+'){
- prior=1;
- ans=Left.ans+Right.ans;
- }
- if(operation=='-'){
- prior=2;
- ans=Left.ans-Right.ans;
- }
- if(operation=='*'){
- prior=3;
- ans=Left.ans*Right.ans;
- }
- if(operation==':'){
- prior=4;
- ans=Left.ans/Right.ans;
- }
- if(operation=='('){
- prior=0;
- primer="-";
- if (ans<0)primer+="(";
- primer+=Right.primer;
- if (ans<0) primer+=")";
- ans=-Right.ans;
- return;
- }
- if(prior>Left.prior && Left.prior){
- primer+='(';
- primer+=Left.primer;
- primer+=')';
- }
- else primer+=Left.primer;
- primer+=' ';
- primer+=operation;
- primer+=' ';
- if (prior>Right.prior || Right.prior==2 || Right.prior==4){
- primer+='(';
- primer+=Right.primer;
- primer+=')';
- }
- else primer+=Right.primer;
- }
- //==============================================================================
- Answer::~Answer(){
- if(Left) delete Left;
- if(Right) delete Right;
- }
- //==============================================================================
- Generator::Generator(int kolP, int kolD, int left, int right, int *boxOperation){
- if (kolP<0 || kolD<0 || left>right) throw PossibleErrors(incorrect_atribute);
- this->kolP=kolP;
- this->kolD=kolD;
- this->left=left;
- this->right=right;
- for(int i=0; i<5; i++)
- this->boxOperation[i]=boxOperation[i];
- srand((int)time(0));
- }
- //==============================================================================
- void Generator::inFile(string fileName){
- Answer *AS;
- ofstream fine(fileName.c_str());
- for(int i=0; i<kolP; i++){
- AS=this->generateOneAnswer();
- fine<<AS->Primer()<<" = \n"<<endl;
- fine<<"Ответ: "<<AS->answer()<<"\n\n";
- delete AS;
- }
- }
- //==============================================================================
- void Generator::inVector(vector<string> &h){
- string primer;
- Answer *AS;
- for(int i=0; i<kolP; i++){
- AS=this->generateOneAnswer();
- primer=AS->Primer();
- primer+=" = ";
- ostringstream out;
- out<<AS->answer();
- primer+=out.str();
- h.push_back(primer);
- delete AS;
- }
- }
- //==============================================================================
- Answer* Generator::recurs(int num,int kolD){
- int operandL, operandR;
- int N, M, index, cnt(0);
- char operation;
- string operations="+-*:(";
- ostringstream out;
- if (kolD==0)
- return new Answer(num);
- while (1){
- cnt++;
- if(cnt>=10000)
- throw PossibleErrors(not_successful);
- index=rand()%5;
- if(!boxOperation[index]) continue;
- operation=operations[index];
- operandL=rand()%(right-left+1)+left;
- if(operation=='+')
- operandR=num-operandL;
- if(operation=='-')
- operandR=operandL-num;
- if(operation=='*'){
- if (operandL==0) continue;
- if (num%operandL) continue;
- operandR=num/operandL;
- }
- if(operation==':'){
- if(num==0) continue;
- if(operandL%num )continue;
- operandR=operandL/num;
- if(operandR==0) continue;
- }
- if(operation=='('){
- if(-num>right || -num<left || num==0 )continue;
- return new Answer(*(new Answer(0)),operation,*recurs(-num,kolD-1));
- }
- if(operandR>right || operandR<left)continue;
- break;
- }
- N=rand()%kolD;
- M=kolD-N-1;
- return new Answer(*recurs(operandL,N),operation,*recurs(operandR,M));
- }
- //==============================================================================
- Answer* Generator::generateOneAnswer(){
- int num=rand()%(right-left+1)+left;
- return recurs(num,kolD);
- }
- //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement