[Courses] answers - lesson 8

anca14 at anca14.ro anca14 at anca14.ro
Sun Nov 10 13:43:45 EST 2002


  Buu. Here are my answers to the exercises for Lesson
8.

=============================================================================

[1] Write a function that counts the number of words in
a string.
    (Your documentation should describe exactly how you
define a
    word.)  Write a program to test your new procedure.

word definition:
   you can type what characters you want, but you MUST
press the spacebar
   softly :) because the counter is incremented when
space is *met* 
   ( SEE -> if(ascii==32), where ascii is obtained
converting the decimal
   character into ASCII character with toascii(), and
32 is the ASCII 
   character for space.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//anca14

#include <stdio.h>
#include <ctype.h>  //needed for toascii()

char string[1000];

int main (void)
{
   printf("string: ");
   fgets(string, sizeof(string), stdin);
   printf("your string has %d words.\n",count());
   return 0;
}

int count(void)
{
   int i,counter=0;
   int ascii;
   
   for(i=0;i<strlen(string);i++)
   {
      ascii=toascii(string[i]);    
      if(ascii==32)
         counter++;   
   }
   return counter+1;   
}

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

SYNOPSIS
     #include <ctype.h>

     int
     toascii(int c);

DESCRIPTION
     The toascii() function strips all but the low 7
bits from a letter,
     including parity or other marker bits.

RETURN VALUES
     The toascii() function always returns a valid
ASCII character.

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

SYNOPSIS
     #include <string.h>

     size_t
     strlen(const char *s);

DESCRIPTION
     The strlen() function computes the length of the
string s.

RETURN VALUES
     The strlen() function returns the number of
characters that precede the
     terminating NUL character.

=============================================================================

[2] Take the calculator program and rewrite it using
functions for each 
of the math functions (add, subtract, multiply,
divide). Add a couple 
of new math functions to the calculator, and implement
them as program 
function calls as well.  Update all of your
documentation for the 
program accordingly. (The calculator program was
introduced in Lesson
Six, and a `switch' modification to it was made in
Lesson Seven.)

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

1] Requirements.
================
All you need is a C compiler, and permission to execute
binaries :) 

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

2] Specification program.
=========================
calc: console calculator with 7 functions
date: Sun Nov 10 18:21:44 EET 2002    ;)
author: anca m. holban

usage:

forevah# ./calc
choose operation.

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

operation: 2
enter 2 numbers: [eg.:14 14]: 67 2
67.00 * 2.00 = 134.00
forevah# ./calc
choose operation.

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

operation: 5
enter 2 numbers: [eg.:14 14]: 9 13
cube(9.00) = 729.00 
cube(13.00) = 2197.00 
forevah# 

BUGS: this program DOES NOT give an error message when
the user enters
      letters instead of numbers. i'm still working at
it :)

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

3] Code design.
===============

i need help here :)

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -     
  
4] Makefile.
============

# Makefile -- for program calc

OBJF = calc.o
CFLG = -g -lm -Wall -ansi -pedantic

target: $(OBJF)
        gcc -o calc $(OBJF) $(CFLG)

$(OBJF) : calc.c
        gcc -c calc.c

clean:
        rm *.o calc

# End of Makefile

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

5] Coding.
==========

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

int operator;


int operation(void)
{
   
   puts("choose operation.\n");
   do 
   {
      puts("0 - add      1 - substract   2 - multiply  
3 - divide");
      puts("4 - square   5 - cube        6 - square
root \n");
         printf("operation: ");
         scanf("%d",&operator);
      } while ( operator < 0 || operator > 8);

return operator;
}


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 div (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)
{

   float x,y;
   
   operation();
   
   printf("enter 2 numbers: [eg.:14 14]: ");
   scanf("%f %f",&x,&y);
   
   switch (operator)
      {
      case 0:
         printf("%.2f + %.2f = %.2f\n",x,y,add(x,y));
         break;
      case 1:
         printf("%.2f - %.2f = %.2f\n",x,y,sub(x,y));
         break;
      case 2:   
         printf("%.2f * %.2f = %.2f\n",x,y,mul(x,y));  
   
         break;
      case 3:
         printf("%.2f / %.2f = %.2f\n",x,y,div(x,y));
         break;
      case 4:
         printf("square(%.2f) = %.2f \n",x,square(x));
         printf("square(%.2f) = %.2f\n",y,square(y));
         break;
      case 5:    
         printf("cube(%.2f) = %.2f \n",x,cube(x));
         printf("cube(%.2f) = %.2f \n",y,cube(y));
         break;
      case 6:
         printf("sqrt(%.2f) = %.2f
\n",x,square_root(x));
         printf("sqrt(%.2f) = %.2f
\n",y,square_root(y));
         break;  
      }  
   }


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


5] Testing.
===========

0 - add:         235 + 123 = 358
1 - substract:   453 - 144 = 309
2 - multiply:     14 *  14 = 196
3 - divide:      500 /  25 =  20
4 - square:      sqr  (9)  =  81
5 - cube:          (3)3    =  27
6 - square root: sqrt(625) =  25

forevah# ./calc
choose operation.

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

operation: 0     
enter 2 numbers: [eg.:14 14]: 235 123
235.00 + 123.00 = 358.00

forevah# ./calc

[...........]

453.00 - 144.00 = 309.00

14.00 * 14.00 = 196.00

500.00 / 25.00 = 20.00

square(9.00) = 81.00 

cube(3.00) = 27.00 

sqrt(625.00) = 25.00 



==============================================================================

that's all, i guess :)
bye
peace :)

anca m. holban



More information about the Courses mailing list