Skip to main content

Posts

Showing posts from September, 2020

Any expression can be represented using three types of expressions (Infix, Postfix and Prefix). We can also convert one type of expression to another type of expression like Infix to Postfix, Infix to Prefix, Postfix to Prefix and vice versa. To convert any Infix expression into Postfix or Prefix expression we can use the following procedure… Find all the operators in the given Infix Expression. Find the order of operators evaluated according to their Operator precedence. Convert each operator into required type of expression (Postfix or Prefix) in the same order. To convert Infix Expression into Postfix Expression using a stack data structure, We can use the following steps... Read all the symbols one by one from left to right in the given Infix Expression. If the reading symbol is operand, then directly print it to the result (Output). If the reading symbol is left parenthesis '(', then Push it on to the Stack. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left parenthesis is poped and print each poped symbol to the result. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first pop the operators which are already on the stack that have higher or equal precedence than current operator and print them to the result.

 #include<stdio.h> #include<stdlib.h> void evaluate(); void push(char); char pop(); int prec(char); char infix[30], postfix[30], stack[30]; int top = -1; void main() {             printf("\nEnter the valid infix expression:\t");             scanf("%s", infix);             evaluate();             printf("\nThe entered infix expression is :\n %s \n", infix);             printf("\nThe corresponding postfix expression is :\n %s \n", postfix); } void evaluate() {             int i = 0, j = 0;             char symb, temp;             push('#');             for(i=0; infix[i] != '\0'; i++)             {                       ...

Shyam has a lot of books messed on the floor. Therefore, he wants to pile up the books that still have some remaining exercises into a single pile. He will grab the books oneby-one and add the books that still have remaining exercises to the top of the pile. Whenever he wants to do a book exercise, he will pick the book with the minimum number of remaining exercises from the pile. In order to pick the book, he has to remove all the books above it. Therefore, if there are more than one books with the minimum number of remaining exercises, he will take the one which requires the least number of books to remove. The removed books are returned to the messy floor. After he picks the book, he will do all the remaining exercises and trash the book. Since number of books is rather large, he needs your help to tell him the number of books he must remove, for picking the book with the minimum number of exercises. Whenever he wants to do a book exercise, there is at least one book in the pile. Implement a program for this scenario

 #include<iostream> using namespace std; class Books { public:  string subName[50]; int subEx[50], num; void readData() { int i; cout << "Enter number of books: "; cin >> num; for(i=0; i<num; i++) { cout << "Enter book name: "; cin >> subName[i]; cout <<"Enter number of exercises in " << subName[i] << ": "; cin >> subEx[i]; } } void sortStack() { string tempName; int i, j, tempEx; for(i=0; i<num-1; i++) { for (j=0; j<num-i-1; j++) { if(subEx[j] > subEx[j+1]) { tempEx = subEx[j]; subEx[j] = subEx[j+1]; subEx[j+1] = tempEx; tempName = subName[j]; subName[j] = subName[j+1]; subName[j+1] = tempName; } } } } void displayStack() { int j; for(j=0;j<num;j++) cout<<"\nRemaining exercies in "...

Total Pageviews