C Primer Plus (第六版)第七章 编程 练习题 部分 解答

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

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.提示用户输入一周工作时数,然后打印工资总额、税金和净收入。
假设

[C primer plus] 编程练习 ——第七章 【c prime plus编程练习 第七章 阅读详情

相关推荐

C Primer Plus 第6版 编程练习参考答案 第7章

目前在学C Primer Plus,记录以下自己的学习过程 ^-^

weixin_58979213的博客 438

2020-06-30

编写一个程序读取输入读到#字符停止程序要打印每个输入字符以及对应ASCII(十进制)。每行打印8个字符字符-ASCLL”组合。建议:使用字符计数求模运算符(%)在每8个循环周期时打印一个换行符。 #include<stdio.h> int main(void) { char ch; int i = 0; printf("please enter character\n"); while ((ch = getchar())!='#') { if (i % 8 == 0

weixin_46332624的博客 494

C primer plus (第六版) 习题答案//总结篇

当我刚开始学习C语言的时候,我选择了《C primer plus》这本书。 而作为C语言的经典入门书籍,书上的内容令我受益颇多。 可当我为课后习题苦恼不已时,我的手边并没有一份较为完备,准确的习题答案。 借 的一句话: 所以我决定自己重新手打一份准确,完备的《C primer plus》的答案,以供初入此道的萌新们参考。 当然,如有疏漏,欢迎在评论区提出。 ...

weixin_45638291的博客 6万+

(C语言编写一个程序读取输入读到#字符停止,然后报告读取的空格数、换行符数其他所有字符的数量 。

代码】(C语言编写一个程序读取输入读到#字符停止,然后报告读取的空格数、换行符数其他所有字符的数量。

gy200203的博客 1233

7.12 编程练习题1

/*1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数,换行符数其他所有字符的数量*/ #include #include //getchar()头文件 int main (void) { char ch; int kgs = 0; int hhf = 0; int qtz = 0; printf("

风萧萧、水易寒 625

C primer plus 复习题答案(上)

C primer plus 复习题答案 第一章 初识C语言 ​ 对编程而言,可移植性意味着什么 在软件工程中,可移植性,又译为移植性、可携性,是指使用高阶语言写成的软件,在不同环境下,是否具备可以被重复使用的性质。一般来说,软件是否具备可移植性的衡量标准,在于进行软件移植时,需要付出多少工时为代价。具备高可移植性的软件,在移植到不同系统平台时,并不需要做太多事情,因此能够减少软件开发及布署时的成本。为了使软件具备高度可移植性,程序员需要使应用程序界面抽象化以及模组化。 以低阶语言,例如汇编语言,写成的软

Jonathan的博客 3万+

C Primer Plus第六版(中文版)编程练习答案(完美修订版)汇总

本文是博主编写的C Primer Plus第六版(中文版)编程练习答案的所有链接

CLOVER的博客 15万+

C Primer Plus (第六版) 第十章_编程练习答案

有什么不对的地方,欢迎给我留言 no1.c // 修改程序10.7的rain.c,用指针进行计算(仍然要声明并初始化数组) # include <stdio.h> # define MONTHS 12 # define YEARS 5 int main(void) { const float rain[YEARS][MONTHS] = { {4.3 , 4.3 , ...

weixin_44603568的博客 2710

C primer plus 第六版 第七章 编程练习 答案

7.1 //编写一个程序读取输入读到#字符停止,然后报告读取的空格数 //换行符数所有其他字符的数量 #include &amp;lt;stdio.h&amp;gt; #define STOP '#' #define SPACE ' ' int main(void) { char ch; int lines=1; int spaces=0; int others=0; while ((ch =...

Double____C的博客 2765

C++ Primer Plus第六版(中文版)课后编程练习答案(重置版)汇总

本文是博主编写的C++ Primer Plus第六版(中文版)编程练习答案的所有链接

CLOVER的博客 3万+

C primer plus(第六版) 第四章答案

C primer plus(第六版))第四章答案 /* 第一题 */ #include<stdio.h> int main(void) { char first_name[20],last_name[20]; printf("Please input your first_name.\n"); scanf("%s",first_name); printf("Pleas...

weixin_45638291的博客 6391

C Primer Plus 第六版 编程练习题及详细答案

本文提供了《C Primer Plus》第6版各章节编程练习的标准答案与个人解答代码,涵盖基础语法到高级应用的完整内容。以下是核心要点: 基础应用 第1-3章:单位转换(英寸/厘米)、ASCII字符处理、浮点数格式输出等基础练习,如1.1英寸转换程序、3.4浮点数显示函数。 流程控制 第5章:循环与条件语句应用,如5.1分钟转小时程序、5.5工资计算器(含多种税率处理)。 函数设计 第9章:自定义函数实现,如9.1返回较小值函数、9.3指定字符打印函数、9.5替换较大值的指针应用。 数组与指针 第10章:数

misterxize的博客 1699

C primer plus(第六版) 第九章答案

C primer plus(第六版) 第九章答案 /* First */ #include<stdio.h> double min( double x, double y ) { return ( x < y ? x : y); } int main() { double x,y,result; scanf("%lf",&x); scanf("%lf",&a...

weixin_45638291的博客 3786

C Primer Plus (第六版) 第十五章_编程练习答案

no1.c //编写一个函数,把二进制字符串转换为一个数值.例如,有下面语句: //char * pbin = "01001001" ; //那么把pbin作为参数传递给函数后,他应该返回一个int类型的值25; # include <stdio.h> # include <limits.h> # include <string.h> # include ...

weixin_44603568的博客 2229

《C Primer Plus第六版 习题 第四章

【4.8.1】/**编写一个程序,提示用户输入姓,然后以“名,姓”的格式打印出来**/#include <stdio.h>int main() { char last_name[20]; char first_name[20]; printf("\n请输入您的姓氏:\n"); scanf("%s",first_name); pr

TCP404 9492

C Primer Plus 第六版 第11章 编程答案

C Primer Plus 第六版 第11章 编程答案 1.第1题 #include <stdio.h> #define SIZE 10 char * getnchar(char *, int); int main(void) { char st[SIZE]; char * sts; printf("请输入:"); sts = getnchar(st, SIZE - 1); if (sts == NULL) puts("输入失败");

LGDNSX 581

《C Primer Plus第六版 习题 第二章

【2.12.1】/***************************************************** *编写一个程序,调用一次printf函数把你的姓名打印在一行 *再调用一次printf函数,把你的姓名分别打印在两行 *再调用两次printf函数,把你的姓名打印在一行 。 ***********************************************

TCP404 4587

C primer plus(第六版) 第五章答案

C primer plus(第六版))第四章答案 /* 第一题 */ #include<stdio.h> #define MIN 60 int main(void) { int min, hour, sec; printf("Please enter minutes.\n"); scanf("%d",&min); while (min > 0) { ...

weixin_45638291的博客 5051

C Primer Plus答案

C Primer Plus 第六版 第二章课后编程练习答案: http://blog4jimmy.com/2017/10/18.html C Primer Plus 第六版 第三章课后编程练习答案: http://blog4jimmy.com/2017/10/22.html C Primer Plus 第六版 第四章课后编程练习答案: http://blog4jimmy.com/2017/10/3...

qq_43776742的博客 1529
chenwei8711
博客等级 码龄17年 1粉丝 1原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值