-
Yes, use std::string. Also, read up on stringstreams (#include <sstream>) to help in parsing from string to int. Using getline is good, but it does mean that you need another round of parsing to individual string tokens. Again, stringstream can help.
-
Code:
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
void calculation(int &ope1, int &ope2, char &input);
using namespace std;
stack <int> a;
int main()
{
char str[100];
cin.get(str, 100);
cout << str << endl;
system ("pause");
}
Ok, if all of this code correct. Step by step first
-
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string post;
cout << "Enter expression: ";
getline (cin , post);
system ("pause");
return 0;
}
is this correct. I am unsure about <sstream>
-
Code:
#include <iostream>
#include <string>
#include <stack>
void calculation(int &oper1, int &oper2, char &chr);
using namespace std;
int main()
{
int number;
stack <int> a;
char ch;
cout << "Enter expression: ";
while (cin >> number)
{
a.push(number);
while (cin >> ch)
{
switch (number)
{
case '+':
a.pop();
a.push(number + number);
break;
case '-':
a.pop();
a.push(number - number);
break;
case '*':
a.pop();
a.push(number * number);
break;
case '/':
a.pop();
a.push(number / number);
break;
}
a.pop();
}
cout << a.top() << endl;
}
system ("pause");
return 0;
}
Give up on sstream. Still cant solve
-
OK, this is a pity post - 50 posts, and 2 weeks, but I don't think you've learnt a great deal.
Code:
case '+':
int num1 = a.top(); a.pop();
int num2 = a.top(); a.pop();
a.push( num1 + num2 );