BIT-CS-2005年复试机试

2019-02-02  本文已影响2人  小白之白小明

1、给定一个程序,关于字符串的,要求输入并调试,说出此程序的意图。意图是按字母顺序对两个字符串比较排序。第二问要求用尽可能少的语句对该程序进行修改,使其能够对两个字符串比较长度排序。

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>  //动态数组(不定长数组)
using namespace std;
//定义排序比较函数
bool len(string a, string b)
{
    return (a.length()<b.length());
}
int main()
{
    string s;
    vector<string> str;
    cout << "请输入字符串,以00结束:";
    while (cin >> s) {
        if (s == "00")
            break;
        str.push_back(s);
    }
    sort(str.begin(), str.end());
    //使用迭代器访问vector
    vector<string>::iterator numer;
    for (numer = str.begin(); numer != str.end(); numer++)
        cout << *numer << " ";
    cout << endl;
    
    sort(str.begin(), str.end(), len);
    //使用下标访问元素
    for (int i = 0; i < str.size(); i++)
        cout << str[i] << " ";
    cout << endl;

    system("pause");
    return 0;
} 

2、编写一个日期类,要求按 xxxx-xx-xx 的格式输出日期,实现加一天的操作,不考虑闰年问题,所有月份设为 30 天。本题黑盒测试时,输入 2004 年 3 月 20日,得到加一天后时间为 2004-3-21 ,能得一部分分数。输入 2004 年 3 月 30 日,得到加一天后时间为2004-4-1,能得一部分分数。输入 2004 年 12 月 30日,得到加一天后时间为 2005-1-1 ,且有时间越界处理,能得全部分数。本题满分 30。

#include<iostream>
using namespace std;
class date{
private:
    int year, month, day;
public:
    date() {};   
    date(int i, int j, int k) :year(i), month(j), day(k) {};   //构造函数初始化列表
    void show() {
        day += 1;
        if (day == 31) {
            month++;
            day = 1;
        }
        if (month == 13) {
            year++;
            month = 1;
        }
        cout << year << "-" << month << "-" << day;
    }
    
};
int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    date n(a,b,c);
    n.show();
    system("pause");
    return 0;
} 

3.编写一个复数类,要求有 4 条。一是有构造函数能对复数初始化。二是对复数 c1 ,c2 ,c3 能实现连加运算,令c=c1+c2+c3+ 此处可以重载加法操作符。三是有函数实现两个复数相加,并按照 a+ib的形式输出。四是能实现对一个复数 c=a+ib,定义 double x=c 有效,使 x 的值为实部和虚部之和。本题满分 50。

#include<iostream>
using namespace std;
class fushu {
private:
    double a, b;
public:
    fushu (){}
    fushu(int i, int j) :a(i), b(j) {}
    fushu operator + (fushu kk) {
        return fushu(a + kk.a, b + kk.b);
    }
    void show() {
        cout << a << "+" << b << "i"<<endl;
    }
    double get() {     //实部+虚部
        return (a + b);    
    }
};
int main()
{
    fushu a(1, 2), b(2, 3), c(3, 4), d;
    d = a + b + c;
    d.show();
    double x=d.get();
    cout << x;
    system("pause");
    return 0;
} 
上一篇 下一篇

猜你喜欢

热点阅读