第十三章课后习题
1、cerr标准错误流使用
#include< iostream>
#include< cmath>
using namespace std;
int main ( ) {
double a, b, c;
cout<< "please input three numbers:" << endl;
cin>> a>> b>> c;
if ( a> 0 && b> 0 && c> 0 ) {
if ( ( a+ b) > c&& ( a+ c) > b&& ( b+ c) > a) {
double s= ( a+ b+ c) / 2 ;
cout<< "the triangle's area:" << sqrt ( s* ( s- a) * ( s- b) * ( s- c) ) << endl;
}
else {
cerr<< "It is not a triangle!" << endl;
}
}
else {
cerr<< "It is not a triangle!" << endl;
}
return 0 ;
}
2、按照格式输出
#include< iostream>
using namespace std;
int main ( ) {
double a;
cout. setf ( ios: : fixed) ;
cout. precision ( 3 ) ;
cout. setf ( ios: : right) ;
while ( cin>> a) {
cout. width ( 10 ) ;
cout<< a<< endl;
}
return 0 ;
}
#include< iostream>
#include< iomanip>
using namespace std;
int main ( ) {
double a;
cout<< setiosflags ( ios: : fixed) << setprecision ( 3 ) << setiosflags ( ios: : right) ;
while ( cin>> a) {
cout<< setw ( 10 ) ;
cout<< a<< endl;
}
return 0 ;
}
3、按要求输出图形
#include< iostream>
using namespace std;
int main ( ) {
for ( int i= 0 ; i< 8 ; i++ ) {
cout. width ( 8 - i) ;
for ( int j= 0 ; j< i* 2 + 1 ; j++ ) {
cout<< 'B' ;
}
cout<< endl;
}
return 0 ;
}
4、磁盘文件输入输出
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int a[ 10 ] ;
ofstream outfile ( "f1.dat" , ios: : out) ;
if ( ! outfile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
cout<< "enter 10 integer numbers:" << endl;
for ( int i= 0 ; i< 10 ; i++ ) {
cin>> a[ i] ;
outfile<< a[ i] << " " ;
}
outfile. close ( ) ;
ofstream outfile2 ( "f2.dat" , ios: : out) ;
if ( ! outfile2) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
cout<< "enter 10 integer numbers:" << endl;
for ( int i= 0 ; i< 10 ; i++ ) {
cin>> a[ i] ;
outfile2<< a[ i] << " " ;
}
outfile2. close ( ) ;
return 0 ;
}
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int a[ 10 ] ;
ifstream infile ( "f1.dat" , ios: : in) ;
ofstream outfile ( "f2.dat" , ios: : app) ;
if ( ! infile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 10 ; i++ ) {
infile>> a[ i] ;
outfile<< a[ i] << " " ;
}
infile. close ( ) ;
outfile. close ( ) ;
return 0 ;
}
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int a[ 20 ] ;
ifstream infile ( "f2.dat" , ios: : in) ;
if ( ! infile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 20 ; i++ ) {
infile>> a[ i] ;
}
for ( int i= 0 ; i< 19 ; i++ ) {
for ( int j= 0 ; j< 19 - i; j++ ) {
if ( a[ j] > a[ j+ 1 ] ) {
int t= a[ j] ;
a[ j] = a[ j+ 1 ] ;
a[ j+ 1 ] = t;
}
}
}
infile. close ( ) ;
ofstream outfile ( "f2.dat" , ios: : out) ;
if ( ! outfile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 20 ; i++ ) {
outfile<< a[ i] << " " ;
}
outfile. close ( ) ;
return 0 ;
}
5、文件处理
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int num[ 5 ] ;
string name[ 5 ] ;
int age[ 5 ] ;
double wage[ 5 ] ;
ofstream outfile ( "f1.dat" , ios: : out) ;
if ( ! outfile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 5 ; i++ ) {
cin>> num[ i] >> name[ i] >> age[ i] >> wage[ i] ;
outfile<< num[ i] << " " << name[ i] << " " << age[ i] << " " << wage[ i] << endl;
}
outfile. close ( ) ;
return 0 ;
}
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int num[ 2 ] ;
string name[ 2 ] ;
int age[ 2 ] ;
double wage[ 2 ] ;
ofstream outfile ( "f1.dat" , ios: : app) ;
if ( ! outfile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 2 ; i++ ) {
cin>> num[ i] >> name[ i] >> age[ i] >> wage[ i] ;
outfile<< num[ i] << " " << name[ i] << " " << age[ i] << " " << wage[ i] << endl;
}
outfile. close ( ) ;
return 0 ;
}
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int num[ 7 ] ;
string name[ 7 ] ;
int age[ 7 ] ;
double wage[ 7 ] ;
ifstream infile ( "f1.dat" , ios: : in) ;
if ( ! infile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 7 ; i++ ) {
infile>> num[ i] >> name[ i] >> age[ i] >> wage[ i] ;
cout<< num[ i] << " " << name[ i] << " " << age[ i] << " " << wage[ i] << endl;
}
infile. close ( ) ;
return 0 ;
}
#include< iostream>
#include< fstream>
using namespace std;
int main ( ) {
int num[ 7 ] ;
string name[ 7 ] ;
int age[ 7 ] ;
double wage[ 7 ] ;
ifstream infile ( "f1.dat" , ios: : in) ;
if ( ! infile) {
cerr<< "open error!" << endl;
exit ( 1 ) ;
}
for ( int i= 0 ; i< 7 ; i++ ) {
infile>> num[ i] >> name[ i] >> age[ i] >> wage[ i] ;
cout<< num[ i] << " " << name[ i] << " " << age[ i] << " " << wage[ i] << endl;
}
infile. close ( ) ;
cout<< endl;
int n;
int flag= 0 ;
while ( cin>> n) {
flag= 0 ;
if ( n== 0 ) break ;
for ( int i= 0 ; i< 7 ; i++ ) {
if ( n== num[ i] ) {
flag= 1 ;
cout<< "No." << i+ 1 << " " ;
cout<< num[ i] << " " << name[ i] << " " << age[ i] << " " << wage[ i] << endl;
break ;
}
}
if ( flag== 0 ) cout<< "Can't find the num!" << endl;
}
return 0 ;
}
6、修改例13.17