PDA

View Full Version : مشکل در محاسبه عبارت پسوندی



mahdi.manian
پنج شنبه 07 خرداد 1394, 23:05 عصر
با عرض سلام.

بنده برنامه ای نوشتم که یک عبارت جبری میانوندی دریافت می کند و آن را به عبارت پسوندی تبدیل می کند. سپس با استفاده از عبارت پسوندی به دست آمده و گرفتن مقادیر متغیر ها از ورودی، عبارت را محاسبه می کند.

تبدیل عبارت میانوندی و پسوندی به درستی کار می کند اما محاسبه عبارت پسوندی مشکل دارد. ممنون میشم راهنمایی کنید چه کد هایی باید ویرایش شود.

در ضمن بنده پشته را در این برنامه با آرایه پیاده کردم. اگر مهمه حتما با Struct پیاده بشه ممنون میشم تبدیل را برایم انجام دهید چون خودم نتوانستم.

سورس کامل:

// Jabri.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include "iostream"
using namespace std;


//Convert infix to postfix
void polish(char arr[], char res[]);
//Set priority of operators
int priority(char a, char b);
//Detection of the operator
int isOperator(char t);
//Calculation algebraic expression
long value(char arr[]);


int _tmain(int argc, _TCHAR* argv[])
{
char infix[100], postfix[100];
cout<<"Please enter infix value:"<<endl;
cin>>infix;


system("cls");
polish(infix, postfix);


cout<<"Postfix value is: "<<postfix<<endl<<endl<<"Result: "<<value(postfix)<<endl<<endl<<endl;
system("pause");


return 0;
}


void polish(char arr[], char res[]) {
char stack[100];
//Null all of result arrray
for(int i=0; i<100; i++)
res[i]=NULL;
//Define 2 variables for stack end and result end
int s_end=0, r_end=0;
//Add ( and ) to array
int len=strlen(arr);
for(int i=len; i>0; i--)
arr[i]=arr[i-1];
arr[0]='(';
arr[len+1]=')';
len+=2;
arr[len]=0;


for(int i=0; i<len; i++) {
//if element is ( push it to stack
if(arr[i]=='(') {
s_end++;
stack[s_end]= '(' ;
}
//if element is ) pop from stack for each operator
else if(arr[i]==')') {
while( stack[s_end] != '(' ) {
res[r_end] = stack[s_end];
r_end++;
s_end--;
}
}
//if element is operator, add it to result from stack
else if(isOperator(arr[i]) ) {
while( isOperator(stack[s_end] ) && priority( stack[s_end] ,arr[i] ) ) {
res[r_end] = stack[s_end];
r_end++;
s_end--;
}
s_end++;
stack[s_end]= arr[i] ;
}
//Add operands to end of result
else {
res[r_end]=arr[i];
r_end++;
}
}
}


int isOperator(char t) {
if(t =='+' || t =='-' ||t =='*' ||t =='/' || t =='^')
return 1;
else
return 0;
}


int priority(char a, char b) {
if (a=='^') return 1;
if (b=='^') return 0;
if (a=='*' || a=='/') return 1;
if (b=='*' || b=='/') return 0;
return 1;
}


long value(char arr[]) {
long result=0, a=0, b=0;
char stack[100];
//Define variable for end of stack
int s_end=0;

int len=strlen(arr);
for(int i=0; i<len; i++) {
//if element is operand push value to stack
if ( !isOperator(arr[i])) {
cout<<"Please enter "<<arr[i]<<": ";
cin>>a;
s_end++;
stack[s_end]=a;
}
//if element is operator
else {
switch (arr[i])
{
case '+':
b = stack[s_end-1];
result = a + b;
stack[s_end]=result;
break;
case '-':
b = stack[s_end-1];
result = a - b;
stack[s_end]=result;
break;
case '*':
b = stack[s_end-1];
result = a * b;
stack[s_end]=result;
break;
case '/':
b = stack[s_end-1];
result = a / b;
stack[s_end]=result;
break;
case '^':
b = stack[s_end-1];
result = a ^ b;
stack[s_end]=result;
break;
}
}
}
system("cls");
return(result);
}




با تشکر.

rahnema1
شنبه 09 خرداد 1394, 14:56 عصر
سلام تو گوگل جستجو کنید برنامه آماده به زبان ++c براتون میاد مطابق با اونها تصحیح کنید

mahdi.manian
شنبه 09 خرداد 1394, 21:15 عصر
سلام.

چیزی پیدا نکردم. ممنون میشم اگه شما چیزی پیدا کردید لینک بدید.

در ضمن من قصد دارم به جای ساختار برای پشته از آرایه استفاده کنم.


ممنون.

rahnema1
شنبه 09 خرداد 1394, 21:26 عصر
این فقط چند تا
https://gist.github.com/mycodeschool/7867739
http://www.c4learn.com/data-structure/c-program-convert-infix-expression-to-postfix-using-stack
http://www.thecrazyprogrammer.com/2014/02/c-program-and-algorithm-for-conversion-of-an-expression-from-infix-to-postfix.html

mahdi.manian
شنبه 09 خرداد 1394, 21:52 عصر
بنده سوالم کدی برای محاسبه عبارت پسوندی با استفاده از متغیر ها و گرفتن مقدار آن ها از ورودی بود. اما هر این سه لینک که محبت کردید ارسال کردید برای تبدیل عبارت میانوندی به پسوندی بود که بنده قبلا نوشته بودم و نیازی نداشتم.

البته بنده کد محاسبه عبارت پسوندی را هم نوشته ام ولی مشکلاتی دارد که تقاضا می کنم کد بنده بررسی شود که مشکلات آن برای حل مشخص شود.


با تشکر.

rahnema1
شنبه 09 خرداد 1394, 21:57 عصر
شما فقط سوال را بو من صد تا نتیجه برای شما می فرستم
http://www.thecrazyprogrammer.com/2014/02/c-program-and-algorithm-for-evaluation-of-a-postfix-expression.html
http://stackoverflow.com/questions/16372941/evaluate-a-postfix-expression-using-a-stack-and-array-in-c