Thread: arithmetic calculation ...again

  1. #1
    wombat
    Guest

    arithmetic calculation ...again

    hi there...


    to sean 135...thanx for the free parser. but then i'm not allowed to use any <string.h> functions. any ideas?

    and thanx rahaydenuk for the input on RPN. actually it sort of confused me a bit but it's all good.

    well if anybody out there would like to help...i'm trying to do an arithmetic function that reads a line from stdin and process it accordingly.

    eg.

    input : 4+5/3
    result:3

    note -> order of precedence is not used


    any help appreciated! thanx !

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: arithmetic calculation ...again

    Originally posted by wombat
    to sean 135...thanx for the free parser. but then i'm not allowed to use any <string.h> functions. any ideas?
    You mean the strlen function he used?
    Code:
    pos = strlen(str)-1;
    Just rewrite that to:
    Code:
    while(str[pos]) ++pos;
    --pos;

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Hi, Im going to assume only the +-/* operations. What I'd do is first read the whole thing in as a string with fgets(..). Im doing this way because I've assumed the equation can vary in length. Next I would read in a character at a time, if it is a number print it to a string. When you get to an operator then I would put the number on a stack, or a list or something and continue reading in the next number. I'll let you figure out the rest, like the priority of */ over +-. There probably are other numerous ways to about this. But this is a possibility. Hope this helps

    Cheers kwigibo

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    What you can do is parse the original string into a new string with tags. For example:
    3+5*2
    would become
    (Num 3)(Op1 +)(Num 5)(Op2 *)(Num 2)
    Then you can determine the order of operation. The * would go first so it would be rewritten like:
    (Num 3)(Op1 +)(Num 10)
    Then:
    (Num 13)
    You have your answer. You could also use paranthesis by giving them a tag like (Par#) where number is how embedded it is in other parenthesis.

    You can do a search for parsing where I believe Prelude gave a good example of this type of parsing.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Re: arithmetic calculation ...again

    Originally posted by Monster
    You mean the strlen function he used?
    Code:
    pos = strlen(str)-1;
    Just rewrite that to:
    Code:
    while(str[pos]) ++pos;
    --pos;
    strlen is not string.h is it?
    hello, internet!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: Re: arithmetic calculation ...again

    Originally posted by moi
    strlen is not string.h is it?
    Of course it is. Why do you think they call it strlen?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Re: Re: Re: arithmetic calculation ...again

    Originally posted by quzah
    Of course it is. Why do you think they call it strlen?

    Quzah.
    oopsie
    hello, internet!

  8. #8
    BlacKLeopard
    Guest

    Question

    Hi Guys....

    Somebody can help me make a simple arithmetic calculation program using STACK.

    i) pop
    ii) push
    iii) ispeek
    iii) isempty
    iv) Isfull

    Plzz... ??

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Somebody can help me make a simple arithmetic calculation program using STACK.

    Look around for some sample code. Try writing something and post your questions here. In other words, yes we'll help, but you have to start it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Push.
    stack[top++] = new_item;

    Pop.
    pop_item = stack[--top];

    Peek.
    peek_item = stack[top - 1];

    Isempty.
    if( top == 0 )

    Isfull.
    if( top == N_ITEMS )

    Stacks are very straightforward.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  11. #11
    wombat
    Guest

    whats wrond with this code?

    could somebody try and fix this code for me?


    void arithmetic()
    {

    char [20];
    int number = 0, result = 0, i;
    char operator = '1';


    printf("Enter input:");
    fgets(input, 20, stdin);


    for(i=0; input[i] != '\n'; i++)
    {
    while(isdigit(input[i]))
    {
    number *= 10;
    number += input[i];
    i++;
    }

    if(operator == '1')
    {
    result += number;
    }
    else
    {
    switch(operator)
    {
    case '+': result += number; break;
    case '-': result -= number; break;
    case '*': result *= number; break;
    case '/': result /= number; break;
    default: printf("Error in switch operator.\n");
    return 0;
    }
    }
    number = 0;

    if(input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')
    {
    operator = input[i];
    printf("Result: %d", result);

    }

    }
    return 0;
    }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >wombat could somebody try and fix this code for me?
    What do you think is wrong with it?

    >char [20];
    Is not a valid statement.
    >char input[20];
    ...would be better.

    >number += input[i];
    This will not work properly, you'll need to convert the ASCII input to a number. Something like
    >number += input[i] - '0';

    Have another go, and post again when you get stuck. And don't forget the code tags.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  2. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  3. arithmetic calculation
    By wombat in forum C Programming
    Replies: 3
    Last Post: 09-02-2002, 05:17 PM
  4. Replies: 1
    Last Post: 11-19-2001, 04:45 PM
  5. Arithmetic Problems using ProC
    By PaulB in forum C Programming
    Replies: 2
    Last Post: 10-10-2001, 02:47 AM