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;
}

Recommended Answers

All 6 Replies

This is more advice about posting. Be sure to tag with your version of Visual Studio and reveal if you alread did research. The first hit on Google is https://msdn.microsoft.com/en-us/library/d7fz9ckx.aspx so I'm going with you know to do such research and try findings like this.

The code above does not lead me to why you have this error. Visual Studio version may differ here.

i am using 2015 Visual Studio, the problem to the error it says that i am missing an stdafx.h file which i don’t have on the shared project, i don’t know if it’s the fact that i am using 2 int main, see i never worked on shared projects.

So there is more to the error message. If you are missing some .h file you have to add that. Also, the link does make other suggestions. As I noted, the code above does not lead me to any suggestion on its own other than to work on your discussion by adding details and sharing what work you've done.

As it stands, add the missing include and do what the article I linked to above.

I did added the extra file however, though now i get more errors which are C2065: cout , cin undeclared indentifier which make no sense because these errors doesnt have to be there. the file runs perfectly by it self however when i run it as a shared project it doesnt. so i am out options

ye, well i tried to avoid shared projects but i cannot find a way to write the postfix.cpp code in the other project.
i tried rewriting the code the same way I did on first one, just adding more functions to it however, i got all sorts of errors, failed dramatically.
I will go through the shared project link you provided, hopefully I will make some sense out of it.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.