Skip to content

Instantly share code, notes, and snippets.

@nsssayom
Created February 25, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsssayom/0d35e548606e7a61d1993c85ed180e6e to your computer and use it in GitHub Desktop.
Save nsssayom/0d35e548606e7a61d1993c85ed180e6e to your computer and use it in GitHub Desktop.
Arithmetic infix to postfix
#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