PDA

View Full Version : خواهش می کنم... کدی ساده برای تبدیلاتinfix postfix prefix



amator
یک شنبه 29 خرداد 1384, 23:05 عصر
باز هم سلام
من فرصت کمی دارم.
لطفا کمک کنید

برنامه پیمایشی تبدیل عبارات infix postfix prefix به یکدیگر به صورت بازگشتی و غیر بازگشتی به ساده ترین شکل و روش

Very Very Tanx

navid_R
دوشنبه 30 خرداد 1384, 08:39 صبح
به بین این به دردت می خوره



/*

Prog. : Prog. to convert Infix To Postfix


*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

struct node
{
char value;
float res;
node *left, *right;
};

int isVariable(char);
void inorder(node *);
void preorder(node *);
void postorder(node *);
float eveluate(node *);
void listexpr(node *);

main()
{
node *temp;
node *stack [20];
char postfix[20];
int top = -1, i = 0;
float result = 0;
clrscr();

cout << "\n Enter the Postfix notation :- ";
cin >> postfix;

while(postfix[i] != '\0')
{
temp = new node;
temp->left = NULL;
temp->right = NULL;
temp->value = postfix[i];

if(!isVariable(postfix[i]))
{
temp->right = stack[top--];
temp->left = stack[top--];
}
stack[++top] = temp;
i++;
}
node *root;
root = stack[top--];

cout << "\n In Order : ";
inorder(root);
cout << "\n Pre Order : ";
preorder(root);
cout << "\n Post Order : ";
postorder(root);

cout << "\n\n";
result = eveluate(root);
listexpr(root);
cout << "\n\n The Result of the Expression is :- " << result;

getch();
return 0;

}

// Function to eveluate the expression and find its result.
float eveluate(node * list)
{
float res = 0;

switch(list->value)
{
case '+' : res = eveluate(list->left);
res += eveluate(list->right);
break;

case '-' : res = eveluate(list->left);
res -= eveluate(list->right);
break;

case '*' : res = eveluate(list->left);
res *= eveluate(list->right);
break;

case '/' : res = eveluate(list->left);
res /= eveluate(list->right);
break;

default : cout << " Enter the value for " << list->value << " :- ";
cin >> res;
list->res = res;
}

return res;
}

// Function to check whether its variable or operator.
int isVariable(char ch)
{
if (
ch != '+' &&
ch != '-' &&
ch != '*' &&
ch != '/' &&
ch != '(' &&
ch != ')'
)
return 1;
else
return 0;
}


// Function to travel In-Orderly (Recursive).
void inorder(node *list)
{
if(list == NULL)
return;

inorder(list->left);

cout << " " << list->value;

inorder(list->right);
}

// Function to travel Post-Orderly (Recursive).
void postorder(node *list)
{
if(list == NULL)
return;

postorder(list->left);

postorder(list->right);

cout << " " << list->value;
}

// Function to travel Pre-Orderly (Recursive).
void preorder(node *list)
{
if(list == NULL)
return;

cout << " " << list->value;

preorder(list->left);

preorder(list->right);
}


// Function to display the values in the expression.
void listexpr(node *list)
{
if(list == NULL)
return;

listexpr(list->left);

if(isVariable(list->value))
cout << " " << list->res;
else
cout << " " << list->value;

listexpr(list->right);
}

/* OUTPUT

Enter the Postfix notation :- ab*cd/+e-

In Order : a * b + c / d - e
Pre Order : - + * a b / c d e
Post Order : a b * c d / + e -

Enter the value for a :- 2
Enter the value for b :- 3
Enter the value for c :- 6
Enter the value for d :- 2
Enter the value for e :- 4
2 * 3 + 6 / 2 - 4

The Result of the Expression is :- 5

*/

H_Ghaffarian
دوشنبه 30 خرداد 1384, 09:24 صبح
سلام
شما اگه یکمی خودتون وقت می گذاشتید و کتابها ساختمان داده رو مطالعه می کردید نیازمند طرح این سئوال نبودید. به هرحال شما دانشجوی کامپیوتر هستید. مگه نه ::نوشتن::

amator
دوشنبه 30 خرداد 1384, 10:21 صبح
نوید جان بابت کمک شما بسیار بسیار تشکر :flower: :reading: :موفق:
خدا خیرت بدهد.


اما H_Ghaffarian عزیز ، بنده زیاد مقصر نیستم، چون اولا یک روز تا دو روز اون هم بین امتحانات وقت دارم برای پروژه تازه تعریف شده.
دوما دانشگاه ما تازه تاسیس و متاسفانه اساتید هم بی تجربهو تازه وارد تر از ما، استاد از جزوه ای به ما تدریس نمود که مواردی چون مرتب سازی و ماتریسها و ... آن تا جلسه آخر طول کشید و موارد مهمی مثل لیست پیوندی و همین infix و... را در جلسه فوق العاده تدریس کرد و جالب آن که از مبحث های ماتریس و مرتب سازی سوالی در امتحان نداده و می گوید آنها جزو سیلابس ها نیستند.
حالا خودت ببین ...
به هر حال از توجه شما هم ممنون.

navid_R
سه شنبه 31 خرداد 1384, 11:49 صبح
خواهش می کنم!