C Primer Plus 8 学习笔记
C Primer Plus 8
数据输入/输出和输入验证
Promgraming Test
(1)在遇到EOF之前,作为字符流读取输入,要求打印每个输入字符以及其对应的ACSII十进制
#include<stdio.h>
int main(void)
{
int counter = 0;
char ch;
while((ch = getchar())!=EOF)
{
if (counter++ == 10){
printf("\n");
counter = 1;
}
if (ch>='\040')
{
printf("\'%c\'---%3d",ch,ch);
}else if(ch=='\n'){
printf("\\n---\\n.\n");
counter = 0;
}else if(ch=='\t'){
printf("\\t---\\t");
}else{
printf("\'%c'---^%c ",ch,(ch+64));
}
}
return 0;
}
(2)读取字符输入,统计字符中大小写的数量,可用到ctype.h库中的isupper()和islower()来判断。
#include<stdio.h>
int main(void)
{
int lowercase = 0;
int uppercase = 0;
char ch;
while((ch = getchar())!=EOF)
{
if(ch>='A' && ch<='Z')
uppercase++;
if(ch>='a' && ch<='z')
lowercase++;
/*也可以用islower()和isupper()判断*/
/*if(isupper(ch)) uppercase++
if(isloower(ch)) lowercase++
需要添加头文件ctype.h*/
}
printf("There are %d uppercase,and %d lowercase in that file!\n",uppercase,lowercase);
return 0;
}
(3)使用二分查找判断被查找数与区间中值的大小关系判断,不断缩小查找范围
#include<stdio.h>
int main(void)
{
int head = 1;
int tail = 100;
int guess = (head+tail)/2;
char ch;
printf("Pick an integer from 1 to 100.I will try to guess it.\n");
printf("Respond with a y if my guess is right.\n");
printf("Respond with an n if my guess is wrong.\n");
do
{
printf("Un...is your number %d?",guess);
if(getchar()=='y')
break;
printf("Well,then,%d is larger or smaller than yours?(l or s)",guess);
while((ch = getchar())=='\n')continue;
if(ch=='l'||ch=='L')
{
tail = guess - 1;
guess = (head+tail)/2;
continue;
}
//如果输入l则表明目标数在区间前半区,因此可以舍去中数到终止位置后半段数据,切换tail和guess
else if(ch == 's'||ch=='S')
{
head = guess + 1;
guess = (head+tail)/2;
continue;
}
//如果输入s则表明目标数在区间后半区,因此可以舍去中数到终止位置前半段数据,切换head和guess
else{continue;}
}while(getchar()!='y');
printf("I know i can do it!\n");
getchar();
return 0;
}
(4)给出一个供选择的工资等级菜单,使用switch语句完成工资等级选择。
#include<stdio.h>
#define Extra_hour 1.5
#define Base_tax 0.15
#define Extra_tax 0.2
#define Exceed_tax 0.25
void show_menu(void);//显示基本工资的函数
float get_hours(void);//获取用户工作时长的函数
void calc_salary(float base_salary,float hours);//根据基本工资和时长计算工资税金净输入的函数
int main(void)
{
float hours = 0;
char selected;
do
{
show_menu();
scanf("%c",&selected);
switch(selected)
{
case '1':
printf("Hello,you selected 8.75/hr.Enter the work hours:\n");
scanf("%f",&hours);
calc_salary(8.75,hours);
break;
case '2':
printf("Hello,you selected 8.75-hr.Enter the work hours:\n");
scanf("%f",&hours);
calc_salary(9.33,hours);
case '3':
printf("Hello,you selected 8.75/hr.Enter the work hours:\n");
scanf("%f",&hours);
calc_salary(10.00,hours);
break;
case '4':
printf("Hello,you selected 8.75/hr.Enter the work hours:\n");
scanf("%f",&hours);
calc_salary(11.20,hours);
break;
case '5':
break;
default:
printf("Error selected.Please retry!\n");
getchar();
break;
}
}while(selected!='5');
printf("Done.\n");
return 0;
}
//以下是写函数环节。。。
void show_menu(void)//显示菜单
{
char s1[] = "(1) 8.75/hr";
char s2[] = "(2) 9.33/hr";
char s3[] = "(3) 10.00/hr";
char s4[] = "(4) 11.20/hr";
char s5[] = "(5) quit.";
printf("---------------------------------------------\n");
printf("%-40s",s1);
printf("%-40s\n",s2);
printf("%-40s",s3);
printf("%-40s\n",s4);
printf("%-40s\n",s5);
printf("---------------------------------------------\n");
}
void calc_salary(float base_salary,float hours)
{
float salary,tax,taxed_salary;
if(hours<=30)//工作时间少于30h的情况
{
salary = hours*base_salary;
tax = salary*Base_tax;
taxed_salary = salary -
