سلام
من ميخواستم برنامه اي بنويسم كه بتونه يك معادله مثل
رو كه كاربر وارد ميكنه حل كنه.3+(4*6)+7-(5/2)
لطفا كمكم كنيد.:11:
Printable View
سلام
من ميخواستم برنامه اي بنويسم كه بتونه يك معادله مثل
رو كه كاربر وارد ميكنه حل كنه.3+(4*6)+7-(5/2)
لطفا كمكم كنيد.:11:
باید ابتدا آن را از حالت infix (حالت معمولی) به حالت postfix ببریم:
الگوریتمش اینجوریه:
سپس از روی postfix مقدار عبار ترا به دست آورید:کد:1. Initialize an empty stack (string stack), prepare input infix expression and clear RPN string
2. Repeat until we reach end of infix expression
1. Get token (operand or operator); skip white spaces
2. If token is:
1. Left parenthesis: Push it into stack
2. Right parenthesis: Keep popping from the stack and appending to RPN string until we reach the left parenthesis.
If stack becomes empty and we didn't reach the left parenthesis then break out with error "Unbalanced parenthesis"
3. Operator: If stack is empty or operator has a higher precedence than the top of the stack then push operator into stack. Else if operator has lower precedence then we keep popping and appending to RPN string, this is repeated until operator in stack has lower precedence than the current operator.
4. An operand: we simply append it to RPN string.
3. When the infix expression is finished, we start popping off the stack and appending to RPN string till stack becomes empty.
کد:1. Initialize stack (integer stack) for storing results, prepare input postfix (or RPN) expression.
2. Start scanning from left to right till we reach end of RPN expression
3. Get token, if token is:
1. An operator:
1. Get top of stack and store into variable op2; Pop the stack
2. Get top of stack and store into variable op1; Pop the stack
3. Do the operation expression in operator on both op1 and op2
4. Push the result into the stack
2. An operand: stack its numerical representation into our numerical stack.
4. At the end of the RPN expression, the stack should only have one value and that should be the result and can be retrieved from the top of the stack.
Parsing دقیقاً برای همین کار هستش.
توی گوگل دنبال Parser بگردی یه چیزایی پیدا میکنی.
منم یه Parser خیلی قدرتمند نوشتم که فکر کنم جالب باشه ببینید:
کد:http://ashiyane.org/forums/showthread.php?threadid=13622
خيلي از دوستان متشكرم
ولي آقاي حميدرضاي عزيز اگه يه تكه كد نمونه هم ميزاشتي خيلي بهتر ميشد.
آخه من يكم آماتورم
سلامنقل قول:
این دیگه الگوریتم کاملشه! اگه قرار بود نمونه کد بگذارم که فهمیدنش خیلی سخت تر بود.
شما اگه با توجه به الگوریتم بالا کدتون رو بنویسین کار خواهد کرد.
خوشبختانه با راهنمايي هاي دوستان خوبم موفق شدم.
كدشم ميزارم برا استفاده ديگر دوستان عزيز:
کد:#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#define max 30
void topostfix();
int intstack[max],inttop=-1;
void intpush(int);
int intpop();
int postfix_count(char[]);
char pop(void);
void push(char);
int Isunder(void);
int Isover(void);
int prio(char);
int top=-1,i=0,j=0;
char stk[max];
char infix[max];
char postfix[max];
void main()
{
clrscr();
cout << "Please enter your expression:";
cin >> infix ;
topostfix();
cout << endl << "Your postfix expression is :" << postfix << endl ;
cout<<"\nYour Answer is: ";
cout<<postfix_count(postfix)<<endl;
getch();
}
void topostfix()
{
for(i=0;infix[i]!='\0';i++)
switch(infix[i])
{
case '(' :
push( '(' );
break;
case ')' :
while(stk[top]!='(')
postfix[j++]=pop();
if (stk[top]=='(')
top--;
break;
case '/':
case '*':
case '%':
case '+':
case '-':
case '^':
if (!Isunder())
if (prio(infix[i]) > prio(stk[top]) )
push(infix[i]);
else
{
postfix[j++]=pop();
push(infix[i]);
}
else
push(infix[i]);
break;
default :
postfix[j++] = infix[i];
}
while (!Isunder())
postfix[j++]=pop();
postfix[j]='\0';
}
char pop()
{
if (!Isunder())
return stk[top--];
return 0;
}
void push(char ch)
{
if (!Isover())
stk[++top]=ch;
else
cout << "Stack is full";
}
int Isover()
{
if (top == max-1)
return 1;
else
return 0;
}
int Isunder()
{
if (top == -1)
return 1;
else
return 0;
}
int prio(char ch)
{
int ret;
switch(ch)
{
case '(' :
ret=1;
break;
case '-' :
ret=2;
break;
case '+' :
ret=3;
break;
case '*' :
ret=4;
break;
case '/' :
ret=5;
break;
case '%':
ret=6;
break;
case '^':
ret=7;
}
return ret;
}
void intpush(int item) {
if (inttop==max-1)
cout<<"Stack is full";
else
intstack[++inttop]=item;
}
int intpop() {
if (inttop==-1)
cout<<"stack is empty";
else
return(intstack[inttop--]);
return 0;
}
int postfix_count(char st[]) {
int i,a,b,k,c=1;
for(i=0;st[i];i++)
{
switch(st[i])
{
case '+':
a=intpop();
b=intpop();
intpush(b+a);
break;
case '-':
a=intpop();
b=intpop();
intpush(b-a);
break;
case '*':
a=intpop();
b=intpop();
intpush(b*a);
break;
case '/':
a=intpop();
b=intpop();
intpush(b/a);
break;
case '^':
a=intpop();
b=intpop();
for (k=0;k<a;k++)
c*=b;
intpush(c);
case '%':
a=intpop();
b=intpop();
intpush(b%a);
break;
default:
intpush(st[i]-48);
}
}
return intstack[inttop];
}
با تشكر از دوستان.
:10:
برنامه ای که شما نوشتید اجرا می شه ولی عبارت توان دار رومحاسبه نمیکنه مرتبا پیغام stack is empty رو میده ؟مقدار 30 که در ابتدای برنامه تعریف کردین اندازه ی پشته ست؟ در مورد توابع isunder ,isover,intpush,intpop هم میشه توضیح بدید؟