'How do I add an expression if it's a string?
I'm trying to write a calculator program that has the features of addition, subtraction, and the notation X^ which means X^2. I'm completely lost and I don't know what to do. This is the code I have so far and an example of an expression I'm trying to solve.
1000 + 6^ - 5^ + 1;
and my code is
#include <iostream>
using namespace std;
int main()
{
int sum;
char answer, num, sign, add = '+', minus = '-';
cin >> answer;
string str;
str += answer;
while (cin >> sign)
{
if (sign == '^')
{
str += add + answer;
}
else if (sign == '+')
{
cin >> num >> sign;
if ((cin >> sign) == '^')
{
str += add + num + add + num;
}
else
{
str += add + num;
}
}
else if (sign == '-')
{
cin >> num >> sign;
if ((cin >> sign) == '^')
{
str += minus + num + minus + num;
}
else
{
str += minus + num;
}
}
else if (sign == ";")
{
for(int i = 0; i < str.length(); i++)
{
int digit = str[i] - 48;
}
}
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
