版本9和10间的区别
于2007-12-07 18:48:46修订的的版本9
大小: 8367
编辑: czk
备注:
于2007-12-07 18:51:16修订的的版本10
大小: 8404
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
#pragma section-numbers 2
行号 7: 行号 8:
== 任务1 == [[TableOfContents]]

== 任务 ==
行号 20: 行号 23:
== 任务2 == == 任务 ==
行号 33: 行号 36:
== 任务3 == == 任务 ==
行号 45: 行号 48:
== 任务4 == == 任务 ==
行号 93: 行号 96:
== 任务5 == == 任务 ==
行号 111: 行号 114:
== 任务7 == == 任务 ==
行号 169: 行号 172:
== 任务8 == == 任务 ==
行号 194: 行号 197:
== 任务9 == == 任务 ==
行号 224: 行号 227:
== 任务10 == == 任务 ==
行号 281: 行号 284:
== 任务6 == == 任务 ==
行号 304: 行号 307:
== 任务11 == == 任务 ==

C语言实例教程 Illustrated C

本教程主要为程序设计的初学者设计,通过C语言了解程序设计的基本概念。同时,也供学习过C语言,但是也针对程序设计的一些基本概念不甚了解的读者设计,可以用来加深对基本概念的理解。

本教程以任务驱动的方式设计,读者跟随教程完成一个一个的小的程序设计任务,从而掌握C语言的基本语法和程序设计的方法。本教程不就语法细节进行详述,有关语法细节可以参看相关链接。

TableOfContents

1. 任务

设计一个C语言程序,在屏幕上输出Hello world。编译并且运行它。

   1 #include <stdio.h>
   2 int main() {
   3     printf("Hello world\n");
   4     return 0;
   5 }

目标:了解C语言程序的基本结构,掌握C语言程序编译运行的方法。

2. 任务

设计一个C语言程序,计算并输出边长是5的正方形的面积。

   1 #include <stdio.h>
   2 int main() {
   3     printf("The area of the square is %d.", 5 * 5);
   4     return 0;
   5 }

目标:了解字面常量、表达式的基本概念

3. 任务

设计一个程序,计算并输出半径是5的圆形的面积。

   1 #include <stdio.h>
   2 int main() {
   3     printf("The area of the circle is %f", 3.14159 * 5 * 5);
   4     return 0;
   5 }

目标:了解类型的概念

4. 任务

设计一个程序,计算并输出一个半径是5的圆形和一个边长是5的正方形的面积总和。

   1 #include <stdio.h>
   2 int main() {
   3     printf("The total area of the square and circle is %f.", 5*5 + 3.14159*5*5);
   4     return 0;
   5 }

   1 #include <stdio.h>
   2 int main() {
   3     int square_area;
   4     float circle_area;
   5     float total_area;
   6 
   7     square_area = 5 * 5;
   8     circle_area = 3.14159 * 5 * 5;
   9     total_area = square_area + circle_area;
  10     printf("The total area of the square and circle is %f", total_area);
  11     return 0;
  12 }

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5     float radius;
   6     float circle_area;
   7     float total_area;
   8 
   9     length = 5;
  10     square_area = length * length;
  11     radius = 5;
  12     circle_area = 3.14159 * radius * radius;
  13     total_area = square_area + circle_area;
  14     printf("The total area of the square and circle is %f", total_area);
  15     return 0;
  16 }

目标:掌握变量的基本概念

5. 任务

设计一个程序,输入正方形的边长,输出正方形的面积

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5     scanf("%d", &length);
   6     sqaure_area = length * length;
   7     printf("The area of the square is %d.", square_area);
   8     return 0;
   9 }

目标:掌握简单的输入输出,进一步理解变量的概念

6. 任务

输入5个正方形的边长,输出它们的面积。

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5 
   6     printf("Please enter the length of square:");
   7     scanf("%d", &length);
   8     sqaure_area = length * length;
   9     printf("The area of the square is %d.", square_area);
  10 
  11     printf("Please enter the length of square:");
  12     scanf("%d", &length);
  13     sqaure_area = length * length;
  14     printf("The area of the square is %d.", square_area);
  15 
  16     printf("Please enter the length of square:");
  17     scanf("%d", &length);
  18     sqaure_area = length * length;
  19     printf("The area of the square is %d.", square_area);
  20 
  21     printf("Please enter the length of square:");
  22     scanf("%d", &length);
  23     sqaure_area = length * length;
  24     printf("The area of the square is %d.", square_area);
  25 
  26     printf("Please enter the length of square:");
  27     scanf("%d", &length);
  28     sqaure_area = length * length;
  29     printf("The area of the square is %d.", square_area);
  30     return 0;
  31 }

   1 #include <stdio.h>
   2 
   3 void input_calculate_output() {
   4     printf("Please enter the length of square:");
   5     scanf("%d", &length);
   6     sqaure_area = length * length;
   7     printf("The area of the square is %d.", square_area);
   8 }
   9 int main() {
  10     input_calculate_output();
  11     input_calculate_output();
  12     input_calculate_output();
  13     input_calculate_output();
  14     input_calculate_output();
  15     return 0;
  16 }

