C Primer Plus (第六版)第七章 编程练习题 部分答题代码
最近在学习C Primer Plus ,以下是其中第七章编程题,我写的代码。已测试通过。(IDE :Codeblocks),不妥之处请指正
1.读取输入读到#字符停止,然后报告读取的空格数、换行数和所有其他字符
#include <stdio.h>
int main(void)
{
char ch;
unsigned n_space=0;/*空格数*/
unsigned n_lf=0;/*换行符数*/
unsigned n_other;/*其他字符数*/
int inword=0;
printf("Please enter text to be analyzed(# to terminate):\n");
while((ch=getchar())!='#')
{
inword++;
switch(ch)
{
case '\n': n_lf++;
break;
case ' ': n_space++;
break;
default : n_other++;
break;
}
}
if(inword==0)
printf("没有字符输入!\n ");
else
printf("空格数=%u,换行符数=%u,其他字符=%u\n",n_space,n_lf,n_other);
printf("Bye!\n");
return 0;
}
2.读取输入,读到#停止,然后打印每个输入字符以及对应的ASCII码值(十进制)。每行打印8个“字符-ASCII码”组合
/* 读取输入,读到#停止,然后打印每个输入字符以及对应的ASCII码值(十进制)。
每行打印8个“字符-ASCII码”组合*/
#include <stdio.h>
#include <string.h>//包含strlen()函数
#define COL 8//每行打印组合数
#define SIZE 1024//最大字符限制
#define L '-'//字符与ASCII码中间的破折号
#define LF '&'//使用&代替换行字符打印
int main(void)
{
char ch;
char text[SIZE]={0};
unsigned index=0;
/* *************读取输入数据并放入数组用于打印******************* */
printf("Enter text to analyzed:# to terminate.\n ");
while((ch=getchar())!='#'&&index<SIZE)
{
text[index]=ch;
index++;
}
/* ************丢弃超出数组大小限制的输入部分******************** */
while(getchar()!='\n')
continue;//丢弃多余输入,输入溢出部分超过了数组大小限制
/* ************打印已经读入数组的字符**************************** */
index=0;
while(index<strlen(text))
{
if(index%COL==0)
printf("\n");//每8个字符-ASCII码组合,打印一个换行符
printf("%2c%c%-8d",(text[index]=='\n')?LF:text[index],L,text[index]);/*如果是换行符使用&号符打印*/
index++;
}
if(index==0)
printf("None text entered\n");
printf("Bye!\n");
return 0;
}
3.读取整数,直到用户输入0,输入结束后,报告用户输入的偶数个数,这些偶数的平均值,奇数的个数及其奇数的平均值。
/*读取整数,直到用户输入0,输入结束后,报告用户输入的偶数个数,这些偶数的平均值,奇数的个数及其奇数的平均值。*/
#include <stdio.h>
int main(void)
{
long data;//用户输入的数值
long sum_e=0;//偶数和
long sum_o=0;//奇数和
unsigned even=0;//偶数个数
unsigned odd=0;//奇数个数
unsigned num=0;//用户输入数值个数
/* ****************数值录入及分析************************************ */
printf("Please enter some interer to anayzed:enter number 0 to terminate.\n");//提示用户输入整数
while(scanf("%ld",&data)==1&&data!=0)//输入无异常及数值不等于0时循环
{
num++;
if(data%2==0)
{
even++;//偶数个数递增
sum_e+=data;//偶数求和
}
else
{
odd++;//奇数个数递增
sum_o+=data;//奇数求和
}
}
/* ******************分析结果报告************************************ */
if(num==0)
printf("None integer entered!");//未进入循环时,即无有效数值被分析时提示
else
{
printf("The number of even numbers is %u,and the average of even numbers is %ld\n",even,sum_e/even);//偶数分析报告
printf("The number of odd numbers is %u,and the average of odd numbers is %ld\n",odd,sum_o/odd);//奇数分析报告
}
printf("Bye!\n");
return 0;
}
4.使用if else 语句编写–读取输入,读到#停止,
用感叹号代替句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换
5. 使用switch 语句再编写一次
两题代码放在一起了
/* 使用if else 语句编写--读取输入,读到#停止,
用感叹号代替句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换*/
/* 使用switch 语句再编写一次*/
#include <stdio.h>
int main(void)
{
char ch;//用户输入字符
unsigned num=0;//替换次数
/* **********任务处理循环********** */
printf("Please enter text to process.\n");
printf("Enter # to terminate.\n");
while((ch=getchar())!='#')
{
/* **************使用if else语句编写************** */
/*
if(ch=='.')
{
printf("!");
num++;
}
else if(ch=='!')
{
printf("!!");
num++;
}
else
printf("%c",ch);
*/
/* ***********使用switch 语句编写***************** */
switch(ch)
{
case '.': num++,printf("!");
break;
case '!': num++,printf("!!");
break;
default : putchar(ch);
}
}
/* **********结果报告***************************** */
printf("符号替换了%u次",num);
printf("Bye!\n");
return 0;
}
6.读取输入,读到#停止,报告ei出现的次数
#include <stdio.h>
int main(void)
{
char ch;//当前字符
char prev='\n';//前一个字符
unsigned times=0;//ei字符出现次数
printf("Enter text to analyzed:(# to terminate)\n");
while((ch=getchar())!='#')
{
if(prev=='e'&&ch=='i')
times++;
prev=ch;
}
printf("The\"ei\" finded %u times\n",times);
printf("Bye!\n");
return 0;
}
7.提示用户输入一周工作时数,然后打印工资总额、税金和净收入。
假设