《简单的C程序设计教学课件Ppt.ppt》由会员分享,可在线阅读,更多相关《简单的C程序设计教学课件Ppt.ppt(35页珍藏版)》请在三一文库上搜索。
1、第3章 简单的C程序设计,内容提要,字符输入输出函数 格式输入输出函数,C语言中的语句,变量声明语句 int x, y; 表达式语句 i+; i = i + 1; 空语句 ; 复合语句 t = x; x = y; y = t; 控制语句,顺序结构程序的基本操作,如何在程序中给变量赋值? 赋值表达式语句 赋值表达式; Total = m * pow(1+r, n); 如何进行数据的输入输出? 输入:从标准输入设备上输入数据到计算机内存 输出:将计算机内存中的数据送到标准输出设备 C语言中输入输出操作通过调用标准库函数来实现 #include “stdio.h“ 在当前目录和TC指定的目录中找该文
2、件 #include 在由TC指定的文件目录中找该文件,复合语句,在什么情况下使用复合语句? 条件语句和循环语句在语法上只允许带一条语句 当分支和循环中需要进行多项操作时, t=x; x=y; y=t; ,被当作一条语句来处理,例3.1,main() int a = 0; int a = 1; printf(“In: a = %dn“, a); printf(“Out: a = %dn“, a); ,In: a = 1 Out: a = 0,空语句,空语句有什么作用? 什么也不做,只表示语句的存在 自顶向下程序设计时用在那些未完成的模块中,main() DataInitialze(); Dat
3、aProcess(); DataOutput(); ,DataInitialze() ; ,字符输入输出函数,字符输出函数 putchar(ch) 输出一个字符ch 字符输入函数 getchar() 无参数 函数值为从输入设备接收的字符,#include main() char ch; printf(“Press a key and then press Enter:“); ch = getchar(); printf(“You pressed “); putchar(ch); putchar(n); ,运行程序,Press a key and then press Enter:,311,A,
4、You pressed,A,例3.2,格式输出函数,格式输出函数 printf(格式控制字符串, 输出项表列); 输出若干个任意类型的数据 printf(“a=%d b=%f“, a, b);,函数名,输 出 表 列,普通字符,%d 以带符号十进制整数输出 %o 以八进制无符号整数输出(无前导0) %x 以十六进制无符号整数输出(无前导0x) %u 以十进制无符号整数输出 %c 以字符形式输出单个字符 %s 输出一个字符串 %f 以小数形式输出浮点数(6位小数) %e 以标准指数形式输出(6位小数) %g 选用%f,%e中输出宽度较小的一种格式,printf格式字符,l 长整型整数,加在d、o
5、、x、u前 L long double型数,加在f、e、g前 m 表示数据占用的最小宽度 数据宽度大于m,按实际宽度输出 数据宽度小于m时,补空格 .n 对实数表示输出n位小数 对字符串表示最多输出的字符个数 - 改变输出内容的对齐方式 缺省为右对齐,printf附加格式说明符,#include main() float f1 = 100.15799, f2 = 12.55, f3 = 1.7; int n1 = 123, n2 = 45, n3 = 6; printf(“printf WITHOUT width or precision specifications:n“); printf(
6、“%fn%fn%fn“, f1, f2, f3); printf(“%dn%dn%dn“, n1, n2, n3); printf(“printf WITH width and precision specifications:n“); printf(“%5.2fn%6.1fn%3.0fn“, f1, f2, f3); printf(“%5dn%6dn%3dnn“, n1, n2, n3); ,例3.8,格式输入函数,格式输入函数 scanf(格式控制字符串, 地址表列); scanf(“%d,%f”, ,格式输入函数,常见错误 scanf(“%d,%fn”,%d 以带符号十进制形式输入整型数
7、据 %o 以八进制无符号形式输入(无前导0) %x 以十六进制无符号形式输入(无前导0x) %c 以字符形式输入单个字符 %s 输入字符串,以非空字符开始,遇第一个 空白字符结束 %f 以小数形式输入浮点数 %e 以标准指数形式输入,scanf格式字符,l 加在d、o、x、u前:输入长整型 加在f、e 前:输入双精度型 L 加在f、e 前:输入long double型 h 加在d、o、x 前:输入短整型 m 表示数据占用的宽度 * 本输入项在读入后不赋给相应的变量,scanf附加格式说明符,输入数据时,遇以下情况时该数据认为结束 遇空格、回车、TAB 键 遇宽度 :%3d 遇非法输入,格式输入
8、函数,#include main() int a, b; printf(“Please input a and b:“); scanf(“%2d%*2d%2d“, ,Please input a and b:,a=12, b=56, a+b = 68,123456,例3.9,#include main() int a, b; printf(“Please input a and b:“); scanf(“%2d%*2d%2d“, ,Please input a and b:,a=12, b=5, a+b = 17,12345a,例3.9,#include main() int a, b; sc
9、anf(“%d %d“, ,问题1:当要求程序输出结果为 a = 12, b = 34 时,用户应该如何输入数据?,12 34,输入数据的格式控制例3.10,#include main() int a, b; scanf(“%d %d“, ,问题2:当限定用户输入数据以逗号为分隔符,即输入数据格式为: 12, 34 时,应修改程序中的哪条语句?怎样修改?,scanf(“%d,%d“, ,输入数据的格式控制例3.10,#include main() int a, b; scanf(“%d %d“, ,问题3:语句scanf(“%d %d“, 时,用户应该如何输入数据?,a = 12, b = 3
10、4,输入数据的格式控制例3.10,#include main() int a, b; scanf(“%d %d“, ,问题4:限定用户输入数据为以下格式为 1234 同时要求程序输出结果为a = 12, b = 34,scanf(“%2d%2d“, ,输入数据的格式控制例3.10,#include main() int a, b; scanf(“%d %d“, ,问题5:限定用户输入数据为以下格式为 12 34 同时要求程序输出结果为a = “12“, b = “34“,scanf(“%d%d“, ,输入数据的格式控制例3.10,#include main() int a, b; scanf(
11、“%d %d“, ,问题6:设计程序使得用户可以以任意字符(回车、空格、制表符、逗号、其它)作为分隔符进行数据的输入,scanf(“%d%*c%d“, ,输入数据的格式控制例3.10,#include main() int data1, data2, sum; char op; printf(“Please enter the expression data1 + data2n“); scanf(“%d%c%d“, ,Please enter the expression data1 + data2,第1次测试,12 + 3,12 3129 = 3141,C格式符存在的问题及其解决例3.11,
12、#include main() int data1, data2, sum; char op; printf(“Please enter the expression data1 + data2n“); scanf(“%d%c%d“, ,Please enter the expression data1 + data2,第2次测试,12 3,12 3 = 15,C格式符存在的问题及其解决例3.11,#include main() int data1, data2, sum; char op; printf(“Please enter the expression data1 + data2n“
13、); scanf(“%d%c%d“, ,Please enter the expression data1 + data2,第3次测试,12+3,12+3 = 15,C格式符存在的问题及其解决例3.11,#include main() int a; char b; float c; printf(“Please input an integer:“); scanf(“%d“, ,Please input an integer:,希望得到的运行结果,12,Please input an character :,a,Please input a float number:,3.5,integer:
14、12,character :a,float number:3.500000,C格式符存在的问题及其解决例3.12,#include main() int a; char b; float c; printf(“Please input an integer:“); scanf(“%d“, ,Please input an integer:,结果好像很奇怪嘛!,12,Please input an character :,Character:,Please input a float number:,3.5,integer:12,float number:3.500000,C格式符存在的问题及其
15、解决例3.12,#include main() int a; char b; float c; printf(“Please input an integer:“); scanf(“%d“, ,Please input an integer:,程序修改后得到的运行结果,12,Please input an character :,a,Please input a float number:,3.5,integer:12,character :a,float number:3.500000,C格式符存在的问题及其解决例3.12,#include main() int data1, data2, sum; char op; printf(“Please enter the expression data1 + data2n“); scanf(“%d%1s%d“, ,12+3,12 + 3,12 + 3,再回头来看例3.10,想以任意分割符输入加法算式,可能吗?,这一章我们学习了,字符输入函数getchar() 字符输出函数putchar() 格式输入函数scanf() 格式输出函数printf() 格式控制问题难点,作业,P6467 习题3.13.6,
链接地址:https://www.31doc.com/p-3841535.html