目标:掌握函数的概念(无参数、无返回值)

7. 任务

输入5个正方形的边长,输出它们的面积总和。

   1 #include <stdio.h>
   2 
   3 int input_calculate() {
   4     printf("Please enter the length of square:");
   5     scanf("%d", &length);
   6     sqaure_area = length * length;
   7     return square_area;
   8 }
   9 int main() {
  10     int total_area = 0;
  11     total_area = total_area + input_calculate_output();
  12     total_area = total_area + input_calculate_output();
  13     total_area = total_area + input_calculate_output();
  14     total_area = total_area + input_calculate_output();
  15     total_area = total_area + input_calculate_output();
  16     printf("The total area of the squares is %d.", total_area);
  17     return 0;
  18 }

目标:了解有返回值的函数的概念

8. 任务

设计一个程序,已知圆的圆心在(3.0, 4.5),圆周上有一点(7.0, 7.5),计算并输出圆的面积。

   1 #include <stdio.h>
   2 #include <math.h>
   3 int main() {
   4     float radius;
   5 
   6     radius = sqrt( (7.0-3.0)*(7.0-3.0) + (7.5 - 4.5)*(7.5 - 4.5) );
   7     printf("The area of the circle is %f", 3.14159 * radius * radius);
   8 }

   1 #include <stdio.h>
   2 #include <math.h>
   3 float square(float x) {
   4     return x*x;
   5 }
   6 int main() {
   7     float radius;
   8 
   9     radius = sqrt( square(7.0-3.0) + square(7.5 - 4.5) );
  10     printf("The area of the circle is %f", 3.14159 * square(radius));
  11 }

目标:了解有参数的函数的概念

9. 任务

写一个程序,输出边长是1,2,3,4,5的正方形的面积。

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5     length = 1;
   6     square_area = length * length;
   7     printf("The area of the square is %d.", square_area);
   8     length = 2;
   9     square_area = length * length;
  10     printf("The area of the square is %d.", square_area);
  11     length = 3;
  12     square_area = length * length;
  13     printf("The area of the square is %d.", square_area);
  14     length = 4;
  15     square_area = length * length;
  16     printf("The area of the square is %d.", square_area);
  17     length = 5;
  18     square_area = length * length;
  19     printf("The area of the square is %d.", square_area);
  20     return 0;
  21 }

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5 
   6     length = 1;
   7     while(length <= 5) {
   8         square_area = length * length;
   9         printf("The area of the square is %d.", square_area);
  10         length = length + 1;
  11     }
  12 }

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5 
   6     for(length = 1; length <= 5; length = length + 1) {
   7         square_area = length * length;
   8         printf("The area of the square is %d.", square_area);
   9     }
  10 }

目标:掌握循环的概念

10. 任务

设计一个程序,输入正方形的边长,输出正方形的面积。如果输入的是一个负数,给出错误提示。

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5     scanf("%d", &length);
   6     if(length < 0) {
   7         printf("Error! Please input a positive integer!");
   8     } 
   9     else {
  10         square_area = length * length;
  11         printf("The area of the square is %d.", square_area);
  12     }
  13     return 0;
  14 }

目标:掌握分支的概念,缩进的编程风格

11. 任务

输入5个正方形,输出面积最大的正方形的面积。

   1 #include <stdio.h>
   2 int main() {
   3     int length;
   4     int square_area;
   5     int max_square_area;
   6 
   7     max_square_area = 0;
   8     for(length = 1; length <= 5; length = length + 1) {
   9         square_area = length * length;
  10         if(square_area > max_square_area) {
  11             max_square_area = square_area;
  12         }
  13     }
  14     printf("The max area of the squares is %d.", max_square_area);
  15 }

目标:进一步掌握循环和分支语句配合。

Illustrated_C (2008-02-23 15:34:57由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.