谭浩强《C++程序设计》书后习题 第十三章-第十四章

最近要复习一下C和C++的基础知识,于是计划把之前学过的谭浩强的《C程序设计》和《C++程序设计》习题重新做一遍。

编译环境为:操作系统32位Win7,编译工具VC++6.0

第十三章:输入输出流

10.1)输入三角形的三边边长a、b、c,通过海伦公式计算三角形面积,用cerr输出有关出错的信息

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    double a, b, c, s, area;
    cin >> a >> b >> c;
    if(a <= 0 || b <= 0 || c <= 0)
    {
        cerr << "三角形每个边长应大于0" << endl;
    }
    else if(a + b <= c || a + c <= b || b + c <= a)
    {
        cerr << "三角形两边之和须大于第三边" << endl;
    }
    else
    {
        s = (a + b + c) / 2.0;
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        cout << "Area: " << area << endl;
    }

    return 0;
}

10.2)从键盘输入一批数值,要求保留3位小数,在输出时上下行小数点对齐

#include<iostream>

using namespace std;

int main()
{
    cout.setf(ios :: right);   //右对齐
    cout.setf(ios :: dec);     //十进制
    cout.setf(ios :: showpos); //对正数显示+号
    cout.setf(ios :: fixed);   //浮点数以定点格式输出

    double a;
    while(true)
    {
        cout.precision(3); //实数精度3位
        cout.width(20);    //字段宽度10位
        cout.fill(' ');    //填充字符为' '

        cin >> a;
        cout << a << endl;
    }

    cout.unsetf(ios :: right);   //取消右对齐
    cout.unsetf(ios :: dec);     //取消十进制
    cout.unsetf(ios :: showpos); //取消对正数显示+号
    cout.unsetf(ios :: fixed);   //取消浮点数以定点格式输出

    return 0;
}

10.3)编程序,在显示屏上显示一个字母B组成的三角形

#include<iostream>

using namespace std;

int main()
{
    int i, j;
    for(i = 0; i < 8; i++)
    {
        //cout << i << ":";
        for(j = 8 - i - 1; j > 0; j--)
        {
            cout.put(' ');
        }
        for(j = 0; j < i * 2 + 1; j++)
        {
            cout.put('B');
        }
        cout << endl;
    }

    return 0;
}

10.4)建立两个磁盘文件f1.dat和f2.dat

1:从键盘键入20个整数,分别存在两个磁盘文件中,每个文件存10个

2:从f1.dat中读取10个数放在f2.dat的后面

