0

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

  • 4
    What is the question? So far it sounds like you expect us to write your code for you – UnholySheep Jun 04 '18 at 07:54
  • 1
    "i am not expert yet i need a code" - no it ain't what we're doing here. – Joseph D. Jun 04 '18 at 07:55
  • if you want map something to something your probably want a `std::map`, eg for mapping values to user supplied variable names you could use a `std::map` – 463035818_is_not_an_ai Jun 04 '18 at 07:58
  • @user463035818 so mapping could perform like detect the value for a variable and name ? and i will search for mapping and see what i come up with – Ahmed Awaad Jun 04 '18 at 08:10
  • @UnholySheep the question is how i will split the string to many variables and in each know the value and the name as idea and ways not expecting writing the code ,man it's a single function in a project anyway – Ahmed Awaad Jun 04 '18 at 08:12
  • Look for a basic parser. Chances are equations are one of the basic examples. – Jorge Bellon Jun 04 '18 at 08:39
  • Your array is only *count* in length, but you'll be reading count+1 items (and you try to print count+1 items). – Sean Burton Jun 04 '18 at 12:48
  • @SeanBurton, but until the line i print the variables, my array count good , and also some unknown char appears, never know why ? – Ahmed Awaad Jun 04 '18 at 18:53

1 Answers1

0

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.

Ashkan
  • 1,050
  • 15
  • 32
  • Thanks for reply , it is good idea but i don't just adding values , i want add values of same variable like "X1" , all values before "X1" will be add and get only on "X1" with this result – Ahmed Awaad Jun 04 '18 at 08:32
  • Then please make your question more concise. Make an step by step example so that we can see what you mean. Provide a more complex example as well so that we see the full complexity of your problem. – Ashkan Jun 04 '18 at 08:48