[关闭]
@iwktd981220 2019-09-22T01:42:28.000000Z 字数 2752 阅读 712

19级图灵班第一节课

19图灵班


这节课安排

有空的话

源代码

hello_world.c

  1. // hello_world.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. printf("Hello World\n");
  6. return 0;
  7. }

variable.c

  1. #include <stdio.h>
  2. int main() {
  3. // Define a variable `a`
  4. int a;
  5. // Assign `a` with 1
  6. a = 1;
  7. // Initialization variable `b`
  8. // Which means that declare and assign
  9. int b = 2;
  10. printf("a = %d\nb = %d\n", a, b);
  11. // We can define many variables at a time
  12. int a1, a2, a3, a4;
  13. // Integer
  14. // 2 bytes
  15. short s = 1;
  16. int i = 1;
  17. long int i1 = 1;
  18. long long i2 = 1;
  19. unsigned int u3 = -1;
  20. printf("u3 = %lu\n", u3);
  21. // Floating point
  22. float f = 1.0;
  23. double d = 3.14;
  24. printf("f = %f, d = %lf\n", f, d);
  25. // Character in ASCII
  26. char c = 'a';
  27. printf("char c = %c\n", c);
  28. return 0;
  29. }

read_from_input.c

  1. #include <stdio.h>
  2. // For more placeholder, please consult
  3. // https://en.cppreference.com/w/c/io/fprintf
  4. int main() {
  5. // Sometimes we need to know a number/string/... from user
  6. // Put a `&` before variable `a`
  7. // `%d` in `""` is a place holder for variables
  8. int d, a;
  9. scanf("%d%d", &d, &a);
  10. printf("%d %d\n", a, d);
  11. char c = 'a';
  12. printf("char = %c, f = %f, d = %lf", c, 3.14, 1.0);
  13. return 0;
  14. }

array_loop.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. // We have got one variable `a`
  5. int a = 1;
  6. printf("%d", a);
  7. // We can define many variables at a time
  8. int a1, a2, a3, a4;
  9. // What if we need no more than one?
  10. // Like 100 int variables?
  11. int arr[10];
  12. int arr1[] = {1, 2, 3, 4, 5, 6};
  13. // As we get array now, how do we use it?
  14. // What we need is loop
  15. // array could be used from subscript `0`
  16. // `i++` equivalent to `i = i +1` and `i += 1`
  17. int n = 0;
  18. while (n < 10) {
  19. printf("%d,", arr[n]);
  20. n += 1;
  21. }
  22. printf("\n");
  23. // Control flow:
  24. // 1. Initialization: `int i = 0;`
  25. // 2. Test condition: `i < 6`, go to 6 if predicate does not fulfill
  26. // 3. {} body
  27. // 4. Incresment of i: `i++`
  28. // 5. Go to 2
  29. // 6. Done
  30. for (int i = 0; i < 6; i++) {
  31. printf("%d,", arr1[i]);
  32. }
  33. printf("\n");
  34. // Do-while guarantee it will be do at least one time.
  35. int i = 0;
  36. int arr2[4] = {};
  37. do {
  38. printf("%d,", arr2[i++]);
  39. } while (i < 4);
  40. return 0;
  41. }

example.c

  1. #include <stdio.h>
  2. int main() {
  3. printf("Enter the amount of student number:");
  4. int num;
  5. scanf("%d", &num);
  6. int arr[num];
  7. for (int i = 0; i < num; i++) {
  8. printf("Enter %d th studen's score:", i);
  9. scanf("%d", &arr[i]);
  10. }
  11. // Code below is equivalent to code above
  12. // int i = 0;
  13. // while (i < num) {
  14. // printf("Enter %d th studen's score:", i);
  15. // scanf("%d", &arr[i]);
  16. // i++;
  17. //}
  18. int i = 0;
  19. while (i < num) {
  20. printf("The %d th student's score is %d\n", i, arr[i]);
  21. i++;
  22. }
  23. return 0;
  24. }

new_func.c

  1. #include <stdio.h>
  2. /*
  3. * Two ways to declare new function
  4. * 1. Declare before use, then implement it after. In this case: bar()
  5. * 2. Define before use. In this case: foo()
  6. */
  7. void bar();
  8. void foo() {
  9. printf("Hello ");
  10. printf("World\n");
  11. }
  12. int main() {
  13. foo();
  14. bar();
  15. return 0;
  16. }
  17. void bar() {
  18. printf("Hello ");
  19. printf("Class");
  20. }

sum_and_fac.c

  1. #include <stdio.h>
  2. int sum(int num) {
  3. int sum = 0;
  4. for (int i = 1; i <= num; i++) {
  5. sum = sum + i;
  6. }
  7. return sum;
  8. }
  9. int factorial(int n) {
  10. if (n < 0) {
  11. return 0;
  12. }
  13. if (n == 0) {
  14. return 1;
  15. } else {
  16. return factorial(n - 1) * n;
  17. }
  18. }
  19. int main() {
  20. int num;
  21. scanf("%d", &num);
  22. printf("sum of 1 to %d is %d\n", num, sum(num));
  23. printf("factorial of %d is %d\n", num, factorial(num));
  24. return 0;
  25. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注