Mam programik, który konwertuje i/lub oblicza wyrażenia w ONP. I niby wszystko działa ale ma pewne wady, których nie umiem rozwiązać :
- Można wpisać liczby tylko od 0 do 9 (np 10 czyta jako 1 i 0 i brakuje wtedy operatorów)
- Nie obsługuje spacji (po wprowadzeniu 1 + 1 program się wiesza, 1+1 jest ok)
Będę mega wdzięczny za pomoc
#include<iostream>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<istream>
using namespace std;
const int maxlength = 100;
typedef int elementtype, position;
bool operatory(char o)
{
return o == '+' || o == '-' || o == '*' || o == '/' || o == '^' || o == '~';
}
int wagi(char w)
{
if(w == '^')
return 3;
else if(w == '*' || w == '/')
return 2;
else if(w == '~' || w == '-' || w == '+')
return 1;
else
return 0;
}
class Stos
{
int S[maxlength];
position top;
public:
Stos()
{
top = -1;
}
void PUSH(elementtype x)
{
S[++top] = x;
}
void POP()
{
if((EMPTY() == false))
S[top--];
}
bool EMPTY()
{
if(top == -1)
return true;
else
return false;
}
elementtype TOP()
{
if(EMPTY() == false)
return S[top];
}
void MAKENULL()
{
while(top != -1)
S[top--];
}
elementtype pokazTOP()
{
if(EMPTY() == false)
cout<<(int)S[top]<<endl;
else
cout<<"Stos pusty"<<endl;
}
};
void konwersja(const string wyrazenie, char rezultat[], int dlugosc)
{
Stos s;
int r = 0;
int i = 0;
while(i < dlugosc)
{
char kursor = wyrazenie.at(i);
if(kursor == '(' || kursor == '~')
{
s.PUSH(kursor);
i++;
continue;
}
if(kursor == ')')
{
while(s.EMPTY() == false && s.TOP() != '(')
{
rezultat[r++] = s.TOP();
s.POP();
}
if(s.EMPTY() == false)
s.POP();
i++;
continue;
}
int waga = wagi(kursor);
if(waga == 0)
rezultat[r++] = kursor;
else{
if(s.EMPTY())
s.PUSH(kursor);
else{
while(s.EMPTY() == false && s.TOP() != '(' && waga <= wagi(s.TOP()))
{
rezultat[r++] = s.TOP();
s.POP();
}
s.PUSH(kursor);
}
}
i++;
}
while(s.EMPTY() == false)
{
rezultat[r++] = s.TOP();
s.POP();
}
rezultat[r] = 0;
}
obliczanie(const string &wyrazenie)
{
Stos s;
int dlugosc = wyrazenie.size();
int a,b, temp, wynik;
b = -(maxlength) + 1;
a = -(maxlength) + 1;
for(int i = 0; i <= dlugosc; i++)
{
if(wyrazenie[i] == '=')
break;
if(operatory(wyrazenie[i]))
{
b = (int)s.TOP() - (maxlength/2 - 2);
s.POP();
if(wyrazenie[i] != '~')
{
a = (int)s.TOP() - (maxlength/2 - 2);
s.POP();
}
switch(wyrazenie[i])
{
case '*': temp = b*a; break;
case '/': temp = a/b; break;
case '+': temp = b+a; break;
case '-': temp = a-b; break;
case '^': temp = pow(a,b); break;
case '~': temp = b * (-1); break;
}
s.PUSH((char)(temp + (maxlength/2 - 2)));
a = b = -(maxlength) + 1;
wynik = temp;
}
else
s.PUSH(wyrazenie[i]);
}
return wynik;
}
void bezSpacji(const string &tekst, string &tekst2)
{
int i,j = 0;
tekst2 = "";
while(j == 0)
{
for(i = 0; i < tekst.size(); i++)
{
if(tekst.at(i) == ' ')
j = 1;
else
tekst2 = tekst2 + tekst.at(i);
}
}
//return tekst2;
}
int main()
{
char wynik[maxlength];
int wybor, dlugosc;
string znak, dane, wyrazenie, wyrazenie1;
do
{
system("cls");
cout<<"|------------ MENU ------------|"<<endl;
cout<<" Wybierz: \n"<<endl;
cout<<" 1. Wyrazenie w ONP."<<endl;
cout<<" 2. Konwersja do ONP."<<endl;
cout<<" 3. Konwersja i obliczenie."<<endl;
cout<<" 4. Zamknij program"<<endl;
cin>>wybor;
switch(wybor)
{
case 1:
cout<<"\n\n************ Wyrazenie w ONP. ************\n"<<endl;
cout<<"Podaj wyrazenie: "<<endl;
cout<<"musi byc zakonczone '='"<<endl;
//getline(cin, dane);
cin>>dane;
dlugosc = (int)dane.size();
cout<<" Wynik: "<<obliczanie(dane)<<endl;
getchar();
break;
case 2:
cout<<"\n\n************ Konwersja do ONP. ************"<<endl;
cout<<"Podaj wyrazenie: "<<endl;
cin>>dane;
//getline(cin, dane);
//getchar();
//bezSpacji(wyrazenie, wyrazenie1);
//wyrazenie1 = dane;
dlugosc = (int)dane.size();
wynik[dlugosc];
konwersja(dane, wynik, dlugosc);
cout<<" Wyrazenie w ONP: "<<wynik<<endl;
getchar();
break;
case 3:
cout<<"\n\n************ Konwersja i obliczenie ************"<<endl;
cout<<"Podaj wyrazenie: "<<endl;
cin>>dane;
//getline(cin, dane);
//getchar();
dlugosc = (int)dane.size();
wynik[dlugosc];
konwersja(dane, wynik, dlugosc);
cout<<" Wyrazenie w ONP: "<<wynik<<endl;
cout<<""<<endl;
cout<<" Wynik: "<<obliczanie(wynik)<<endl;
getchar();
break;
case 4:
exit(0);
default:
cout<<"Zly numer."<<endl;
}
cout<<"\n Zakonczyc? (t)"<<endl;
cin>>znak;
}
while(znak != "t");
system("cls");
return 0;
}
MAKENULL
i czemu tam jest pętla a nietop = -1;
?