400 likes | 423 Views
Learn about the background of C programming language, main parts of C programs, arithmetic and relation operators, and decision-making statements. Practice coding exercises in C programming.
E N D
Content • Background • Main Parts of C Programs • 標準輸出入裝置與輸出入函式 • 標準輸出入函式 • 算術運算子(Arithmetic Operators) • 關係運算子(Relation Operators) • 邏輯運算子(Logical Operators) • 巨集 The #definePreprocessor
The Development of the C Language 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
CPU ALU Input Device Output Device Input Output Control Memory Computer Architecture
C Program DesignIntroduction to C Programming Main Parts of C Programs
Our First C Program Hello World /* HelloWorld.cpp Our first program */ #include <iostream> using namespace std; /* function main begins program execution */ main() { cout << "hello, world\n"; }
C Program DesignIntroduction to C Programming 算術運算子(Arithmetic Operators)
y = 2 * x * x + 3 * x + 7; 範例: y = 2x2+3x+7 // PolynormialEval.cpp #include <cstdlib> #include <iostream> 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; 範例: y = 2x2+3x+7
C Program DesignIntroduction to C Programming 關係運算子(Relation Operators)
The result of a relation operation is true (nonzero) or false (zero). Relation Operators in C
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
If-Else Statement if (expression) statement1 else statement2
start true expression false statement1 statement2 end If expression is evaluated to true (nonzero), statement1 is executed; otherwise, statement2 is executed. If-Else Statement if (expression) statement1 else statement2 expression
If-Else Statement start if (expression) statement1 else statement2 expression true expression false statement1 statement2 option end
start true expression false statement end If-Statement if (expression) statement expression
範例: Decision Making (I) // pass1.cpp #include <cstdlib> #include <iostream> using namespace std; intmain(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; }
範例: Decision Making (II) // pass2.cpp #include <cstdlib> #include <iostream> using namespace std; intmain(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; }
start expression false end true statement While-Statement while(expression) statement expression
範例:華氏攝氏 #include <cstdlib> #include <iostream> 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; }
練習 • Using • Redo the same task but with the starting number, ending number, and step being given by a user.
練習 • 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.” • The same as above, but the program reports all factors of that number in 2, 3, 5, 7, 11. • The same as above, but the program reports all possible factors of that number.
C Program DesignIntroduction to C Programming 邏輯運算子 (Logical Operators)
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
Examples if((celsius >= 25.0) && (celsius <= 28.0)) printf("It is very comfortable\n"); if((celsius < 20.0) || (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");
注意事項 int total, count; . . . . . . . . . if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); count若為零將產生divide by zero之exception.
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"); Why? int total, count; . . . . . . . . . if(count > 0 &&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"); Why? int total, count; . . . . . . . . . if(count == 0 ||total / count < 60) printf("Failed\n"); else printf("Passed\n");
練習 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); } • 預測以下程式將產生何種結果 • 實作以下程式視預測是否正確
C Program DesignIntroduction to C Programming 巨集 The #define Preprocessor
#define for text substitution #include <stdio.h> #definePI 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); }