1 / 40

C Program Design Introduction to C Programming

C Program Design Introduction to C Programming. 主講人:虞台文. Content. Background Main Parts of C Programs 標準輸出入裝置與輸出入函式 標準輸出入函式 算術運算子 ( Arithmetic Operators ) 關係運算子 ( Relation Operators ) 邏輯運算子 (Logical Operators) 巨集  The #define Preprocessor.

savagel
Télécharger la présentation

C Program Design Introduction to C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Program DesignIntroduction to C Programming 主講人:虞台文

  2. Content • Background • Main Parts of C Programs • 標準輸出入裝置與輸出入函式 • 標準輸出入函式 • 算術運算子(Arithmetic Operators) • 關係運算子(Relation Operators) • 邏輯運算子(Logical Operators) • 巨集  The #definePreprocessor

  3. C Program DesignIntroduction to C Programming Background

  4. 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

  5. CPU ALU Input Device Output Device Input Output Control Memory Computer Architecture

  6. C Program DesignIntroduction to C Programming Main Parts of C Programs

  7. Our First C Program  Hello World

  8. 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"; }

  9. Some Common Escape Sequences

  10. Editing  Compiling  Linking  Execution

  11. C Program DesignIntroduction to C Programming 算術運算子(Arithmetic Operators)

  12. Arithmetic Operators

  13. Precedence of Arithmetic Operators

  14. 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; }

  15. y = 2 * x * x + 3 * x + 7; 範例: y = 2x2+3x+7

  16. C Program DesignIntroduction to C Programming 關係運算子(Relation Operators)

  17. The result of a relation operation is true (nonzero) or false (zero). Relation Operators in C

  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) statement1 else statement2

  20. 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

  21. If-Else Statement start if (expression) statement1 else statement2 expression true expression false statement1 statement2 option end

  22. start true expression false statement end If-Statement if (expression) statement expression

  23. 範例: 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; }

  24. 範例: 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; }

  25. start expression false end true statement While-Statement while(expression) statement expression

  26. 範例:華氏攝氏

  27. 範例:華氏攝氏

  28. 範例:華氏攝氏 #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; }

  29. 練習 • Using • Redo the same task but with the starting number, ending number, and step being given by a user.

  30. 練習 • 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.

  31. C Program DesignIntroduction 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 < 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");

  35. 注意事項 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 && 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");

  37. 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");

  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); } • 預測以下程式將產生何種結果 • 實作以下程式視預測是否正確

  39. C Program DesignIntroduction to C Programming 巨集 The #define Preprocessor

  40. #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); }

More Related