程序清单6.2

#include<iostream>
using namespace std; void main() {
char ch;
cout << "Type, and I shall repeat.\n";
cin.get(ch);
while(ch != '.')
{
if (ch == '\n')
cout << ch;
else
cout << ++ch;
cin.get(ch);
}
system("pause");
}

程序清单6.5

#include<iostream>
using namespace std; const int Size = ;
void main() {
float naaq[Size];
int i = ;
float temp;
cout << "First value:";
cin >> temp;
while (i < Size&&temp >= ) {
naaq[i] = temp;
++i;
if (i<Size)
{
cout << "Next value:";
cin >> temp;
}
}
if ( == i)
cout << "No data!" << endl;
else {
cout << "Enter your NAAQ:";
float you;
cin >> you;
int count = ;
for (int j = ; j < i; j++)
{
if (naaq[j] > you)
++count;
}
cout << count << "个数字比你的大" << endl;
}
system("pause");
}

程序清单6.8(字符函数库cctype)

#include<iostream>
#include<cctype>
using namespace std; void main() {
cout << "Enter text for analysis,and type @ to terminate input."<<endl;
char ch;
int space = , digit = , chars = , punct = , others = ; cin.get(ch);
while (ch!='@')
{
if (isalpha(ch)) chars++;
else if (isspace(ch)) space++;
else if (isdigit(ch)) digit++;
else if (ispunct(ch)) punct++;
else others++;
cin.get(ch);
}
cout << chars << " letters,"
<< space << " whitespace,"
<< digit << " digit,"
<< punct << " punctuations,"
<< others << " others." << endl;
system("pause");
}

程序清单6.13

#include<iostream>
using namespace std;
const int Max = ;
void main() {
double fish[Max];
cout << "Enter the weights of your fish.\nYou may enter up to " << Max << " fish<q to terminate>." << endl;
cout << "fish #1: ";
int i = ;
while (i<Max&&cin >> fish[i])
{
if (++i < Max)
cout << "fish #" << i + << ": ";//i+1和++i不同,i+1对i的值没有影响
}
double total = 0.0;
for (int j = ; j < i; j++)//i=5
total += fish[j];
if (i == )
cout << "No fish!" << endl;
else
cout << total / i << "=average weight of " << i << " fish" << endl;
system("pause");
}

根据自己的习惯重新编写

#include<iostream>
using namespace std;
const int Max = ;
void main() {
double fish[Max],sum=;
cout << "Enter the weights of your fish.\nYou may enter up to " << Max << " fish<q to terminate>." << endl;
int i;
for (i = ; i < Max; i++)
{
cout << "fish #" << i + << ": ";
if (cin >> fish[i])//输入成功返回true
sum += fish[i];
else
break;
}
if (i == )
cout << "No fish!" << endl;
else
cout << sum/i<< "=average weight of " << i << " fish" << endl;
system("pause");
}

程序清单6.14

#include<iostream>
using namespace std;
const int Max = ;
void main() {
double golf[Max],sum=;
cout << "Enter your golf scores.\nYou must enter " << Max << " rounds." << endl;
int i;
for (i = ; i < Max; i++)
{
cout << "round #" << i + << ": ";
while (!(cin>>golf[i]))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Please enter a number:";
}
sum += golf[i];
}
cout << sum/Max<< "=average score " <<Max<< " rounds" << endl;
system("pause");
}

程序清单6.15(文本I/O)

//文件输出(对程序而言)

#include<iostream>
#include<fstream>
using namespace std; void main() {
char automobile[];
int year;
double a_price, d_price; //声明ofstream对象并将其同文件关联起来
ofstream outFile;
outFile.open("first.txt"); cout << "Enter the make and model of automobile:";
cin.getline(automobile, );//cin.getline:不断读取,直到遇到换行符(少于50个字符),在末尾加上一个空字符,换行符被丢弃
cout << "Enter the model year:";
cin >> year;
cout << "Enter the original asking price:";
cin >> a_price;
d_price = 0.913*a_price; cout << fixed;//表示用一般的方式输出浮点数,比如num=0.00001,cout输出为1e-005,加了fixed后再输出就为0.000010
cout.precision();//第一位精确,第二位四舍五入,比如num = 318.15,precision(2)为3.2e+02,precision(4)为318.2
cout.setf(ios_base::showpoint);//强制显示小数点
cout << "Make and model: " << automobile << endl;
cout << "Year: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << d_price << endl; outFile << fixed;
outFile.precision();
outFile.setf(ios_base::showpoint);
outFile << "Make and model: " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl; outFile.close();
system("pause");
}

程序清单6.16

//文件读入(对程序而言)

#include<iostream>
#include<fstream>//文件I/O
#include<cstdlib>//exit()
using namespace std;
const int SIZE = ; void main()
{
char filename[SIZE];
ifstream inFile;//声明ifstream对象
cout << "Enter name of data file:";
cin.getline(filename, SIZE);
inFile.open(filename);//关联文件 if (!inFile.is_open())//文件打开失败
{
cout << "Could not open the file " << filename << endl;
exit(EXIT_FAILURE);
}
double value, sum = 0.0;
int count = ; inFile >> value;
while (inFile.good())//输入正确
{
++count;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout << "End of file reached." << endl;
else if (inFile.fail())
cout << "Input terminated by data misamatch." << endl;
else
cout << "Input terminated for unknown reason." << endl;
if (count == )
cout << "No data processed." << endl;
else {
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close(); system("pause");
}

要想正确运行,首先在源代码文件夹中创建一个包含double数字的文本文件。

为何会少了最后一个数字17.5呢?

在文本文件中,输入最后的文本17.5后应该按下回车键,然后再保存文件。

[C++ Primer Plus] 第6章、分支语句和逻辑运算符(一)程序清单的更多相关文章

  1. C++ primer plus读书笔记——第6章 分支语句和逻辑运算符

    第6章 分支语句和逻辑运算符 1. 逻辑运算符的优先级比关系运算符的优先级低. 2. &&的优先级高于||. 3. cctype中的函数P179. 4. switch(integer- ...

  2. C++ Primer Plus 6th 读书笔记 - 第6章 分支语句和逻辑运算符

    1. cin读取错误时对换行符的处理 #include <iostream> using namespace std; int main() { double d; char c; cin ...

  3. [C++ Primer Plus] 第7章、函数(一)程序清单——递归,指针和const,指针数组和数组指针,函数和二维数组

    程序清单7.6 #include<iostream> using namespace std; ; int sum_arr(int arr[], int n);//函数声明 void ma ...

  4. c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  5. [C++ Primer Plus] 第6章、分支语句和逻辑运算符(二)课后习题

    一.复习题 3. #include<iostream> using namespace std; void main() { char ch; int c1, c2; c1 = c2 = ...

  6. C++ Primer Plus读书笔记(六)分支语句和逻辑运算符

    1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...

  7. 重拾c++第三天(6):分支语句与逻辑运算符

    1.逻辑运算符 && || ! 2.关系运算符优先级高于逻辑运算符 3.cctype库中好用的判断 4. ?:符号用法: 状态1?结果1:结果2 5.switch用法: switch ...

  8. 《C++ Primer Plus》读书笔记之四—分支语句和逻辑操作符

    第六章 分支语句和逻辑操作符 1.&&的优先级低于关系操作符. 2.取值范围:取值范围的每一部分都使用AND操作符将两个完整的关系表达式组合起来: if(age>17&& ...

  9. JavaSE教程-03Java中分支语句与四种进制转换

    一.分支语句 计算机源于生活,程序模拟现实生活,从而服务生活 行为模式 1,起床,刷牙,洗脸,吃早餐,上课,回家,睡觉(顺序性) 2,如果时间不太够,打个滴滴快车,如果时间够,坐个地铁(选择性) 3, ...

随机推荐

  1. 【CF660E】Different Subsets For All Tuples 结论题

    [CF660E]Different Subsets For All Tuples 题意:对于所有长度为n,每个数为1,2...m的序列,求出每个序列的本质不同的子序列的数目之和.(多个原序列可以有相同 ...

  2. ubuntu的apt-get install的默认安装路径(转)

    一.apt-get 安装 deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb或者直接双击此文件 dpkg 是Debian ...

  3. ArcGIS AddIN开发之 设置当前工具为Edit Tool

    在GIS数据处理中,经常需要选择要素,再进行操作.所以,为了处理的方便,可以将当前工具处理结束后,将当前工具设置为Edit Tool,以方便下一次的选择处理. 相关资料: 1.ArcMap Name ...

  4. ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空

    原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...

  5. python语法_input

    input:与用户的交互,返回用户输入的值 注意:input接受的所有数据都为字符串,即便输入的为数字,依然会被当成字符串

  6. java-03-动手动脑

    1. 问题:这两种方式定义的变量是一样的吗? 早期我们经常这样定义变量  int value=100;前面的示例中这样定义变量  MyClass obj = new MyClass(); 回答:一般情 ...

  7. Laravel开发采坑系列问题

    2017年12月22日17:40:03 不定时更新 版本5.4.X 一下是可能会遇到的坑 1,必须的写路由转发才能访问控制器,当然你可以自动路由访问,但是需要些匹配规则,其实还是转发了 好多人讨论过自 ...

  8. Codeforces 584E - Anton and Ira - [贪心]

    题目链接:https://codeforces.com/contest/584/problem/E 题意: 给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 ...

  9. javascript匿名函数 闭包

    匿名函数 (function(){                console.info("111111111");            })(); var my = (fun ...

  10. Oracle实用操作

    查询用户下所有表:select * from tab; 删除表: drop table 表名; 但是删除表后还是会查询到BIN开头的垃圾表,drop后的表存在于回收站: 清空回收站所有表:  purg ...