Обратная польская запись. Компиляция на Release. C++
Цитата:
#include "stdafx.h"
#include "iostream"
#include "string"
#include "stack"
using namespace std;
int priority(char w);
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Vvedite virazhenije"<<endl;
string inputstr;
cin >> inputstr;
int k=0;
if (inputstr[0]=='-' || inputstr[0]=='+') inputstr='0'+inputstr;
for (int i=1; inputstr!=0; i=i+1)
{
if ((inputstr[i-1]=='(' || inputstr[i-1]=='{' || inputstr[i-1]=='[') && (inputstr=='+' || inputstr=='-'))
{
inputstr.insert(i, "0");
}
}
while (inputstr[k]!=0)
{
if (inputstr[k+1]=='=') inputstr[k+1]=0;
k=k+1;
}
char *stroka=new char[k];
for (int i=0; i<=k; i++) stroka=inputstr;
stack <char> forPolka;
forPolka.push('@');
int n=0, m=0;
while (stroka[n]!=0)
{
if (stroka[n]=='(' || stroka[n]=='{' || stroka[n]=='[') {stroka[n]='(';}
else if (stroka[n]==')' || stroka[n]=='}' || stroka[n]==']') {stroka[n]=')';}
else if ((stroka[n]>='0' && stroka[n]<='9') && !(stroka[n+1]>='0' && stroka[n+1]<='9')) m=m+1;
else m=m+1;
n=n+1;
}
char *result=new char[m];
m=0;
bool error=false;
int j=0;
if (stroka[0]==')' || stroka[0]=='}' || stroka[0]==']' || stroka[0]=='*' || stroka[0]=='/' || stroka[0]=='^') error=true;
while (j<n && error==false)
{
if (stroka[j]>='0' && stroka[j]<='9')
{
result[m]=stroka[j];
m=m+1;
if (!(stroka[j+1]>='0' && stroka[j+1]<='9'))
{
result[m]='!';
m=m+1;
}
if (stroka[j+1]=='(' || (stroka[j+1]>='A' && stroka[j+1]<='Z') || (stroka[j+1]>='a' && stroka[j+1]<='z')) error=true;
}
else if ((stroka[j]>='A' && stroka[j]<='Z') || (stroka[j]>='a' && stroka[j]<='z'))
{
result[m]=stroka[j];
m=m+1;
if ((stroka[j+1]>='0' && stroka[j+1]<='9') || stroka[j+1]=='(' || (stroka[j+1]>='A' && stroka[j+1]<='Z') || (stroka[j+1]>='a' && stroka[j+1]<='z')) error=true;
}
else if (stroka[j]=='+' || stroka[j]=='-' || stroka[j]=='*' || stroka[j]=='/')
{
while (priority(forPolka.top())>=priority(stroka[j]))
{
result[m]=forPolka.top();
forPolka.pop();
m=m+1;
}
forPolka.push(stroka[j]);
if (stroka[j+1]=='+' || stroka[j+1]=='-' || stroka[j+1]=='*' || stroka[j+1]=='/' || stroka[j+1]==')') error=true;
}
else if (stroka[j]=='(')
{
forPolka.push('(');
if (stroka[j+1]=='+' || stroka[j+1]=='-' || stroka[j+1]=='*' || stroka[j+1]=='/' || stroka[j+1]==')') error=true;
}
else if (stroka[j]==')')
{
while (forPolka.top()!='(' && forPolka.top()!='@')
{
result[m]=forPolka.top();
forPolka.pop();
m=m+1;
}
if (forPolka.top()=='@') error=true;
else forPolka.pop();
if (stroka[j+1]=='(' || stroka[j+1]=='+' || stroka[j+1]=='-' || stroka[j+1]=='*' || stroka[j+1]=='/' || (stroka[j+1]>='A' && stroka[j+1]<='Z') || (stroka[j+1]>='a' && stroka[j+1]<='z')) error=true;
}
else
{
cout << "Proverte vvod. Ne te simvoli!";
error=true;
}
j=j+1;
}
while (forPolka.top()!='(' && forPolka.top()!='@')
{
result[m]=forPolka.top();
forPolka.pop();
m=m+1;
}
if (forPolka.top()=='(') error=true;
else forPolka.pop();
result[m]=0;
if (!error)
{
cout<< "Obr polskaia zapis"<<endl;
for (int i=0; i<m; i=i+1)
{
if (result>='0' && result<='9')
{
while (result!='!')
{
cout << result;
i=i+1;
}
cout << " ";
}
else cout << result << " ";
}
cout << endl;
stack <float> forCount;
float temp = 0, a = 0, b = 0;
bool zero=0;
for (int i=0; i<m; i=i+1)
{
if ((result>='A' && result<='Z') || (result>='a' && result<='z'))
{
cout << result <<"=";
cin >> temp;
forCount.push(temp);
}
if (result>='0' && result<='9')
{
temp=0;
while (result!='!')
{
temp=temp*10+(result-48);
i=i+1;
}
forCount.push(temp);
}
if (result=='+' || result=='-' || result=='*' || result=='/')
{
b=forCount.top();
forCount.pop();
a=forCount.top();
forCount.pop();
if (result=='+') a=a+b;
if (result=='-') a=a-b;
if (result=='*') a=a*b;
if (result=='/')
{
if (b!=0) a=a/b;
else zero=1;
}
forCount.push(a);
}
}
if (zero) cout << "Oshibka"<<endl;
else cout << stroka << "=" << forCount.top() << endl;
forCount.pop();
}
else cout << "Oshibka"<<endl;
char exit = 0;
cin >> exit;
return 0;
}
int priority(char w)
{
if (w=='*' || w=='/') return 2;
else if (w=='-' || w=='+') return 1;
else return 0;
}
#include "iostream"
#include "string"
#include "stack"
using namespace std;
int priority(char w);
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Vvedite virazhenije"<<endl;
string inputstr;
cin >> inputstr;
int k=0;
if (inputstr[0]=='-' || inputstr[0]=='+') inputstr='0'+inputstr;
for (int i=1; inputstr!=0; i=i+1)
{
if ((inputstr[i-1]=='(' || inputstr[i-1]=='{' || inputstr[i-1]=='[') && (inputstr=='+' || inputstr=='-'))
{
inputstr.insert(i, "0");
}
}
while (inputstr[k]!=0)
{
if (inputstr[k+1]=='=') inputstr[k+1]=0;
k=k+1;
}
char *stroka=new char[k];
for (int i=0; i<=k; i++) stroka=inputstr;
stack <char> forPolka;
forPolka.push('@');
int n=0, m=0;
while (stroka[n]!=0)
{
if (stroka[n]=='(' || stroka[n]=='{' || stroka[n]=='[') {stroka[n]='(';}
else if (stroka[n]==')' || stroka[n]=='}' || stroka[n]==']') {stroka[n]=')';}
else if ((stroka[n]>='0' && stroka[n]<='9') && !(stroka[n+1]>='0' && stroka[n+1]<='9')) m=m+1;
else m=m+1;
n=n+1;
}
char *result=new char[m];
m=0;
bool error=false;
int j=0;
if (stroka[0]==')' || stroka[0]=='}' || stroka[0]==']' || stroka[0]=='*' || stroka[0]=='/' || stroka[0]=='^') error=true;
while (j<n && error==false)
{
if (stroka[j]>='0' && stroka[j]<='9')
{
result[m]=stroka[j];
m=m+1;
if (!(stroka[j+1]>='0' && stroka[j+1]<='9'))
{
result[m]='!';
m=m+1;
}
if (stroka[j+1]=='(' || (stroka[j+1]>='A' && stroka[j+1]<='Z') || (stroka[j+1]>='a' && stroka[j+1]<='z')) error=true;
}
else if ((stroka[j]>='A' && stroka[j]<='Z') || (stroka[j]>='a' && stroka[j]<='z'))
{
result[m]=stroka[j];
m=m+1;
if ((stroka[j+1]>='0' && stroka[j+1]<='9') || stroka[j+1]=='(' || (stroka[j+1]>='A' && stroka[j+1]<='Z') || (stroka[j+1]>='a' && stroka[j+1]<='z')) error=true;
}
else if (stroka[j]=='+' || stroka[j]=='-' || stroka[j]=='*' || stroka[j]=='/')
{
while (priority(forPolka.top())>=priority(stroka[j]))
{
result[m]=forPolka.top();
forPolka.pop();
m=m+1;
}
forPolka.push(stroka[j]);
if (stroka[j+1]=='+' || stroka[j+1]=='-' || stroka[j+1]=='*' || stroka[j+1]=='/' || stroka[j+1]==')') error=true;
}
else if (stroka[j]=='(')
{
forPolka.push('(');
if (stroka[j+1]=='+' || stroka[j+1]=='-' || stroka[j+1]=='*' || stroka[j+1]=='/' || stroka[j+1]==')') error=true;
}
else if (stroka[j]==')')
{
while (forPolka.top()!='(' && forPolka.top()!='@')
{
result[m]=forPolka.top();
forPolka.pop();
m=m+1;
}
if (forPolka.top()=='@') error=true;
else forPolka.pop();
if (stroka[j+1]=='(' || stroka[j+1]=='+' || stroka[j+1]=='-' || stroka[j+1]=='*' || stroka[j+1]=='/' || (stroka[j+1]>='A' && stroka[j+1]<='Z') || (stroka[j+1]>='a' && stroka[j+1]<='z')) error=true;
}
else
{
cout << "Proverte vvod. Ne te simvoli!";
error=true;
}
j=j+1;
}
while (forPolka.top()!='(' && forPolka.top()!='@')
{
result[m]=forPolka.top();
forPolka.pop();
m=m+1;
}
if (forPolka.top()=='(') error=true;
else forPolka.pop();
result[m]=0;
if (!error)
{
cout<< "Obr polskaia zapis"<<endl;
for (int i=0; i<m; i=i+1)
{
if (result>='0' && result<='9')
{
while (result!='!')
{
cout << result;
i=i+1;
}
cout << " ";
}
else cout << result << " ";
}
cout << endl;
stack <float> forCount;
float temp = 0, a = 0, b = 0;
bool zero=0;
for (int i=0; i<m; i=i+1)
{
if ((result>='A' && result<='Z') || (result>='a' && result<='z'))
{
cout << result <<"=";
cin >> temp;
forCount.push(temp);
}
if (result>='0' && result<='9')
{
temp=0;
while (result!='!')
{
temp=temp*10+(result-48);
i=i+1;
}
forCount.push(temp);
}
if (result=='+' || result=='-' || result=='*' || result=='/')
{
b=forCount.top();
forCount.pop();
a=forCount.top();
forCount.pop();
if (result=='+') a=a+b;
if (result=='-') a=a-b;
if (result=='*') a=a*b;
if (result=='/')
{
if (b!=0) a=a/b;
else zero=1;
}
forCount.push(a);
}
}
if (zero) cout << "Oshibka"<<endl;
else cout << stroka << "=" << forCount.top() << endl;
forCount.pop();
}
else cout << "Oshibka"<<endl;
char exit = 0;
cin >> exit;
return 0;
}
int priority(char w)
{
if (w=='*' || w=='/') return 2;
else if (w=='-' || w=='+') return 1;
else return 0;
}
Вообще программа достаточно большая, и разбираться в ней мне лень.
Возможно ты забываешь инициализировать переменные перед использованием - в дебаге они часто инициализируются нулями автоматически.
Ещё мне не очень нравится вот это:
Код:
if (stroka[0]==')' || stroka[0]=='}' || stroka[0]==']' || stroka[0]=='*' || stroka[0]=='/' || stroka[0]=='^') error=true;
Кажись приоритет у || и && больше чем у ==.
Я бы поставил скобки.
[highlight=haskell]
calc :: String -> [Float]
calc = foldl f [] . words
where
f (x:y:zs) "+" = (y + x):zs
f (x:y:zs) "-" = (y - x):zs
f (x:y:zs) "*" = (y * x):zs
f (x:y:zs) "/" = (y / x):zs
f xs y = read y : xs
[/highlight]
О функциональном программировании уже по этому примеру стоит иметь некоторое понятие..;)