Dالسلام عليكم
هذا برنامج لcalculator ابعرف اذاكان صح او لا واش الاخطلء اللي فيه
اتمنى اشوف رايكم
#include <ctype.h>
#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <string.h>
class stack
{private:
int top;
char array[20];
public:
bool isempty();
bool isfull();
stack();
void push(char );
char pop();
int val();
char pop2(); };
class calculator
{char A[20];
stack operatr;
stack operand;
public:
calculator ();
void find();
void topost();
float calculate(int,int,char);
};
int stack::val()
{return top;
}
stack::stack()
{top=0;
}
bool stack::isempty()
{ if(top==0)
return true;
return false;
}
char stack::pop2()
{return array[top-1];
}
bool stack::isfull()
{ if(top!=19)
return false;
return true;
}
void stack::push(char y)
{ if(!(isfull()))
{array[top]=y; ++top;
}
else{ cout<<"\n stack is full"; }
}
char stack::pop()
{
if(top!=0)
{--top;
return(array[top]);
}
else
{cout<<"\n stack is Empty";
return 0; }
}
float calculator::calculate(int s1,int s2,char op)
{
switch(op)
{ case '*':return(s1*s2);break;
case '/':return(s1/s2);break;
case '+':return(s1+s2);break;
case '-':return(s1-s2);break;}
}
calculator::calculator()
{ gets(A);
}
void calculator::topost()
{ int s1,s2, flag=1,j=0,m=0,top=-1,h=0;
char o;
float output,out;
for(int d=0;d<strlen(A);d++)
if(A[d]!='*'&&A[d]!='/'&&A[d]!=' '&&A[d]!='+'&&A[d]!='-'&&(A[d]<'0'||A[d]>'9'))
{ flag=0;
break;}
for(int k=0;k<strlen(A);k++)
{if(!(A[k]<'0'|| A[k]>'9'))
operand.push(A[k]-'0');
else if( A[k]!=' ')
operatr.push(A[k]);
else if ((A[k]=='+'||A[k]=='-'||A[k]=='*'||A[k]=='/')&&(operatr.pop2()=='*'||operatr.pop2()=='/'))
{s1=operand.pop();
s2=operand.pop();
o=operatr.pop(); cout<<"\n"<<s1<<s2<<o;
output=calculate(s1,s2,o);
operatr.push(A[k]);
operand.push(output);
}
else if((operand.val()==0)&&(A[k]=='-'||A[k]=='+'||A[k]=='*'||A[k]=='/'))
operatr.push(A[k]);
}
if(flag)
while(operatr.val()!=0)
{s1=operand.pop();
s2=operand.pop();
o=operatr.pop(); cout<<"\n"<<s1<<s2<<o;
out=calculate(s1,s2,o);
operand.push(out);
}
cout<<"\n output = "<<out;getch();
}
void main()
{
char u,z,ch2;int num[50];
int y,x,top1=0,top2=0,a,i;
bool flag=true;
cout<<"\n Please Enter Expretion .... ";
calculator m;
m.topost() ;
getch(); }