Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <iostream>
- #include <QVector>
- using namespace std;
- class Equation
- {
- QString MainStr;
- public:
- Equation(QString a)
- {
- MainStr = a;
- }
- ~Equation(){}
- QVector<QString> *VecR(QStringList *list)
- {
- QVector <QString> *vec = new QVector<QString>;
- for(int i = 0; i < list->size(); i++)
- {
- vec->push_back(list->at(i));
- }
- return vec;
- }
- void Operation(QVector <QString> *List, int i, int action)
- {
- double arg1 = List->at(i - 2).toDouble();
- double arg2 = List->at(i - 1).toDouble();
- if(action == 1)
- arg1 = arg1 / arg2;
- if(action == 2)
- arg1 = arg1 * arg2;
- List->replace(i, QString::number(arg1));
- List->remove(i-1);
- List->remove(i-2);
- }
- void Act(QVector <QString> *List, int i)
- {
- if(List->at(i) == "/")
- {
- Operation(List, i, 1);
- }
- if(List->at(i) == "*")
- {
- Operation(List, i, 2);
- }
- }
- void Solution()
- {
- QStringList *list = new QStringList;
- *list = MainStr.split(" ");
- QVector <QString> *List = VecR(list);
- bool ok;
- for(int i = 0; i < List->size(); i++)
- {
- if(List->at(i).toDouble(&ok))
- continue;
- else
- {
- Act(List, i);
- i = 0;
- }
- }
- for(int i = 0; i < List->size(); ++i)
- {
- cout << " " << List->at(i).toStdString();
- }
- }
- };
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Equation b("6 3 / 3 3 * *");
- b.Solution();
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement