Thread: desperately need help for my 9-function continuous calculator

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    5

    desperately need help for my 9-function continuous calculator

    i've been working on this for 2 days now and i can't seem to get it done. my brain's really drained and i've to submit this in 1 and a half hour that's why i decided to seek help.

    the calculator should be able to calculate the following: 4 basic operations, power (^), factorial (!), sin (s), cos (c), and nth root of a number (r).

    examples of what the user can enter are: (those in bold are user inputs)
    5+5
    10^2
    100-50
    50r2
    7.0710 and so on

    it can also be

    5!
    120-115
    5s
    0.087 and so on

    but all i've accomplished after 2 days is only 1 operation! say 5+5 or 68^2 and so on.. there's no continuity in the calculator.

    i can't seem to figure out the scanf for 3 inputs (say 5^2 in first calculation), 2 inputs (say5+5 in either initial or succeeding calculation) or 1 input (say 5! in succeeding calculation).

    i know the codes for the 9-functions and ran them individually, but i can't seem to put them together!

    i hope someone would help me on this. thanks.
    Code:
    #include <stdio.h>
    double sum(double x,double y);
    double diff(double x,double y);
    double prod(double x,double y);
    double quo(double x,double y);
    double power(double b,double e);
    double factorial(double x);
    double sine(double x);
    double cosine(double x);
    double root(double base,double rad);
    int main(){
        double a,b,result;
        char o;
        printf("Legend\n + - * / ^ ! s c r\n");
        
        for(;1;){
        scanf("&#37;lf%c%lf",&a,&o,&b);
        getchar();
        switch(o)
        {
                 case '+':
                      result=sum(a,b);
                      printf("%.2lf",result);
        
                      break;
                 case '-':
                      result=diff(a,b);
                      printf("%.2lf",result);
    
                      break;
                 case '*':
                      result=prod(a,b);
                      printf("%.2lf",result);
                      break;
                 case '/':
                      result=quo(a,b);
                      printf("%.2lf",result);
    
                      break;
                 case '^':
                      result=power(a,b);
                      printf("%lf",result);
                 
                      break;
                 case '!':
                      result=factorial(a);
                      printf("%.0lf",result);
                 
                      break;
                 case 's':
                      result=sine(a);
                      printf("%.4lf",result);
                 
                      break;
                 case 'c':
                      result=cosine(a);
                      printf("%.4lf",result);
    
                      break;
                 case 'r':
                      
                      result=root(a,b);
                      printf("%.6lf",result);
    
                      break;
                 default:
                         main();
        }                 
        }     
           
           scanf("%c%lf",&o,&a);
           
           switch(o){
                     case '+':
                      result=sum(result,a);
                      printf("%.2lf",result);
                      
                      break;
                 case '-':
                      result=diff(result,a);
                      printf("%.2lf",result);
                      
                      break;
                 case '*':
                      result=prod(result,a);
                      printf("%.2lf",result);
                 
                      break;
                 case '/':
                      result=quo(result,a);
                      printf("%.2lf",result);
                      
                      break;
                 case '^':
                      result=power(result,a);
                      printf("%lf",result);
                      
                      break;
                 case '!':
                      result=factorial(result);
                      printf("%.0lf",result);
                      
                      break;
                 case 's':
                      result=sine(result);
                      printf("%.4lf",result);
                      
                      break;
                 case 'c':
                      result=cosine(result);
                      printf("%.4lf",result);
                      
                      break;
                 case 'r':
                      result=root(result,a);
                      printf("%.6lf",result);
                      
                      break;
                 default:
                         main();
           }
           
           
        getchar();
        getchar();
           
    }       
    double sum(double x, double y){
           double sum;
           sum=x+y;
           return sum;
    }
    double diff(double x, double y){
           double diff;
           diff=x-y;
           return diff;
    }
    double prod(double x, double y){
           double prod;
           prod=x*y;
           return prod;
    }
    double quo(double x,double y){
           double quo;
           quo=x/y;
           return quo;
    }
    double power(double b,double e){
           if(b==0 && e==0){
                printf("Invalid.\n");
                main();
                }
            else if(e==1){
                 return b;
                     }
            else if(e==0){
                 return 1;
                     }
            else{
                 return b*power(b,e-1);
                 }
    }
    double factorial(double x){
            int ctr;
            double fact;
            if(fact<0){
                       printf("Invalid.\n");
                       main();
                       }
            else{
            fact=1;
            for(ctr=1;ctr<=x;ctr++){
                                       fact*=ctr;
                                       }
            return fact;
            }
    }
    double sine(double x){
            int n;
            double sin,final_sin=0;
            for(n=0;n<=200;n++){
                               sin=(power(-1,n)*power(x,(2*n)+1))/factorial((2*n)+1);
                               final_sin+=sin;
                               }
            return final_sin;
    }
    double cosine(double x){
            int n;
            double cos,final_cos=0;
            for(n=0;n<=200;n++){
                               cos=(power(-1,n)*power(x,2*n))/factorial(2*n);
                               final_cos+=cos;
                               }
            return final_cos;
    }
    double root(double base, double rad){
        double up,low,fu,fl,m,fm;
        printf("\nEnter radicand: ");
        scanf("%lf",&rad);
        printf("\nyour equation is x^(%lf) - %lf = 0.",rad,base);
        printf("\nEnter upper limit: ");
        scanf("%lf",&up);
        printf("enter lower limit: ");
        scanf("%lf",&low);
        fu=power(up,rad)-base;
        fl=power(low,rad)-base;
        printf("\nfu=%lf and fl=%lf.",fu,fl);
        if((fu*fl)<0){
                      
                      while((up-low)>0.00001)
                      {
                      m=(up+low)/2;
                      fm=power(m,rad)-base;
                      if((fu*fm)>0){
                                    up=m;
                                    }
                      if((fu*fm)<0){
                                        low=m;
                                        }
                      }
                      }
        else{
             printf("\nInvalid guesses.\n");
             power(base,rad);
             }
        return m;
    }
    Last edited by harlow23; 05-10-2007 at 10:11 PM.

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Perhaps using a string parser? That is, keep reading characters, in stages, until a certain character is encountered. If numbers are encountered, treat them as numbers using atoi and atof to convert the string into a number. When a special character is encountered that isn't a number, it is processed until numbers start again and whatever is found, the related operation/function is used (logged at first). The string is continued to be parsed until either the end or another operator is encountered. Once everything is logged and processed, the numbers are applied and the operator function used based on that. This is just one method but may be a poor method.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    5
    we haven't been introduced to strings so we're not allowed to do it.
    also, math.h shouldn't be used.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > and i've to submit this in 1 and a half hour that's why i decided to seek help.
    You're sunk then on this assignment, it seems that you've just handed it in.

    Given a day, we could have done something meaningful about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    A grammar, a lexical scanner, a syntactical parser, some evaluation here and there ... and maybe 4-5 hours and you could really impress your teacher.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Why oh why are you calling the main function from within the code? Never do this. Main is the starting point for program execution and should never be called.

    main();
    Double Helix STL

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Also, better indentation could work. In addition, why are there many calls to "getchar", two consecutive ones at the end even. Your logic also seems a bit faulty. Take the factorial function for one.

    Code:
    double factorial(double x){
            int ctr;
            double fact;
            if(fact<0){
                       printf("Invalid.\n");
                       main();
                       }
            else{
            fact=1;
            for(ctr=1;ctr<=x;ctr++){
                                       fact*=ctr;
                                       }
            return fact;
            }
    }
    Flagged in a reddish orange color is one of the problems. Fact, in this case, is uninitialized and in your if statement, it should reference the parameter passed to the function.
    Last edited by ulillillia; 05-11-2007 at 01:13 AM. Reason: Improved contrast
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. function pointer calculator
    By bertazoid in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 09:12 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM