presentation source

Download Report

Transcript presentation source

Real World
Applications:
Computing Resistance
• Problem
– Given single-character codes
for the colored bands that
mark a resistor, compute its
resistance in ohms. The
color codes are as follows:
Black 0, Brown 1,
Red 2, Orange 3, Yellow 4,
Green 5, Blue 6,
Violet 7,Gray 8, White 9.
– If the integer codes of the
bands are (in order) c1, c2,
and c3, the resistance in
ohms is
R = (10*c1+c2)*10c3
Sample
Input/Output
• What is the resistance for
a resistor with color mark
?
The colored bands are coded as follows:
COLOR
CODE
-------Black--------> B
Brown--------> N
Red----------> R
Orange-------> O
Yellow-------> Y
Green--------> G
Blue---------> E
Violet-------> V
Gray---------> A
White--------> W
Enter three codes. ERO
Resistance in ohms: 62000.000000
Main Program
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_codes(void);
double decode_char(char code);
main()
{
char code1, code2, code3;
double R;
double c1, c2, c3;
int flag;
print_codes();
printf("\n\nEnter three codes. ");
code1 = getchar();
code2 = getchar();
code3 = getchar();
c1 = decode_char(code1);
c2 = decode_char(code2);
c3 = decode_char(code3);
if(c1 == -999.0 || c2 == -999.0 || c3 == -999.0)
printf("\n\n\tBad code - cannot compute R.\n");
else {
R = (10.0*c1 + c2)*pow(10.0, c3);
printf("\n\n\tResistance in ohms:\t%f\n", R);
}
return EXIT_SUCCESS;
}
Print_codes
function
/*
This function prints a menu of color
codes to guide the user in entering input.
*/
void print_codes(void)
{
printf("\n\n\tThe colored bands"
" are coded as follows:\n\n\t");
printf("COLOR\t\t\tCODE\n\t");
printf("-----\t\t\t----\n\n");
printf("\tBlack--------> B\n");
printf("\tBrown--------> N\n");
printf("\tRed----------> R\n");
printf("\tOrange-------> O\n");
printf("\tYellow-------> Y\n");
printf("\tGreen--------> G\n");
printf("\tBlue---------> E\n");
printf("\tViolet-------> V\n");
printf("\tGray---------> A\n");
printf("\tWhite--------> W\n");
}
Decode_char
function
/*
This function expects a character (color code) and
returns a double precision floating-point number as its
value. If the code is not legal, it returns a value that
signals that fact. */
double decode_char(char code)
{
switch (code) {
case 'B':
return 0.0;
case 'N':
return 1.0;
case 'R':
return 2.0;
case 'O':
return 3.0;
case 'Y':
return 4.0;
case 'G':
return 5.0;
case 'E':
return 6.0;
case 'V':
return 7.0;
case 'A':
return 8.0;
case 'W':
return 9.0;
default:
return -999.0;
}
}
The Preprocessor
• The C Preprocessor
processes a C source file
before the compiler
translates the program
into object code.
• The preprocessor follows
the programmer's
(preprocessing) directives.
• All preprocessor directives
start with the character #.
The CC command
• When you issue the cc or gcc
command,
– The C preprocessor (known
as cpp on our system)
processes the C source file.
Processing means including
the files in #include <. . .>,
replacing macro definition by
its replacement text, etc.
– C compiler (known as cc1 on
our system) compiles the
resulting file into object file.
– The loader (known as ld)
finally uses the object file to
produce executable
program.
#include ...
• The preprocessor directive
#include <stdio.h>
includes a copy of this file
at the point of this
command. The file is
located at "standard"
locations.
#include "mydefs.h"
includes the file mydefs.h
from the working
directory.
#define ...
#define EOF (-1)
• associates EOF with (-1):
every occurrence of EOF is
replaced by the text (-1)
by the preprocessor. The
defined identity EOF is
called a macro.
• Use this form of macro if a
constant is used
frequently in a program.
• Macro definition can only
be logically one line. Use
backslash to continue a
line. E.g.,
#define EOF \
(-1)
Some macro
definitions in
Math.h
#define M_E 2.7182818284
#define M_PI 3.141592653
#define M_SQRT2 1.4142135
In your program, instead of
using numerical numbers,
you can use the symbolic
names for the constants.
E.g.
X = sin(M_PI/4);
y = pow(M_E, x);
Parameterized
Macros
#define SQR(x)
((x)*(x))
• After this definition, the
preprocessor replaces
every occurrence of the
form
SQR(a)
by the replacement text
((a)*(a))
For example:
w = SQR(x) + 1;
z = SQR(w+1);
becomes
w = ((x)*(x)) + 1;
z = ((w+1)*(w+1));
Putting as many
parentheses as
possible in macros
#define SQR(x)
x*x
• The preprocessor replaces
every occurrence of the form
SQR(a)
by the replacement text
a*a
For example:
w = SQR(x) + 1;
z = SQR(w+1);
becomes
w = x*x + 1;
z = w+1*w+1;
This is 2w+1, not
(w+1)2 as intended.
Parameterized
Macros
#define PRINT3(e1,e2,e3) \
printf("\n%c\t%c\t%d", \
(e1), (e2), (e3) )
• After this definition, the
preprocessor replaces
every occurrence of the
form
PRINT3(a,b,c)
by the replacement text
printf("\n%c\t%c\t%d",
(a), (b), (c) )
Macros Versus
Functions
#define min(x,y) \
( ( (x)<(y) )?(x):(y) )
• After this definition, the
preprocessor replaces
every occurrence of the
form
min(a,b)
with the replacement text
( ( (a)<(b) )?(a):(b) )
where a and b can be ANY
text.
Macros are not functions.
There are no arguments
passing, or return value to
talk about.
Conditional,
Miscellaneous
#undef MC
cancels the definition made to
the symbol MC.
#define MC
makes MC defined (not to any
particular replacement text).
#ifdef MC
#define ROWS
#define COLS
#endif
1000
1000
• defines ROWS and COLS only if
MC is defined.
Conditional,
Miscellaneous
#ifndef MC
#define ROWS
#define COLS
#endif
1000
1000
• defines ROWS and COLS only if
MC is not defined.
#if defined(MC)
#define ROWS 10
#elif defined(MM)
#define ROWS 20
#else
#define ROWS 0
#endif
Reading/Home
Working
• Read Chapter 5, page 172
to 190.
• Work on Problems
– Section 5.5, page 176,
exercise 1, 3.
– Section 5.6, page 190,
exercise 1, 3, 7, 9, 15.
• Check your answers in the
back of the textbook. Do
not hand in.