zoukankan      html  css  js  c++  java
  • C++第七章习题

    7.1 C++为什么要有自己的输入输出系统?

             C++的编译系统对数据类型进行严格的检查,凡是类型不正确的数据都不可能通过编译。

             C++中需要定义众多的用户自定义类型且能够输入输出。

    7.2 C++有哪4个预定义的流对象?他们分别与什么具体设备相关联?

             标准输入流对象cin、标准输出流对象cout、非缓冲型的标准出错流对象cerr和缓冲型的标准出错流对象clog。

             cin对应标准输入设备

             cout对应标准输出设备

             cerr对应标准错误输出设备

             clog对应标准错误输出设备

    7.3. cerr和clog间的区别是?

             cerr不经过缓冲区直接显示错误信息。而clog存放在缓冲区,缓冲区满或遇上endl时再输出。

    7.4 C++提供哪两种控制输入输出格式的方法?

             一种是使用ios类中的有关格式控制的流成员函数进行格式控制,另一种是使用称为操纵符的特殊类型的函数控制。

    7.5 C++进行文件输入输出的基本过程是什么?

             首先创建一个流对象,然后将这个流对象与文件相关联,即打开文件,此时才能进行读写操作,读写操作完成后再关闭这个文件。

    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 << "请输入一个2行3列矩阵:" << 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 << "文件打开失败!" << 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 << "打开文件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 << "打开文件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 << "打开文件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 << "打开文件file2.txt失败!\n";
            abort();
        }
        out >> str;
        in.close();
        out.close();
        return 0;
    }

    7.15 不会。

  • 相关阅读:
    在网页中象GMAIL一样检测客户端是否连接到网络
    How to adjust IFrame height on it's content (转载)
    asp.net工程中aspx文件与codebehind文件的关联问题
    一个在VC中调用web service的方法
    往mssql 表的自增长字段中添加值
    iis7上安装php5+mysql
    试用淘宝开放平台
    关于IOCP的方方面面
    Overlapped I/O
    为什么需要allocator的rebind接口
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2552819.html
Copyright © 2011-2022 走看看