1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
char* eatspaces(char* str, int length, char* expression);
int addNumbers(char* str, int length, int num1, int num2);
int findLength(char* str, int length);
int main(void)
{
const int max = 50;
char equation[max];
char* pequation = equation;
cout << "Equation: " << endl;
cin.getline(equation, 15, '\n');
int length = 0;
for (int i = 0; equation[i] != '\0'; i++)
length++;
char* expression = new char[length];
expression = eatspaces(equation, length, expression);
int num1Length = findLength(expression, length);
int num2Length = num1Length + 2;
int total = addNumbers(expression, length, num1Length, num2Length);
cout << "total is: " << total << endl;
delete[] expression;
expression = NULL;
pequation = NULL;
return 0;
}
char* eatspaces(char* str, int length, char* expression)
{
int spaceCounter = 0;
for (int i = 0; i < length; i++)
{
if (str[i] == ' ')
continue;
else
expression[spaceCounter] = str[i];
spaceCounter++;
}
return expression;
}
int findLength(char* str, int length)
{
int num1Length = 0;
for (int i = 0; str[i] != '+'; i++)
num1Length++;
return num1Length;
}
int addNumbers(char* str, int length, int num1, int num2)
{
int total = 0;
for (int i = 0; i < num1; i++)
total += (str[i] - 48);
num1++;
for (; num1 < length; num1++)
total += (str[num1] - 48);
return total;
}
|