C++程序设计原理与实践习题答案第⼗⼀章第11章习题答案第⼗⼀章习题答案
11.1初一不能洗头吗
#include"../../std_lib_facilities.h"
int main()
try
关于冬至的手抄报{
string ifname;
cout <<"Enter a input file name\n";
cin >> ifname;
ifstream ifs{ ifname };
if(!ifs)
error("can't open file ", ifname);
string ofname;//新的⽂件名
cout <<"Enter a output file name\n";
cin >> ofname;
ofstream ofs{ ofname };
if(!ofs)
error("can't open file ", ofname);
//把输⼊⽂件中的字母转换为⼩写字母,输出到新⽂件中
for(char ch{0}; (ch);)
{
ch =tolower(ch);
ofs << ch;
}
return0;
}
catch(runtime_error& e)
{
cerr <<"Runtime error: "<< e.what()<<'\n';
股票网址return1;
}
catch(...)
{
cerr <<"Exception occured!\n";
return2;
}
11.2
int main()
try
{
string ifname;
cout <<"Enter a input file name\n";
cin >> ifname;
ifstream ifs{ ifname };
if(!ifs)
error("can't open file ", ifname);
string word;
cout <<"Enter a word\n";
cin >> word;
string line;
int line_num{1};
while(getline(ifs, line))
{
/* string的成员函数 find() 返回 string::size_type 类型的值。
以下标形式标记查找匹配所发⽣的位置;
或者返回⼀个名为 string::npos 的特殊值(它说明查找没有匹配的)。 string 类将 npos 定义为保证⼤于任何有效下标的值。
*/
if(line.find(word)!= string::npos)
cout << line_num <<' '<< line <<'\n';
line_num++;
}
return0;进德修业
}
catch(runtime_error& e)
{
cerr <<"Runtime error: "<< e.what()<<'\n';
return1;
}
catch(...)
{
cerr <<"Exception occured!\n";
return2;
}
11.3
bool is_vowel(char ch)
{
const string vowels{"AaEeIiOoUu"};
for(char v : vowels)
if(ch == v)
return true;
return fal;
}
int main()
try
{
string ifname;
cout <<"Enter a input file name\n";
cin >> ifname;
ifstream ifs{ ifname };
if(!ifs)
error("can't open file ", ifname);
string ofname;
cout <<"Enter a output file name\n";
cin >> ofname;
ofstream ofs{ ofname };
if(!ofs)
error("can't open file ", ofname);
for(char ch{0}; (ch);)
if(!is_vowel(ch))
ofs << ch;
return0;
}
catch(runtime_error& e)
{
cerr <<"Runtime error: "<< e.what()<<'\n'; return1;
}
catch(...)
{
cerr <<"Exception occured!\n";
return2;
}
11.4
#include"../../std_lib_facilities.h"
enum class Ba {
oct, dec, hex
};
const vector<string>bs{ "octal","decimal","hexadecimal"};
struct Integer {
int n;
Ba b;
};
void write(const Integer& i)
{
{
cout << showba;
switch(i.b)
{
ca Ba::oct:
cout << oct <<tw(10)<< i.n <<"\toctal\t\tconverts to\t";
break;
ca Ba::dec:
cout << dec <<tw(10)<< i.n <<"\tdecimal\t\tconverts to\t";
天真妈妈
break;
ca Ba::hex:
cout << hex <<tw(10)<< i.n <<"\thexadecimal\tconverts to\t"; break;
延安除奸
}
cout << dec <<tw(8)<< i.n <<"\tdecimal\n";
}
int main()
try
学校环境
{
cout <<"How many integer do you want to enter?\n";
int n;
理化生
while(cin >> n)
if(n <=0)
cout <<"Try again! How many integer do you want to enter?\n";
el
break;
cout <<"Now enter"<< n <<" integer, you can type in octal with 0 prefix, \ decimal with no prefix, hexadecimal with 0x or 0X prefix\n";
vector<Integer>vi;
char ch;
int i;
Ba b;
while(n--)
{
(ch)&&!isdigit(ch))
continue;
if(ch =='0')
{
char ch2;
(ch2)&&(ch2 =='x'|| ch2 =='X'))//hex
{
cin >> hex >> i;
b = Ba::hex;
}
el if(isdigit(ch2))//oct
{
cin.putback(ch2);
cin >> oct >> i;
b = Ba::oct;
}
el
{
// integer 0
cin.putback(ch2);
i =0;
b = Ba::dec;
}
}
el
{
cin.unget();
cin >> dec >> i;
b = Ba::dec;
b = Ba::dec;
}
vi.push_back(Integer{ i,b });
}
for(const Integer& i : vi)
write(i);
return0;
}
catch(runtime_error& e)
{
cerr <<"Runtime error: "<< e.what()<<'\n'; return1;
}
catch(...)
{
cerr <<"Exception occured!\n";
return2;
}
11.5