#include <iostream>
#include <stack>
using namespace std;
class strategy{
public:
virtual double DoOperations(int a,int b)=0;
};
class pluss: public strategy{
public:
double DoOperations(int a,int b)
{
return a+b;
}
};
class minuss: public strategy{
public:
double DoOperations(int a,int b)
{
return a-b;
};
};
class dablepluss: public strategy{
public:
double DoOperations(double a,double b)
{
return a*b;
};
};
class del: public strategy{
public:
double DoOperations(double a,double b)
{
return a/b;
};
};
class calcul{
strategy* strategia = NULL;
public:
double execute(double a,double b)
{if(strategia==NULL)
return 0;
return strategia->DoOperations(a,b);
}
void setStrat(strategy* strategia)
{
this->strategia = strategia;
}
};
class Lecsema
{
public:
char type;
int num;
};
void Maths(stack<Lecsema>numbers,stack<Lecsema>znak,Lecsema item)
{
double a,b,c;
switch (znak.top().type)
{
case '+':
a=numbers.top().num;
numbers.pop();
b=numbers.top().num;
numbers.pop();
calcul cal;
cal.setStrat(new pluss());
c=cal.execute(a,b);
item.num=c;
item.type='0';
numbers.push(item);
}
};
int main()
{double num;
char ch;
stack<Lecsema>numbers;
stack<Lecsema>znak;
Lecsema item;
while(1){
ch=cin.peek();
if(ch=='\n')
{break;}
if(ch>='0' && ch<='9' )
{
cin>>num;
item.type='0';
item.num=num;
numbers.push(item);
continue;
}
if(ch=='+'|| ch=='-' || ch=='*' || ch=='/')
{
item.type=ch;
item.num=0;
znak.push(item);
cin.ignore();
continue;
}#include <iostream>
#include <stack>
using namespace std;
class strategy{
public:
virtual double DoOperations(int a,int b)=0;
};
class pluss: public strategy{
public:
double DoOperations(int a,int b)
{
return a+b;
}
};
class minuss: public strategy{
public:
double DoOperations(int a,int b)
{
return a-b;
};
};
class dablepluss: public strategy{
public:
double DoOperations(double a,double b)
{
return a*b;
};
};
class del: public strategy{
public:
double DoOperations(double a,double b)
{
return a/b;
};
};
class calcul{
strategy* strategia = NULL;
public:
double execute(double a,double b)
{if(strategia==NULL)
return 0;
return strategia->DoOperations(a,b);
}
void setStrat(strategy* strategia)
{
this->strategia = strategia;
}
};
class Lecsema
{
public:
char type;
int num;
};
void Maths(stack<Lecsema>numbers,stack<Lecsema>znak,Lecsema item)
{
double a,b,c;
switch (znak.top().type)
{
case '+':
a=numbers.top().num;
numbers.pop();
b=numbers.top().num;
numbers.pop();
calcul cal;
cal.setStrat(new pluss());
c=cal.execute(a,b);
item.num=c;
item.type='0';
numbers.push(item);
}
};
int main()
{double num;
char ch;
stack<Lecsema>numbers;
stack<Lecsema>znak;
Lecsema item;
while(1){
ch=cin.peek();
if(ch=='\n')
{break;}
if(ch>='0' && ch<='9' )
{
cin>>num;
item.type='0';
item.num=num;
numbers.push(item);
continue;
}
if(ch=='+'|| ch=='-' || ch=='*' || ch=='/')
{
item.type=ch;
item.num=0;
znak.push(item);
cin.ignore();
continue;
}
}
Maths(numbers,znak,item);
cout<<numbers.top().num;
return 0;
}
}
Maths(numbers,znak,item);
cout<<numbers.top().num;
return 0;
}
по факту,это должен быть однострочний кальлятор ,сейчас в нем прописана одна операция в функции "Maths" .и как минмум он должен щитать вот такие виражение :
"24+64" и подобное, при нажатие на ентер должен появиться ответ 88, но заместь него появляеться число 64 ,и как бы большим не было виражение,всегда будет ответ число после последнего знака,мне кажеться ошибка в функции "Maths"