1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Mathematically Evaluating an integer array

Discussion in 'C' started by Iasthaai, Jul 21, 2006.

  1. Iasthaai

    Iasthaai New Member

    Joined:
    Jul 21, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I'm reading input from a user which will consist of all digits. I will read these digits into an integer array and then intersparse them with operators such as + and -.

    My question is, after I do this, how do I evaluate this expression to an integer?
    I.E.
    I read in >>>>> "123456", then I manipulate the integer array to >>>>> "12+3456"
    Now, what do I do to evaluate that array so that it is the integer 3468? :confused:
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Just parse the array in a loop making the loop counter to be multiple of 10.

    Code:
    // Loops get you one number
    int temp;
    for(int i=1,j=0;;i*=10,j++)
    {
      if(arr[j] == "Digit")
        temp += arr[j]*i;
      else
        break;  
    }
    Then get the symbol and then second number and operate on the number depending on the symbol.
     

Share This Page