您的位置:首页 > 编程语言 > C语言/C++

C++ primer plus(第六版)学习笔记、习题答案(5)

2014-12-14 23:04 761 查看

第六章:分支语句和逻辑运算符

近来考试,耽误了进度

。。

第一部分:学习笔记

1.分析代码:

for (i = 0; i < Max; i++)
{
cout << "round #" << i+1 << ": ";
while (!(cin >> golf[i])) {
cin.clear();     // reset input
while (cin.get() != '\n')
continue;    // get rid of bad input
cout << "Please enter a number: ";
}
}

golf[]数组为int类型,如果输入的不为数字,或者由于各种原因输入失败,while (!(cin >> golf[i])) 将执行,首先clean()方法重置输入,如果省略这条语句,程序将拒绝继续读取输入。接下来,程序在while中循环使用cin.get()来读取行尾之前的所有输入(那些不是数字的字符),从而删除这一行中的错误输出。



2.cin是如何处理输入的

同时处理输入行 38.5 19.2

char ch;
cin >> ch;

输入的第一个字符3被赋给ch, 这里存储的数值3是字符3的编码,执行上述输入语句后,输入队列中的下一个字符为字符8

int n;
cin >> n;

在这种情况下,cin将不断读取,直到遇到非数字字符,将38的二进制编码复制到n中。

double x;
cin >> x;

cin将不断读取,直到遇到第一个不属于浮点数的字符。

char word[50];
cin >> word;

在这种情况下,cin将不断读取,直到遇到空白字符,也就是说读取3、8句点和5,使得空格称为输入队列的下一个字符。然后,cin将这4个字符的字符编码存储到数组word中,并在末尾加上一个空字符。
char word[50];
cin.geline(word, 50);
cin将不断读取,直到遇到换行符,所有的字符都被存储到word中,并在末尾加上一个空字符。换行符被丢弃,输入队列的下一个字符是下一行中的第一个字符。

3.setprecision、fixed、showpoint用法

cout << fixed;//
cout.precision(2);//以上面的搭配使用,精度为小数点后两位
cout.setf(ios_base::showpoint);//

贴一个网上的总结:
setprecision、fixed、showpoint的用法总结:
首先要加头文件:iomanip
一:setprecision

作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入。

比如:double s=20.7843000,

cout<<setprecision(1)<<s<<endl;会输出2e+001,因为要输出一个数字,所以只有2.

cout<<setprecision(2)<<s<<endl;会输出21。

cout<<setprecision(3)<<s<<endl;会输出20.8。

cout<<setprecision(6)<<s<<endl;会输出20.7843。

cout<<setprecision(7)<<s<<endl;会输出20.7843。

cout<<setprecision(8)<<s<<endl;会输出20.7843。


可见,小数部分末尾为0时,是输不出来的!

要想输出来,就得用showpoint了。

特别提示

