Ok i have a calculator program, but it can only do positive numbers, can anyone point me in the direction to make it get negative numbers also?
#include <iostream>
#include <iomanip>
using namespace std;
struct Node{
float number;
Node *next;
};
Node* push(Node *stack, float data){
Node *utility;
utility = new Node;
utility -> number = data;
utility -> next = stack;
return utility;
}
Node* pop(Node *stack, float &data){
Node *temp;
if (stack != NULL){
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "\nERROR: Empty stack.\n";
return stack;
}
int main()
{
float answer, operand1, operand2;
char ch = ' '; //just so that the while loop runs
Node *utility, *top;
utility = new Node;
utility -> number = 0;
utility -> next = NULL;
top = new Node;
top -> number = 0;
top -> next = utility;
cout << "Enter the operation: ";
while(ch != '\n')
{
cin >> noskipws >> ch;
int operand = 0;
while(ch == ' ')
cin >> ch;
if((ch >= '0')&&(ch <= '9')){
while(ch != ' '){
operand = operand*10 + (ch-48);
cin >> ch;
}
top = push(top, operand);
}else{
top=pop(top, operand1);
top=pop(top, operand2);
switch(ch){
case '+': answer = operand2 + operand1;break;
case '-': answer = operand2 - operand1;break;
case '*': answer = operand2 * operand1;break;
case '/': answer = operand2 / operand1;break;
}
top=push(top, answer);
}
}
pop(top, answer);
cout << "\nAnswer: " << answer << endl;
system("pause");
}
This program was not made by me but i want to modify it to take negative numbers aswell.
Just need the idea
Thank you