C++ 基础练习 - Chapter 10 (英文版)

Review Questions:

10.1 What is a stream?

Answer:

The I/O stream in C++ is designed to work with a wide variety of devices including terminals, disks and tape drives. Although each devices is very different, the I/O system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as stream.

10.2 How do the I/O facilities in C++ differ from that in C?

Answer:

We can design our own manipulators for certain special purposes in C++ but not in C.

10.3 Why are the words such as cin and cout not considered as keywords?

Answer:

cin and cout are related to iostream object, that’s why we must include iostream header file in our program to use cin and cout . Hence cin and cout are considered as iostream objects, not keywords.

10.4 How is cout about to display various types of data without any special instructions?

Answer:

The biggest single advantage to the stream methods is : they are type-safe. If you change a variable’s type, the sub sequent sstream operations using that variable will either automatically accommodate the change, or will indicate an incompatibility at compile time. thus cout is able to display various types of data without any special instructions.

10.5 Why is it necessary to include the file iostream in all our program?

Answer:

Because any program has input or output statements and without iostream header file we cannot use inout or output statements, that’s why it’s necessary to include the file iostream in all our programs.

10.6 Discuss the various forms of get() function supported by the input stream. How are they used?

Answer:

There are two types of get() functions. We can use both get(char)* and get(void) prototype to fetch a character including the blank space.

10.7 How do the following two statements differ in operation?

cin >> c;
cin.get(ch);

Answer:

cin>>c; will read a character, but it will skip the white space and new line character.

cin.get (ch); will read a character including white space and newline character.

10.8 Both cin and getline() function can be used for reading a string. Comment.

Answer:

cin reads a string and stop when a space or newline is encountered.

getline(); function reads a whole line of text that ends with a newline character.

10.9 Discuss the implications of size parameter in the following statement:

cout.write(line, size);

Answer:

The first argument “line” represents the name of the string to be displayed and the second argument “size” indicates the number of characters to display.

10.10 What does the following statement do?

cout.write(s1, m).write(s2, n);

Answer:

It is equivalent to the following two statements:

cout.write(s1, m);
cout.write(s2, n);

so it will print two strings s1 and s2.

10.11 What role does the iomanip file play?

Answer:

It provides a set of functions called manipulators which can be used to manipulate the output formats.

10.12 What is the role of fill() function? When do we use this function?

Answer:

It’s known as filling and padding function. The unused posotions of the field are filled with white spaces, by default. We can use the fill() function to fill the unused positions by any desired character.

General form: cout.fill(ch);

Example: cout.fill('*');

10.13 Discuss the syntax of setf() function.

Answer:

Syntax of setf function: cout.setf(arg1, arg2);

here arg1 is one of the formatting flags and arg2 is bit field.

Example: cout.setf(ios::left, ios::adjustfield);

10.14 State whether the following statements are TRUE or FALSE.

a. C++ stream is a file. (TRUE)

b. C++ never truncates(截断) data. (FALSE)

c. The get() function provides a single-character input that does not skip over the white space. (TRUE)

d. The header file iomanip can be used in place of iostream. (TRUE)

e. A programmer can define a manipulator that could represent a set of format functions. (TRUE)

Debugging Exercises:

10.1 State errors, if any, in the following statements.

a. cout << put("John");

Correction:

put() a member function of ostream class. It’s use to output a character, and it doesn’t return any value, so

cout.put("John");

b. cout << width();

Correction:

width() function does not return any value., so

cout.width();

c. wout.width(10).precision(3);

Correction:

MUST be written seperately., so

cout.width(10);
cout.precision(3);

d. cout.set((ios::scientific, ios::left));

Correction:

if you want to see output as scientfic format you can achieve this as follow:

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

if you want to see output at left field you can achieve this as follow:

cout.setf(ios::left, ios::adjusttield);

e. ch =cin.get(); (No error)

f. cin.get().get();

(cannot be concatenated)

Correction:
cin.get();
cin.get();

g. cin.get(c).get(); (No error)

h. cout<<setw(5)<<setprecision(2); (No error)

Programming Exercises:

10.1 Write a program to read a list containing item name, item code, and cost ineractively and produce a three column output as shown below.

在这里插入图片描述

Note that the name and code are left-justified and the cost is right justified with a precision of two digits. Trailing zeros are shown.

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

class item
{
private:
    char name[30];
    int code;
    float cost;
public:
    get_data(char *n, int c, float co)
    {
        strcpy(name, n);
        code = c;
        cost = co;
    }
    void display();
};

void item::display()
{
    cout.precision(2);
    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);
    cout.setf(ios::left, ios::adjustfield);
    cout<<setw(40)<<name<<code;
    cout.setf(ios::right, ios::adjustfield);
    cout<<setw(15)<<cost<<endl;
}

int main()
{
    item a[5];
    a[0].get_data("Turbo C++", 1001, 250.95);
    a[1].get_data("C Primer", 905, 957);
    a[2].get_data("Algorithm", 1111, 120.5);
    a[3].get_data("Principle of Electronics", 2220, 150.85);
    a[4].get_data("Atomic Habits", 6666, 150.00);
    cout << setw(10) << "Name" << setw(34) << "Code" << setw(15) << "Cost" << endl;

    for(int i=0; i<70;i++)
        cout << "-";
    cout << endl;

    for(int n=0;n<5;n++)
        a[n].display();

    return 0;
}

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值