[Courses] lesson 8 calculator - improved

anca14 at anca14.ro anca14 at anca14.ro
Mon Nov 11 13:13:44 EST 2002


   buu :)
i've modified the calculator program from lesson 8. 
now, when the user types something else instead of
a number, the program outputs an error message and 
then exits:

## first, calc-v1.1 asks a number between 0 and 6.
 - if the input is not a number, it does smth like
   this:
- - - - - - - - - - - - - - - - - - - - - - - - - 
forevah# ./calc-v1.1 

choose operation [ chacter != number to exit :)] 

0 - add      1 - substract   2 - multiply   3 - divide
4 - square   5 - cube        6 - square root 

operation: a
incorrect key
- - - - - - - - - - - - - - - - - - - - - - - - - -

 - if the input is a number between 7 and 9:
- - - - - - - - - - - - - - - - - - - - - - - - - -
forevah# ./calc-v1.1

choose operation [ chacter != number to exit :)]

0 - add      1 - substract   2 - multiply   3 - divide
4 - square   5 - cube        6 - square root 

operation: 8
0 - add      1 - substract   2 - multiply   3 - divide
4 - square   5 - cube        6 - square root 

operation: 
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
  the loop ends when is typed a number between 0 and 6
  or a chacter that is not a number

## after the first step, the program asks two numbers.
  if one of them is not a number, it displays "incorrect
  key" and exits.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - 
forevah# ./calc-v1.1

choose operation [ chacter != number to exit :)]

0 - add      1 - substract   2 - multiply   3 - divide
4 - square   5 - cube        6 - square root 

operation: 0
enter the first number: 23
enter the second number: fd
incorrect key
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- 

i guess calc-v1.1 ain't *buggy* anymore :)

here is the source code:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// calc-v1.1

// anca14

#include <stdio.h>
#include <math.h>
#include <stdlib.h> //needed for atof() and atoi()

// function prototype
int check_number();

// global variable
int operator;

// asks a character, puts it in a string, and calls
check_number
// to verify if it's a number; uses atoi() to convert
it to integer
int operation(void)
{
   char operator_check[2];
    
   puts("\nchoose operation [ chacter != number to exit
:)]\n");
   do  
   {
      puts("0 - add      1 - substract   2 - multiply  
3 - divide");
      puts("4 - square   5 - cube        6 - square
root \n");
         printf("operation: ");
         scanf("%s",operator_check);

         check_number(operator_check);
         operator=atoi(operator_check);

   } while (operator <0 || operator >6);

return operator;
}


// checks if the input character is a number;
// if it isn't, displays an error message and
// exits the program.
int check_number(char string[])
{
   int i;
   char tester;

   for(i=0;i<strlen(string);i++)
      {
      tester=string[i];
      if (tester=='0' || tester=='1' || tester=='2' ||
          tester=='3' || tester=='4' || tester=='5' ||
          tester=='6' || tester=='7' || tester=='8' ||
          tester=='9')
         continue;
      else
         {
         printf("incorrect key\n");
         exit(1);
         }
      }
   return 0;
}



// addition, substraction, etc.
float add (float x, float  y)
   {
   return ( x + y );
   }
float  sub (float x, float y)
   {
   return (x-y);
   }
float  mul (float x, float y)
   {
   return (x*y);
   }
float divi (float x, float y)
   {
   return (x / y);
   }     
float square(float x)
   {
   return ( (float) pow(x, 2));
   }
float square_root(float x)
   {   
   return ( (float) sqrt(x));
   }
float cube (float x)
   {
   return  ( (float) pow(x, 3));
   }



int main(void)
{

   char xf[1000],yf[1000];
   float x,y;
   
   operation();

   printf("enter the first number: ");
   scanf("%s",xf);
   x=atof(xf);
   check_number(xf);

   printf("enter the second number: ");
   scanf("%s",yf);
   y=atof(yf);
   check_number(yf);

   switch (operator)
      {
      case 0:
         printf("\n%.2f + %.2f =
%.2f\n\n",x,y,add(x,y));
         break;
      case 1:
         printf("\n%.2f - %.2f =
%.2f\n\n",x,y,sub(x,y));
         break;
      case 2:
         printf("\n%.2f * %.2f =
%.2f\n\n",x,y,mul(x,y));
         break;
      case 3:
         printf("\n%.2f / %.2f =
%.2f\n\n",x,y,divi(x,y));
         break;
      case 4:
         printf("\nsquare (%.2f) = %.2f
\n",x,square(x));
         printf("square (%.2f) = %.2f\n\n",y,square(y));
           square(x);
         break;
      case 5:
         printf("\ncube(%.2f) = %.2f \n",x,cube(x));
         printf("cube(%.2f) = %.2f \n\n",y,cube(y));
         break;
      case 6:
         printf("\nsquare root (%.2f) = %.2f
\n",x,square_root(x));
         printf("square root (%.2f) = %.2f
\n\n",y,square_root(y));
         break;
      // default is not needed beacause of the
conditions
      // i've put for the operation option.
      // see-> while (operator <0 || operator >6);
      }
    return 0;
   }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   that's all, falks.
   peace ;)
  
      anca m. holban



More information about the Courses mailing list