Created
February 25, 2019 16:28
-
-
Save nsssayom/0d35e548606e7a61d1993c85ed180e6e to your computer and use it in GitHub Desktop.
Arithmetic infix to postfix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
char input[]="9+8*2/6"; | |
char* head = input; | |
void expr(); | |
void match(char ch); | |
void exprPrime(); | |
void term(); | |
void factor(); | |
int main(){ | |
expr(); | |
} | |
void expr(){ | |
term(); | |
exprPrime(); | |
} | |
void match(char ch){ | |
if (ch == *head){ | |
head++; | |
} | |
} | |
void exprPrime(){ | |
if (*head == '+'){ | |
match('+'); | |
term(); | |
cout << "+"; | |
exprPrime(); | |
} | |
else if (*head == '-'){ | |
match('-'); | |
term(); | |
cout << "-"; | |
exprPrime(); | |
} | |
else{ | |
term(); | |
} | |
} | |
void term(){ | |
if (*head=='*'){ | |
match('*'); | |
factor(); | |
cout << "*"; | |
term(); | |
} | |
else if (*head=='/'){ | |
match('/'); | |
factor(); | |
cout << "/"; | |
term(); | |
} | |
else{ | |
factor(); | |
} | |
} | |
void factor(){ | |
if ( isdigit(*head)){ | |
cout << *head; | |
head++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment