【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 声明一 ...
随机推荐
- XE3随笔15:使用 IXMLHTTPRequest 简单获取网页源代码
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- Python执行命令行
背景 我们知道,虽然会破坏平台独立性,但是有的时候需要在代码里面调用命令行来获取一些信息,那么了解在 Python 中如何执行命令行至关重要 使用介绍 Python 中使用命令行可以通过 subpro ...
- MongoDB(NoSQL) 入门
一.简介 NoSQL数据库因其可扩展性使其变得越来越流行,利用NoSQL数据库可以给你带来更多的好处, MongoDB是一个用C++编写的可度可扩展性的开源NoSQL数据库. 本文主要讲述MongoD ...
- Config文件的读写
using System; using System.Configuration; using System.Xml; namespace COMMON { public class ConfigHe ...
- archlinux 安装过程记录
2014年安装了一次,使用U盘启动安装的,但是当时网络有问题,断断续续,没有做详细记录. 现在到了杭州,重新来一次. 使用U盘安装 下载ISO :http://mirrors.163.com/arch ...
- BZOJ 1564: [NOI2009]二叉查找树
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1564 Description Input Output 只有一个数字,即你所能得到的整棵树的访 ...
- Caring for our seniors
We all have our own journeys to make. And I have been thought that our journeys define us. Some jour ...
- java的三元运算符
1.三元运算符语法:判断表达式?表达式1:表达式2: (1)三元运算符适合于判断2个值到底使用哪一个! public static void mian(String[] args){ int sex= ...
- IAR之文件路径设置
1.命令解释 $PROJ_DIR$表示工程所在路径 $TOOLKIT_DIR$表示IAR安装目录所在头文件路径 \..\表示返回上一级目录. 2.头文件路径设置 打开工程文件,找到"opti ...
- Oracle查看用户操作sql语句以及数据库日志
--查看日志文件 select member from v$logfile; --查看表空间使用情况 SELECT SUM(bytes) / (1024 * 1024) AS free_space, ...