2014_顺序程序设计讲解.ppt
文本预览下载声明
#include stdio.h void main ( ) { char ch, ch1, ch2; //变量定义 ch = getchar(); //读取一字符 putchar(\n); //换行 ch1 = z - (z - ch + 1) % 26; //求前驱字符 ch2 = a + (ch - a + 1) % 26; //求后继字符 printf(ch1 = %c, ch2 = %c\n,ch1,ch2); //显示结果 } ch1 = v, ch2 = x 运行结果(假设输入字母为w): 【例】数据的格式化输入 输入一学生的学号(8位数字)、生日(年-月-日)、性别(M:男,F:女)及三门功课(语文、数学、英语)的成绩,现要求计算该学生的总分和平均分,并将该学生的全部信息输出(包括总分、平均分)。 #include stdio.h void main ( ) { unsigned long no; //学号 unsigned int year, month, day; //生日(年、月、日) unsigned char sex; //性别 float chinese, math, english; //语文、数学、英语成绩 float total, average; //总分、平均分 printf (input the students NO: ); scanf (%8ld, no); printf (input the students Birthday(yyyy-mm-dd): ); scanf (%4d-%2d-%2d, year, month, day); fflush (stdin); //清除键盘缓冲区 printf (input the students Sex(M/F): ); scanf (%c, sex); printf (input the students Scores(chinese, math, english): ); scanf (%f,%f,%f, chinese, math, english); total = chinese + math + english; //计算总分 average = total / 3; //计算平均分 printf (\n===NO=======birthday==sex==chinese==math== english==total==average\n); printf (%08ld %4d-%02d-%02d %c %-5.1f %-5.1f %-5.1f %-5.1f %-5.1f\n, no, year, month, day, sex, chinese, math, english, total, average); } 假设输入 : input the students NO input the students Birthday(yyyy-mm-dd):1987-9-8↙ input the students Sex(M/F):M↙ input the students Scores(chinese, math, english):90,80,90↙ 输出结果 : ===NO=======birthday==sex==chinese==math==english==total==average 1987-09-08 M 90.0 80.0 90.0 260.0 86.7 本章小结 本章主要内容: (1) 格式化输入、输出库函数的使用。重点介绍了格式化输入函数printf和格式化输出函数scanf的功能及使用方法,其中格式控制字符串是我们要重点关注的地方,格式化输入和输出可以按照某种输入输出格式来进行。 (2) 字符的非格式化输入、输出库函数的使用。 (3) 程序的控制结构。任何复杂的算法都可以由顺序结构、选择(分支)结构和循环结构这三种基本结构组成。由此构成了程序的三种控制结构,这三种控制结构在程序中相互嵌套,从而构
显示全部