Systemnahe Programmierung in C: Anweisungen für Mehrweg-Verzweigungen |
|
1#include <stdio.h>
2
3void
4star (int x)
5{
6 switch (x)
7 {
8 case 1:
9 printf ("*");
10
11 case 2:
12 printf ("**");
13
14 case 3:
15 printf ("***");
16
17 case 4:
18 printf ("****");
19 }
20}
|
1#include <stdio.h>
2
3void
4star (int x)
5{
6 switch (x)
7 {
8 case 1:
9 printf ("*");
10 break;
11
12 case 2:
13 printf ("**");
14 break;
15
16 case 3:
17 printf ("***");
18 break;
19
20 case 4:
21 printf ("****");
22 break;
23 }
24}
|
1int
2switchFuntion (char ch)
3{
4 switch (ch)
5 {
6 case 'A':
7 return 1;
8
9 case 'B':
10 return 2;
11
12 case 'C':
13 return 3;
14
15 case 'D':
16 return 4;
17
18 default:
19 return -1;
20 }
21}
|
1int
2isPunc (char c)
3{
4 switch (c)
5 {
6 case '.':
7 case ',':
8 case ';':
9 case ':':
10 case '!':
11 return 1;
12
13 default:
14 return 0;
15 }
16}
|
1#include <stdio.h>
2
3enum errorNumbers
4{
5 ERR_INPUT_VAL,
6 ERR_OPERAND,
7 ERR_OPERATOR,
8 ERR_TYPE
9};
10
11/* wenig elegante Routine zur Fehlerausgabe */
12
13void
14printError (int errorCode)
15{
16 switch (errorCode)
17 {
18 case ERR_INPUT_VAL:
19 printf ("Fehler: falscher Eingabewert.\n");
20 break;
21
22 case ERR_OPERAND:
23 printf ("Fehler: falscher Operand.\n");
24 break;
25
26 case ERR_OPERATOR:
27 printf ("Fehler: unbekannter Operator.\n");
28 break;
29
30 case ERR_TYPE:
31 printf ("Fehler: Inkompatible Werte.\n");
32 break;
33
34 default:
35 printf ("Fehler: unbekannter Fehler %d\n", errorCode);
36 break; /* redundant */
37 }
38}
|
|
1extern void processPrime (int x);
2extern void processComposite (int x);
3extern int prime (int x);
4
5void
6processNumberSimple (int x)
7{
8
9 if (prime (x))
10 processPrime (x);
11 else
12 processComposite (x);
13
14}
15
16void
17processNumberEfficientlyButWithLongCode (int x)
18{
19
20 switch (x)
21 {
22
23 case 2:
24 case 3:
25 case 5:
26 case 7:
27 processPrime (x);
28 break;
29
30 case 4:
31 case 6:
32 case 8:
33 case 9:
34 case 10:
35 processComposite (x);
36 break;
37
38 default:
39 if (prime (x))
40 processPrime (x);
41 else
42 processComposite (x);
43 break;
44 }
45}
46
47void
48processNumberTricky (int x)
49{
50
51 switch (x)
52 default:
53 if (prime (x))
54 case 2:
55 case 3:
56 case 5:
57 case 7:
58 processPrime (x);
59 else
60 case 4:
61 case 6:
62 case 8:
63 case 9:
64 case 10:
65 processComposite (x);
66
67}
|
|
1#include <limits.h>
2
3int
4largeSwitch (long int i)
5{
6 switch (i)
7 {
8 case LONG_MIN:
9 return 1;
10 case 0:
11 return 2;
12 case LONG_MAX:
13 return 3;
14 default:
15 return 0;
16 }
17}
|
Letzte Änderung: 03.11.2016 | © Prof. Dr. Uwe Schmidt |