(C语言之复习demo

tech2025-04-05  12

#include <stdio.h> /* (1)init part - to give a init or a common part which is necessary for prog- -ram and can't be given commentary lines added on it. (2)indepent part - only one part can be existed in all demo when executing a program, Meanwhile,Any other "independent part" shoud be given commentary lines on it. */ int main() { /* 在输入输出函数中: 格式说明由%开始,并以格式字符结束,用于指定各输出值参数的输出格式. 函数printf()中的格式修饰符: 在函数printf()的格式说明中,可以在%和格式符之间插入格式修饰符,用于对输出格式进 行微调,如指定输出数据的域宽,显示精度,左对齐,右对齐等. */ /* --independent block1 使用const常量定义pi,编程从键盘输入圆的半径radius,计算并输出圆的周长和面积 使其输出的数据保留两位小数点. */ const double pi = 3.14159; double radius, circumference, area; printf("Input radius:"); scanf_s("%lf", &radius); circumference = 2 * pi * radius; area = pi * radius * radius; printf("printf WITHOUT width or precision specifications:\n"); printf("circumference = %f, area = %f\n", circumference, area); printf("printf WITH width or precision specifications:\n"); printf("circumference = %7.2f, area = %7.2f", circumference, area); /* Input radius:5.3 printf WITHOUT width or precision specifications: circumference = 33.300854, area = 88.247263 printf WITH width or precision specifications: circumference = 33.30, area = 88.25 */ /* CONCLUSION: %m.n + 格式字符 m 代表域宽 ---输出数据占m个字符宽度 .n 代表输出精度 ---输出数据显示精度为n,指保留n位小数 注:小数点也占1个字符位置 m为正整数,当输出数据宽度小于m时,数据向右对齐,左边多余位补空格 m为负整数,向左对齐,右边多余位补空格 */ /* precise [prɪˈsaɪs] ① (ADJ-GRADED) 明确的;确切的 You use precise to emphasize that you are referring to an exact thing, rather than something vague. ② (ADJ-GRADED) 精确的;准确的 Something that is precise is exact and accurate in all its details. precision [prɪˈsɪʒn] ① (N-UNCOUNT) 精确;精密 If you do something with precision, you do it exactly as it should be done. specific [spəˈsɪfɪk] ① (ADJ-GRADED) (部位、问题、题目等)特定的,特别的 You use specific to refer to a particular fixed area, problem, or subject. ② (ADJ-GRADED) (表达)明确的,确切的,具体的 If someone is specific, they give a description that is precise and exact. You can also use specific to describe their description. specification [ˌspesɪfɪˈkeɪʃn] ① (N-COUNT) 规格;具体要求 A specification is a requirement which is clearly stated, for example about the necessary features in the design of something. */ /* 函数scanf(): scanf()函数的格式控制字符串中存在除格式说明符以外的其他字符,那么这些字符必须 在输入数据时由用户从键盘原样输入. scanf()输入数值型数据时, 遇到以下几种情况都认为输入数据结束: (1)遇到空格符、回车符、制表符; (2)达到输入域宽; (3)遇非法字符输入; */ /* the init part only for block2 to block4 */ int a, b; /* --independent block2 %2d%2d --GETTING THE RESULT FROM THE FOLLOWING CONCLUSION 1: */ scanf_s("%2d%2d", &a, &b); printf("a = %d, b = %d\n", a, b); /* 1234 a = 12, b = 34 CONCLUSION 1: scanf()函数中: %m + 格式字符: 按照指定宽度从输入的数据中截取所需的m个数据读入到相应变量. 注:scanf() 没有精度修饰符. */ /* --independent block3 %d%*c%d --GETTING THE RESULT FROM THE FOLLOWING CONCLUSION 2 */ //用户可以用"任意字符"作为分隔符进行数据的输入 scanf_s("%d%*c%d", &a, &b);//%*c 读取数据进入变量时,忽略读取一个字符型数据 printf("a = %d, b = %d\n", a, b); /* --independent block4 %2d%*2d%2d --GETTING THE RESULT FROM THE FOLLOWING CONCLUSION 2 */ //用户输入123456 scanf_s("%2d%*2d%2d", &a, &b); printf("a = %d, b = %d\n", a, b); /* 123456 a = 12, b = 56 %*2d --- 代表中间有二位输入的数值型数据被截取忽略读取,之后输入的数据开始读入到相应的变量 CONCLUSION 2: scanf()函数中: %*m + 格式字符: 按照指定宽度从输入的数据中截取m个数据忽略掉,后面的数据读入到相应的变量. */ return 0; }
最新回复(0)