I am solving a problem for finding maximum value of an arithmetic expression. But I am having problem in taking input for the expression as no of characters are not fixed.
Input Format:
- The only line of the input contains a string s of length 2n + 1 for some n, with symbols s0 , s1 , . . . , s2n.
- Each symbol at an even position of s is a digit (that is, an integer from 0 to 9)
- while each symbol at an odd position is one of three operations from {+,-,*}.
But I want digits and symbols in different arrays(digits in an int array and operations in a char array) for my solution.
This is how I implemented originally:
string s;
std::cin >> s;
n=s.size();
cout<<"n= "<<n<<endl;
vector<long long> no;
vector<char> ops;
for(int i = 0; i <n; i++)
{
if(i%2==0)
{
no.push_back(s[i]);
}
else{
ops.push_back(s[i]);
}
}
But I am not being able to get required input, instead getting this:
INPUT:
5-8+7*4-8+9
OUTPUT:
n = 11
no[0] = 53
no[1] = 56
no[2] = 55
no[3] = 52
no[4] = 56
no[5] = 57
ops[0] = -
ops[1] = +
ops[2] = *
ops[3] = -
ops[4] = +
I have also tried one more solution:
vector<long long> no;
vector<char> ops;
int i=0;
while(cin)
{
cout<<"i= "<<i<<endl;
if(i%2==0)
{
int s;
cin>>s;
if(s=='\0')
{
exit();
}
cout<<"s= "<<s<<endl;
no.push_back((int)s);
cout<<"no= "<<no[i/2]<<endl;
}
else
{
char s;
cin>>s;
if(s=='\0')
{
exit();
}
cout<<"s= "<<s<<endl;
ops.push_back(s);
cout<<"ops= "<<ops[(i-1)/2]<<endl;
}
i++;
}
But this goes into infinite loop.
Please help me out