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);
}
با تشکر.
بنده برنامه ای نوشتم که یک عبارت جبری میانوندی دریافت می کند و آن را به عبارت پسوندی تبدیل می کند. سپس با استفاده از عبارت پسوندی به دست آمده و گرفتن مقادیر متغیر ها از ورودی، عبارت را محاسبه می کند.
تبدیل عبارت میانوندی و پسوندی به درستی کار می کند اما محاسبه عبارت پسوندی مشکل دارد. ممنون میشم راهنمایی کنید چه کد هایی باید ویرایش شود.
در ضمن بنده پشته را در این برنامه با آرایه پیاده کردم. اگر مهمه حتما با 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);
}
با تشکر.