版本5和6间的区别
于2007-12-07 17:31:55修订的的版本5
大小: 4909
编辑: czk
备注:
于2007-12-07 17:36:19修订的的版本6
大小: 4905
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 42: 行号 42:
== 任务3.2 == == 任务4 ==
行号 87: 行号 87:
== 任务3.5 == == 任务5 ==
行号 103: 行号 103:
== 任务4 == == 任务6 ==
行号 124: 行号 124:
== 任务5 == == 任务7 ==
行号 181: 行号 181:
== 任务6 == == 任务8 ==

C语言实例教程 Illustrated C

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

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

1. 任务1

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

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

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

2. 任务2

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

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

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

3. 任务3

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

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

目标:了解类型的概念

4. 任务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 }

   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 }

   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 }

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

5. 任务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 }

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

6. 任务6

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

   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 }

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

7. 任务7

写一个程序,输出边长是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 
  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 }

目标:掌握循环的概念

8. 任务8

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

9. 任务7

目标:掌握带参数的函数的概念

10. 任务8

目标:掌握有返回值的函数的概念

11. 任务9

目标:掌握数组的概念

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

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