c++ program
#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
int PostfixOperation(string elements);
int Ope(char ope, int ope1, int ope2);
bool Operator(char x);
bool Numvalue(char w);
int main(){
while(1){
int choice;
yy:
cout << " "<<endl;
cout << " "<<endl;
cout<<" compilation of machine problems"<<endl;
cout << " "<<endl;
cout << "1. Operation Elements using Arrays"<<endl;
cout << "2. Postfix Evaluator"<<endl;
cout << "3. Hammer's Query"<<endl;
cout << "4. Binary Search Trees"<<endl;
cout << "5. Bubble Sort"<<endl;
cout << "6. Selection Sort"<<endl;
cout << "7. Insertion Sort"<<endl;
cout << "Enter your choice: ";
cin >> choice;
cout<<" "<<endl;
if(choice==1){
cout<<" Operation Elements using Arrays "<<endl;
string word[100],a[50];
int num,numelem;
xx:
cout<<endl;
cout<<"[1] add elements"<<endl;
cout<<"[2] Modify elements"<<endl;
cout<<"[3] Delete elements"<<endl;
cout<<"[4] Display elements"<<endl;
cout<<"[5] Count elements"<<endl;
cout<<"[6] exit"<<endl;
cin>>num;
cout<<endl;
if(num==1){
cout<<"[1] add elements"<<endl;
cout<<"enter number of elements: ";
cin>>numelem;
for(int x=0;x<numelem;x++){
cin>>word[x];
}
cout<<"<Back to main menu>"<<endl;
goto xx;
}
if(num==2){
int pos;
cout<<"Please enter position: "<<endl;
cin>>pos;
a[50]=word[pos];
cout<<"enter string: ";
cin>>word[pos];
cout<<"modified "<< a[50] <<" to "<<word[pos];
goto xx;
}
if(num==3){
int pos;
string a[90];
cout<<"Please enter position: "<<endl;
cin>>pos;
a[90] = word[pos];
word[pos]= " ";
cout<< a[90]<<" is deleted "<<word[pos];
goto xx;
}
if(num==4){
int a;
string v;
for(int x=numelem;x>=0;x--){
cout<<word[x]<<" ";
}
goto xx;
}
if(num==5){
int a;
a = numelem;
for(int x=0;x<numelem;x++){
if(word[x]==" "){
a--;
}
}
cout<<"there are "<< a << " elements";
goto xx;
}
if(num==6){
goto yy;
}
}
else if(choice==2){
string elements;
cout<<"Enter elements: "<<endl;
getline(cin,elements);
int Output = PostfixOperation(elements);
cout<<"Output = "<<Output<<endl;
}
}
}
int PostfixOperation(string elements){
stack<int> mystack;
for(int i = 0;i< elements.length();i++) {
if(elements[i]==' '||elements[i]==',')
{
continue;
}
else if(Operator(elements[i])) {
int ope2 = mystack.top(); mystack.pop();
int ope1 = mystack.top(); mystack.pop();
int result = Ope(elements[i], ope1, ope2);
mystack.push(result);
}
else if(Numvalue(elements[i])){
int ope = 0;
while(i<elements.length() && Numvalue(elements[i])) {
ope = (ope*10) + (elements[i] - '0');
i++;
}
i--;
mystack.push(ope);
}
}
return mystack.top();
}
bool Numvalue(char w)
{
if(w >= '0' && w <= '9')
return true;
return false;
}
bool Operator(char x)
{
if(x =='+'|| x =='*')
return true;
return false;
}
int Ope(char ope, int ope1, int ope2)
{
if(ope == '+') return ope1 + ope2;
else if(ope == '*') return ope1 * ope2;
else cout<<"Invalid "<<endl;
return 0;
}
Public Last updated: 2017-11-19 11:36:01 AM
