c++课后习题解答6-10 下载本文

{ cout<<\ abort(); }

char ch; int n=0;

while(!f.eof()) { f.get(ch); n++; }

cout<<\该文件字符数为 \ f.close(); }

3. 编程给一个文件的所有行上加行号,并存到另一个文件中。 答:将del.cpp文件编写行号后存入del22.cpp文件中,程序如下: #include

#include #include void main() {

fstream inf,outf;

inf.open(\ if(!inf) { cout<<\ abort(); }

outf.open(\ if(!outf) { cout<<\ abort(); }

char s[80]; int n=1;

while(!inf.eof()) { inf.getline(s,sizeof(s)); outf<

inf.close(); outf.close(); }

10.6 使用C++语句实现下列各种要求

1. 设置标志使得十六进制数中字母按大写格式输出。 答: cout.setf(ios::hex,ios::basefield);

cout.setf(ios::uppercase);

2. 设置标志以科学记数法显示浮点数。

41

答: cout.setf(ios::scientific,ios::floatfield);

3. 按右对齐方式,域为5位,输出常整型数123,并使用'#'填充空位。 答: cout.setf(ios::right,ios::adjustfield); cout.width(5); cout.fill(‘#’); const int a=123; cout<

4. 按域宽为i,精度为j(i和j为int型数),输出显示浮点数d。 答: int j=10,j=5; cout.width(i); coutprecision(j); cout<

5. 使用前导0的格式显示输出域宽为10的浮点数1.2345。 答: cout.width(10); double d=1.2345; cout.fill(‘0’); cout<

42