C Primer Plus 第7章 C控制语句:分支和跳转 编程练习
作业练习
1.
#include <stdio.h>
int main(void) {
char ch;
int spare, other, n; //空格,其他字符,换行
spare = other = n = 0;
while ((ch = getchar()) != '#') {
if (ch == ' ')
spare++;
else if (ch == '\n')
n++;
else
other++;
}
printf("spare:%d\nn:%d\nother:%d", spare, n, other);
return 0;
}
2.
#include <stdio.h>
int main(void) {
char ch;
int count;
while ((ch = getchar()) != '#'){
printf("%3d ",ch);
count++;
if (count % 8 == 0)
printf("\n");
}
return 0;
}
3.
#include <stdio.h>
int main(void) {
int value, count_o, count_e;
float odd, even;
odd = even = count_o = count_e = 0;
while ((scanf("%d", &value)) == 1 && value != 0) {
if (value % 2 == 0) {
odd = odd + value;
count_o++;
} else {
even = even + value;
count_e++;
}
}
if (count_o > 0)
printf("odd average:%.2f count:%2d\n", odd / count_o, count_o);
if (count_e > 0)
printf("even average:%.2f count:%2d\n", even / count_e, count_e);
return 0;
}
4.
#include <stdio.h>
int main(void) {
char ch;
int count = 0;
while ((ch = getchar()) != '#'){
if(ch == '.'){
ch = '!';
count++;
} else if (ch == '!'){
printf("%c");
count++;
}
printf("%c",ch);
}
printf("Number:%d",count);
return 0;
}
5.
#include <stdio.h>
int main(void) {
char ch;
int count = 0;
while ((ch = getchar()) != '#'){
switch (ch){
case '.':ch='!';count++;
break;
case '!':printf("%c",ch);count++;
break;
}
printf("%c",ch);
}
printf("Number:%d",count);//一共替换了多少次。
return 0;
}
6.
#include <stdio.h>
int main(void) {
char ch,ch2,ch3;
//ch3为前一个字符,ch为当前字符,我真没想到。。
int count = 0;
while ((ch=getchar()) != '#'){
ch3=ch2;ch2=ch;
if (ch3 == 'e' && ch == 'i')
count++;
}
printf("%d",count);
return 0;
}
7.
#include <stdio.h>
#define WORK_OVERTIME 40
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define WAGE 10.0
#define WAGE_OVERTIME 15.0
#define BREAK1 300.0
#define BREAK2 450.0
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + ((BREAK2 - BREAK1) * RATE2))
int main(void) {
int hr;
double wage,income,tax;
printf("Please enter the hr (q to quit) :");
while ((scanf("%d",&hr)) == 1){
if (hr <= WORK_OVERTIME)
wage = hr * WAGE;
else wage = WORK_OVERTIME * WAGE +((hr - WORK_OVERTIME) * WAGE_OVERTIME);
if (wage <= BREAK1){
tax = wage * RATE1;
}
else if (wage > 300 && wage <= 450 ){
tax = BASE1 + (wage - BREAK1) * RATE2;
}
else tax = BASE2 + (wage - BREAK2) * RATE3;
income = wage - tax;
printf("The wage: %.2f income:%.2lf tax:%.2lf\n", wage, income, tax);
printf("Please enter the hours (q to quit) :");
}
return 0;
}
8.
#include <stdio.h>
#define WORK_OVERTIME 40
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300.0
#define BREAK2 450.0
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + ((BREAK2 - BREAK1) * RATE2))
int main(void) {
int hr,number;
double wage,income,tax,wage2;
printf("------------------------------------------------------------------------\n");
printf("Enter the number number corresponding to the desired pay rate or action:\n");
printf("1) $8.15/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("------------------------------------------------------------------------\n");
printf("Please enter your choise:");
while ((scanf("%d", &number)) == 1){
switch (number){
case 1: wage2 = 8.75;
break;
case 2: wage2 = 9.33;
break;
case 3: wage2 = 10.00;
break;
case 4: wage2 = 11.20;
break;
default:
printf("Please enter the : 1 2 3 4 5 \n");
printf("Please enter your choise:");
continue; //continue使得程序跳过该循环的其余部分
}
if (number == 5) break;
printf("Please enter the hours:");
scanf("%d",&hr);
if (hr <= WORK_OVERTIME)
wage = hr * wage2;
else wage = WORK_OVERTIME * wage2 +((hr - WORK_OVERTIME) * wage2 * 1.5);
if (wage <= BREAK1){
tax = wage * RATE1;
}
else if (wage > 300 && wage <= 450 ){
tax = BASE1 + (wage - BREAK1) * RATE2;
}
else tax = BASE2 + (wage - BREAK2) * RATE3;
income = wage - tax;
printf("The wage: %.2f income:%.2lf tax:%.2lf\n", wage, income, tax);
printf("------------------------------------------------------------------------\n");
printf("Enter the number number corresponding to the desired pay rate or action:\n");
printf("1) $8.15/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("------------------------------------------------------------------------\n");
printf("Please enter your choise:");
}
return 0;
}
9.
#include <stdio.h>
//写这题的时候我是懵逼的,只好看了下答案。2333
//解决方式是能被div整除的就不是素数
int main(void) {
int limit;
int num;
int div;
int numisprime; //当其为1的时候num为素数,如果num不是素数时,其变为0
printf("Please enter a positive integer:");
while ((scanf("%d", &limit)) == 1 && limit > 0) {
if (limit > 1)
printf("Here are the prime numbers up through %d\n", limit);
else
printf("No primes.\n");
for (num = 2; num <= limit; num++)
{
for (numisprime = 1, div = 2; (div * div) <= num; div++)
if (num % div == 0)
numisprime = 0;
if (numisprime == 1)
printf("%d is prime.\n", num);
}
printf("Please enter a positive integer (q to quit) :");
}
return 0;
}
10.
#include <stdio.h>
#define RATE1 0.15
#define RATE2 0.28
int main(void) {
int number;
double wage,tax,wage2; //wage工资,tax税收,wage2税金分界点
printf("------------------------------------------------------------------------\n");
printf("Enter the number number corresponding to the desired pay rate or action:\n");
printf("1) Single tax 2) Household tax\n");
//1.单身 2.户主 3.共有 4.离异
printf("3) Married Total tax 4) Married divorced tax\n");
printf("5) quit\n");
printf("------------------------------------------------------------------------\n");
printf("Please enter your choise:");
while ((scanf("%d", &number)) == 1 && number != 5){
switch (number){
case 1: wage2 = 17850; //单身
break;
case 2: wage2 = 23900; //户主
break;
case 3: wage2 = 29750; //已婚,共有
break;
case 4: wage2 = 14875; //已婚,离异
break;
default:
printf("Please enter the : 1 2 3 4 5 \n");
printf("Please enter your choise:");
continue; //continue使得程序跳过该循环的其余部分
}
printf("Please enter the wage:");
scanf("%lf",&wage);
if (wage <= wage2)
tax = wage *RATE1;
else tax = wage2 * RATE1 + ((wage - wage2) * RATE2);
printf("Your tax is :%.2lf\n", tax);
printf("Please enter your choise:");
}
return 0;
}
11.
#include <stdio.h>
#define GOODS1 2.05 //货物1
#define GOODS2 1.15 //货物2
#define GOODS3 1.09 //货物3
#define BREAK1 5
#define BREAK2 20
#define BASE1 6.5
#define BASE2 14
#define DISCOUNT_MONEY 100
#define DISCOUNT 0.05
int main(void) {
int number;
double weight,cost,discount,cost2,goods;
double all;
double a,b,c,w_a,w_b,w_c;
printf("请选择需要购买的货物:\n");
printf("1)洋蓟:2.05/lb\n");
printf("2)甜菜:1.15/lb\n");
printf("3)胡萝卜:1.09/lb\n");
printf("q)退出选购\n");
printf("请输入你的选择:");
while (scanf("%d", &number) == 1 ){
switch (number){
case 1:printf("请输入需要洋蓟的重量:");
scanf("%lf", &a);
break;
case 2:printf("请输入需要甜菜的重量:");
scanf("%lf", &b);
break;
case 3:printf("请输入需要胡萝卜的重量:");
scanf("%lf", &c);
break;
default:printf("输入错误,请输入合适的选项(1,2,3,q):");
continue;
}
w_a = w_a + a;
w_b = w_b + b;
w_c = w_c + c;
a = b = c = 0;
printf("请输入你的选择:");
}
cost = w_a * GOODS1 + w_b * GOODS2 + w_c * GOODS3;
weight = w_a + w_b + w_c;
if (cost >= DISCOUNT_MONEY){
discount = cost * DISCOUNT;
cost = cost * (1 - DISCOUNT);
}
if (weight <= BREAK1)
cost2 = BASE1;
else if (weight > BREAK1 && weight <= BREAK2)
cost2 = BASE2;
else cost2 = BASE2 + ((weight - BREAK2) * 0.5);
printf("所需洋蓟的磅数:%.2lf\n", w_a);
printf("所需甜菜的磅数:%.2lf\n", w_b);
printf("所需胡萝卜的磅数:%.2lf\n", w_c);
printf("订购的总磅数:%.2lf\n", weight);
printf("所需洋蓟的费用:%.2lf\n", w_a * GOODS1);
printf("所需甜菜的费用:%.2lf\n", w_b * GOODS2);
printf("所需胡萝卜的费用:%.2lf\n", w_c * GOODS3);
printf("订单的运输和装卸费用:%.2f\n", cost2);
printf("订单的折扣:%.2f\n", discount);
printf("订单的总费用(包括运输费用,而且还要减去折扣):%.2f\n",cost + cost2);
return 0;
}
C Primer Plus 第7章 C控制语句:分支和跳转 编程练习的更多相关文章
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- C控制语句--分支和跳转
/*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...
- C Primer Plus 第6章 C控制语句:循环 编程练习
记录下写的最后几题. 14. #include <stdio.h> int main() { double value[8]; double value2[8]; int index; f ...
- C Primer Plus_第三章_数据和C_复习题与编程练习
Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更 ...
- C Primer Plus_第6章_循环_编程练习
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- 零基础学Python--------第3章 流程控制语句
第3章 流程控制语句 3.1程序的结构 计算机在解决某个具体问题时,主要有3种情况,分别是顺序执行所有的语句.选择执行部分语句和循环执行部分语句.程序设计中的3种基本结构为顺序结构.选择结构和循环结构 ...
- Python实验报告——第3章 流程控制语句
实验报告 [实验目的] 1.掌握python中流程控制语句的使用,并能够应用到实际开发中. [实验条件] 1.PC机或者远程编程环境 [实验内容] 1.完成第三章流程控制语句实例01-09,实战一到实 ...
- 【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转
学习总结 1.if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else. 2.根据个人几年的编程经验,太多的if…else…嵌套会加大代码的 ...
随机推荐
- wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果
上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个 两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...
- 通过CSS显示垂直文本
原文链接: CSS Vertical Text 原文日期: 2014年03月18日 翻译日期: 2014年3月22日 翻译人员: 铁锚 示例地址: http://davidwalsh.name/dem ...
- SpriteBuilder中的粒子系统属性
一个粒子发射器可以有2种模式,放射状和重力的(radial or gravity) 放射状模式允许你去使用发射器创建粒子旋涡状环绕在指定位置的效果. 当启用重力效果,你可以使得粒子在任何方向任意飞行, ...
- 【63】关系数据库常用的sql语句总结
创建表 语法 CREATE TABLE <表名>(<列名> <数据类型>[列级完整性约束条件] [,<列名> <数据类型>[列级完整性约束条 ...
- 关于Service中bindService注意的几个问题
最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...
- windows linux—unix 跨平台通信集成控制系统----文件搜索
跨平台的网络通信,跟设备的集成控制,牵扯到在各种平台下的文件搜索问题,windows下面的已经有了. 地址如下: http://blog.csdn.net/wangyaninglm/article/d ...
- PS 滤镜算法原理——碎片效果
%%% Fragment %%% 对原图做四个方向的平移,然后对平移的结果取平均 %%% 碎片效果 clc; clear all; Image=imread('4.jpg'); Image=doubl ...
- C语言算法---求鞍点
题目:有一个3X4矩阵,要求输出其鞍点(行列均最大的值),以及它的行号和列号. int a[3][4] = {{123,94,-10,218}, {3 ...
- 开源项目AndroidReview学习小结(2)
读书破万卷下笔如有神 作为入门级的android码农的我,还是需要多多研读开源代码 下面继续接着上一篇的分析,这一篇主要介绍第一个tab,ReviewFragment的分析,界面看起来简单,背后的逻辑 ...
- leetCode之旅(5)-博弈论中极为经典的尼姆游戏
题目介绍 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...