Выбрать главу

The following general programs illustrate the use of various library routines available with the mikroC language.

Example 4.18

Write a function to convert the string pointed to by p into lowercase or uppercase, depending on the value of a mode parameter passed to the function. If the mode parameter is nonzero, then convert to lowercase, otherwise convert it to uppercase. The function should return a pointer to the converted string.

Solution 4.18

The required program listing is given in Figure 4.29 (program CASE.C). The program checks the value of the mode parameter, and if this parameter is nonzero the string is converted to lowercase by calling function ToLower, otherwise the function ToUpper is called to convert the string to uppercase. The program returns a pointer to the converted string.

/*********************************************************************

                CONVERT A STRING TO LOWER/UPPERCASE

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

This program receives a string pointer and a mode parameter. If the mode is 1

then the string is converted to lowercase, otherwise the string is converted

to uppercase.

Programmer: Dogan Ibrahim

File:       CASE.C

Date:       May, 2007

***********************************************************************/

unsigned char *Str_Convert(unsigned char *p, unsigned char mode) {

 unsigned char *ptr = p;

 if (mode != 0) {

  while (*p != '\0') *p++ = ToLower(*p);

 } else {

  while (*p != '\0') *p++ = ToUpper(*p);

 }

 return ptr;

}

Figure 4.29: Program for Example 4.18

Example 4.19

Write a program to define a complex number structure, then write functions to add and subtract two complex numbers. Show how you can use these functions in a main program.

Solution 4.19

Figure 4.30 shows the required program listing (program COMPLEX.C). At the beginning of the program, a data type called complex is created as a structure having a real part and an imaginary part. A function called Add is then defined to add two complex numbers and return the sum as a complex number. Similarly, the function Subtract is defined to subtract two complex numbers and return the result as a complex number. The main program uses two complex numbers, a and b, where,

a = 2.0 – 3.0j

b = 2.5 + 2.0j

/*************************************************************

           COMPLEX NUMBER ADDITION AND SUBTRACTION

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

This program creates a data structure called complex having a real part and

an imaginary part. Then, functions are defined to add or subtract two complex

numbers and store the result in another complex number.

The first complex number is,  a = 2.0 – 2.0j

The second complex number is, b = 2.5 + 2.0j

The program calculates, c = a + b

and,                    d = a − b

Programmer: Dogan Ibrahim

File:       COMPLEX.C

Date:       May, 2007

***************************************************************/

/* Define a new data type called complex */

typedef struct {

 float real;

 float imag;

} complex;

/* Define a function to add two complex numbers and return the result as

 a complex number */

complex Add(complex i, complex j) {

 complex z;

 z.real = i.real + j.real;

 z.imag = i.imag + j.imag;

 return z;

}

/* Define a function to subtract two complex numbers and return the result as

 a complex number */

complex Subtract(complex i, complex j) {

 complex z;

 z.real = i.real - j.real;

 z.imag = i.imag - j.imag;

 return z;

}

/* Main program */

void main() {

 complex a, b, c, d;

 a.real = 2.0; a.imag =−3.0; // First complex number

 b.real = 2.5; b.imag = 2.0; // second complex number

 c = Add(a, b);              // Add numbers

 d = Subtract(a, b);         // Subtract numbers

}

Figure 4.30: Program for Example 4.19

Two other complex numbers, c and d, are also declared, and the following complex number operations are performed:

The program calculates, c = a + b and, d = a – b

Example 4.20

A projectile is fired at an angle of y degrees at an initial velocity of v meters per second. The distance traveled by the projectile (d), the flight time (t), and the maximum height reached (h) are given by the following formulas:

 

Write functions to calculate the height, flight time, and distance traveled. Assuming that g=9.81m/s², v=12 m/s, and θ=45°, call the functions to calculate the three variables. Figure 4.31 shows the projectile pattern.

Figure 4.31: Projectile pattern

Solution 4.20

The required program is given in Figure 4.32 (program PROJECTILE.C). Three functions are defined: Height calculates the maximum height of the projectile, Flight_time calculates the flight time, and Distance calculates the distance traveled. In addition, a function called Radians converts degrees into radians to use in the trigonometric function sine. The height, distance traveled, and flight time are calculated and stored in floating point variables h, d, and t respectively.

/**********************************************************************