3:对f2.dat中的20个整数进行排序

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
    int a[20], i, j, temp;

    //样例输入 1 3 5 7 9 11 13 15 17 19 18 16 14 12 10 8 6 4 2 0
    cout << "请输入20个整数:";

    //输入10个数存入f1.dat
    ofstream outfile1("f1.dat", ios :: out);
    if(!outfile1)
    {
        cerr << "f1.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 10; i++)
    {
        cin >> a[i];
        outfile1 << a[i] << " ";
    }
    outfile1.close();

    //输入10个数存入f2.dat
    ofstream outfile2("f2.dat", ios :: out);
    if(!outfile2)
    {
        cerr << "f2.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 10; i < 20; i++)
    {
        cin >> a[i];
        outfile2 << a[i] << " ";
    }
    outfile2.close();

    //从f1.dat中读入10个数存到f2.dat后
    ifstream infile1("f1.dat", ios :: in);
    if(!infile1)
    {
        cerr << "f1.dat 打开错误" << endl;
        exit(1);
    }
    ofstream outfile3("f2.dat", ios :: out | ios :: app);
    if(!outfile3)
    {
        cerr << "f2.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 10; i++)
    {
        infile1 >> temp;
        outfile3 << temp << " ";
    }
    infile1.close();
    outfile3.close();

    //将f2.dat中的20个数排序后写回
    //读取20个数
    ifstream infile2("f1.dat", ios :: in);
    if(!infile2)
    {
        cerr << "f1.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 20; i++)
    {
        infile2 >> a[i];
    }
    infile2.close();
    //排序
    for(i = 0; i < 20; i++)
    {
        for(j = i + 1; j < 20; j++)
        {
            if(a[i] > a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    //将排序后的数字写回
    ofstream outfile4("f2.dat", ios :: out);
    if(!outfile4)
    {
        cerr << "f2.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 20; i++)
    {
        outfile4 << a[i] << " ";
    }
    outfile4.close();

    return 0;
}

10.5)编程实现下列功能

1:按职工号大小由小到大顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存

2:从键盘输入两个员工数据,增加到文件末尾

3:从键盘键入一个号码,从文件中查找数据,如果有则输出职工数据,没有则输出“查无此人”

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;

//职工
struct Person
{
    int m_Id;
    string m_Name;
    int m_Age;
    int m_Wage;

    Person(int i, string n, int a, int w): m_Id(i), m_Name(n), m_Age(a), m_Wage(w) { }
    void PrintInfo()
    {
        cout << "===================" << endl;
        cout << "Id:\t" << this -> m_Id << endl;
        cout << "Name:\t" << this -> m_Name << endl;
        cout << "Age:\t" << this -> m_Age << endl;
        cout << "Wage:\t" << this -> m_Wage << endl;
        cout << "===================" << endl;
    }
};

Person *p[5] = 
{
    new Person(101, "Tsybius", 23, 7100),
    new Person(102, "Galatea", 21, 6600),
    new Person(105, "Quintus", 15, 5000),
    new Person(104, "Aurelia", 20, 8000),
    new Person(103, "Julius", 30, 5050)
};

int main()
{
    //对5组数据按ID进行排序
    int i, j;
    Person *temp;
    for(i = 0; i < 5; i++)
    {
        for(j = i + 1; j < 5; j++)
        {
            if(p[i] -> m_Id > p[j] -> m_Id)
            {
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }

    //将排好序的数据存入到文件f.dat
    ofstream outfile1("f.dat", ios :: out);
    if(!outfile1)
    {
        cerr << "f.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 5; i++)
    {
        outfile1 << p[i] -> m_Id << " " 
            << p[i] -> m_Name << " "
            << p[i] -> m_Age << " "
            << p[i] -> m_Wage << endl;
    }
    outfile1.close();

    //再写入两个职工信息
    //示例输入 108 Titus 40 4000 109 Tulius 35 5000
    cout << "请输入2个职工信息:";
    ofstream outfile2("f.dat", ios :: out | ios :: app);
    if(!outfile2)
    {
        cerr << "f.dat 打开错误" << endl;
        exit(1);
    }
    temp = new Person(0, "", 0, 0);
    cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
    outfile2 << temp -> m_Id << " "
        << temp -> m_Name << " "
        << temp -> m_Age << " "
        << temp -> m_Wage << endl;
    cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
    outfile2 << temp -> m_Id << " "
        << temp -> m_Name << " "
        << temp -> m_Age << " "
        << temp -> m_Wage << endl;
    outfile2.close();

    //输出文件中的全部职工数据
    ifstream infile1("f.dat", ios :: in);
    if(!infile1)
    {
        cerr << "f.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 7; i++)
    {
        infile1 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
        temp -> PrintInfo();
    }
    infile1.close();

    //输入一个ID检测是否存在
    int id;
    cout << "请输入一个ID:";
    cin >> id;
    bool isFound = false;
    ifstream infile2("f.dat", ios :: in);
    if(!infile2)
    {
        cerr << "f.dat 打开错误" << endl;
        exit(1);
    }
    for(i = 0; i < 7; i++)
    {
        infile2 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
        if(temp -> m_Id == id)
        {
            temp -> PrintInfo();
            isFound = true;
            break;
        }
    }
    if(!isFound)
    {
        cerr << "没有找到这个人" << endl;
    }
    infile2.close();

    return 0;
}

10.6)在例13.17的基础上,修改程序,将存在在c数组中的数据读入并显示出来

#include<iostream>
#include<strstream>

using namespace std;

struct Student
{
    int m_Num;
    char m_Name[20];
    float m_Score;
};

int main()
{
    Student stud[3] = { 1001, "Li", 78, 1002, "Wang", 89.5f, 1004, "Fun", 90 };
    
    //将信息存入到字符串中
    char c[50];
    ostrstream strout(c, sizeof(c));
    int i;
    for(i = 0; i < 3; i++)
    {
        strout << stud[i].m_Num << " "
            << stud[i].m_Name << " " 
            << stud[i].m_Score << " ";
    }
    strout << ends;
    cout << "array c:" << c << endl;

    //从字符串中读取信息
    Student stud2[3];
    istrstream strin(c, sizeof(c));
    for(i = 0; i < 3; i++)
    {
        strin >> stud2[i].m_Num >> stud2[i].m_Name >> stud2[i].m_Score;
        cout << stud2[i].m_Num << " " 
            << stud2[i].m_Name << " " 
            << stud2[i].m_Score << endl;
    }

    return 0;
}

第十四章:输入输出流

14.1)求一元二次方程ax^2+bx+c=0的实根,如果方程没有实根,则输出有关警告信息

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    double a, b, c, delta;
    cin >> a >> b >> c;
    try
    {
        //计算Δ
        delta = b * b - 4 * a * c;
        if(delta < 0)
        {
            throw delta;
        }

        //求根
        double x1 = (-1 * b + sqrt(delta)) / (2 * a);
        double x2 = (-1 * b - sqrt(delta)) / (2 * a);
        cout << "x1=" << x1 << ";x2=" << x2 << endl;
    }
    catch(double)
    {
        //Δ<0的情况
        cout << "b^2-4ac=" << delta << "<0" << endl;
        cout << "方程无实根" << endl;
    }
    cout << "程序运行完毕" << endl;

    return 0;
}

14.3)在两个命名空间中实现Student类

#include<iostream>
#include<string>

using namespace std;

namespace ns1
{
    struct Student
    {
        int m_ID;
        string m_Name;
        int m_Age;
        string m_Addr;
        //构造函数
        Student(int id, string na, int ag, string ad): 
            m_ID(id), m_Name(na), m_Age(ag), m_Addr(ad) { }
    }
    stud[3] = 
    {
        Student(101, "Tsybius", 23, "XXX"),
        Student(102, "Galatea", 21, "YYY"),
        Student(103, "Aurelia", 22, "ZZZ")
    };
}

namespace ns2
{
    struct Student
    {
        int m_ID;
        string m_Name;
        char m_Sex;
        string m_Addr;
        //构造函数
        Student(int id, string na, char sx, string ad): 
            m_ID(id), m_Name(na), m_Sex(sx), m_Addr(ad) { }
    }
    stud[3] = 
    {
        Student(101, "Tsybius", 'm', "XXX"),
        Student(102, "Galatea", 'f', "YYY"),
        Student(103, "Aurelia", 'f', "ZZZ")
    };
}

int main()
{
    int id;
    cout << "请输入一个ID:";
    cin >> id;

    //搜索内容
    ns1 :: Student *s1;
    ns2 :: Student *s2;
    for(int i = 0; i < 3; i++)
    {
        if(ns1 :: stud[i].m_ID == id)
        {
            s1 = &(ns1 :: stud[i]);
        }
        if(ns2 :: stud[i].m_ID == id)
        {
            s2 = &(ns2 :: stud[i]);
        }
    }

    cout << "ID:\t" << s1 -> m_ID << endl;
    cout << "Name:\t" << s1 -> m_Name << endl;
    cout << "Age:\t" << s1 -> m_Age << endl;
    cout << "Sex:\t" << s2 -> m_Sex << endl;
    cout << "Addr:\t" << s1 -> m_Addr << endl;
    
    return 0;
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/314958

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值