Chapter 7 C ++ exercises

Source: Internet
Author: User

7.1 why does C ++ have its own input/output system?

The C ++ compilation system strictly checks the data types. Any data with incorrect types cannot be compiled.

C ++ needs to define a large number of user-defined types and be able to input and output.

 

Which of the four predefined stream objects does 7.2 C ++ have? What specific devices are they associated?

Standard input stream object Cin, standard output stream object cout, non-buffer standard error Stream object cerr, and buffer standard error Stream object clog.

Standard Input Device for CIN

Cout corresponds to the standard output device

Cerr corresponds to a standard error output device

Clog corresponds to a standard error output device

 

7.3. What is the difference between cerr and clog?

Cerr directly displays error messages without passing through the buffer. While clog is stored in the buffer zone, and the buffer zone is full or is output when the Endl is met.

 

7.4 which two methods does C ++ provide to control the input and output formats?

One is to use stream member functions related to format control in the IOS class for format control, and the other is to use special types of function control called operators.

 

7.5 what is the basic process of C ++ for file input and output?

First, create a stream object, and associate the stream object with the file. That is, open the file before you can perform read and write operations. After the read and write operations are completed, close the file.

 

7.6-7.8 BCA

 

7.9

# Include <iostream> # Include <Iomanip> Using   Namespace  STD;  Int Factorial ( Int  N ){ If (N = 0 | N = 1  )  Return  N;  Return Factorial (n- 1 )* N ;}  Int  Main (){  For ( Int I = 1 ; I <= 9 ; I ++) {Cout <SETW ( 5 ) < Factorial (I );  If (I % 3 = 0  ) {Cout < Endl ;}}  Return   0  ;} 

 

7.10

# Include <iostream> # Include <Iomanip>Using   Namespace  STD;  Int  Main (){  For ( Int I = 1 ; I <= 7 ; I ++ ) {Cout <SETW ( 16 - I );  For ( Int J =1 ; J <= ( 2 * I- 1 ); J ++ ) {Cout < '  A  '  ;} Cout < Endl ;}  Return   0  ;} 

 

7.11

# Include <iostream> # Include <Iomanip>Using   Namespace  STD;  Class  Matrix {  Private  :  Int Data [ 2 ] [ 3  ];  Public  : Matrix () {} friend ostream & Operator <(Ostream &, Matrix & ); Friend istream & Operator > (Istream &, Matrix & ); Friend Matrix  Operator + (Matrix &, Matrix & ) ;}; Ostream & Operator <(Ostream & OS, Matrix & A ){  For ( Int I = 0 ; I < 2 ; I ++ ){  For (Int J = 0 ; J < 3 ; J ++ ) {OS <A. Data [I] [J] < "   "  ;} OS < Endl ;}  Return  OS;} istream & Operator >>( Istream & In , Matrix & A) {cout < "  Enter a two-row and three-column matrix:  " < Endl;  For ( Int I = 0 ; I < 2 ; I ++ ){  For ( Int J = 0 ; J < 3 ; J ++ ){ In > A. Data [I] [J] ;}}  Return   In  ;} Matrix  Operator + (Matrix & A, Matrix & B) {matrix temp;  For ( Int I = 0 ; I < 2 ; I ++ ){  For ( Int J = 0 ; J < 3 ; J ++ ) {Temp. Data [I] [J] = A. Data [I] [J] + B. Data [I] [J] ;}}  Return  Temp ;}  Int  Main () {matrix M1, M2, total; CIN > M1> M2; Total = M1 + M2; cout < Total;  Return  0  ;} 

 

7.12

# Include <iostream> # Include <Fstream> Using   Namespace  STD;  Int  Main () {fstream fout (  "  Stock.txt  " , IOS :: Out  );  If (!Fout) {cout < "  An error occurred while opening the file!  " < Endl;  Return   1  ;} Fout < "  Shen Fa Zhan 000001 \ n  "  ; Fout < "  Shang Hai Qi che 600104 \ n  " ; Fout < "  Guang Ju Neng yuan 000096  "  ; Fout. Close ();  Return   0  ;} 

 

7.13

# Include <iostream> # Include <Fstream> Using   Namespace  STD;  Int  Main (){ Char STR [ 30  ]; Fstream  In ( "  File1.txt  " , IOS :: In  );  If (! In  ) {Cout < "  An error occurred while opening file1.txt! \ N  " ; Abort ();}  In >>Str >>> STR;  For ( Int I = 0 ; I < 30 ; I ++ ){  If (STR [I]! = 0 & (STR [I] < '  A  '  ) {STR [I] + = ( '  A  ' - '  A  '  ) ;}} Fstream  Out ( "  File2.txt  " , IOS :: Out  );  If (! Out  ) {Cout < "  An error occurred while opening file2.txt! \ N  "  ; Abort ();}  Out > STR;  In  . Close ();  Out  . Close ();  Return   0  ;} 

 

7.14

# Include <iostream> # Include <Fstream>Using   Namespace  STD;  Int  Main (){  Char STR [ 30  ]; Fstream  In ( "  File1.txt  " , IOS :: In  );  If (! In ) {Cout < "  An error occurred while opening file1.txt! \ N  "  ; Abort ();}  In >>Str >>> STR;  For ( Int I = 0 ; I < 30 ; I ++ ){  If (STR [I]! = 0 & (STR [I] < '  A  '  ) {STR [I] + = ( '  A  ' - '  A  '  ) ;}} Fstream  Out ( "  File2.txt  " , IOS: APP );  If (! Out  ) {Cout < "  An error occurred while opening file2.txt! \ N  "  ; Abort ();}  Out > STR;  In  . Close ();  Out  . Close ();  Return  0  ;} 

 

7.15 won't.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.