Infix to Postfix to Answer Program (thought process right?) - c++

I'm aware that there's already stuff here regarding this but I'm just posting to check if my logic lines up with my code (if I'm thinking about this whole infix Postfix thing the way I should be).
The code I've seen regarding this topic on this site look a little different from mine. I'm kind of a novice at C++. Anyway, my code so far looks like it is just fine and should work just fine but it's not working the way it should. I've included my thought process as comments in the code below. Please let me know if what I'm thinking is okay.
Here's the code:
#include <iostream>
#include <stack>
#include <sstream>
#include <string>
using namespace std;
int priority (string item)
{
int prior = 0;
if (item == "(")
{
prior = 1;
}
if ((item == "+") || (item == "-"))
{
prior = 2;
}
else if ((item == "*") || (item == "/"))
{
prior = 3;
}
else
{
prior = 4;
}
return prior;
}
int main()
{
stack<string> st;
string output;
cout << "Welcome. This program will calculate any arithmetic expression you enter" << endl;
cout << "Please enter an arithmetic expression below: " << endl;
char line[256];
cin.getline(line, 256);
string exp;
exp = line;
cout << "This is the expression you entered: ";
cout << exp << endl;
string item;
istringstream iss(exp);
iss >> item;
// While there are still items in the expression...
while ( iss )
{
// If the item is a number
if (isdigit('item') == true)
{
output = output + " " + item;
}
// If the stack is empty
else if (st.size() == 0)
{
st.push(item);
}
// If the item is not a number
else if (isdigit('item') == false)
{
// convert that item in the expression to a string
atoi(item.c_str());
int prior1 = 0;
int prior2 = 0;
// If that string is a LEFT PARENTHESIS
if (item == "(")
{
st.push(item);
}
// If that string is a RIGHT PARENTHESIS
else if (item == ")")
{
while ((st.empty() == false) && (st.top() != "("))
{
output = st.top() + " ";
st.pop();
}
st.pop();
}
else
{
// store what is returned from "priority" in "prior1"
prior1 = priority(item);
// pass item on top of the stack through "priority"
// store that in "prior2;
prior2 = priority(st.top());
// while loop here, while item has a higher priority...
// store numbers in a variable
while (prior1 > prior2)
{
output = output + " " + st.top();
st.pop();
prior2 = priority(st.top());
}
}
}
}
while (st.empty() == false)
{
output = st.top() + " ";
st.pop();
}
cout << "Here's the postfix: " << output << endl;
}
This is for evaluating postfix to an actual answer:
// EVALUATE POSTFIX
stack<int> st2;
string output2;
istringstream iss2(exp);
iss2 >> item;
while (iss)
{
// If the item is a number
if (isdigit('item') == true)
{
st.push(item);
}
if (isdigit('item') == false)
{
// store item in a variable
// pop another item off the stack
// store that in a variable.
// apply operator to both.
// push answer to the top of the stack.
int m = 0;
int n = 0;
int total = 0;
m = st2.top();
st2.pop();
n = st2.top();
st2.pop();
if (item == "+")
{
total = m + n;
st2.push(total);
}
else if (item == "-")
{
total = m - n;
st2.push(total);
// add the thing you popped of the stack with the first thing on the stack
}
else if (item == "*")
{
total = m * n;
st2.push(total);
}
else if (item == "/")
{
total = m / n;
st2.push(total);
}
}
}
cout << "Here's your answer: " << st2.top() << endl;
}
Am I thinking about this whole infix-postfix thing the way I should be? Am I on track to solving this problem? My apologies if this question seems simple. I'm kind of a novice at this. Thank you very much.

Related

How to debug the error message "abort() has been called"?

There is no red line under the code. When I start without debugging, after I entered the email, a failure is popped up, "abort() has been called". How can I debug this?
#include <iostream>
#include <string>
using namespace std;
int main() {
string email;
bool good = false;
while (good == false)
{
there:
cout << "Email is ";
cin.ignore();
getline(cin, email);
cout << email.length();
int a;
for (int q = 0; q <= email.length(); q++)
{
char x = email.at(q);
int count = 0;
while (x == '#')
{
a = 1;
count++;
}
if (count > 1)
{
a = 0;
}
}
if ((email.at(0) == '#') || (email.at(email.length()) == '#') || (a == 0))
{
cout << "Input is invalid. One character ‘#’ must be found. Moreover, there must be some characters before and after the character ‘#’." << endl;
goto there;
}
else
{
good = true;
}
}
}
I see a lot of problems in your code. First check my comment for one about your for-loop.
Next, in your for loop, you while-loop will be an infinite loop if the first character is indeed '#', as you would never break out of it.
while (x == '#')
{
a = 1;
count++;
}

C++ Mathematical Expression Parser Issue

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include "NodeType.h"
using namespace std;
// Test if token is an operator
bool isOperator(char token);
int getPrecedence(char token);
bool comparePrecedence(char tokenA, char tokenB);
int main()
{
stack<char> tokenStack;
queue<char> tokenQueue;
string expression= "", postfix= "";
char x;
cout<<"Please enter a mathematical expression: "<<endl;
getline(cin, expression);
cout<<expression.length()<<endl;
for(int i = 0; i <= expression.length(); i++)
{
x = expression[i];
if(isdigit(x))
{
tokenQueue.push(x);
}
if(isOperator(x))
{
while((!tokenStack.empty()) && (comparePrecedence(x, tokenStack.top() == true)))
{
char z = tokenStack.top();
tokenQueue.push(z);
tokenStack.pop();
}
tokenStack.push(x);
}
if(x == '(')
{
tokenStack.push(x);
}
if(x == ')')
{
while((!tokenStack.empty()) && (tokenStack.top() != '('))
{
char z = tokenStack.top();
tokenQueue.push(z);
tokenStack.pop();
}
tokenStack.pop();
}
while(!tokenStack.empty())
{
char z = tokenStack.top();
tokenQueue.push(z);
tokenStack.pop();
}
}
return 0;
}
int getPrecedence(char token)
{
if((token == '+') || (token == '-'))
{
return 1;
}
else if((token == '*') || (token == '/'))
{
return 2;
}
else if ((token == '(') || (token == ')'))
return 0;
else
return 99;
}
// Test if token is an operator
bool isOperator(char token)
{
return token == '+' || token == '-' ||
token == '*' || token == '/';
}
bool comparePrecedence(char tokenA, char tokenB)
{
if(getPrecedence(tokenA) < getPrecedence(tokenB))
return true;
else
return false;
}
For some reason I cannot get my code to work correctly. It always throws a
Thread 1: EXC_BAD_ACCESS (code=EXC_1386_GPFLT) error. It is also not correctly placing the '+' sign when I test using a simple string such as: (3+4).
The Queue should look like: 34+ but it hold 3+4. It seems to me that the '+' operator never gets pushed onto the stack. Can anyone please help me find what I should be focussing my attention on?
Debugging code is a valuable skill to learn, it's my opinion that it should form a much more important part of curricula in schools.
For example, if you modify your code to output all the stack and queue operations thus:
int main()
{
stack<char> tokenStack;
queue<char> tokenQueue;
string expression= "", postfix= "";
char x;
cout<<"Please enter a mathematical expression: "<<endl;
getline(cin, expression);
cout<<expression.length()<<endl;
for(int i = 0; i <= expression.length(); i++)
{
x = expression[i];
if(isdigit(x))
{
tokenQueue.push(x);
cout << "qpush A " << x << '\n';
}
if(isOperator(x))
{
while((!tokenStack.empty()) && (comparePrecedence(x, tokenStack.top() == true)))
{
char z = tokenStack.top();
tokenQueue.push(z);
cout << "spop G " << z << '\n';
cout << "qpush B " << z << '\n';
tokenStack.pop();
}
tokenStack.push(x);
cout << "spush E " << x << '\n';
}
if(x == '(')
{
tokenStack.push(x);
cout << "spush F " << x << '\n';
}
if(x == ')')
{
while((!tokenStack.empty()) && (tokenStack.top() != '('))
{
char z = tokenStack.top();
tokenQueue.push(z);
cout << "spop H " << z << '\n';
cout << "qpush C " << z << '\n';
tokenStack.pop();
}
cout << "spop I " << tokenStack.top() << '\n';
tokenStack.pop();
}
while(!tokenStack.empty())
{
char z = tokenStack.top();
tokenQueue.push(z);
cout << "spop J " << z << '\n';
cout << "qpush D " << z << '\n';
tokenStack.pop();
}
}
return 0;
}
and run it with a simple 3+4, you'll see the following output:
qpush A 3
spush E +
spop J +
qpush D +
qpush A 4
So you are placing the operation on the stack. However, you later take it off the stack and put it on the queue before you place the next digit on the queue.
That's definitely the wrong order but, if you examine the code, it's not just a small snippet that has two lines in the wrong order (that would be too easy).
The code that's doing that transfer from stack to queue is the final while loop in main() which, after every single character, transfers all the items from the stack to the queue, effectively rendering your stack superfluous.
That's where you should be looking but I'll give you a clue. You don't want to be transferring stack to queue after every character, only for those that involve numbers.
There may well be other problems after you solve that one but that method (debugging output every time you do something important) should be able to give you enough information to fix whatever comes along.

