سلام دوستان منم یه ماشین حساب نوشتم که عبارات ریاضی رو میگیره بدون فاصله و اونا رو بر اساس اولویت عملگر محاسبه و در خروجی مینویسه : (فعلا فقط عملگرهای +و*و/و- و اعداد صحیح میخوام گسترشش بدم تا بتونه sin, cos, log رو هم محاسبه کنه اگه میشه راهنمایی کنید ) توان(^) هم اضافه شد ، پرانتز گذاری رو هم میفهمه :
ورودی :
10-9^2*2-81-10
خروجی :
-243
برنامه :
// stake solution.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
struct node
{
char info;
node *left;
node *right;
};
node *getnode()
{
node* q;
q= new struct node;
return q;
}
void push(node * &top,char item)
{
node *p;
p=getnode();
p->info=item;
p->left=top;
top->right=p;
top=p;
top->right =NULL;
}
int power(int a1,int a2)
{
int x=a1;
for(int i=1;i<a2;i++)
a1=a1*x;
return a1;
}
char pop(node * &top)
{
char x;
if(top->left!=NULL)
{
node *p;
p=getnode();
x =top->info;
p=top;
top=top->left;
delete(p);
return x;
}
else
return '#';
}
int _tmain(int argc, _TCHAR* argv[])
{
char n1ch[100];
char n2ch[100];
char n3ch[100];
int n3[10];
for(int u=0;u<10;u++)
n3[u]=0;
node *top;
top=getnode();
top->left=NULL;
int i=0,k=0;
int a1=1,a2=1;
char ch;
cout<<"\nplease enter equation : ";
cin.getline(n1ch,98);
while(n1ch[i]!=NULL)
i++;
int s=0;
int t=-1;
int r=-1;
int y=0;
int ss=0;
bool zz=false;
for(int g=0;g<=i;g++)
{
if(n1ch[g]=='(')
ss++;
if(n1ch[g]==')')
ss--;
if(ss<0)
{
zz=true;
break;
}
}
for(int q=0;q<i;q++)
{
if(int(n1ch[q])>=48 && int(n1ch[q])<=57)
{
t++;
r++;
if(n3[s]!=0)
n3[s]=n3[s]*10+(int(n1ch[q])-48);
else
n3[s]=int(n1ch[q])-48;
}
else if(n1ch[q]=='(' || n1ch[q]==')')
y++;
else
{
if(s>1)
r=r+s-1;
n1ch[s+t-r+y]='p';
t++;
s++;
r=0;
}
}
if(s>1)
r=r+s-1;
int v=0;
n1ch[s+t-r+y]='p';
for(int g=0;g<i;g++)
if(!(int(n1ch[g])>=48 && int(n1ch[g])<=57))
{
n3ch[v]=n1ch[g];
v++;
}
while(true)
{
if(s==0)
{
cout<<"hade aghal 2 adad va 1 amalgar vared konid!";
break;
}
else if(zz==true||ss!=0)
{
cout<<"prantez gozari dorost nist ";
break;
}
else
{
for(int j=0;j<i;j++)
{
if(n3ch[j]=='(')
push(top,n3ch[j]);
else if(n3ch[j]=='p')
{
n2ch[k]=n3ch[j];
k++;
}
else if(n3ch[j]=='+' || n3ch[j]=='-' || n3ch[j]=='/' || n3ch[j]=='*'|| n3ch[j]=='^')
{
while((n3ch[j]=='*' || n3ch[j]=='/') && (top->info=='*' || top->info=='/' || top->info=='^' ))
{
n2ch[k]=pop(top);
k++;
}
while((n3ch[j]=='^') && (top->info=='^'))
{
n2ch[k]=pop(top);
k++;
}
while((n3ch[j]=='+' || n3ch[j]=='-') && (top->info=='*' || top->info=='/' || top->info=='^' || top->info=='+' || top->info=='-'))
{
n2ch[k]=pop(top);
k++;
}
push(top,n3ch[j]);
}
else if(n3ch[j]==')')
{
while(top->info!='(')
{
n2ch[k]=pop(top);
k++;
}
pop(top);
}
}
while(top->left!=NULL)
{
n2ch[k]=pop(top);
k++;
}
//for(int f=0;f<k;f++)
// cout<<n2ch[f];
for(int d=0;d<k;d++)
{
if(n2ch[d]=='(')
{
for(int f=d;f<k-1;f++)
n2ch[f]=n2ch[f+1];
k--;
}
}
cout<<"\n";
//for(int f=0;f<k;f++)
// cout<<n2ch[f];
int a=0;
int h=0;
while(n2ch[k-1]!='n')
{
int z=0;
int p=0;
a=0;
int n4[10];
while(true)
{
if(n2ch[a]=='+' || n2ch[a]=='*' || n2ch[a]=='^' || n2ch[a]=='/' || n2ch[a]=='-')
break;
a++;
}
for(h=0;h<a;h++)
{
if(n2ch[h]=='p')
{
p=h;
z++;
}
}
a1=n3[z-2];
a2=n3[z-1];
switch(n2ch[a])
{
case '+':n3[z-2]=a1+a2;
n2ch[p]='0';
n2ch[a]='n';
for(int d=z;d<=s;d++)
n3[d-1]=n3[d];
s--;
break;
case '-':n3[z-2]=a1-a2;
n2ch[p]='0';
n2ch[a]='n';
for(int d=z;d<=s;d++)
n3[d-1]=n3[d];
s--;
break;
case '*':n3[z-2]=a1*a2;
n2ch[p]='0';
n2ch[a]='n';
for(int d=z;d<=s;d++)
n3[d-1]=n3[d];
s--;
break;
case '/':n3[z-2]=a1/a2;
n2ch[p]='0';
n2ch[a]='n';
for(int d=z;d<=s;d++)
n3[d-1]=n3[d];
s--;
break;
case '^':n3[z-2]=power(a1,a2);
n2ch[p]='0';
n2ch[a]='n';
for(int d=z;d<=s;d++)
n3[d-1]=n3[d];
s--;
break;
}
}
}
cout<<"\nAnswer is : ";
cout<<n3[0];
break;
}
getch();
return 0;
}