Part1: 格式化输出函数printf()和格式化输入函数scanf()
ex2_1.cpp
#include <stdio.h> #include <stdlib.h> int main() { int x = 1234; float f = 123.456; double m = 123.456; char ch = ‘a‘; char a[] = "Hello, world!"; // 定义一个数组a,数组中连续存放了字符串常量hello,world! int y = 3, z = 4; printf("%d %d\n", y, z); printf("y=%d, z=%d\n", y, z); printf("%8d,%2d\n", x, x); printf("%f, %8f, %8.1f, %0.2f, %.2e\n", f, f, f, f, f); printf("%lf\n", m); printf("%3c\n", ch); printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n", a, a, a, a, a); system("pause"); return 0; }
控制了小数点的位数或在屏幕上占的空间
ex2_2.cpp
#include <stdio.h> #include <stdlib.h> int main() { double x, y; char c1, c2, c3; int a1, a2, a3; scanf_s("%d%d%d", a1, a2, a3); //少& printf("%d,%d,%d\n", a1, a2, a3); scanf_s("%c%c%c", &c1, &c2, &c3); printf("%c%c%c\n", c1, c2, c3); scanf_s("%f,%lf", &x, &y); //double类型用%lf printf("%f,%lf\n", x, y); system("pause"); return 0; }
Part2: 验证性内容
从键盘上输入3个数,让它们代表3条线段的长度,编写一个c程序判断这3条线段组成的三角形属于什么类型
(一般三角形、等腰三角形、等边三角形或不构成三角形)。(程序源码: ex2_3.cpp)
#include <stdio.h> #include <stdlib.h> int main() { double a, b, c; scanf_s("%lf %lf %lf", &a, &b, &c); if (a<0 || b<0 || c < 0) printf("不能构成三角形\n"); else if (a + b > c && a + c>b && b + c>a) { if (a == b && a == c) printf("构成等边三角形\n"); else if (a == b || a == c || b == c) printf("构成等腰三角形\n"); else printf("构成一般三角形\n"); } else printf("不能构成三角形\n"); system("pause"); return 0; }
要求用户从键盘输入0~9之间的数字,选择屏幕配色方案,打印字符串。(程序源码: ex2_4.cpp)
#include <stdio.h> #include <stdlib.h> int main() { char choice; // 显示菜单 printf("输入0~9以内的数字,选择屏幕背景色前景色方案: \n"); printf("1-\t黑底绿色\n"); printf("2-\t白底黑色\n"); printf("3-\t蓝底白色\n"); printf("其它-\t黑底白色(默认)\n"); printf("\n请输入, 选择你想使用的屏幕配色方案: "); // 要求用户从键盘输入0~9的数字,选择屏幕配色方案 choice = getchar(); // 根据用户输入,设置屏幕配色方案 if (choice == ‘1‘) { system("color 02"); printf("《黑客帝国》一类的影视剧常见这种屏幕色:)\n"); } else if (choice == ‘2‘) { system("color f0"); printf("大部分编辑器默认是这种配色:)\n"); } else if (choice == ‘3‘) { system("color 1f"); printf("这是机器蓝屏故障时的不讨喜色:)\n"); } else{ system("color 0f"); printf("控制台程序默认小黑窗:)\n"); } printf("programming is fun, just try@_@\n"); system("pause"); return 0; }
part3: 编程练习
从键盘上输入一个三位数以内的十进制整数,判断其是否满足正序、逆序相同。例如,121满足。156不满足。
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { int length, flag = 1; char a[100]; gets_s(a); length = strlen(a); for (int i = 0; i <= length / 2; i++) { if (a[i] != a[length - i - 1]) { flag = 0; break; } } if (flag == 1) printf("正序和逆序相同\n"); else printf("正序和逆序不同\n"); system("pause"); return 0; }
从键盘上输入年份和月份,计算该月有多少天。(要求:用if语句实现)
说明:教材p70 例3.25是用switch语句实现的。结合示例程序,理解算法逻辑,用if语句编程实现。
#include <stdio.h> #include <stdlib.h> int main() { int year, month, day, leapyear; scanf_s("%d%d", &year, &month); if (year<0||month<1||month>12) { printf("输入的数据错误\n"); exit(0); } leapyear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) day = 31; else if (month == 4 || month == 6 || month == 9 || month == 11) day = 30; else day = 28 + leapyear; printf("%d年%d月的天数为:%d\n", year, month, day); system("pause"); return 0; }
从键盘输入一个百分制成绩(整型量),要求输出成绩等级:优秀、良好、中等、及格、不及格。(要求:用
switch语句实现)
#include <stdio.h> #include <stdlib.h> int main() { int score; printf("输入分数(0~100):"); scanf_s("%d", &score); if (score < 0 || score>100) { printf("输入数据错误\n"); exit(0); } switch (score / 10) { case 10: case 9: printf("优秀\n"); break; case 8: printf("良好\n"); break; case 7: printf("中等\n"); break; case 6: printf("及格\n"); break; default: printf("不及格\n"); break; } system("pause"); return 0; }
实验总结与体会
输入要注意变量前的地址符,输出要注意变量的类型(%lf,%ld……)
基础性练习,难度不大
原文:https://www.cnblogs.com/faspk/p/11773546.html