#include <stdio.h> #include <stdlib.h> #include <string.h> // Define maximum size for the stack. #define MAX 100 // Struct for the stack. typedef struct { int top; char items[MAX]; } Stack; // Function to initialize the stack. void initStack(Stack *s) { s->top = -1; } // Function to check if the stack is empty. int isEmpty(Stack *s) { return s->top == -1; } // Function to push an element to the stack. void push(Stack *s, char value) { if (s->top < MAX - 1) { s->items[++s->top] = value; } else { printf("Stack is full!\n"); } } // Function to pop an element from the stack. char pop(Stack *s) { if (!isEmpty(s)) { return s->items[s->top--]; } else { return '\0'; // Return null character if stack is empty. } } // Function to check the precedence of operators. int precedence(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; case '%': return 3; case '(': return 0; } return -1; // Return -1 for invalid operators. } // Function to convert infix expression to postfix expression. char* infixToPostfix(char* infix) { static char postfix[MAX]; int i, j=0; Stack s; initStack(&s); for (i = 0; infix[i] != '\0'; i++) { if (infix[i] == '(') { push(&s, infix[i]); } else if (infix[i] == ')') { while (!isEmpty(&s) && s.items[s.top] != '(') { postfix[j++] = pop(&s); } pop(&s); // Remove the opening parenthesis '(' from the stack. } else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '%') { while (!isEmpty(&s) && precedence(infix[i]) <= precedence(s.items[s.top])) { postfix[j++] = pop(&s); } push(&s, infix[i]); } else { postfix[j++] = infix[i]; } } while (!isEmpty(&s)) { postfix[j++] = pop(&s); } postfix[j] = '\0'; // Null terminator for the string. return postfix; } int main() { char P[] = "(A-(B/C)*D+E)*F%G"; char* Q; Q = infixToPostfix(P); printf("Infix expression: %s\n", P); printf("Postfix expression: %s\n", Q); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}