Download presentation
Presentation is loading. Please wait.
Published byJeffry Neal Modified over 9 years ago
1
C Program Design Introduction to C Programming 主講人:虞台文
2
Content Background Main Parts of C Programs 標準輸出入裝置與輸出入函式 標準輸出入函式 算術運算子 (Arithmetic Operators ) 關係運算子 (Relation Operators ) 邏輯運算子 (Logical Operators) 巨集 The #define Preprocessor
3
C Program Design Introduction to C Programming Background
4
C History Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming – Operating systems – Utility programs – Compilers – Filters Evolved from B, which evolved from BCPL The Development of the C Language
5
Computer Architecture ALU Control CPU Input Output Memory Input Device Output Device
6
C Program Design Introduction to C Programming Main Parts of C Programs
7
Our First C Program Hello World
8
/* HelloWorld.cpp Our first program */ #include using namespace std; /* function main begins program execution */ main() { cout << "hello, world\n"; } /* HelloWorld.cpp Our first program */ #include using namespace std; /* function main begins program execution */ main() { cout << "hello, world\n"; }
9
Some Common Escape Sequences
10
Editing Compiling Linking Execution
11
C Program Design Introduction to C Programming 算術運算子 (Arithmetic Operators)
12
Arithmetic Operators
13
Precedence of Arithmetic Operators
14
範例 : y = 2x 2 +3x+7 // PolynormialEval.cpp #include using namespace std; int main(int argc, char *argv[]) { int x, y; cout << "x = "; /* prompt */ cin >> x; /* read x */ /* Evaluate y = 2x^2 + 3x + 7 */ y = 2 * x * x + 3 * x + 7; cout << "y = 2 * " << x << " * " << x ; cout << " + 3 * " << x << " + 7 = " << y << endl; system("PAUSE"); return EXIT_SUCCESS; } // PolynormialEval.cpp #include using namespace std; int main(int argc, char *argv[]) { int x, y; cout << "x = "; /* prompt */ cin >> x; /* read x */ /* Evaluate y = 2x^2 + 3x + 7 */ y = 2 * x * x + 3 * x + 7; cout << "y = 2 * " << x << " * " << x ; cout << " + 3 * " << x << " + 7 = " << y << endl; system("PAUSE"); return EXIT_SUCCESS; } y = 2 * x * x + 3 * x + 7;
15
範例 : y = 2x 2 +3x+7 y = 2 * x * x + 3 * x + 7;
16
C Program Design Introduction to C Programming 關係運算子 (Relation Operators)
17
Relation Operators in C The result of a relation operation is true (nonzero) or false (zero).
18
Statements Simple Statements lower = 0; upper = 300; step = 20; fahr = lower; Null Statement ; // a null statement Compound Statements (block statements) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } 4 simple statements 1 compound statement
19
If-Else Statement if (expression) statement 1 else statement 2
20
If-Else Statement if (expression) statement 1 else statement 2 expression If expression is evaluated to true (nonzero), statement 1 is executed; otherwise, statement 2 is executed. If expression is evaluated to true (nonzero), statement 1 is executed; otherwise, statement 2 is executed. start end expression statement 1 statement 2 truefalse
21
If-Else Statement if (expression) statement 1 else statement 2 expression start end expression statement 1 statement 2 truefalse option
22
If- Statement if (expression) statement expression start end expression statement true false
23
範例 : Decision Making (I) // pass1.cpp #include using namespace std; int main(int argc, char *argv[]) { int threshold = 75; int score; cout << "Enter your score, please\n"; cin >> score; if (score >= threshold){ cout << "Incredible, you passed with a merit\n"; } system("PAUSE"); return 0; } // pass1.cpp #include using namespace std; int main(int argc, char *argv[]) { int threshold = 75; int score; cout << "Enter your score, please\n"; cin >> score; if (score >= threshold){ cout << "Incredible, you passed with a merit\n"; } system("PAUSE"); return 0; }
24
範例 : Decision Making (II) // pass2.cpp #include using namespace std; int main(int argc, char *argv[]) { int threshold = 75; int score; cout << "Enter your score, please\n"; cin >> score; if (score >= threshold){ cout << "Incredible, you passed with a merit\n"; } else{ cout << "You failed, unlucky\n"; } system("PAUSE"); return 0; } // pass2.cpp #include using namespace std; int main(int argc, char *argv[]) { int threshold = 75; int score; cout << "Enter your score, please\n"; cin >> score; if (score >= threshold){ cout << "Incredible, you passed with a merit\n"; } else{ cout << "You failed, unlucky\n"; } system("PAUSE"); return 0; }
25
While- Statement while(expression) statement expression start end expression statement true false
26
範例:華氏 攝氏
28
#include using namespace std; /* print Fahrenheit-Celsius table for fahr = 0, 20,..., 300 */ int main(int argc, char *argv[]) { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; cout << fahr << "\t" << celsius << endl; fahr = fahr + step; } system("PAUSE"); return 0; } #include using namespace std; /* print Fahrenheit-Celsius table for fahr = 0, 20,..., 300 */ int main(int argc, char *argv[]) { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; cout << fahr << "\t" << celsius << endl; fahr = fahr + step; } system("PAUSE"); return 0; }
29
練習 1. Using 2. Redo the same task but with the starting number, ending number, and step being given by a user.
30
練習 3. Write a C program which asks a user to input a positive integer. The program then reports the minimum factor of that number in 2, 3, 5, 7, 11 if available. Otherwise, reports “fail to find any factor in 2, 3, 5, 7, 11.” 4. The same as above, but the program reports all factors of that number in 2, 3, 5, 7, 11. 5. The same as above, but the program reports all possible factors of that number.
31
C Program Design Introduction to C Programming 邏輯運算子 (Logical Operators)
32
Logical Operators && ( logical AND ) – Returns true if both conditions are true || ( logical OR ) – Returns true if either of its conditions are true ! ( logical NOT, logical negation ) – Reverses the truth/falsity of its condition – Unary operator, has one operand
33
Truth Tables
34
Examples if((celsius >= 25.0) && (celsius <= 28.0)) printf("It is very comfortable\n"); if((celsius >= 25.0) && (celsius <= 28.0)) printf("It is very comfortable\n"); if((celsius 32.0)) printf("It is bad\n"); else printf("It is comfortable\n"); if((celsius 32.0)) printf("It is bad\n"); else printf("It is comfortable\n"); if(!(celsius <= 18.0)) printf("It is not very cold\n"); else printf("It is better to take a jacket\n"); if(!(celsius <= 18.0)) printf("It is not very cold\n"); else printf("It is better to take a jacket\n");
35
注意事項 int total, count;......... if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); count 若為零將產生 divide by zero 之 exception.
36
注意事項 int total, count;......... if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(total / count >= 60 && count > 0) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(total / count >= 60 && count > 0) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(count > 0 && total / count >= 60) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(count > 0 && total / count >= 60) printf("Passed\n"); else printf("Failed\n"); Why?
37
注意事項 int total, count;......... if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); int total, count;......... if(total / count < 60 || count == 0) printf("Failed\n"); else printf("Passed\n"); int total, count;......... if(total / count < 60 || count == 0) printf("Failed\n"); else printf("Passed\n"); int total, count;......... if(count == 0 || total / count < 60) printf("Failed\n"); else printf("Passed\n"); int total, count;......... if(count == 0 || total / count < 60) printf("Failed\n"); else printf("Passed\n"); Why?
38
練習 main() { int x=1,y=2,z=3; int p,q; p = (x>y) && (z<y); /* False i.e. 0 */ q = (y>x) || (y>z); /* True i.e. 1 */ printf(" %d && %d = %d\n",p,q,p&&q); printf(" %d || %d = %d\n",p,q,p||q); /* Can mix "logical" values and arithmetic */ printf(" %d && %d = %d\n",x,q,x&&q); printf(" %d || %d = %d\n",p,y,p||y); /* Exercise the NOT operator */ printf(" ! %d = %d\n",p,!p); printf(" ! %d = %d\n",q,!q); /* NOT operator applied to arithmetic */ printf(" ! %d = %d\n",z,!z); } main() { int x=1,y=2,z=3; int p,q; p = (x>y) && (z<y); /* False i.e. 0 */ q = (y>x) || (y>z); /* True i.e. 1 */ printf(" %d && %d = %d\n",p,q,p&&q); printf(" %d || %d = %d\n",p,q,p||q); /* Can mix "logical" values and arithmetic */ printf(" %d && %d = %d\n",x,q,x&&q); printf(" %d || %d = %d\n",p,y,p||y); /* Exercise the NOT operator */ printf(" ! %d = %d\n",p,!p); printf(" ! %d = %d\n",q,!q); /* NOT operator applied to arithmetic */ printf(" ! %d = %d\n",z,!z); } 1. 預測以下程式將產生何種結果 2. 實作以下程式視預測是否正確
39
C Program Design Introduction to C Programming 巨集 The #define Preprocessor
40
#define #include #define PI 3.14159 main() { double radius; printf("Enter the radius of a circle:"); scanf("%lf", &radius); printf("The perimeter of the circle: %lf\n", 2.0 * PI * radius); printf("The area of the circle: %lf\n", PI * radius * radius); } #include <stdio.h> #define PI 3.14159 main() { double radius; printf("Enter the radius of a circle:"); scanf("%lf", &radius); printf("The perimeter of the circle: %lf\n", 2.0 * PI * radius); printf("The area of the circle: %lf\n", PI * radius * radius); } for text substitution
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.