Converting to expression to string of array elements

This is a discussion on Converting to expression to string of array elements within the C Programming forums, part of the General Programming Boards category; This code converts an expression A*B-C-D+E to 5*3-2-3+4 using the array elements but it seems to work for only single ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Converting to expression to string of array elements

    This code converts an expression A*B-C-D+E to 5*3-2-3+4 using the array elements but it seems to work for only single digits, can anyone assist me here, to make it work fr double digits?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAXCOLS 80
    
    int main()
    {
       const int num_operand =5;
        const int num_operatr = 4;
       const int strglen=num_operand+num_operatr;
        char operand;
        int i, j;
        char string[MAXCOLS];
        char getexp[strglen+1];
        char a[]={'5','3','2','3','4','2','5','4','6','8'};
                     
    
        printf("Enter a command of the sort A+B*C_D*E:\n");
        fgets(getexp, sizeof getexp, stdin);
    
        for(j=0; getexp[j] != '\0'; j++)
        {
           operand=getexp[j];
           
           if(operand >= 'A' && operand <='Z')
               string[j]=a[operand-(int)('A')];
           else
               string[j]=(char)operand;
        }
        string[j] ='\0';
    
        printf("Expression\n");
        printf("%s", string);
        
        getchar();
        return 0;
     }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why are you starting a new thread?

    I gave you some hints in the other thread - but you don't have to do it that way - you could just build up the output string as you go along - functins such as sprintf() might help here.

    --
    Mats

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The reason I give you hints instead of actually telling you how to do it is that you'll learn to think for yourself, and you may not think I'm kind right now, but hopefully in the future you'll realize that this is useful. I just tested my previous hint, and with adding one variable and changing about 4-5 lines of your above code (this included changing the type of array a and editing it), I got:


    Code:
    // this is what  a looks like:
        int a[]={55,33,2,77,88,2,5,4,6,8};
    
    Enter a command of the sort A+B*C-D*E:
    A+B+C
    Expression
    55+33+2
    --
    Mats

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Hi, Mats I've been trying this since then with the hint you gave to me, but have not been able to crack it. I've not been idle since then, we equally learn by example. Since I've been on here I've equally learnt some new things have done some work on my own. So if you can put me through that will be nice.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so what do YOU think you need to do to make it print larger numbers than single digits (aside from changing the array a to contain something that isn't single digits, of course).

    I'm happy to "walk you through the steps", but I'm not going to carry you, agreed?

    --
    Mats

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    that's using sprintf()

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I've suggested using sprintf, but I was asking what you think you need to change in your code to make that work...

    For example, is there anything in the loop that parses the string that needs to change?

    --
    Mats

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    I can't see anything here.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so in more detail: Once you've identified a "variable", what do you need to do to put the number in it's place, if it's not a single digit?

    --
    Mats

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another hint: Once you've added your multi-digit number, where are you in the string?

    --
    Mats

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    It will be better if you can give me the solution to this as you said you've worked it out for me to study it and know what I've done wrong, and learn from it. If we're seeing each other physically it would have been easier for me to answer your questions and for you to see the effort I've made so far.

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Can someone help me out here!!!

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    Wow

    Another hint: Once you've added your multi-digit number, where are you in the string?

    --
    Mats
    He /did/ tell you what you need to do. Did you read what he wrote or just ignore it because he didn't literally type it for you?

    If you don't account for double digit numbers taking up two "characters" in your string, than your next write statement will write over whatever was there previously:

    A=43
    B=2
    "A+B"
    start: [-,-,-]
    1: [4,3,-]
    2: [4,+,-]
    3: [4,+,2]

    There's my "hint"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  3. Array having Integer and String elements
    By skm in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2004, 02:44 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21