[Courses] lesson 9 exercises

anca14 at anca14.ro anca14 at anca14.ro
Tue Nov 12 13:13:39 EST 2002


   buu ;)
  here are my macros. and btw, feel the difference
between
these 2 *testers*. I DID. :)

// used in calc-v1.1 :


// 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;
}

// *discovered* today:

int check_number(char string[])
{
   int i;
   char tester;

   for(i=0;i<strlen(string);i++)
      {
      tester=string[i];
      if (isdigit(i)) 
         continue;
      else
         {
         printf("incorrect key\n");
         exit(1);
         }
      }
   return 0;
}
                  
// ARGH :)

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


[1] Write a macro that returns TRUE if its parameter is
divisble by 10
    and FALSE otherwise.

#define DIV_TEN(x) (remainder(x,10)) ?
printf("FALSE\n") : printf("TRUE\n");



[2] Write a macro is_decnum that returns TRUE if its
argument is a
    decimal number.

#define is_decnum(x) (isdigit(x)) ? printf ("TRUE\n") :
printf("FALSE\n");



[3] Write a second macro is_hexnum that returns TRUE if
its argument
    is a hexadecimal digit (0-9, A-F, a-f). This macro
should reference
    the first macro: is_decnum.

#define is_hexnum(x)  (isxdigit(x)) ? printf("TRUE\n")
: printf ("FALSE\n");



[4] Write a preprocessor macro that swaps two integers.
(For the real
    hacker write one that does not use a temporary
variable declared
    outside the macro.)

i don't have the slightest idea about what am i suposed
to do :)

===============================================================
source codes and tests
======================

// ex. 1
// anca14

#include <stdio.h>
#include <math.h>

// if the remainder equals 0, outputs TRUE, else FALSE
#define DIV_TEN(x) (remainder(x,10)) ?
printf("FALSE\n") : printf("TRUE\n");

int main (void)
{
   double x;
   printf("enter number: ");
   scanf("%lf",&x);

   DIV_TEN(x);

   return 0;
}
++++++++++ TEST +++++++++
forevah# ./1
enter number: 3
FALSE
forevah# ./1
enter number: 30
TRUE

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - 

// ex 2
// anca14 

#include <stdio.h>
#include <ctype.h>

#define is_decnum(x) (isdigit(x)) ? printf ("TRUE\n") :
printf("FALSE\n");

int main (void)
{
   char x;
   
   printf("enter char: ");
   scanf("%c",&x);

   is_decnum(x);

   return 0;
}
+++++++++ TEST +++++++++
forevah# ./2
enter char: 3
TRUE
forevah# ./2
enter char: x
FALSE

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -

// ex 3
// anca14

#include <stdio.h>
#include <ctype.h>

// if isxdigit(x) is not 0, displays TRUE, else FALSE
#define is_hexnum(x)  (isxdigit(x)) ? printf("TRUE\n")
: printf ("FALSE\n");

int main (void)
{
   char x;

   printf("enter char: ");
   scanf("%c",&x);

   is_hexnum(x);

   return 0;
}
+++++++++ TEST ++++++++++
forevah# ./3
enter char: 7
TRUE
forevah# ./3
enter char: d
TRUE
forevah# ./3
enter char: x
FALSE

################################################################################

    peace ;)

        anca m. holban



More information about the Courses mailing list