
please is there another to answer this
Code
#include<iostream>
#include<cmath>
#include<stack>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
bool isNumber(string line);
void evolutePostfixExpr(string,ostream&);
int operation(int , int , string);
int main()
{
ifstream inFile;
ofstream outFile;
string filename;
string expression;
int choice;
cout << "Enter the file name: ";
cin >> filename;
inFile.open(filename);
cout << "\n1. Ouput on console." << endl;
cout << "2. Ouput on file." << endl<<"Your choice: ";
cin >> choice;
if (choice == 2)
{
cout << "\nEnter the output file name: ";
cin >> filename;
outFile.open(filename);
cout << "\nWrite to " << filename << endl;
}
while (!inFile.eof())
{
getline(inFile, expression);
if (choice == 2)
evolutePostfixExpr(expression,outFile);
else
evolutePostfixExpr(expression, cout);
}
return 1;
}
void evolutePostfixExpr(string expr,ostream &out)
{
stack<int> stk;
string value,errorMsg;
bool invalid = false;
stringstream ss(expr);
int ans = 0;
int a, b;
while (ss >> value)
{
if (isNumber(value))
stk.push(stoi(value));
else if (value == "+" || value == "-" || value == "*" || value == "/")
{
if (stk.empty() || stk.size() == 1)
{
invalid = true;
errorMsg = "To many operators";
break;
}
a = stk.top();
stk.pop();
b = stk.top();
stk.pop();
ans = operation(a, b, value);
stk.push(ans);
}
else
{
invalid = true;
errorMsg = "Illegal Operation";
break;
}
}
if (invalid)
{
out.width(30); out << left << expr;
out << errorMsg << endl;
return;
}
if (stk.size() > 1)
{
invalid = true;
errorMsg = "To few operators.";
}
if (invalid)
{
out.width(30); out << left << expr;
out << errorMsg << endl;
return;
}
else
{
out.width(30); out << left << expr;
out << errorMsg << stk.top()<<endl;
}
}
bool isNumber(string s) {
if (s.size() == 0) return false;
for (int i = 0; i<s.size(); i++) {
if ((s[i] >= '0' && s[i] <= '9') == false) {
return false;
}
}
return true;
}
int operation(int a, int b, string op) {
//Perform operation
if (op == "+")
return b + a;
else if (op == "-")
return b - a;
else if (op == "*")
return b * a;
else if (op == "/")
return b / a;
return 0;
}


Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- // FILE: DPQueue.h// CLASS PROVIDED: p_queue (priority queue ADT)//// TYPEDEFS and MEMBER CONSTANTS for the p_queue class:// typedef _____ value_type// p_queue::value_type is the data type of the items in// the p_queue. It may be any of the C++ built-in types// (int, char, etc.), or a class with a default constructor, a// copy constructor, an assignment operator, and a less-than// operator forming a strict weak ordering.//// typedef _____ size_type// p_queue::size_type is the data type considered best-suited// for any variable meant for counting and sizing (as well as// array-indexing) purposes; e.g.: it is the data type for a// variable representing how many items are in the p_queue.// It is also the data type of the priority associated with// each item in the p_queue//// static const size_type DEFAULT_CAPACITY = _____// p_queue::DEFAULT_CAPACITY is the default initial capacity of a// p_queue that is created by the default…arrow_forwardC PROGRAMMING Instructions: Use the code template below to implement the length() and reverse() functions into the main() /* This program will create a string which is the reverse of an original string using pointers. * Performs string handling, manipulates pointers, passes pointers to functions. */ #include <stdio.h>#include <stdlib.h> int length( ){ while(str1[len++] != '\0'); // Calculate length of string len--; // Remove the null character len--; // Array index start from 0 to (length -1) return ; } void reverse( ){ while (counter < len) { i = str1[counter]; str1[counter] = str1[len]; str1[len] = i; counter++; len--; } return ;} int main (){ char str1[41]; // String to be Reversed printf("Enter some text (No spaces and 40 characters max): "); scanf("%40s", str2); printf("Reversed string: %s\n", str1); return 0;}arrow_forward>> IN C PROGRAMMING LANGUAGE ONLY << COPY OF DEFAULT CODE, ADD SOLUTION INTO CODE IN C #include <stdio.h>#include <stdlib.h>#include <string.h> #include "GVDie.h" int RollSpecificNumber(GVDie die, int num, int goal) {/* Type your code here. */} int main() {GVDie die = InitGVDie(); // Create a GVDie variabledie = SetSeed(15, die); // Set the GVDie variable with seed value 15int num;int goal;int rolls; scanf("%d", &num);scanf("%d", &goal);rolls = RollSpecificNumber(die, num, goal); // Should return the number of rolls to reach total.printf("It took %d rolls to get a \"%d\" %d times.\n", rolls, num, goal); return 0;}arrow_forward
- C++ programming task. main.cc file: #include <iostream>#include <map>#include <vector> #include "plane.h" int main() { std::vector<double> weights{3.2, 4.7, 2.1, 5.5, 9.8, 7.4, 1.6, 9.3}; std::cout << "Printing out all the weights: " << std::endl; // ======= YOUR CODE HERE ======== // 1. Using an iterator, print out all the elements in // the weights vector on one line, separated by spaces. // Hint: see the README for the for loop syntax using // an iterator, .begin(), and .end() // ========================== std::cout << std::endl; std::map<std::string, std::string> abbrevs{{"AL", "Alabama"}, {"CA", "California"}, {"GA", "Georgia"}, {"TX", "Texas"}}; std::map<std::string, double> populations{ {"CA", 39.2}, {"GA", 10.8}, {"AL", 5.1}, {"TX", 29.5}}; std::cout <<…arrow_forwardhere is a and b part plz solve c part #include <iostream>#include <string.h>#include <stdlib.h>#include <sstream>#include <cmath>using namespace std; int main(){ /*Variable declarations*/int higestDegree,higestDegree1;int diffDegree,smallDegree;std::stringstream polynomial,addpolynomial;/*prompt for degree*/std::cout<<"Please enter higest degree of polynomial"<<std::endl;std::cin>>higestDegree; std::cout<<"Please Enter highest degree of 2nd polynomial"<<std::endl;std::cin>>higestDegree1;int *cofficient=new int[higestDegree+1];//cofficient array creation for 1st polynomailint *cofficient1=new int[higestDegree1+1];//cofficient array creation for 2st polynomail /*taking cofficient for 1st polynomial*/std::cout<<"Please enter cofficient of each term from higest degree to lowest for 1st polynomial"<<std::endl;for(int i=0;i<=higestDegree;i++){std::cin>>cofficient[i];}/*taking cofficient for 2st…arrow_forwardplease answerarrow_forward
- #include <bits/stdc++.h>using namespace std;int main() { double matrix[4][3]={{2.5,3.2,6.0},{5.5, 7.5, 12.6},{11.25, 16.85, 13.45},{8.75, 35.65, 19.45}}; cout<<"Input no in first row of matrix"<<endl; for(int i=0;i<3;i++){ double t; cin>>t; matrix[0][i]=t; } cout<<"Contents of the last column in matrix"<<endl; for(int i=0;i<4;i++){ cout<<matrix[i][2]<<" "; } cout<<"Content of first row and last column element in matrix is: "<<matrix[0][3]<<endl; matrix[3][2]=13.6; cout<<"Updated matrix is :"<<endl; for(int i=0;i<4;i++){ for(int j=0;j<3;j++){ cout<<matrix[i][j]<<" "; }cout<<endl; } return 0;} Please explain this codearrow_forwardExpression Conversion Design a program which can tansfer an infix expression into a postfix expression and compute its result. Suppose the infix expression only includes’ *’, ‘/’, ‘+’, ‘-‘, ‘(‘, ‘)’ and the numbers are all integers. [Basic Requirements] 1) You are required to use stack. 2) The infix expression is inputted from keyboard as a string. If the input is legal, please convert the infix expression into postfix expression, calculate the result, and finally output the postfix expression and its value. 3) If the infix expression entered is illegal, the program can prompt the user to input incorrectly and prompt the user to re-enter. Please do the program according to the requirements above and it should be in C language...also after the code i want a report that the algorithm explanations of the code plesse don't just copy other student program and give me back please please pleasearrow_forwardThe destination of a function's return value may be represented as a sequence of instructions. When making modifications to the stack, keep in mind that they must not prevent the method from returning to its caller.arrow_forward
- #include <iostream>#include <pthread.h>#include <stdlib.h> #define TOTAL_THREADS 4 int count;pthread_mutex_t the_mutex; // phread mutex variable - initialize here if using the initializer macro void* myFunction(void* arg){int actual_arg = *((int*) arg);for(unsigned int i = 0; i < 10; ++i) {// TODO:// Use a Pthread mutex to control// access to the critical region. // Beginning of the critical regioncount++;std::cout << "Thread #" << actual_arg << " count = " << count << std::endl; // End of the critical region// TODO:// Relinquish access to the Pthread mutex// since critical region is complete. // Random wait - This code is just to ensure that the threads// show data sharing problemsint max = rand() % 100000;for (int x = 0; x < max; x++);// End of random wait code}pthread_exit(NULL);} int main(){int rc[TOTAL_THREADS];pthread_t ids[TOTAL_THREADS];int args[TOTAL_THREADS];// TODO: Initialize the pthread mutex here if using the…arrow_forwardLanguage/Type: C++ binary trees pointers recursion Write a function named hasPath that interacts with a tree of BinaryTreeNode structures representing an unordered binary tree. The function accepts three parameters: a pointer to the root of the tree, and two integers start and end, and returns true if a path can be found in the tree from start down to end. In other words, both start and end must be element data values that are found in the tree, and end must be below start, in one of start's subtrees; otherwise the function returns false. If start and end are the same, you are simply checking whether a single node exists in the tree with that data value. If the tree is empty, your function should return false. For example, suppose a BinaryTreeNode pointer named tree points to the root of a tree storing the following elements. The table below shows the results of several various calls to your function: 67 88 52 1 21 16 99 45 Call Result Reason hasPath(tree, 67, 99) true path exists…arrow_forwardAll files are included below, seperated by the dashes. "----" //driver.cpp #include <iostream> #include <string> #include "stackLL.h" #include "queueLL.h" #include "priorityQueueLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLLstk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout<<"Popping: "<<stk.pop() <<endl; cout<<"Popping: "<<stk.pop() <<endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout<<"Popping: "<<stk.pop() <<endl; } // output order: 11,2,23,19,17,3,7,13,5 stackLLstkx; stkx.push(5); stkx.push(10); stkx.push(15); stkx.push(20); stkx.push(25); stkx.push(30); stkx.insertAt(-100, 3); stkx.insertAt(-200, 7); stkx.insertAt(-300, 0); //output order: -300,30,25,20,-100,15,10,5,-200 while( ! stkx.empty() ) cout<<"Popping: "<<stkx.pop() <<endl; ///////////////////////////////////////…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





