Hello, I am trying to write a program in which I will be using a string to add or subtract a mathematical equation. That is, a mathematical equation will be inputted in a string and then that will be used to produce an end result..
I tried to make a program, but it is giving run time error and not working properly. According to my logic, it should be working. Here's the code.
(Please note that this is simply a code to separate integers from string.. Once it is done, then mathematical operation is very easy)
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
|
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
string a = "5+10+100";
int f = 0, size = 0;
int *num = NULL, *temp;
string t;
for (int i = 0; i < a.length(); i++)
{
if (a[i] == '+' || a[i] == '-' || a[i+1] == '\0')
{
if (a[i+1] == '\0')
t = a.substr(f, i);
else
t = a.substr(f, i-1);
f = i + 1;
temp = new int[size + 1];
for (int i = 0; i < size; i++)
temp[i] = num[i];
delete[]num;
num = temp;
num[size] = stoi(t);
size++;
}
}
for (int i = 0; i < size; i++)
cout << num[i] << endl;
cout << endl;
system("pause");
return 0;
}
|
The logic here is this that there is first index number which will be stored in variable int... And the last index number will be whatever stored in "i".
So, whenever "+" comes, I will take a substring from "f" to "i-1" (the character before "+"), i.e. "5" in first case.
However, there is still an error being generated on num[size] = stoi(t) and I have no idea why is it so :/
(You can see how I am using size here.. Basically it stays zero so that I can add a value into that index i.e. 0th index for first case. And then I make an addition in it.)
Now, I changed a little bit of code..
And declared another int "l" for last number of substring.. I forgot to change its value, but it still worked well.
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
|
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
string a = "5+10+100";
int f = 0, l = 0, size = 0;
int *num = NULL, *temp;
string t;
for (int i = 0; i < a.length(); i++)
{
if (a[i] == '+' || a[i] == '-' || a[i+1] == '\0')
{
t = a.substr(f, l-1);
f = i + 1;
temp = new int[size + 1];
for (int i = 0; i < size; i++)
temp[i] = num[i];
delete[]num;
num = temp;
num[size] = stoi(t);
size++;
}
}
for (int i = 0; i < size; i++)
cout << num[i] << endl;
cout << endl;
system("pause");
return 0;
}
|
It is giving a perfect result.
1 2 3 4 5 6
|
5
10
100
Press any key to continue . . .
|
But I don't get the reason as to why?
First thing, the value of "l" never changed from '0' so how come it is still getting 10 or 100? Because in both cases, the last value isn't 0 but instead it is '3' in case 10 and '7' in case 100. So how is substring working perfectly in this case?
And now, even if we consider that the index stored in int l is correct then how is it working for 100? Because the loop works until the last index of the string (which is '7' in case of this string)
Then it checks whether a[i+1] == '\0' is true... Which will come out to be true.
And then it makes a substring of the main string by,
t = a.substring (f, l-1)
so, in this case, values will be,
t = a.substring (5, 7-1)
That means, it should only take "10" into consideration and store "10" into the string 't'
But it is giving 100 in the output, meaning it stores "100" into the string 't'.
Can anyone explain that why and how is this working?
Thank you.