(如果再在这些语句后面加个两个语句:

cout<<1<<endl;

cout<<1.00800<<endl;


猜到会输出什么吗?

第一条输出:1。不是浮点型。

第二条为:1.008。承接setprecision(8)的这条规则语句。

注:

如果直接有语句

int main()

{

cout<<1<<endl;

cout<<1.00<<endl;

}


第一条输出:1。

第二条也为:1。按整型输出



二:setprecision与showpoint

语法:在输出语句前声明:cout.setf(ios::showpoint);就行了!

还比如:double s=20.7843000,

cout.setf(ios::showpoint);

cout<<setprecision(1)<<s<<endl;就会输出2.e+001,注意,2和e之间多了一个“.”。

cout<<setprecision(2)<<s<<endl;会输出21.。多个点!

cout<<setprecision(3)<<s<<endl;会输出20.8。

cout<<setprecision(6)<<s<<endl;会输出20.7843。

cout<<setprecision(7)<<s<<endl;会输出20.78430。

cout<<setprecision(8)<<s<<endl;会输出20.784300。


可见,就会输出想要的数据数目!

特别提示

(如果再在这些语句后面加个两个语句:

cout<<1<<endl;

cout<<1.0080<<endl;


猜到会输出什么吗?

第一条输出:1。不是浮点型。

第二条也为:1.0080000。承接setprecision(8)的这条规则语句。

三:setprecision与fixed

如果想要保留几位小数,那setprecision就得与fixed合作了!!

语法:在输出语句前声明:cout.setf(ios::fixed);

比如:
double s=20.7843909

cout.setf(ios::fixed);

cout<<setprecision(1)<<s<<endl;就会输出2.8  。

cout<<setprecision(2)<<s<<endl;会输出21.78。多个点!

cout<<setprecision(3)<<s<<endl;会输出20.784。

cout<<setprecision(6)<<s<<endl;会输出20.784391。

cout<<setprecision(7)<<s<<endl;会输出20.7843909。

cout<<setprecision(8)<<s<<endl;会输出20.78439090。


特别提示

(如果也再在这些语句后面加个两个语句:

cout<<1<<endl;

cout<<1.008<<endl;


猜到会输出什么吗?

第一条输出:1。

第二条为:1.00800000。

就是承接了setprecision(8)的这条规则语句,是浮点型的都会保留8个小数。是整型的还是整型!)

语句也可以写成:cout<<fixed<<setprecision(2)<<s<<endl;

就算后面的语句没有写<<fixed,同样会按有<<fixed处理。

比如有语句:

cout<<fixed<<setprecision(2)<<s<<endl;

A:cout<<setprecision(7)<<s<<endl;

B:cout<<setprecision(8)<<s<<endl;


AB语句均会按保留7个,8个小数处理,不会再按有7或8个浮点数处理。

如果下面有语句c:

cout<<1.008<<endl;也会保留8个小数。

四:setprecision、showpoint与fixed

{cout<<fixed<<setprecision(2)<<123.456<<endl;//输出的结果是123.46
cout<<showpoint<<12345.0006666<<endl;//输出12345.0
cout<<fixed<<setprecision(2)<<123.456<<endl;}


比如:double s=20.7843909

1.有语句

cout<<setprecision(2)<<s<<endl;//输出21

cout<<fixed<<s<<endl;//输出20.78

2.有语句:

cout<<setprecision(2)<<s<<endl;//输出21

cout<<showpoint<<s<<endl;//输出21.(有个点)

3.有语句:

cout<<fixed<<s<<endl;//输出20.78391
cout<<showpoint<<s<<endl;//输出20.78391
4.有语句:

cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
cout<<showpoint<<s<<endl;//输出20.78

5.有语句:

cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//21.(有个点)
cout<<fixed<<s<<endl;//20.78

reference: http://www.tuicool.com/articles/zie6Fz7

4.简单文件输入输出

①写入到文本文件中
ofstream outFile;
ofstream fout;

outFile.open("fish.txt");
char filename[50];
cin >> filename;
fout.open(filename);

头文件:fstream
声明对象后 outFile 与fout 使用open方法将其与文件名“fish.txt"和自己输入的文件 名filename关联起来。如果文件名不存在,则创建,存在的话会将其原有的内容截短至0,然后加入行的内容(默认情况下)。
② 读取文本文件
ifstream inFile;
ifstream fin;

inFile.open("boeling.txt");
char filename[50];
cin >> filename;
fin.open(filename);

方法:inFile.eof()检验是否到达EOF,inFile.fail()可以检查EOF和类型不匹配

第二部分:课后习题

6.1
//2014/12/15

#include<iostream>
#include<cctype>

int main()
{
using namespace std;
char ch;
ch = cin.get();
while(ch != '@')
{
if (isalpha(ch))
{
if (islower(ch))
{
ch = toupper(ch);
}
else
{
ch = tolower(ch);
}
cout << ch;
}
ch = cin.get();
}
cin.get();
cin.get();
return 0;
}

6.2
//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>

using namespace std;
//const int n = 10;
int main()
{
ifstream inFile;
char filename[20];
cout << "Enter name of data file: ";
cin.getline(filename,20);
inFile.open(filename);

if (!inFile.is_open())
{
cout << "could not open the file" << filename << endl;
cout << "program terminating.\n";
exit(EXIT_FAILURE);
}

double value;
double sum = 0.0;
int count = 0;

inFile >> value;
while (inFile.good())
{
++count;
sum += value;
cout <<  "your file the " << count << " number is " << "    " << value << endl;
inFile >> value;

}

if (inFile.eof())
{
cout << "end of file reached.\n";
}
else if (inFile.fail())
{
cout << "Input terminated by data mismatch.\n";
}
else
cout << "input terminated for unknown reason.\n";

if (count == 0)
{
cout << "No data processing.\n";
}
else
{
cout << "Items read :" << count << endl;
cout << "sum:" << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
cin.get();
cin.get();
return 0;
}

note: 文件scores.txt的内容为:

18 19 18.5 13.5 14

16 19.5 20 18 12
注意:最后一个数字后面还要有一个空格,否则程序将不会读到最好一个数字。

6.3
//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>

using namespace std;
//const int n = 10;
int main()
{
char ch;
cout << "Please enter one of the following choices:\nc) carnivore\tp) pianist\t\n";
cout << "t) tree\tg) game\n";

cin >> ch;
while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
{
cout << "please enter a c,p,t,or g:";
cin >> ch;
}

switch (ch)
{
case 'c':cout << "this is carnivore!";break;
case 'p':cout << "this is pianist!";break;
case 't':cout << "this is tree";break;
case 'g':cout << "this is game";break;

default:;
}
cin.get();
cin.get();
return 0;
}


[align=left]6.4[/align]
//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>

using namespace std;
const int strsize = 10;

struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};

void display(char,bop[]);
void preference(bop[]);
int main()
{

bop p[5] = {{"a1","b1","c1",0},{"a2","b2","c2",1},{"a3","b3","c3",2},{"a4","b4","c4",0},{"a5","b5","c5",1}};

cout << "Benevolent order of programmers report\n";
cout << "a. display by name \tb. display by tittle\n";
cout << "c. display by bopname \td.display by preference\n";
cout << "q. quit";

bool temp = true;
char ch;
int i;

cout << "Enter your choice: ";
//cin >> ch;
while (temp)
{
cin >> ch;
switch (ch)
{
case 'a':
display('a',p);cout << "Next choice: ";break;
case 'b':
display('b',p);cout << "Next choice: ";break;
case'c':
display('c',p);cout << "Next choice: ";break;
case 'd':
preference(p);cout << "Next choice: ";break;

case 'q':temp = false;cout << "bye!";
break;
}
}

cin.get();
cin.get();
return 0;
}

void display(char ch,bop p[])
{
int j;
if (ch == 'a')
{
for(j = 0 ; j < 5; j++)
cout << p[j].fullname << endl;
}
else if (ch == 'b')
{
for (j = 0; j < 5; j++)
cout << p[j].title << endl;
}
else
for (j = 0; j < 5; j++)
cout << p[j].bopname << endl;

}

void preference(bop p[])
{
int j;
for (j = 0; j < 5; j++)
{
switch (p[j].preference)
{
case 0:
cout << p[j].fullname << endl;break;
case 1:
cout << p[j].title << endl;break;
case 2:
cout << p[j].bopname << endl;break;
}
}
}

[align=left]这个程序花了我好长时间,解释一下:题目意思是如果选择d的话表示按照各个人的喜好排序,0=fullname,1=title,2=bopname;[/align]

[align=left]6.5[/align]
//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>

using namespace std;
double income(double);

int main()
{
double tv;
double js;
cout << "please input your income: ";
bool temp;
//cin >> tv;

while (temp = (cin >> tv) && tv >= 0.0)
{
js = income(tv);
cout << "your revenue is : " << js << endl;
cout << "Enter again: ";
//cin >> tv;
}
if (!temp || tv < 0)
{
cout << "you enter a negative or a illegal number!\n";
}
cin.get();
cin.get();
return 0;
}

double income(double tv)
{
if (tv <= 5000)
{
return 0.0;
}
else if (tv > 5000 && tv <= 15000)
{
return (tv - 5000) * 0.1;
}
else if (tv > 15000 && tv <= 35000)
{
return (1000 * 0.1 + (tv - 15000) * 0.15);
}
else
return (10000 * 0.1 + 20000 * 0.15 + (tv - 35000) * 0.2);
}


6.6
//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>
#include<string>

using namespace std;
double income(double);

int main()
{
struct denote
{
string name;
double num;
};
int n;
int count1 = 0;
int count2 = 0;
cout << "please input number:";
cin >> n;
denote* de = new denote
;

for (int i = 0; i < n; i++)
{
cout << "enter the " << i + 1 << " information:\n";
cout << "enter his name: ";
cin >> de[i].name;
cout << "enter his denote: ";
cin >> de[i].num;
}

cout << "Grand Patrons:" << endl;

for (int i = 0; i < n; i++)
{
if (de[i].num > 1000)
{
count1++;
cout << de[i].name << '\t' << de[i].num << endl;
}
if (count1 == 0)
{
cout << "none";
}

}

cout << "Patrons:" << endl;
for (int i = 0; i < n; i++)
{
if (de[i].num <= 1000)
{
count2++;
cout << de[i].name << '\t' << de[i].num << endl;
}
if (count2 == 0)
{
cout << "none";
}

}

delete[] de;
cin.get();
cin.get();
cin.get();
return 0;
}


6.7

//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>
#include<string>

using namespace std;
const int n = 10;

int main()
{
char word
;
char temp;
int other = 0;
int vowel = 0;
int consonant = 0;

cout << "Enter words (q to quit):\n";

do
{
cin >> word;
temp = word[0];
if (!isalpha(temp))
{
other++;
}
else if (temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u')
{
vowel++;
}
else
consonant++;
if (temp == 'q' && strlen(word) == 1)
{
consonant--;
break;
}
} while (true);

cout << vowel <<  " words beginning with vowels\n";
cout << consonant <<  " words beginning with consonants\n";
cout << other <<  " others";

cin.sync();
cin.get();

return 0;
}


6.8
//2014/12/15

#include<iostream>
#include<cctype>
#include<fstream>
#include<array>
#include<string>

using namespace std;
const int n = 10;

int main()
{
char ch;
int count = 0;
ifstream infile;
infile.open("scores.txt");

infile >> ch;
while (infile.good())
{
count++;
infile >> ch;
}

cout << "the file have" << count << " character! ";
cin.sync();
cin.get();

return 0;
}


6.9
明天来写。。。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: