5.Part 5
5.1程序严谨性
保证程序的严谨性
#include<stdio.h>
int main(void)
{
unsigned int age;
//为保证程序严谨性,保证年龄输入的是小于三位数的正整数,以此调整程序
printf("Please input your age:\n");
int age_intput_result = scanf_s("%3u", &age);
//输入的前三位为年龄,即age = 输入数字的前三位(无符号类型整数)
//输入类型为无符号数时,scanf结果为1并赋值给age_input_result
if (age_intput_result != 1) {
printf("输入的数字不合法!请必须是小于三位数的正整数!\n");;
}
else
{
printf("age = %u\n", age);
}
return 0;
}
5.2?else if
#include<stdio.h>
int main(void)
{
int age = 120;
if (age <= 18) {
printf("满足成年了!\n");
}
else if (age <= 30) {
printf("满足结婚年龄!\n");
}
else if (age <= 70)
{
printf("满足养老年龄!\n");
}
else if (age <= 100)
{
printf("高寿\n");
}
else
{
printf("神人\n");
}
return 0;
}
|