作业练习

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控制语句:分支和跳转 编程练习的更多相关文章

  1. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  2. C控制语句--分支和跳转

    /*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...

  3. C Primer Plus 第6章 C控制语句:循环 编程练习

    记录下写的最后几题. 14. #include <stdio.h> int main() { double value[8]; double value2[8]; int index; f ...

  4. C Primer Plus_第三章_数据和C_复习题与编程练习

    Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更 ...

  5. 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 ...

  6. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  7. 零基础学Python--------第3章 流程控制语句

    第3章 流程控制语句 3.1程序的结构 计算机在解决某个具体问题时,主要有3种情况,分别是顺序执行所有的语句.选择执行部分语句和循环执行部分语句.程序设计中的3种基本结构为顺序结构.选择结构和循环结构 ...

  8. Python实验报告——第3章 流程控制语句

    实验报告 [实验目的] 1.掌握python中流程控制语句的使用,并能够应用到实际开发中. [实验条件] 1.PC机或者远程编程环境 [实验内容] 1.完成第三章流程控制语句实例01-09,实战一到实 ...

  9. 【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转

    学习总结 1.if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else. 2.根据个人几年的编程经验,太多的if…else…嵌套会加大代码的 ...

随机推荐

  1. 跨平台移动APP开发进阶(四)AngularJS简介

    AngularJS 是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件. 它的创新点在于,利用 数据绑定 和 依赖注入 ...

  2. Cocos2D v2.0至v3.x简洁转换指南(三)

    Cocos2D 3.3中的注意事项 如果你在使用Cocos2D 3.3+(是SpriteBuilder 1.3+的一部分)你将不得不替分别的换所有存在的UITouch和UITouchEvent为CCT ...

  3. MT6575 3G切换2G

    因为了节省成本,需要从现在的3G方案切换置2G方案,做的修改,做个笔记. 一: 将MTK给过来的补丁编译出如下文件. 二:在mediatek/custom/common/modem/  路径下增加一个 ...

  4. 并发服务器--02(基于I/O复用——运用epoll技术)

    本文承接自上一博文I/O复用——运用Select函数. epoll介绍 epoll是在2.6内核中提出的.和select类似,它也是一种I/O复用技术,是之前的select和poll的增强版本. Li ...

  5. Unix/Linux中的read和write函数

    文件描述符 对于内核而言,所有打开的文件都通过文件描述符引用.文件描述符是一个非负整数.当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符.当读或写一个文件时,使用open或creat ...

  6. Android百分比布局支持库(android-percent-support)

    Android中提供了五种布局,其中用的最多的就是:LinearLayout, RelativeLayout 和 FrameLayout这三种布局,在对某一界面进行布局时最先想到也是通过这三种来布局的 ...

  7. Android WebKit 内核

    一.WebKit简介 WebKit是一个开源的浏览器网页排版引擎,包含WebCore排版引擎和JSCore引擎.WebCore和JSCore引擎来自于KDE项目的KHTML和KJS开源项目.Andro ...

  8. TCP连接建立系列 — 连接请求块

    连接请求块(request_sock)之于TCP三次握手,就如同网络数据包(sk_buff)之于网络协议栈,都是核心的数据结构. 内核版本:3.6 Author:zhangskd @ csdn blo ...

  9. schema workbench的操作

    1.schema workbench连不上sql,如下: 看不懂,这是驱动的问题,你只要使用最新的驱动,mysql-connector-java-5.1.13-bin.jar 将对应驱动放入schem ...

  10. thrift实现HDFS文件操作

    thrift 文件如下 namespace java com.pera.file.transform struct  File{     1:string path ,     2:string co ...