ASSIGNMENT 5 CS 392 : TCPTUTOR

12
785

PROBLEM STATEMENT

Write a C program to transform an Infix Expression into its equivalent Postfix expression.

ALGORITHM

Let Q be an arithmetic expression written in infix notation. The algorithm uses a stack to temporarily hold operators and left parenthesis. The postfix expression P will be constructed from left to right using operands from Q and the operators which are removed from the stack.

Step 1: Push ‘(’ on to the stack and add ‘)’ to the end of Q.

Step 2: Scan Q left to right and repeat steps 3 to 6 for each element in Q until Q is empty.

Step 3: If an operand is encountered, add it to P.

Step 4: If a left parenthesis is encountered, push it onto stack.

Step 5: If an operator  is encountered then:

a) Repeatedly pop from stack and add to P each operator (on top of stack) which has the same precedence or higher than operator.

b) Add operator to stack.

[end of if]

Step 6: If a right parenthesis is encountered:

a) Repeatedly pop from stack and add to P each operator until a ‘(’ is encountered.

b) Remove left parenthesis ‘)’.

 

SOURCE CODE

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define SIZE 50
char arr[SIZE];
int top=-1;
int pre(char s)
{
switch(s)
{
case ‘^’:
return 5;
case ‘*’:
case ‘/’:
return 4;
case ‘+’:
case ‘-‘:
return 3;
default:
return 1;
}
}
void push(char c)
{
arr[++top]=c;
}
char pop()
{
return arr[top–];
}
void main()
{
int i,j,l;
char ch, infix[SIZE],postfix[SIZE];
j=0;
printf(“Enter Infix expression:”);
fflush(stdin);
gets(infix);
l=strlen(infix);
for(i=0;i<l;i++)
{
ch=infix[i];
switch(ch)
{
case ‘(‘:
push(ch);
break;
case ‘)’:
do
{
postfix[j++]=pop();
}
while(arr[top]!='(‘);
pop();
break;
case ‘^’:
case ‘*’:
case ‘/’:
case ‘+’:
case ‘-‘:
while(pre(arr[top])>=pre(infix[i]))
{
postfix[j++]=pop();
}
push(infix[i]);
break;
default:
postfix[j++]=infix[i];
}
}
postfix[j]=’\0’;
printf(“%s”,postfix);
}

OUTPUT

Enter Infix expression : A+(B*C-(D/E^F)*G)*H

ABC*DEF^/G*-H*+

***********************

12 COMMENTS

  1. CLINIQUE 倩碧線上購物官網。瀏覽Clinique倩碧官方網站,了解更多線上購物、護膚、彩粧、香氛及禮品詳情。通過過敏性測試,百分百不含香料。

  2. 防曬粉 BB粉底霜 亮麗柔滑打底乳液 亮麗柔滑控油打底乳液 有機幹細胞修護CC霜 潤澤防曬底霜 礦物質潤澤慕斯 礦物質奇幻粉餅 礦物質蜜粉 完美礦物粉底 控油定妝蜜粉 高清控油粉餅 控油蜜 保濕滋潤噴霧 有機抗氧爽膚噴霧 有機抗敏保濕

  3. Vol.91 越睡越美麗 我的夜間修護法寶 化妝品排行榜特別報導 睡眠不僅是生理需求,也是修護肌膚最佳時機!本期特別排行嚴選修護…,在排行榜特別報導中可以看到關於Vol.91 越睡越美麗 我的夜間修護法寶的10個編輯部精選商品和網友相關心得。

  4. I just want to tell you that I am just newbie to blogs and definitely liked your web page. Most likely I’m planning to bookmark your website . You amazingly come with perfect well written articles. Kudos for sharing your web-site.

  5. JUVEDERM由透明質酸Hyaluronic Acid(一種天然多醣體)製造而成,並具有幼滑及高凝聚力的配方。透明質酸(HA)是皮膚組織的主要成份之一,能自然存在於人體肌膚內 JUVEDERM所用的透明質酸非由動物身體提取,可鎖住水份保濕皮膚,從而增加皮膚密度並改善質感 Juvederm 玻尿酸 加強優化面部輪廓,可被身體完全吸引,能自然地修飾面部輪廓 功效可長達24個月以上 JUVEDERM的特點: 效果立即可見 非永久性 非手術性 安全有效 效果自然 JUVEDERM獲歐盟(CE)及美國及藥物管理局(FDA)認證 首先及唯一獲得FDA認證在首次療程後能維持長達一年2-4功效 新世代專員Hylacross科技為產品帶來獨特的物理特質,包括凝聚力、支撐力及柔順度 這是一套由全球著名醫學美容醫生Dr. Maurício de Maio,以JUVÉDERM®系列透明質酸產品為基礎而研發的面部優化療程,藉著簡單程序便達致面部優化效果,不需進行手術,減低風險。

  6. 出門戰鬥格!這些女星都有出席#PFW Marie Claire (HK) Edition 不少女明星都視出席時裝周為衝出國際的途徑,如果成功被外國傳媒影到兼登上它們的網站(其實instagram已經滿足),此行已經完滿。 因此她們由上機、到埗、出街食個早餐都會著得靚一靚,但求顯示自己是f

LEAVE A REPLY

Please enter your comment!
Please enter your name here