C++ :: Array Base Stack Class - Infix To Postfix Conversion
Feb 4, 2014
Array based stack class. So i am having a problem, i do not understand how to convert an infix notation into a postfix notation from a txt file. The infix expressions are stored in a text file. for example inside the text file is
6+9
2+7*8
(1+5)*3
5+(6-2)*((2-8)
at the moment my header file is
#ifndef STACKS_H
#define STACKS_H
#include <iostream>
using namespace std;
const int STACKS_CAPACITY = 128;
typedef int StacksElement;
[Code] ....
but how to make it convert the into postfix...
View 3 Replies
ADVERTISEMENT
Mar 8, 2013
I am Getting following errors I don't know why.I have mentioned the lines (in comments) where these errors are occurring.
__________________________________________________
warning C4018: '<' : signed/unsigned mismatch
error C2064: term does not evaluate to a function taking 0 arguments
error C2064: term does not evaluate to a function taking 0 arguments
__________________________________________________ _
Code:
/////////////////////////////////////////////////
//libraries
/////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
[Code] .....
View 1 Replies
View Related
Mar 27, 2014
I keep getting the same error messages every time on Visual Studio. I don't know where the error is originating. Basically I'm trying to convert an infix expression (A+B-C) to a postfix expression (AB+C-) using stacks.
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include "Expression.h"
#include "stackType.h"
using namespace std;
int main() {
string fileName;
string infixExpression, postfixExpression;
[Code] .....
View 2 Replies
View Related
Apr 12, 2015
I am trying to convert from infix to postfix, and then evaluating the postfix expression to get the final answer. I am having a severe problem though, because for somereason the conversion is not working at all. For example, when I input the first infix expression:
24 + 33 * ( 7 - 5 ) + 8 / 3 it ouputs 24 33 7 5 - 5 ) + 8 / 3( 7 - 5 ) + 8 / 3* ( 7 - 5 ) + 8 / 3+ 33 * ( 7 - 5 ) + 8 / 3 8 3/ 3+ 8 / 3
Which is obviously very wrong. I am not really sure where the problem is though.
Below I have included all of the code needed. I also had to create my own stack class, so I will include it too .
#include "stacks.h"
#include <iostream>
using namespace std;
bool IsOperand(char C) {
if(C >= '0' && C <= '9') return true;
if(C >= 'a' && C <= 'z') return true;
[Code] .....
View 1 Replies
View Related
Sep 25, 2013
I trying to write a code for a calculator using stack is how to convert an operation from infix to postfix . Here is my attempt I created a header file.
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
template<class T>
struct node {
T item;
[Code] .....
View 1 Replies
View Related
Sep 16, 2013
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MAX 50
[Code] ....
[URL] .....
My output should look like this ..
Enter an expression in infix form: (9+6)/(2*4)^15
Specify output expression type,1) Prefix 2)Postfix
The Prefix expression is: /+96^*2415
----------------------------------------------
Enter an expression in infix form: (9+6)/(2*4)^15
Specify output expression type,1) Prefix 2)Postfix
The Postfix expression is:96+24*15^/
View 2 Replies
View Related
Apr 25, 2014
I have the following problem on my C++ Program and I am not sure why it is not working. I am creating a infix to postfix program through an inherited class which I am not sure it is working.
#include <iostream>
#include <stack>
using namespace std;
int in_stack_Priority(char a){
if(a == '*' || a == '/')
return 2;
[Code] .....
View 3 Replies
View Related
Sep 14, 2014
I'm trying to make an infix to postfix notation calculator. The difficult thing is th stack class is custom. It's not to hard to understand, I don't know if the fact that it is that way will not allow me to receive support. The difference is that the pop functions is as such:
stack<char> conversion;
char temp;
conversion.pop(temp);//It receives a parameter and puts the popped element in there.
conversion.peek(temp);//Places the top element in said parameter
not only that... but these are boolean functions. they return true if the operation was completed. So they can be used as conditions.
#include<iostream>
#include<string>
#include<stdio.h>
#include"stack.h"
using namespace std;
int main(void) {
Stack<char> conversion;
string infix,inter,temps;
[Code] .....
The error is that i am mismanaging parenthesis handling and i can't seem to grasp where and how.
An example input is:(35+7)-(9-2)
that input gives me:35 7 + 9 2 ) -
but another input such as :(35+7)/7
outputs as: 35 7 + 7 /. Totally fine.
View 3 Replies
View Related
Apr 26, 2014
Write a program that evaluates postfix expression using array implementation of stack.
The expression [the input] is evaluated from left to right using a stack. When the element read from the expression is an operand, push it into the stack.When the element read from the expression is an operator: Pop two operands from the stack.Evaluate the two operandsPush the result of the evaluation into the stack.
The final result lies on the top of the stack at the end of the calculation. Make sure to display the result before terminating the program.Write a program that evaluates postfix expression using array implementation of stack.
Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define M 20
typedef struct{
[Code] ....
View 1 Replies
View Related
Oct 1, 2013
Program which convert expression from PostFiz to Infix and also wants to evaluate them....
View 3 Replies
View Related
Nov 21, 2014
So I have been working my way through this assignment and I'm just stuck now. I cannot get this work properly It just tells me I'm trying to return a local variable when attempting to return postfix in the to_postfix function. It is line 97 that wont compile. Also I cannot change anything in runner.c.
Calculator.c
Code:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include "stack.h"
[Code] .....
View 11 Replies
View Related
Feb 6, 2014
So i solved my earlier problem but now how do i make it read from the text file that i have and output the postfix notations? Because right now it is only outputting what is currently in the text file for example
6+9
2+7*8
(1+5)*3
5+(6-2)*((2-8)
Header file is
#ifndef STACKS_H
#define STACKS_H
#include <iostream>
using namespace std;
const int STACKS_CAPACITY = 128;
typedef int StacksElement;
[Code]...
test client cpp is
#include <iostream>
using namespace std;
#include "Stacks.h"
#include <fstream>
#include <string>
#include <cassert>
#include <cctype>
[Code]...
View 10 Replies
View Related
May 24, 2014
I need to convert from an infix expression to postfix using stacks!
#ifndef EXPRESSIONMANAGER_H_
#define EXPRESSIONMANAGER_H_
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;
class ExpressionManager : public ExpressionManagerInterface{
[code]....
View 2 Replies
View Related
Sep 28, 2014
I was given a task to convert infix to post fix using both linked lists and stacks in the code so this is what i have written but the problem is it is giving me same error at three different places "missing function header(old style format?)
#include <iostream>
#include <string>
using namespace std;
const int size = 100;
class stack{
private: // Declare a structure for the list
[Code] ....
View 12 Replies
View Related
Dec 14, 2013
i try to write a postfix calculator program using stack in C++ the in put must be the infix expression but can dont know how to write a infix expression in put.
this is my code :
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
using namespace std;
}
[code]....
View 2 Replies
View Related
Mar 15, 2015
I'm having some significant trouble with an assignment to create a postfix calculator that simulates the dc calculator function in linux. I have attached the handout with project instructions, but my main problem at the moment lies with parsing through the string input and adding the numbers into a stack of ints.
Project #2.pdf (47.78K)
Here's a brief summary of the methods used in the switch statement:
OPERATORS AND COMMMAND INPUTS
+ : Pops two values off the stack, adds them, and pushes the result.
- : Pops two values, subtracts the first one popped from the second one popped, and pushes the result.
* : Pops two values, multiplies them, and pushes the result.
/ : Pops two values, divides the second one popped from the first one popped, and pushes the result.
% : Pops two values, computes the remainder of the division that the / command would do, and pushes that.
Commands
p - Prints the value on the top of the stack, without altering the stack. A newline is printed after the value.
f - Prints the entire contents of the stack without altering anything. A newline is printed after each value
n - Prints the value on the top of the stack, pops it off, and does not print a newline after.
c - Clears the stack, rendering it empty.
d - Duplicates the value on the top of the stack, pushing another copy of it. Thus "4d*p" computes 4 squared and prints it.
r - Reverses the order of (swaps) the top two values on the stack.
Exception handling also needs to be added to account for division by zero and and invalid operator.
Right now my biggest problem is that I keep getting the following strange output where a 0 is automatically added to the stack when I call a function or operator. I realize this is probably because of the lines I have placed outside of the for loop that read
//END FOR LOOP
int num = atoi(operands.c_str());
myStack.push(num);
operands = "";
cout << num << " added." << endl;
But when I tried putting these statements INSIDE the for loop, I just get more errors. I've been working on this for a number of hours but I can't figure out my issue. I've also attached an image of my current output.
#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
#include "stack.h"
bool isOperator(const char& input );
[code]....
View 6 Replies
View Related
Mar 21, 2015
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
View 1 Replies
View Related
Jan 16, 2013
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
View 2 Replies
View Related
Apr 8, 2014
Base class has an array, whose size is controlled by the derived class.
I can't use the STL and use a 2003 compiler, so things like std::vector and std::array are out. I also can't use dynamic memory allocation.
So I thought of a few options:
1.
template <int N> class myBaseClass { ... int array[N]; ... }
then class MyClass: public myBaseClass<8> ... etc ...
2.
have a int **array in the base and assign in the derived class.
3.
give the base some virtual methods such as int *getArray or even int &getInt for more safety.
View 6 Replies
View Related
May 2, 2013
The objective is to build a month class that hold data on the number of the month and the name of the month. Using constructors and overloads, set it up to where you can input either the month or the name and it will output the results for both the month number and name.
Here is the code I have so far:
#include<iostream>
#include<string>
using namespace std;
class Month{
private: string name;
int monthNumber;
[Code] ....
It is almost fully compiled if I go by the error list. The only problems I see to be having are with the prefix and postfix overloads.
View 5 Replies
View Related
Mar 30, 2013
Say I have 3 classes:
class Player {
public:
virtual func1();
[code]....
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
View 6 Replies
View Related
Jan 21, 2014
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
#include<iostream>
class A{
public:
[Code].....
View 1 Replies
View Related
Apr 26, 2014
I have my main.cpp like this:
#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}
Can I use a function to simply put values from old to new variables? If yes then how?
View 3 Replies
View Related
Jan 6, 2015
Let's say I have a Car object , and it contains inner Engine object.
Code:
struct Car{
Engine mEngine;
};
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};
The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
View 5 Replies
View Related
Feb 4, 2014
So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;
it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:
std::vector<base> Vec;
then I add an element into it like this
Derived DerElement;
Vec.push_back(DerElement);
then when I type:
for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}
It outputs:
Derived DerElement2;
DerElement2.Update();
and it outputs this:
#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {
[Code] .....
and this is it's output:
Base
Derived
Press any key to continue . . .
View 1 Replies
View Related
Jul 15, 2014
I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:
class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();
[Code] ......
View 3 Replies
View Related