probably wrong if statement, not moving to next row after input

When I run my program, I have to type how many rows do I want in my output. I have a limit from 1 to 100 rows. Each row is a task with a name of the task followed by increasing number, example: Task1:, Task2, .... When I type something into input, it must convert input string /see the code below - except the code in main();/.
My problem is that when I type first input, it should go to next task/next row/ but it doesnt. I type for example 10 strings but they dont go each to next task but they stay in one task..hope you understand now.
#include<iostream>
#include<string>
#include <ctype.h>
using namespace std;
void Convert(string input){
string output = "";
string flag = "";
bool underscore = false;
bool uppercase = false;
if ( islower(input[0]) == false){
cout << "Error!" <<endl;
return;
}
for (int i=0; i < input.size(); i++){
if ( (isalpha( input[i] ) || (input[i]) == '_') == false){
cout << "Error!" <<endl;
return;
}
if (islower(input[i])){
if (underscore){
underscore = false;
output += toupper(input[i]);
}
else
output += input[i];
}
else if (isupper(input[i])){
if (flag == "C" || uppercase){
cout << "Error!"<<endl;
return;
}
flag = "Java";
output += '_';
output += tolower(input[i]);
}
else if (input[i] == '_'){
if (flag == "Java" || underscore){
cout << "Error!" <<endl;
return;
}
flag = "C";
underscore = true;
}
}
cout << output <<endl;
}
int main(){
const int max = 100;
string input;
int pocet_r;
cout << "Zadaj pocet uloh:" << endl;
cin >> pocet_r;
if(pocet_r >= 1 && pocet_r <=100)
{
for (int i = 0; i <pocet_r; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
while (cin >> input)
Convert (input);
while(input.size() > max)
cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
while(input.size() > max)
cin >> input;
while (cin >> input)
Convert(input);
}
}else{
cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}
Your first if in Convert will always fail on a non-underscore and return. I don't think that's what's intended. Agree with other answer on the while cin loop. The next two whiles should be if's apparently. Step thru this code with a debugger and watch it line by line and see where it fails. You've got multiple issues here, and I'm not entirely sure what the intent is.
Edit - I didn't parse the extra parenthesis correctly. The first if in convert is actually okay.

My mathematical expression converter doesn't print correctly

So, I am writing a program which takes the input (a mathematical expression) and converts it into postfix notation and then print it out to the user. When I perform this program on simple mathematical expressions (i.e. 5 + 1 - 3 * 2) it works great. The problem is when I include parentheses. It prints all of the mathematical operators on the last line instead of their proper place. I need the mathematical operators to be in their proper spot. (i. e. 5 1 + 3 - 2 *). Any suggestions?
The real output of this statement 5 - ( 2 * 3 ) + 5 is 5 2 3 5 * - + I need it to look like 5 2 3 * - 5 +
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;
int priority(string item) //prioritizing the operators
{
if(item == "(" || item == ")")
{
return 0;
}
else if(item == "+" || item == "-")
{
return 1;
}
else if(item == "/" || item == "*")
{
return 2;
}
}
void welcome()
{
cout << "blah blah!" << endl;
}
int main()
{
welcome(); // welcome text
stack <string> myStack;
char line[256];
cin.getline( line, 256);
string exp = line;
string item;
string output = "";
istringstream iss(exp);
iss >> item;
while(iss)
{
if(item != "+" && item != "-" && item != "/" && item != "*" && item != "(" && item != ")") //If the char is a number
{
cout << item << endl;
}
else if(myStack.size() == 0) // If the stack is empty
{
myStack.push(item);
}
else if( item == "+" || item == "-" || item == "/" || item == "*") //If the char is an operator
{
if(priority(myStack.top()) < priority(item)) // the item on the stack is greater priority than the array item
{
myStack.push(item);
}
else
{
while(myStack.size() > 0 && priority(myStack.top()) >= priority(item)) //while the stack contains something, and the item on
{
string dpu = myStack.top();
myStack.pop();
output = output + dpu + " ";
}
myStack.push(item);
}
}
else if(item == "(") // left peren
{
myStack.push(item);
}
else if(item == ")") // right peren
{
while(myStack.top() != "(")
{
string dpu = myStack.top();
myStack.pop();
output = output + dpu + " ";
}
myStack.pop();
}
iss>> item;
}
while (myStack.size() > 0 )
{
string dpu = myStack.top();
output = output + dpu + " ";
myStack.pop();
}
cout<< output << endl;
}
well... in your code the numbers are always printed before anything else:
if (item != "+" && item != "-" && item != "/" && item != "*" && item != "(" && item != ")") //If the char is a number
{
cout << item << endl;
}
I'm not familiar with the notation you are requesting, but a stack doesn't seem to me the right datastructure for this task... using a tree seems better to me, since you are parsing

convert infix to postfix and then solve the equation

So, I'm having issues with this program. Can anyone tell me what I'm doing wrong here? The program is supposed to take a simple infix notation mathematical expression (e.g. "5 - 2 + 1") and then convert it to (e.g. "5 2 - 1 +") and then solve it which would be 4. It converts just fine but as soon as it gets into the evaluating part, it doesn't show anything on the command prompt. Can I get some help? Thanks!
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int priority(string item) //prioritizing the operators
{
if(item == "(" || item == ")")
{
return 0;
}
else if(item == "+" || item == "-")
{
return 1;
}
else //if(item == "/" || item == "*") <-- guess didnt need to define this one
{
return 2;
}
}
void welcome()//welcome text
{
cout << "Welcome to this program!" << endl;
cout << "Please enter your equation" << endl;
}
int main()
{
welcome(); // welcome text
stack <string> myStack; // initializing the stack.
char line[256];
cin.getline( line, 256); // this and the proceeding line get the input.
string exp = line;
string item;
string postFix;
istringstream iss(exp);
iss >> item;
while(iss)
{
if(item != "+" && item != "-" && item != "/" && item != "*" && item != "(" && item != ")") //If the char is a number
{
cout << item;
postFix = postFix + item;
}
else if(myStack.size() == 0) // If the stack is empty
{
myStack.push(item);
}
else if( item == "+" || item == "-" || item == "/" || item == "*") //If the char is an operator
{
if(priority(myStack.top()) < priority(item)) // the item on the stack is greater priority than the array item
{
myStack.push(item);
}
else
{
while(myStack.size() > 0 && priority(myStack.top()) >= priority(item)) //while the stack contains something, and the item on
{
cout << myStack.top();
postFix = postFix + item;
myStack.pop();
}
myStack.push(item);
}
}
else if(item == "(") // left peren
{
myStack.push(item);
}
else if(item == ")") // right peren
{
while(myStack.top() != "(")
{
cout << myStack.top();
postFix = postFix + item;
myStack.pop();
}
myStack.pop();
}
iss >> item;
}
while (myStack.size() > 0 ) //When nothing is left to evaluate
{
cout << myStack.top();
postFix = postFix + myStack.top();
myStack.pop();
}
cout << endl;
//PART 2
int x1;
int x2;
int x3;
stack<int> thirdStack;
string exp2 = postFix;
string item2;
istringstream iss2(exp2);
iss2 >> item2;
while(iss2)
if(item2 != "+" && item2 != "-" && item2 != "/" && item2 != "*") //if its a number
{
int n;
n = atoi(item2.c_str());
thirdStack.push(n);
}
else if( item2 == "+" || item2 == "-" || item2 == "/" || item2 == "*") //if its an operator
{
x1 = thirdStack.top();
thirdStack.pop();
x2 = thirdStack.top();
thirdStack.pop();
if(item2 == "+")
{
x3 = x1 + x2;
thirdStack.push(x3);
}
else if(item2 == "-")
{
x3 = x1 - x2;
thirdStack.push(x3);
}
else if(item2 == "/")
{
x3 = x1 * x2;
thirdStack.push(x3);
}
else if(item2 == "*")
{
x3 = x1 / x2;
thirdStack.push(x3);
}
}
}
cout << "The conversion into infix notation is" + thirdStack.top() << endl;
}
There are a number of problems with this code.
In part 1, while your code appears to write out the correct postfix conversion, the postFix string it builds up isn't the same thing.
For example, in some places you have code like this:
cout << myStack.top();
postFix = postFix + item;
You're writing out myStack.top() but adding item to your postFix result. It should be this:
cout << myStack.top();
postFix = postFix + myStack.top();
In addition, you ought to be including spaces between each item that you add to the postFix string. So your result should be 5 2 - 1 + rather than 52-1+. Otherwise when trying to interpret that expression, the first item is going to be interpreted as 52.
Then in part 2, you're missing a call to iss2 >> item2; at the end of the while loop. You're basically just interpreting the first item over and over again, so the code will end up in an infinite loop.
And in your calculations, you have the operand order incorrect. This doesn't matter for addition and multiplication, but it does for subtraction and division. For example, the substraction calculation should be x3 = x2 - x1; rather than x3 = x1 - x2;.
Finally, when streaming out the result you've got a plus when you should have a stream operator. It should look like this:
cout << "The conversion into infix notation is" << thirdStack.top() << endl;