Why does some garbage appears instead of array values?

73 Views Asked by At

I take equation from a user (ex: 1X1+2X2=24 ) whatever the number of variables , i am toking it to 1X1 ,2X2 by +, - or = and then put them in array of char and each toking by x or X, and put it on another array ,but some garbage comes

#include<iostream>
#include <stdio.h>
#include <string.h>
#include<string>
#include <algorithm>
#include<vector>
using namespace std;
int main()
{
    string a;
    cout<<"Enter Equation: "<<endl;
    cin>>a; //string of the equation
    int num= a.size();
    char str[num]; //view it as array of char
    for (int k=0;k<num;k++)
    {
      str[k]=a[k];
      cout<<str[k];
    }

    int i=0;
    char *pch;
    int count = 0;
    for (int i = 0; i < a.size(); i++)
    {
        //counting + , - and = to see how many element i need in array
        if (a[i] == '+'||a[i] == '-'||a[i] == '=')
            count++;
    }

    char *array[count];
    cout<<"\nSplitting string into tokens: "<<a;
    pch = strtok (str,"= + -");
    while (pch != NULL)
    {
    array[i++]=pch;
    pch = strtok (NULL, "= + -");
    }

// printing every variable
    for(i=0;i<count+1;i++)
      {
          cout<<endl<<array[i];
      }

    char *pch_var;
    char *array_var[2];
    for(int j=0; j<count+1;j++)
    {
        cout<<"\nSplitting Variable into tokens: "<<array[j];
        pch_var = strtok (array[j],"x X");
        while (pch_var != NULL)
        {
        array_var[i++]=pch_var;
        pch_var = strtok (NULL, "x X");
        }
        cout<<array_var[0]<<endl<<array_var[1];
    }

    return 0;
}

the out comes be like that

Enter Equation:

1X1+2X2=24 // from user

1X1+2X2=24 //here start my code function

Splitting string into tokens: 1X1+2X2=24

1X1

2X2

24{t{k║

Splitting Variable into tokens: 1X1P■m

]├ï Uï∞â∞SVWh

Splitting Variable into tokens: 2X2P■m

]├ï Uï∞â∞SVWh

1

There are 1 best solutions below

2
Ashkan On

If I understood correctly, you want users to input an equation. Then you want to parse this equation and give an answer. If that is the case, the usual way is to change the equation from infix to prefix or postfix and put it in a stack. Then you process each stack entry and do your calculation.

Assuming that you have the input 1 + 2 (this is infix form) you change it to 1 2 + which is the postfix form.

For your example 1X1+2X2+3X2+3x1=24 the postfix becomes 1 1 x 2 2 x + 3 2 x + 3 1 x + 24 =. You start from left and pop the stack untill first operand, compute the result and push back until the stack is completely processed.

 step 1) 1 1 x 2 2 x + 3 2 x + 3 1 x + 24 =
 step 2)     1     4 + 3 2 x + 3 1 x + 24 =
 step 3)             5 3 2 x + 3 1 x + 24 =
 step 4)             5     6 + 3 1 x + 24 =
 step 5)                     11 3 1 x + 24 =
 step 6)                     11     3 + 24 =
 step 7)                              14 24 =
 step 8)                              false

I think making the expression to prefix is easier for implementation but harder to understand. Here are a few links

http://scanftree.com/Data_Structure/prefix-postfix-infix-online-converter

http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html

I hope this helps.