|
大小: 1483
备注:
|
大小: 4909
备注:
|
| 删除的内容标记成这样。 | 加入的内容标记成这样。 |
| 行号 10: | 行号 10: |
| 目标:掌握C语言程序的基本结构,掌握C语言程序编译运行的方法。 | {{{#!cplusplus #include <stdio.h> int main() { printf("Hello world\n"); } }}} 目标:了解C语言程序的基本结构,掌握C语言程序编译运行的方法。 |
| 行号 13: | 行号 20: |
| 设计一个C语言程序,计算并输出半径是5的圆的面积。 | 设计一个C语言程序,计算并输出边长是5的正方形的面积。 |
| 行号 15: | 行号 22: |
| 目标:掌握常量、表达式的基本概念 | {{{#!cplusplus #include <stdio.h> int main() { printf("The area of the square is %d.", 5 * 5); } }}} 目标:了解字面常量、表达式的基本概念 |
| 行号 18: | 行号 32: |
| 设计一个程序, | 设计一个程序,计算并输出半径是5的圆形的面积。 {{{#!cplusplus #include <stdio.h> int main() { printf("The area of the circle is %f", 3.14159 * 5 * 5); } }}} 目标:了解类型的概念 == 任务3.2 == 设计一个程序,计算并输出一个半径是5的圆形和一个边长是5的正方形的面积总和。 {{{#!cplusplus #include <stdio.h> int main() { printf("The total area of the square and circle is %f.", 5*5 + 3.14159*5*5); } }}} {{{#!cplusplus #include <stdio.h> int main() { int square_area; float circle_area; float total_area; square_area = 5 * 5; circle_area = 3.14159 * 5 * 5; total_area = square_area + circle_area; printf("The total area of the square and circle is %f", total_area); } }}} {{{#!cplusplus #include <stdio.h> int main() { int length; int square_area; float radius; float circle_area; float total_area; length = 5; square_area = length * length; radius = 5; circle_area = 3.14159 * radius * radius; total_area = square_area + circle_area; printf("The total area of the square and circle is %f", total_area); } }}} |
| 行号 23: | 行号 88: |
| 设计一个程序,输入圆的半径,输出圆的面积 | 设计一个程序,输入正方形的边长,输出正方形的面积 {{{#!cplusplus #include <stdio.h> int main() { int length; int square_area; scanf("%d", &length); sqaure_area = length * length; printf("The area of the square is %d.", square_area); } }}} |
| 行号 28: | 行号 104: |
| 目标:掌握分支的概念 | 设计一个程序,输入正方形的边长,输出正方形的面积。如果输入的是一个负数,给出错误提示。 {{{#!cplusplus #include <stdio.h> int main() { int length; int square_area; scanf("%d", &length); if(length < 0) { printf("Error! Please input a positive integer!"); } else { square_area = length * length; printf("The area of the square is %d.", square_area); } } }}} 目标:掌握分支的概念,缩进的编程风格 |
| 行号 31: | 行号 125: |
| 写一个程序,输出边长是1,2,3,4,5的正方形的面积。 {{{#!cplusplus #include <stdio.h> int main() { int length; int square_area; length = 1; square_area = length * length; printf("The area of the square is %d.", square_area); length = 2; square_area = length * length; printf("The area of the square is %d.", square_area); length = 3; square_area = length * length; printf("The area of the square is %d.", square_area); length = 4; square_area = length * length; printf("The area of the square is %d.", square_area); length = 5; square_area = length * length; printf("The area of the square is %d.", square_area); } }}} {{{#!cplusplus #include <stdio.h> int main() { int length; int square_area; length = 1; while(length <= 5) { square_area = length * length; printf("The area of the square is %d.", square_area); length = length + 1; } } }}} {{{#!cplusplus #include <stdio.h> int main() { int length; int square_area; for(length = 1; length <= 5; length = length + 1) { square_area = length * length; printf("The area of the square is %d.", square_area); } } }}} |
C语言实例教程 Illustrated C
本教程主要为程序设计的初学者设计,通过C语言了解程序设计的基本概念。同时,也供学习过C语言,但是也针对程序设计的一些基本概念不甚了解的读者设计,可以用来加深对基本概念的理解。
本教程以任务驱动的方式设计,读者跟随教程完成一个一个的小的程序设计任务,从而掌握C语言的基本语法和程序设计的方法。本教程不就语法细节进行详述,有关语法细节可以参看相关链接。
1. 任务1
设计一个C语言程序,在屏幕上输出Hello world。编译并且运行它。
目标:了解C语言程序的基本结构,掌握C语言程序编译运行的方法。
2. 任务2
设计一个C语言程序,计算并输出边长是5的正方形的面积。
目标:了解字面常量、表达式的基本概念
3. 任务3
设计一个程序,计算并输出半径是5的圆形的面积。
目标:了解类型的概念
4. 任务3.2
设计一个程序,计算并输出一个半径是5的圆形和一个边长是5的正方形的面积总和。
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. 任务3.5
设计一个程序,输入正方形的边长,输出正方形的面积
目标:掌握简单的输入输出,进一步理解变量的概念
6. 任务4
设计一个程序,输入正方形的边长,输出正方形的面积。如果输入的是一个负数,给出错误提示。
目标:掌握分支的概念,缩进的编程风格
7. 任务5
写一个程序,输出边长是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 }
目标:掌握循环的概念
8. 任务6
目标:掌握函数的概念(无参数、无返回值)
9. 任务7
目标:掌握带参数的函数的概念
10. 任务8
目标:掌握有返回值的函数的概念
11. 任务9
目标:掌握数组的概念