Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please help me retrenchment my project or change other form by java. #include #include #include #include #include using namespace std; const ostream& stackTrace(ostream& out, stack
please help me retrenchment my project or change other form by java.
#include
#include
#include
#include
#include
using namespace std;
const ostream& stackTrace(ostream& out, stack callStack){
while(callStack.size() > 0){
out << callStack.top() << endl;
callStack.pop();
}
return out;
}
int main(int argc, char **argv) {
string line;
string command;
string functionName;
string poppedFunction;
bool errorDetected = false;
bool errorNoFunction = false;
bool errorWrongFunction = false;
stack callStack;
size_t lineNumber = 0;
size_t maximumDepth = 0;
while (getline(cin, line) && errorDetected == false) {
lineNumber++;
stringstream parser(line);
parser >> command >> functionName;
//add the function name to the stack
if(command == "call"){
callStack.push(functionName);
}
else if(command == "return"){
//check to see if the maxiumum depth of the stack has been reached
if(callStack.size() > maximumDepth){
maximumDepth = callStack.size();
}
//check to make sure the stack isnt empty
if(callStack.size() == 0){
errorDetected = true;
errorNoFunction = true;
}
//check to make sure the function name matches before popping
else if(callStack.top() != functionName){
errorDetected = true;
errorWrongFunction = true;
}
//pop the stack
else{
callStack.pop();
}
}
else{
errorDetected = true;
}
//clear the stringstream for the next command
parser.clear();
}
if(errorNoFunction){
cout << "Invalid trace at line " << lineNumber << endl;
cout << "Returning from " << functionName
<< " but no functions are currently being called" << endl;
cout << "Stack trace:" << endl;
//do a stack trace
stackTrace(cout,callStack);
}
else if(errorWrongFunction){
cout << "Invalid trace at line " << lineNumber << endl;
cout << "Returning from " << functionName << " instead of "
<< callStack.top() << endl;
cout << "Stack trace:" << endl;
//do a stack trace
stackTrace(cout,callStack);
}
else if((callStack.size() > 0)){
cout << "Invalid trace at line " << lineNumber << endl;
cout << "Not all functions returned" << endl;
cout << "Stack trace:" << endl;
//do a stack trace
stackTrace(cout,callStack);
}
else{
cout << "Valid trace" << endl;
cout << "Maximum call depth was " << maximumDepth << endl;
}
return 0;
}
please help me make the code shorter and clearer,
Thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started