Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <utility>
- #include <sstream>
- #include <string>
- #include <stack>
- #include <algorithm>
- #include "tester.h"
- #define debug(x) std::cout << __FILE__ << " (" << __LINE__ << ") " << #x << " == " << x << std::endl;
- using namespace std;
- std::string trim (std::string s)
- {
- auto poczatek = s.find_first_not_of(" \t\r\n");
- if (poczatek != std::string::npos)
- s = s.substr(poczatek);
- auto koniec = s.find_last_not_of(" \t\r\n");
- if (koniec != std::string::npos)
- s = s.substr(0, koniec + 1);
- return s;
- }
- double parsuj(const std::string & s)
- {
- double wynik = 0;
- double number = 0;
- size_t znacznik = 0;
- stack<double> stos;
- size_t operacja[4] = {0,0,0,0}; //n, +, -, *
- string String_N = s;
- size_t String_S = 0;
- while(1)
- {
- if(String_N.size() && String_N[0] == '+')
- {
- double pierwsza = stos.top();
- stos.pop();
- pierwsza += stos.top();
- stos.pop();
- stos.push(pierwsza);
- String_S = String_N.size();
- znacznik = 0;
- String_N = String_N.substr(1);
- continue;
- }
- try{
- number = stod(String_N, &znacznik);
- String_S = String_N.size();
- String_N = String_N.substr(znacznik);
- stos.push(number);
- }
- catch(std::invalid_argument &e)
- {
- if(znacznik > String_S){
- if(!stos.empty()){
- wynik = stos.top();
- stos.pop();
- }
- return wynik;
- }
- else{
- operacja[0] = String_N.find('n');
- operacja[1] = String_N.find('+');
- operacja[2] = String_N.find('-');
- operacja[3] = String_N.find('*');
- size_t polecenie = 0;
- size_t *minimalny = min_element(operacja, operacja+4);
- polecenie = minimalny-operacja;
- switch(polecenie){
- case 0:{
- if(operacja[0] != string::npos){
- double pierwsza = stos.top();
- stos.pop();
- pierwsza *= -1;
- stos.push(pierwsza);
- }
- break;
- }
- case 1:{
- double pierwsza = stos.top();
- stos.pop();
- pierwsza += stos.top();
- stos.pop();
- stos.push(pierwsza);
- break;
- }
- case 2:{
- double pierwsza = stos.top();
- stos.pop();
- pierwsza = stos.top() - pierwsza;
- stos.pop();
- stos.push(pierwsza);
- break;
- }
- case 3:{
- double pierwsza = stos.top();
- stos.pop();
- pierwsza *= stos.top();
- stos.pop();
- stos.push(pierwsza);
- }
- }
- String_S = String_N.size();
- znacznik = operacja[polecenie];
- String_N = String_N.substr(operacja[polecenie]+1);
- }
- }
- }
- return wynik;
- }
- int main ()
- {
- Tester tester; // Obiekt tester realizuje testy i zwraca raport z testow
- std::string raport = tester.testuj(parsuj);
- std::cout << raport << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement