السلام عليكم ورحمة الله وبركاته
اتمنى مساعدتي البرنامج مكتوب بس المشكله فيه خطاء ياريت تحدون للي وين خطائي
فكرة البرنامج
ندخل المعادلهstring والمعادله المدخله هي postfix
ونستخدم pop و push
لحساب الناتج
مع الانتباه بانه يجيب تحويل الارقام من string الى int
هذا هوه الحل
#include <iostream>
#include <cstring>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
struct NodeRec
{
int eval;
NodeRec* next;
};
void token(NodeRec* temp, char b[4]);
void push(NodeRec* top,NodeRec* temp);
int pop(NodeRec* top);
void result(char a[50]);
void main ()
{
char a[50];
result(a);
getch();
}
//----------------fun puch---------------------------
void push(NodeRec* top,NodeRec* temp)
{
if(top==NULL)
top=temp;
else
{
temp->next=top;
top=temp;
}
}
//------------fun top-------------------------------
int pop(NodeRec* top)
{
int eval;
NodeRec* curn;
curn=top;
if(top==NULL)
cout<<"STACK IS EMPTY";
else
{
eval=top->eval;
top=top->next;
delete curn;
}
return eval;
}
//--------------fun result--------------------------
void result(char a[50])
{
char b[4];
int l,j=0,x,i=0;
NodeRec* top;
top=new NodeRec;
NodeRec* temp;
top=NULL;
cout<<"enter expression in postfix but after number space:";
gets(a);
l=strlen(a);
while(i<l+1)
{
temp=new NodeRec;
temp->eval=0;
temp->next=NULL;
if(a =='+')
{
x=pop(top);
x+=pop(top);
temp->eval=x;
push(top,temp);
i++;
}
else if(a =='-')
{
x=pop(top);
x-=pop(top);
temp->eval=x;
push(top,temp);
i++;
}
else if(a =='*')
{
x=pop(top);
x*=pop(top);
temp->eval=x;
push(top,temp);
i++;
}
else if(a =='/')
{
x=pop(top);
x/=pop(top);
push(top,temp);
i++;
}
else
{
while((a !=' ') && (a !='+') && (a!='-') && (a!='*') && (a!='/'))
{
b[j]=a;
j++;
i++;
}
b[j]='\0';
token(temp,b);
//if ((a !='+') && (a!='-') && (a!='*') && (a!='/'))
push(top,temp);
strcpy(b," ");
j=0;
i++;
}
}
cout<<endl<<"result= "<<top->eval;
}
//-----------------fun token---------------- // لتحويل من سترينق الى انتيقر
void token(NodeRec* temp, char b[4])
{
char *y;
int n;
y=new char;
y=strtok(b,'\0');
n=atoi(y);
temp->eval=n;
}
//------------------------------------------------------