Thread: Calculator program.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Calculator program.

    Code:
    #include <stdio.h>
    #include <stdlib.h> //for exit function.
    void multiply();
    void divide();
    void add();
    void substract();
    /* The floats should be changed maybe to be rounded off. */
    int main()
    {
      int choise;
      printf("Calculator.\n");
      do                      //Should be changed possibly.
      {
       puts("What would you like to do: \n");
       puts("1 to multiply.");
       puts("2 to divide.");
       puts("3 to add.");
       puts("4 to substract.");
       puts("5 to exit.");
       scanf(" %d", &choise);
       switch (choise)
              {
               case(1) :
                         multiply();
                         break; //prevents other cases from executing
               case(2) :
                         divide();
                         break;
               case(3) :
                         add();
                         break;
               case(4) :
                         substract();
                         break;
               case(5) : exit(1); //stlib.h included
                         break; //Not neccesary just looks neat.
               }
       }
       while((choise < 1) ||(choise > 5));
    
      getchar();
      getchar();
      return 0;
    }
    
    void multiply()
    {
     float a, b, c;
     puts("Enter numbers: ");
     scanf(" %f", &a);
     scanf(" %f", &b);
     c = a * b;
     printf("Answer: %f\n", c);
     return;
    }
    
    void divide()
    {
     float a, b, c;
     puts("Enter numbers: ");
     scanf(" %f", &a);
     scanf(" %f", &b);
     c = a / b;
     printf("Answer: %f\n", c);
     return;
    }
    
    void add()
    {
     float a, b, c;
     puts("Enter numbers: ");
     scanf(" %f", &a);
     scanf(" %f",  &b);
     c = a + b;
     printf("Answer: %f\n", c);
     return;
    }
    
    void substract()
    {
     float a, b, c;
     puts("Enter numbers: ");
     scanf(" %f", &a);
     scanf(" %f", &b);
     c = a - b;
     printf("Answer: %f\n", c);
    }
    This is the code I have, and I'm quite pleased with it. Somebody suggested that the loop should be changed. I could of course make it run infinately until the user decides to exit, but is something else wrong with it?

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Looks good to me, i would suggest that you notice the fact of repeating code within your functions.
    It generally is a good practice, to define a new function the moment you find the need to repeat code.

    You could use that fact to write a function, eg: readAB(float *a,float *b); which would read two floats, and call that function instead of repeating code.

    That way if you ever feel something needs to change about user input, for example some error checking, you have one place to edit and not 4.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I should be passing by adress and not by value to do this?

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    I think it looks fine to, you could make it more optimal so a user can do multiple calculations without having to exit. This could maybe be accomplished by use of a boolean variable to replace the exit statement and add an or condition in the do-while that exits the loop when the boolean variable is false. Other than that, the previous suggestion about doing away with repeated code was good to.

  5. #5
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Thanks for the suggestions.
    A little proof of concept:
    Code:
    #include <stdio.h>
    void function();
    
    int main()
    {
     int i;
     puts("Enter an integer: ");
     scanf("%d", &i);
     function(&i);
     printf("%d", i);
     getchar();
     getchar();
     return(0);
    }
    
    void function(int * i)
    {
     *i = 1 + *i;
     return;
    }
    I think I should be able to apply this on the calculator program.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Looks good, but there is some redundant code you code get rid of. One improvement would be to allow the user to enter an expression in one go, then work it out in one go. That would be a lot more user friendly. The easiest way to do this would be to get the bits as command line parameters instead of breking the string down yourself. Heres an example I made:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double Calc(double left, char op, double right)
    {
    	switch(op)
    	{
    		case '+': return left + right;
    		case '-': return left - right;
    		case 'x': return left * right;
    		case '/': return left / right;
    		default:
    			printf("Invalid operator.\nValid ops: + - x /\n");
    			exit(EXIT_FAILURE);
    	}
    }
    
    int main(int argc, char* argv[])
    {
    	if(argc < 4)
    	{
    		printf("Error: Not enough parameters.\n");
    		exit(EXIT_FAILURE);
    	}
    	if(argc % 2)
    	{
    		printf("Error: Invalid parameters.\n");
    		exit(EXIT_FAILURE);
    	}
    	double left=strtod(argv[1], NULL), right;	
    	int i;	
    	for(i=2; i<argc; i+=2)
    	{
    		right = strtod(argv[i+1], NULL);
    		left = Calc(left, argv[i][0], right);
    	}
    	printf("%f\n", left);
    	exit(EXIT_SUCCESS);
    }
    Its nothing amazing, but it might give you some ideas

Popular pages Recent additions subscribe to a feed