Notes
Notes - notes.io |
#include<math.h>
/*** AVAILABLE FUNCTIONS ***/
int isEmpty(Stack*);
void push(Stack*, int);
int pop(Stack*);
int peek(Stack*);
/*** YOUR FUNCTIONS ***/
//Write one function to convert infix expression to postifx expression using stack. Write another function to evaluate a postfix expression
int prec(char c) {
switch(c) {
case '(': return 0;
case '+':;
case '-': return 1;
case '*':;
case '/': return 2;
case '^': return 3;
}
}
void printPostfix(char* expr) {
Stack stk = {NULL};
for(int i = 0;expr[i] ; ++i) {
char c = expr[i];
//TODO: implement the algorithm to convert infix expression to postfix expression using stack based on the value of current input symbol c: if c is opening parenthesis, simply push it on to the stack, if c is an operand just print it, if c is a closing parenthesis pop and print all elements of stack until an opening parenthesis is encountered, otherwise c is an operator and we should pop and print all operators from the stack with equal or higher precedence than c before pushing c itself on the stack
if(c>='0' && c<='9')
printf(" %c ",c);
else if(c == '(')
push(&stk,c);
else if(c == ')'){
while(peek(&stk)!='(')
printf(" %c ",pop(&stk));
pop(&stk);
}
else{
while(!isEmpty(&stk) && prec(peek(&stk))>=prec(c))
printf(" %c ",pop(&stk));
push(&stk,c);
}
//TODO ENDS
}
//TODO: Empty the stack printing any values left in the stack
while(!isEmpty(&stk)){
printf("%c ",pop(&stk));
}
//TODO ENDS
printf("n");
}
int evalPostfix(char* pfx) {
Stack stk = {NULL};
for(int i = 0; pfx[i]; ++i) {
char c = pfx[i];
if(c>='0' && c<='9') push(&stk, c-'0');
else {
//TODO: Evaluate the postfix expression by popping two values from the stack and performing the operation based on the value of c and pushing the result back to the stack
int top = pop(&stk);
int secondTop = pop(&stk);
switch(c){
case '+': push(&stk,secondTop + top); break;
case '-': push(&stk,secondTop - top); break;
case '*': push(&stk,secondTop * top); break;
case '/': push(&stk,secondTop / top); break;
case '^': push(&stk,pow(secondTop,top)); break;
}//very cool
//TODO ENDS
}
}
return peek(&stk);
}
/*** MAIN DRIVER ***/
int main() {
char * expr = "2+5*(3^2-6)^(4+3*2)/9/3-8";
//ASSIGNMENTS
printPostfix(expr);
char * pfx = "2532^6-432*+^*9/3/+8-";
printf("%dn", evalPostfix(pfx));
}
|
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team