Page 2 of 10 FirstFirst 1234 ... LastLast
Results 11 to 20 of 91

Thread: Beginner Programming Challenge #11

  1. #11
    Join Date
    Mar 2010
    Beans
    31
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Beginner Programming Challenge #11

    Quote Originally Posted by Compyx View Post
    I would suggest breaking the code up into functions, adding some whitespace here and there and indenting properly. That would make the code easier to read, debug and extend.

    The C idiom for `while (true)' is `while (1)' or (less common and less readable) `for ( ; ; )'.

    You can define true if you want:
    Code:
    #define true 1
    #define false 0
    or:
    Code:
    typedef enum { false, true } bool;
    will give you a boolean type should you need it.
    thanks the while (1) seams perfect
    whats wrong with my indentation apart from the cases

  2. #12
    Join Date
    Mar 2010
    Beans
    31
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Beginner Programming Challenge #11

    PHP Code:
    //Beginner Programming Challenge #11
    #include <stdio.h> //standard for i/o
    #include <stdlib.h> // standard again
    #include <string.h> // string library
    #include <math.h> // for the power function
    int i,true=2,k[99],is,is2,w,iv,si,dubios,nc,number,zero,neo,num[99];
    /*
     i is standard
     true is something that is always true since i don't know the c version of "while true"
     is is the empty spaces in the string counter
     is2 is the 'how much till the end' counter
     w is like a secondary i
     si counts how many words are between two spaces since operators can only use one space
     dubios seeks dubious behaviour
     nc is the number counter
     number is the temporary number
     zero alerts if someone wants to divide by 0
     neo is the not enough operators counter
     num is the number array 
     */
    char x[100];
    //the string
    float n_to_f;
    // for the dubious output
    int main(){
        while (
    true){
            for (
    i=0;i<99;++inum[i]=k[i]=0// reset the arrays
            
    i=is=is2=w=iv=si=dubios=nc=zero=neo=number=0// reset the variables
            
    printf("> ");
            
    gets(x);
            if (
    strcmp(x,"exit")== 0) break;
            for (
    i=0;i<strlen(x);++i){ //find empty spaces and keep track of them in k
                
    if (x[i]==' ') {
                        
    k[is] = i
                        ++
    is;
                }
            }
            if (
    k[is] != strlen(x)) {k[is]=strlen(x);++is;} // add to the counter the last element
            
    iv=0;// needed later on
            
    for (w=0;w<is;++w){  // for all the empty spaces
                
    is2=k[w]-iv//how many characters does the string fragment have
                
    number=0//reset the number
                
    for (i=iv;i<k[w];++i){ //figure out what the number is
                    
    si=is2;
                    switch(
    x[i]){
                        case 
    '0'...'9'number += ((x[i] - '0') * pow10.0 is2-1));
                                        --
    is2 ;
                                        if (
    is2==0) { //when is reaches 0 save the number 
                                            
    num[nc]=number;
                                            ++
    nc;
                                        }
                                        break;    
                        case  
    '+':    if (si==1){ 
                                        if(
    nc>1){ 
                                            
    num[nc-2]=num[nc-2]+num[nc-1]; 
                                            
    num[nc-1]=num[nc];
                                            --
    nc;
                                        } 
                                        else ++
    neo;
                                    } 
                                    else ++
    dubios;break;
                        case  
    '-':    if (si==1){ 
                                        if(
    nc>1){ 
                                            
    num[nc-2]=num[nc-2]-num[nc-1]; 
                                            
    num[nc-1]=num[nc];
                                            --
    nc;
                                        } 
                                        else ++
    neo;
                                    } 
                                    else ++
    dubios;break;
                        case  
    '/':    if (si==1){ 
                                        if(
    nc>1){ 
                                            if (
    num[nc-1]!=0){ 
                                                
    num[nc-2]=num[nc-2]/num[nc-1]; 
                                                
    num[nc-1]=num[nc]; 
                                                --
    nc;
                                            } 
                                            else ++
    zero;
                                        }  
                                        else ++
    neo;
                                    } 
                                    else ++
    dubios;break;
                        case  
    '*':    if (si==1){ 
                                        if(
    nc>1){ 
                                            
    num[nc-2]=num[nc-2]*num[nc-1]; 
                                            
    num[nc-1]=num[nc]; 
                                            --
    nc;
                                        } 
                                        else ++
    neo;
                                    } 
                                    else ++
    dubios;break;
                        case  
    '^':    if (si==1){ 
                                        if(
    nc>1){ 
                                            
    num[nc-2]=pow(num[nc-2],num[nc-1]); 
                                            
    num[nc-1]=num[nc]; 
                                            --
    nc;
                                        } 
                                        else ++
    neo;
                                    } 
                                    else ++
    dubios;break;
                        case  
    '%':    if (si==1){ 
                                        if(
    nc>1){ 
                                            
    num[nc-2]=num[nc-2]%num[nc-1]; 
                                            
    num[nc-1]=num[nc]; 
                                            --
    nc;
                                        } 
                                        else ++
    neo;
                                    } 
                                    else ++
    dubios;break;
                        
    //for all cases of operands if the string fragment is wider than 1 caracter mark it as dubious behavior and if you dont have at least 2 operands mark it down
                        
    default: ++dubios;break;
                        
    //anything else goes to dubious behaviour
                    
    }
                }
                
    iv=k[w];//we need this to find out the word size
                
    ++iv;
            }
        
    n_to_f=num[nc-1];//transform it to float because thats what the post asks for
        
    if (dubios==0){//if nothing is dubious ... this should be obvious
            
    if (zero!=0printf("Divided by Zero\n"); 
            else if (
    neo>0printf("Not enough operands\n");
            else if (
    neo==&&nc>1printf("Too many operands\n");
            else if (
    nc==0); //if empty string
            
    else printf("%.1f\n",n_to_f); }// if everything goes to plan print the number
         
    else printf("Unrecognized token\n");
        }
    return 
    0;

    Last edited by tooatw; March 29th, 2010 at 05:06 PM.

  3. #13
    Join Date
    Mar 2006
    Location
    Eefde, The Netherlands
    Beans
    432
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question Re: Beginner Programming Challenge #11

    Quote Originally Posted by tooatw View Post
    whats wrong with my indentation apart from the cases
    Well, it's more a matter of too many indents, that's why I suggested breaking up the code into smaller pieces (functions), it'll make the code more readable and cut down on the number of variables needed.

    For example the whole operator handling could be stored in a separate function:
    Code:
    int eval_operator(int operator, int operand1, int operand2)
    {
        int result;
    
        switch (operator) {
    
            case '+':
                /* TODO: catch overflow before it happens */
                result = operand1 + operand2;
                break;
    
            case '-':
                result = operand1 - operand2;
                break;
    
            ...
    
            default:
                fprintf(stderr, "unsupported operator '%c'!\n", operator);
                result = SOME_ERROR_CODE;
                break;
        }
        return result;
    }
    Some people prefer to line up their cases with the switch statement, but that's just a matter of taste, as long as you're consistent.

    Oh, and never use gets(), it's a buffer overflow waiting to happen, use fgets():
    Code:
    fgets(x, 100, stdin);
    Last edited by Compyx; March 29th, 2010 at 05:40 PM. Reason: add suggestion about fgets()

  4. #14
    Join Date
    Jun 2006
    Beans
    2,930

    Re: Beginner Programming Challenge #11

    Is it just me, or are these Beginner challenges more likely suited for the intermediate challenges?
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  5. #15
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner Programming Challenge #11

    Quote Originally Posted by lavinog View Post
    Is it just me, or are these Beginner challenges more likely suited for the intermediate challenges?
    RPN is actually pretty simple to deal with. Every token is whitespace delimited, and evaluation can be implemented easily with a stack. The extra (extra) credit, though, it probably above beginner level - hence it's optional. These are supposed to be challenges, after all.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  6. #16
    Join Date
    Mar 2010
    Beans
    31
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Beginner Programming Challenge #11

    Quote Originally Posted by lavinog View Post
    Is it just me, or are these Beginner challenges more likely suited for the intermediate challenges?
    i'm a beginner
    originally i wanted to get the elements with %s but i dropped it for some reason
    like
    while (1)
    scanf("%s",x)
    and take every s separately with the same switch that i have now
    there are many ways that a beginner can think of
    just for the record, the first time i ever implemented something was about 3 months ago and i failed miserably
    i only started programming for the past 2-3 weeks

  7. #17
    Join Date
    Mar 2006
    Location
    Eefde, The Netherlands
    Beans
    432
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner Programming Challenge #11

    Infix to postfix isn't that hard either, and it too can be implemented using a stack (and a queue).
    The algorithm is a bit more complex than a postfix calculator, and increases when adding unary/ternary operators and function-calls.

  8. #18
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner Programming Challenge #11

    My python entry (now works for every input)
    PHP Code:
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    from sys import exit
    import math

    class Stack (object):                           #  stack class, allows pushing, popping
        
    def __init__(self):                         #+ could be used somewhere else ;)
            
    self.stack = []

        
    def push(selfval):
            
    self.stack = [val] + self.stack

        def pop
    (self):
            
    val self.stack[0]
            
    self.stack.remove(self.stack[0])
            return 
    val

        def __str__
    (self):                     # for the print stack bit below
            
    if(self.stack[0] % == 0):
                return 
    str(int(self.stack[0])) # if the number was whole
            
    return str(self.stack[0])          # else

    def main():
        
    ans 0
        
    print "Everything must be spaced seperated"
        
    while True:
            
    stack Stack()
            try:
                
    string raw_input("Enter your sum: ")
            
    except:
                print 
                exit()
            if(
    string == ""):
                
    pass
            elif
    (string != "Q"):
                
    split string.split(" ")
                for 
    token in split:
                    try:
                        if(
    token == "+"):
                            
    stack.push(stack.pop()+stack.pop())
                        
    elif(token == "-"):
                            
    stack.pop()
                            
    stack.pop()
                            
    stack.push(a)                    
                        
    elif(token == "*"):
                            
    stack.push(stack.pop()*stack.pop())
                        
    elif(token == "/"):
                            
    float(stack.pop())
                            if(
    == 0):
                                print 
    "Divide by 0 error"
                                
    continue
                            
    float(stack.pop())
                            
    stack.push(a)        
                        
    elif(token == "%"):
                            
    float(stack.pop())
                            
    float(stack.pop())
                            
    stack.push(a)
                        
    elif(token == "^"):
                            
    float(stack.pop())
                            
    float(stack.pop())
                            
    stack.push(** a)        
                        
    elif(token == "ans"):
                            
    stack.push(ans)    
                        
    elif(token == "sqrt"):
                            
    stack.push(math.sqrt(stack.pop()))
                        
    elif(token == "cos"):
                            
    stack.push(math.cos(stack.pop()))    
                        
    elif(token == "sin"):
                            
    stack.push(math.sin(stack.pop()))
                        
    elif(token == "fact"):
                            
    stack.push(math.factorial(stack.pop()))    
                        
    elif(token == "pi"):
                            
    stack.push(math.pi)
                        else:
                            
    stack.push(int(token))
                    
    except Exception as e:                        #  if there was an error
                        
    print stack.stack                         #+ its probably the input
                        
    print "Error in input: " str(e)
                        exit()

                print 
    stack          # print result
                
    ans stack.stack[0# for recall later
            
    else:               
                exit() 
    # if it is Q to quit, quit

    if __name__ == '__main__':
        
    main() 
    Output:
    Code:
    matthew@matthew-desktop:~/Documents/Programming/Python$ python reverse_polish.py 
    Everything must be spaced seperated
    Enter your sum: 3 4 - 5 +
    4
    Enter your sum: 3 4 5 * -
    -17
    Enter your sum: 5 1 2 + 4 * + 3 -#
    [3, 17]
    Error in input: invalid literal for int() with base 10: '-#'
    matthew@matthew-desktop:~/Documents/Programming/Python$ python reverse_polish.py 
    Everything must be spaced seperated
    Enter your sum: 5 2 ^
    25
    Enter your sum: 10 5 %
    0
    Enter your sum: 10 3 /
    3.333333333333333
    Enter your sum: 10 0 /
    Divide by 0 error
    10
    Enter your sum: 10 sin
    -0.544021110889
    Enter your sum: 10 cos
    -0.839071529076
    Enter your sum: 10 sqrt
    3.16227766017
    Enter your sum: 10 fact
    3628800
    Enter your sum: pi
    3.14159265359
    Enter your sum: Q
    matthew@matthew-desktop:~/Documents/Programming/Python$
    Last edited by matmatmat; March 31st, 2010 at 08:36 PM. Reason: watch as more and more useless things are added

  9. #19
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: Beginner Programming Challenge #11

    Quote Originally Posted by lavinog View Post
    Is it just me, or are these Beginner challenges more likely suited for the intermediate challenges?
    Interesting point. I vaguely recall a related programming task coming up in one of the courses available to second-year students at the University I attended, but one I don't think I took that subject (too much partying? wrong course? it was a long tima ago). If memory serves correctly it was related to converting regular algebraic expressions to RPN (a.k.a. infix to postfix)
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  10. #20
    Join Date
    Mar 2010
    Beans
    31
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Beginner Programming Challenge #11

    Quote Originally Posted by schauerlich View Post
    Example output, for testing:
    Code:
    eric@river:~$ ./rpn
    > 3 4 +
    7.0
    > 10 3 /
    3.33333333333
    > 2 5 ^
    32.0
    >  10 2 %
    0.0
    > 24 12 3 + 4 % / 6 - 4 * 3 ^
    512.0
    > 10 2 2 % /
    Divided by Zero
    // Incorrect inputs:
    > 10 + 10
    Not enough operands
    > 10 10 10 +
    Too many operands
    > 
    > aw4ojghsiu5esrgs56u7ikdyjdt drthisu 5 hrtgh 5 5 +
    Unrecognized token
    > 45 6 + / & 4
    Unrecognized token
    > q
    eric@river:~$
    i see that you modified it
    so tell us exactly what happens
    if the result is an integer only display one digit after the dot (why?) otherwise display 10?
    why cant it be standard one digit after the , or standard 10

Page 2 of 10 FirstFirst 1234 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •