Nhd 24
New Member
Hi... I need a program to convert an infix arithmetic expression to postfix with the use of stacks... Can anyone tell me how do i go about implementing this? thanks
11 48761 D_C 293
Contributor
There is a standard algorithm to do this. The last paragraph of this website has the steps.
Print the numbers out in normal order, push parenthesis and operators onto the stack. When parenthesis line up, ignore them, don't print anything. If they don't line up, that's an error. Then, depending upon priority of the operator (PEMDAS) push or pop the operator off the stack. If you pop it, display it in the output.
The website explains it better, why don't you go have a look.
Nhd 24
New Member
Hi can anyone help me to check this... Im lost in my own program... I noe some part of it is wrong... can anyone tell me... thanks
#include <iostream>
#include <stack>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string line, str;
int number;
cout<<"Sample Input 1"<<" "<<endl;
cin>>line;
getline(cin, line);
istringstream iss(line); //create string stream
while(!iss.eof( ))
{
iss >> str; //reads from iss into str until next space is met
}
//Postfix evaluation
int eval_postfix(co nst vector<char>&v, stack<int>&st)
{
for (vector<char>:: const_iterator str = v.begin(begin() ; str !=v.end(); ++str)
{
if ('0'<= str && str<='9')
st.push(str-'0')
else
{
int arg2 = st.top();
st.pop();
int arg1 = st.top();
st.pop();
int result;
if (str == "+" || str == "-") //when you compare for char, it's '+' and '-'
{
case '+': result = arg1 + arg2;
return 1;
break;
case '-': result = arg1 - arg2;
resturn 2;
break;
}
else if (str == "*" || str == "/")
{
case '*': result = arg1 * arg2;
break;
return 3;
case '/': result = arg1 / arg2;
break;
return 4;
} //end if else
st.push(result) ;
} //end else
}//end for
int i = st.top();
st.pop();
return i;
}//end eval_postfix
system("pause") ;
return 0;
}
Nhd 24
New Member
Hi i modify my codes but i'm unable to execute it.... can anyone help me to spot the mistake.. thanks -
#include <iostream>
-
#include <stack>
-
#include <vector>
-
#include <sstream>
-
using namespace std;
-
-
int main()
-
{
-
string line, str;
-
int number;
-
-
cout<<"Sample Input:"<<" "<<endl;
-
cin>>line;
-
-
getline(cin, line);
-
istringstream iss(line); //create string stream
-
-
while(!iss.eof())
-
{
-
iss >> str; //reads from iss into str until next space is met
-
}
-
-
//Code of Postfix Evaluation
-
int eval_postfix(const vector<char>&v, stack<int>&st)
-
{
-
for (vector<char>::const_iterator ptr = v.begin(begin(); ptr !=v.end(); ++ptr)
-
{
-
if ('0'<= *ptr && *ptr<='9')
-
st.push(*ptr-'0')
-
else
-
{
-
int arg2 = st.top();
-
st.pop();
-
int arg1 = st.top();
-
st.pop();
-
int result;
-
-
switch (*ptr)
-
{
-
case '+': result = arg1 + arg2;
-
break;
-
case '-': result = arg1 - arg2;
-
break;
-
case '*': result = arg1 * arg2;
-
break;
-
case '/': result = arg1 / arg2;
-
break;
-
} //end switch
-
st.push(result);
-
} //end else
-
}//end for
-
-
int i = st.top();
-
st.pop();
-
return i;
-
}//end eval_postfix
-
-
cout<<i<<"Sample Output:"<<" "<<endl;
-
-
system("pause");
-
return 0;
-
}
-
Banfa 9,065
Recognized Expert Moderator Expert
You have tried to define the function
int eval_postfix(co nst vector<char>&v, stack<int>&st)
inside the function
int main()
This is not allowed in C/C++ move the definition of the function eval_postfix outside the function main.
Nhd 24
New Member
Hi... I put the "int eval_postfix(co nst vector<char>&v, stack<int>&st)" out of the main, i still get error.. the error i get is something to do with this "int main()".. but i cant remove the "int main()"..... I'm sory to bother u but im really weak in my programming...
#include <iostream>
#include <stack>
#include <vector>
#include <sstream>
using namespace std;
int eval_postfix(co nst vector<char>&v, stack<int>&st)
int main()
{
string line, str;
int number;
char *cstr;
cout<<"Sample Input:"<<" "<<endl;
cin>>line;
getline(cin, line);
istringstream iss(line); //create string stream
while(!iss.eof( ))
{
iss >> str; //reads from iss into str until next space is met
}
//Code of Postfix Evaluation
for (vector<char>:: const_iterator ptr = v.begin(begin() ; ptr !=v.end(); ++ptr)
{
if ('0'<= *ptr && *ptr<='9')
st.push(*ptr-'0')
else
{
int arg2 = st.top();
st.pop();
int arg1 = st.top();
st.pop();
int result;
switch (*ptr)
{
case '+': result = arg1 + arg2;
break;
case '-': result = arg1 - arg2;
break;
case '*': result = arg1 * arg2;
break;
case '/': result = arg1 / arg2;
break;
} //end switch
st.push(result) ;
} //end else
}//end for
int i = st.top();
st.pop();
return i;
}//end eval_postfix
//convert string to integer
number = atoi(str.c_str( )); //now number is 1234
//convert integer into string
sprintf(cstr, "%d", number); //print number into cstring
string str(cstr); //makes cstring a string
//checking if all characters of the string are digits
cstr = str.c_str();
if ( isdigit(*cstr) ) //checks the first character of cstr is digit or not
return true;
cout<<"Sample Output:"<<cin<< " "<<endl;
system("pause") ;
return 0;
}
Banfa 9,065
Recognized Expert Moderator Expert
You have to move the entire function, not just the header line
This -
int main()
-
{
-
// main code
-
-
int fn()
-
{
-
// function code
-
}
-
-
// more main code
-
}
-
becomes -
int fn();
-
-
int main()
-
{
-
// main code
-
-
// more main code
-
}
-
-
int fn()
-
{
-
// function code
-
}
-
-
-
Nhd 24
New Member
for this function, for the 2nd sentence , may i noe how do i declare "begin"? i hav error regarding this.... should i declare it in main function or in its own function itself...
int eval_postfix(co nst vector<char>&v, stack<int>&st)
{
for (vector<char>:: const_iterator ptr = v.begin(begin() ; ptr !=v.end(); ++ptr)
{
if ('0'<= *ptr && *ptr<='9')
st.push(*ptr-'0');
else
{
int arg2 = st.top();
st.pop();
int arg1 = st.top();
st.pop();
int result;
switch (*ptr)
{
case '+': result = arg1 + arg2;
break;
case '-': result = arg1 - arg2;
break;
case '*': result = arg1 * arg2;
break;
case '/': result = arg1 / arg2;
break;
} //end switch
st.push(result) ;
} //end else
}//end for
int i = st.top();
st.pop();
return i;
}//end eval_postfix
Banfa 9,065
Recognized Expert Moderator Expert
Not really sure what you are saying but
for (vector<char>:: const_iterator ptr = v.begin(begin() ; ptr !=v.end(); ++ptr)
should be
for (vector<char>:: const_iterator ptr = v.begin(); ptr !=v.end(); ++ptr)
Nhd 24
New Member
Thanks Banfa for ur help.... but do u mind helping me to check my program please.... I've been struggling from juz now and i still cant do it.... Sign in to post your reply or Sign up for a free account.
Similar topics |
by: KidLogik |
last post by:
Hello!
I am converting an infix expression string into a postfix so that I
will be able to evaluate it easier ->
(5*(((9+8)*(4*6))+7)) == 598+46**7+*
I believe the rule is "Replace all occurances of two operands followed
by an operator by their infix equivalent"
|
by: Tony Johansson |
last post by:
Hello Experts!
I'm reading i a book about C++ and they mention infix with telling what it
is.
I hope you out there can do so.
Many thanks!
//Tony
|
by: caramel |
last post by:
i've been working on this program forever! now i'm stuck and going
insane because i keep getting a syntax error msg and i just can't see
what the compiler is signaling to!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
|
by: Thumbski |
last post by:
Alright basically I just have one simple question. I can code perfectly fine and don't have any problems with the .cpp of the infix class but with my header I can't get anything to work really, any guideline as to how to start this would be great. It has to work with assign.cpp which is like this:
int main()
{
Infix infix;
while (true)
{
//cout << "Enter an infix expression like 2*(3+4): " << endl;
|
by: Xah Lee |
last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully
Functional Notations
Xah Lee, 2006-03-15
In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”.
Likewise, they write “(if test this that)” to mean “if (test) {this}
else {that}”. LISP codes are all of the form “(a b c ...)”, where the
| |
by: coolguyjas07 |
last post by:
plzzzzzz help me out to run my source code .........
i hav written dis code to convert infix expression to postfix but not getting desired output.... plzzz help me to get correct output.. d source code is given below:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<process.h>
|
by: madhumitha29 |
last post by:
Does anyone know a simple C program to covert an infix to a postfix expression?plz kindly help!
|
by: aitia |
last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help?
import java.io.*;
import java.util.*;
public class Postfix
{
private static Stack operators = new Stack();
private static Stack operands = new Stack();
|
by: zeroeight |
last post by:
Hi guys,
I'm a newbie here, I just want to seek for help regarding my program. I want to implement an infix to postfix conversion only accepting numbers. Right now, I already debugged the errors and encountering loop everytime I execute it.
Here's my sample code:
********************************************************************
#include<stdio.h>
#include<conio.h>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |