2008年北理复试上机题

1、存储一组姓名,如Apple,Tom,Green,Jack要求能排序、按字母顺序插入、并显示。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<string> v;
    string s;
    cout << "请输入一组姓名用ctrl+Z结束! " << endl;
    while (cin >> s) v.push_back(s);
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)cout << v[i] << "  ";
    cout << endl;
    return 0;
}

2、输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,最后将该文件的路径、该文件名及文件中的内容输出到屏幕。

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
    string fname, name, road, c;
    cout<<"请输入文件路径以及文件名,格式如E:\\xx\\xx\\xx.txt" << endl;
    cin >> fname;
    ofstream out(fname.c_str());
    cout << "请输入文件的内容,Ctrl+Z结束:" << endl;
    while (cin >> c)out << c << endl;
    int idx;
    for (int i = 0; i < fname.length(); i++)
    {
        if (fname[i] == '\\')idx = i;
    }
    road = fname.substr(0, idx - 1);//下标为idx的字符不会包含,左闭右开 
    name = fname.substr(idx + 1, fname.length());
    cout << "文件路径为:" << road << endl << "文件名为:" << name << endl;
    cout << "文件内容为:";
    c = "";//清空原有字符
    ifstream in(fname.c_str());
    while (in >> c)cout << c << " ";
    return 0;
}

3、设计捕获两种不同类型的异常,一个是被0除,另一个是数组越界。

异常知识链接

类名+括号用法链接

#include<iostream>
#include<vector>
using namespace std;
class A {};//自定义异常
class B {};

int main()
{
    double a, b;
    int length;
    vector<int> v;
    cout << "请输入除数和被除数" << endl;
    cin >> a >> b;
    try
    {
        if (b == 0)throw A();//不调用任何构造函数,只创建文件名
        else cout << a / b << endl;
        cout << "请输入数组长度:" << endl;
        cin >> length;
        cout << "请输入数组内容,Ctrl+Z结束:" << endl;
        int cnt = 0;
        while (cin >> a)
        {
            v.push_back(a);
            cnt++;
        }
        if (cnt > length)throw B();
        else
        {
            vector<int>::iterator j;
            for (j = v.begin(); j != v.end(); j++)cout << *j << " ";
        }
    }
    catch (A)
    {
        cout << "0不可以作为除数" << endl;
    }
    catch (B)
    {
        cout << "数组发生了越界" << endl;
    }
    catch (...)
    {
        cout << "发生了其他异常" << endl;
    }
    return 0;
}

4、设计一个程序能计算日期的间隔,如输入两个日期分别为2008-2-3和2008-3-9计算相隔多少天,或2008-2-3加上100天后的日期是多少。

#include<iostream>
#include<cstdio>
using namespace std;

int month[13][2] = { 0,0,31,31,28,29,31,31,30,30,31,31,30,30,31,31,31,31,30,30,31,31,30,30,31,31 };

bool isLeapYear(int x)
{
    return (x % 4 == 0 && x % 100 != 0) || x % 400 == 0;
}

int abs(int x)
{
    return x > 0 ? x : -x;
}

int days(int y, int m, int d)
{
    int yy = 1, mm = 1, dd = 1, ans = 1;
    while (yy!=y || mm != m || dd != d)
    {
        dd++;
        ans++;
        if (dd > month[mm][isLeapYear(y)])
        {
            dd = 1;
            mm++;
            if (mm > 12)
            {
                mm = 1;
                yy++;
            }
        }
    }
    return ans;
}

void count(int y, int m, int d, int n)
{
    while (n--)
    {
        d++;
        if (d > month[m][isLeapYear(y)])
        {
            d = 1;
            m++;
            if (m > 12)
            {
                m = 1;
                y++;
            }
        }
    }
    printf("相加后的日期是%04d-%02d-%02d\n", y, m, d);
}

int main()
{
    cout << "请输入两个日期,形如1999-09-19:" << endl;
    int y1, m1, d1;
    scanf("%d-%d-%d", &y1, &m1, &d1);
    int y2, m2, d2;
    scanf("%d-%d-%d", &y2, &m2, &d2);
    cout << "两个日期相差" << abs(days(y1, m1, d1) - days(y2, m2, d2)) << "" << endl;
    cout << "请输入初始日期和要加上的天数:" << endl;
    int y, m, d, n;
    scanf("%d-%d-%d", &y, &m, &d);
    cin >> n;
    count(y, m, d, n);
    return 0;
}

 

posted @ 2019-08-17 00:40  郭怡柔  阅读(200)  评论(0)    收藏  举报