【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转
学习总结
1、if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else。
2、根据个人几年的编程经验,太多的if…else…嵌套会加大代码的可读性和维护难度。个人认为代码最好不要超过三层if…else…的嵌套,否则最好使用布尔值控制流程。
3、逻辑运算符优先级:!>&&>||
4、运行到continue语句将导致剩余的迭代部分被忽略,开始下一次迭代。continue仅用于循环,而break语句用于循环和switch中。
5、编程题(题1):
#include <stdio.h>
int main(){
int space=,newline=,other=;
char ch;
printf("please enter something:\n");
while((ch=getchar())!='#'){
if(ch=='\n'){
newline+=;
}else if(ch==' '){
space+=;
}else{
other+=;
}
}
printf("space is %d\n",space);
printf("newline is %d\n",newline);
printf("other is %d\n",other);
return ;
}
运行结果:
please enter something:
hello world!
hi nihao.
#ABC
space is 2
newline is 2
other is 19
6、编程题(题11):
#include <stdio.h>
#define ARTICHOKE_UNIT_PRIC 1.25
#define BEET_UNIT_PRICE 0.65
#define CAROTA_UNIT_PRICE 0.89
#define DISCOUNT 0.05
#define T_0_5 3.50
#define T_5_20 10.00
#define T_20_ 0.1 int main(){
double a,b,c,ap,bp,cp,ac,bc,cc,sc,dc,tc;
ap=ARTICHOKE_UNIT_PRIC;
bp=BEET_UNIT_PRICE;
cp=CAROTA_UNIT_PRICE;
printf("how many artichoke you want(pound):");
scanf("%lf",&a);
if(a==)return ; printf("how many beet you want(pound):");
scanf("%lf",&b);
if(b==)return ; printf("how many carota you want(pound):");
scanf("%lf",&c);
if(c==)return ; printf("\n------UNIT PRICE------\n");
printf("artichoke's unit price is $%.2f(one pound) \n",ap);
printf("beet's unit price is $%.2f(one pound)\n",bp);
printf("carota'unit price is $%.2f(one pound)\n",cp); printf("\n------ORDER------\n");
printf("artichoke:%.2fpound\n",a);
printf("beet:%.2fpound\n",b);
printf("carota:%.2fpound\n",c); printf("\nartichoke is $%.2f",a*ap);
printf("\nbeet is $%.2f",b*bp);
printf("\ncarota is $%.2f\n",c*cp);
sc=a*ap+b*bp+c*cp;
printf("\ntotal cost is $%.2f",sc);
dc=sc>?sc*DISCOUNT:;
printf("\ndiscount is $%.2f",dc); printf("\ntotal weight is %.2f",a+b+c);
if(<(a+b+c)<=){
tc=T_0_5;
}
if(<(a+b+c) && (a+b+c)<=){
tc=T_5_20;
}
if((a+b+c)>){
tc=+(a+b+c)*0.1;
}
printf("\nttransport cost is $%.2f",tc);
printf("\norder cost is $%.2f\n",sc-dc+tc); return ;
}
运行结果:
how many artichoke you want(pound):123
how many beet you want(pound):234
how many carota you want(pound):343
------UNIT PRICE------
artichoke's unit price is $1.25(one pound)
beet's unit price is $0.65(one pound)
carota'unit price is $0.89(one pound)
------ORDER------
artichoke:123.00pound
beet:234.00pound
carota:343.00pound
artichoke is $153.75
beet is $152.10
carota is $305.27
total cost is $611.12
discount is $30.56
total weight is 700.00
ttransport cost is $78.00
order cost is $658.56
【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转的更多相关文章
- C Primer Plus 第7章 C控制语句:分支和跳转 编程练习
作业练习 1. #include <stdio.h> int main(void) { char ch; int spare, other, n; //空格,其他字符,换行 spare = ...
- C Primer Plus 第6章 C控制语句:循环 编程练习
记录下写的最后几题. 14. #include <stdio.h> int main() { double value[8]; double value2[8]; int index; f ...
- Go语言学习笔记四: 运算符
Go语言学习笔记四: 运算符 这章知识好无聊呀,本来想跨过去,但没准有初学者要学,还是写写吧. 运算符种类 与你预期的一样,Go的特点就是啥都有,爱用哪个用哪个,所以市面上的运算符基本都有. 算术运算 ...
- C语言学习书籍推荐《C Primer Plus(中文版)(第5版)》下载
普拉塔 (Prata S.) (作者), 云巅工作室 (译者) <C Primer Plus(中文版)(第5版)>共17章,介绍了C语言的基础知识,包括数据类型.格式化输入输出.运算符.表 ...
- 【C语言学习】《C Primer Plus》第1章 概览
学习总结 1.C语言于1972年由贝尔实验室的Dennis Ritchie在与Ken Thompson一起设计UNIX操作系统的时候开发的.的的设计构想来源于Ken Thompson的B语言.Anyw ...
- linux 下C语言学习路线
UNIX/Linux下C语言的学习路线.一.工具篇“公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工 ...
- 郝斌老师C语言学习笔记(一)
在给变量分配内存时,很可能这段内存存在以前其他程序使用留下的值.当使用VC编译器,若编译器发现没有给变量赋值而使用,就会返回一个以“85”开头的很大的数字(此时该段内存中为一个垃圾数,为了避免出现较常 ...
- Unix和Linux下C语言学习指南
转自:http://www.linuxdiyf.com/viewarticle.php?id=174074 Unix和Linux下C语言学习指南 引言 尽管 C 语言问世已近 30 年,但它的魅力仍未 ...
- C语言学习 第八次作业总结
本次作业其实没有新的内容,主要就是复习上一次的一维数组的相关内容.冯老师布置了5道题目,其中涉及到一些比较简单的排序或者是查找的方法.因为数据很少,所以直接使用for循环遍历就可以了. 关于本次作业, ...
- C语言学习 第七次作业总结
C语言学习 第七次作业总结 数组可以分为数组和多下标数组(在传统的国内C语言书本中,将其称为二/多维数组). 数组名称 在之前的课程中,大家应该都有印象,对于int a这样的定义,会为变量 a 声明一 ...
随机推荐
- Linux服务器上安装织梦CMS
安装篇 第一步:配置防火墙(默认情况下,端口80和3306是拒绝访问的,在防火墙上进行配置): vi /etc/sysconfig/iptables(在"COMMIT"的上一行加上 ...
- 对button或radiobutton制作样式
制作样式在drawable中进行 (cheakbox)在样式中点击变换图片,<selector> <item android:state_checked="true&quo ...
- Sass的学习
第一章:Sass简介 一. 什么是CSS预处理器 定义:CSS预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为CSS增加一些编程的特性,将CSS作为目标生成文件,然后开发者就只要使用 ...
- 异常:System.Data.EvaluateException: 未找到列[District].
异常:System.Data.EvaluateException: 未找到列[District]. 这里存在的问题不一定是说,数据源表没有该字段.此问题在于数据库字段包含空格字符.
- 如何将动态生成Word文件
大致的思路是先用office2003或者2007编辑好Word的样式,然后另存为XML,将XML翻译为FreeMarker模板,最后用Java来解析FreeMarker模板并输出Doc.经测试这样方式 ...
- Windows Phone 8.1新特性 - 应用商店启动协议
Windows Phone 8.1 Preview SDK 发布也有几个月了,之前断断续续也写过几篇 Windows Phone 8.1 新特性的文章,今天给大家介绍一下应用商店启动协议相关的知识. ...
- Codeforces #380 div2 D(729D) Sea Battle
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- java-代理模式及动态代理
代理模式 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问.在考虑到性能或安全等因素的情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. ...
- vim 配置
backspace有几种工作方式,默认是vi兼容的.对新手来说很不习惯.对老vi 不那么熟悉的人也都挺困扰的.可以用set backspace=indent,eol,start来解决. indent: ...
- Linux环境变量配置
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置./etc/bashrc:为每一个运 ...