i have 2 sperate projects which i am trying to run together as a shared project but i am failing hard…….
Basically, first project does infix to postfix notation and the second projects does postfix evaluation.
tried to run all that in 1 project but i keep getting all sorts of error and cannot figure it out, I am kind of lost don’t know what to do.
any suggestions would be really appreciated!!!!!!!!!
the first 3 files is infix to postfix project and the last one is posftix evaluation.
this is the calculator.cpp
#include "stdafx.h"
#include "Calculator.h"
#include <iostream>
#include <math.h>
#include <cctype>
#include <conio.h>
Calculator::Calculator()
{
}
Calculator::~Calculator()
{
}
int Calculator::add(int a, int b)
{
return a + b;
}
int Calculator::subtract(int a, int b)
{
return a-b;
}
int Calculator::calculate(int a, char op, int b)
{
int result = 0;
switch (op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
return result;
}
std::stack<char> Calculator::infixToPostfix(std::stack<char> infix)
{
std::stack<char> postfix;
std::stack<char> opStack;
while(!infix.empty())
{
char c = infix.top();
std::cout << "processing: " << c << std::endl;
if (std::isdigit(c))
{
postfix.push(c);
}
else
{
if (!opStack.empty())
{
char opTop = opStack.top();
if ((opTop == '+' || opTop == '-') && (c == '/' || c == '*'))
{
opStack.push(c);
}
else
{
while (!opStack.empty())
{
postfix.push(opStack.top());
opStack.pop();
}
opStack.push(c);
}
}
else
{
opStack.push(c);
}
}
infix.pop();
}
while (!postfix.empty()) {
opStack.push(postfix.top());
postfix.pop();
}
return opStack;
}
double Calculator::solvePostfixNotation(std::stack<char> postfix)
{
return 0;
}
this is the calculatorclass.cpp
// this is where you USE your blueprint
#include "stdafx.h"
#include "Calculator.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
Calculator myCalc;
int numA, numB;
char op;
char keepGoing = 'y';
string userInputString;
// do-while
while (keepGoing == 'y') {
/////////////////////////////////////////
cout << "Enter your input: ";
cin >> userInputString;
// ASCII encoding
//'0' = 48
//'1' = 49
//'2' = 50
cout << "you entered: " << userInputString << endl;
//N X N X N X N X N......
int result = 0;
stack<char> inputStack;
for (int i = userInputString.length() - 1; i >= 0; i--) {
char c = userInputString.at(i);
cout << c << " ";
inputStack.push(c);
}
stack<char> postfix = myCalc.infixToPostfix(inputStack);
//cout << "Result is :" << myCalc.solvePostfixNotation(postfix);
cout << endl << " postfix stack looks like : ";
while (!postfix.empty())
{
cout << postfix.top() << " ";
postfix.pop();
}
numA = userInputString.at(0) - '0';
op = userInputString.at(1);
numB = userInputString.at(2) - '0';
//cout << myCalc.calculate(numA, op, numB)<<endl;
/////////////////////////////////////////
//cout << endl << "Do you want to evaluate the postix expression (y/n)?";
//"Do you want to continue (y/n)?";
cout << endl << "solve this posftix expression (y/n)?";
cin >> keepGoing;
}
}
this is the calculator.h
#pragma once
#include<stack>
// this is where the blueprint sits
class Calculator
{
private:
int numA, numB;
char op;
public:
Calculator();
~Calculator();
int add(int, int);
int subtract(int, int);
int calculate(int, char, int);
std::stack<char> infixToPostfix(std::stack<char> infix);
double solvePostfixNotation(std::stack<char> postfix);
};
this is the posftix evaluation postfix.cpp
#include<iostream>
using namespace std;
#include<math.h>
#include<conio.h>
#define size 6
struct type
{
float data[size];
int top;
};
void push(type*s, float d)
{
if (s->top < 6)
{
s->data[s->top] = d;
s->top++;
}
}
float pop(type*s)
{
if (s->top != 0)
{
s->top--;
return s->data[s->top];
}
return 0;
}
float Operator(char symbol, float iFirstnum, float iSecondnum)
{
switch (symbol)
{
case '+': return (iFirstnum + iSecondnum); break;
case '-': return (iFirstnum - iSecondnum); break;
case '*': return (iFirstnum * iSecondnum); break;
case '/': return (iFirstnum / iSecondnum); break;
default: cout << "wrong operation......\n";
}
return 0;
}
int main() {
float iFirstnum, iSecondnum, symb, value;
char symbol;
type *s;
s = new type;
s->top = 0;
cout << "Enter The Postfix Expression To Evaluate:: \n\n";
cin >> symbol;
while (symbol != '.') {
if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/') {
iSecondnum = pop(s);
iFirstnum = pop(s);
value = Operator(symbol, iFirstnum, iSecondnum);
push(s, value);
}
else {
if (symbol == '0') symb = 0;
else if (symbol == '1') symb = 1;
else if (symbol == '2') symb = 2;
else if (symbol == '3') symb = 3;
else if (symbol == '4') symb = 4;
else if (symbol == '5') symb = 5;
else if (symbol == '6') symb = 6;
else if (symbol == '7') symb = 7;
else if (symbol == '8') symb = 8;
else if (symbol == '9') symb = 9;
push(s, symb);
}
cin >> symbol;
}
value = pop(s);
cout << "The Value:: \n" << value << endl;
system("pause");
return 0;
}