The Beijing Institute of Technology re-examination machine --2005

1, given a program, on the string, and asked to enter debug, say the intent of this program. The intention is alphabetically sorted on two string comparison.
The second question requires as few modifications with the program statement, it is possible to compare the two string lengths sort. 
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(string s1, string s2) {
    return s1.length() < s2.length();
}

bool cmp2(string s1, string s2) {
    return s1 < s2;
}

int main() {
    string s1, s2;
    while(cin >> s1 >> s2) {
        cout << "字母顺序排序:";
        if(cmp2(s1, s2)) cout << s1 << " " << s2;
        else cout << s2 << " " << s1;
        cout << endl;
        cout << "字母长度排序:";
        if(cmp(s1, s2)) cout << s1 << " " << s2;
        else cout << s2 << " " << s1;
        cout << endl;
    }
    return 0;
}
2, the preparation of a date type, the format required by the output date xxxx-xxxx achieve operation plus one day.
Input: three integers separated by spaces, respectively date. Test data will not be a leap year.
Output: xxxx-xxxx output by format, the day after the date indicates the date entered.
#include <iostream>
using namespace std;
class date
{
public:
    int day, month, year;
    void show() {
        if(day == 28 && month == 2) {
            day = 1;
            month++;
        }
        else if(day == 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
            day = 1;
            month++;
        }
        else if(day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
            day = 1;
            month++;
        }
        else day++;
        if(month == 13) {
            month = 1;
            year++;
        }
        cout << year << "-" << month << "-" << day << endl;
    }
};

int main() {
    int y, m, d;
    while(cin >> y >> m >> d) {
        date dd;
        dd.year = y;
        dd.month = m;
        dd.day = d;
        dd.show();
    }
    return 0;
}
3, the preparation of a complex class constructors can initialize complex; overloaded addition operator press output in the form a + bi.
#include <iostream>
using namespace std;

class fushu
{
public:
    int a1, b1, a2, b2;
    void show() {
        if(a1 + a2 == 0) {
            if(b1 + b2 == 0) cout << "0" << endl;
            else cout << b1 + b2 << "i" << endl;
        }
        else {
            if(b1 + b2 > 0) {
                cout << a1 + a2 << "+" << b1 + b2 << "i" << endl;
            }
            else if (b1 + b2 == 0) {
                cout << a1 + a2 << endl;
            }
            else cout << a1 + a2 << b1 + b2 << "i" << endl;
        }
    }
};

int main() {
    int a1, b1, a2, b2;
    fushu fs;
    while (cin >> fs.a1 >> fs.b1 >> fs.a2 >> fs.b2) {
        fs.show();
    }
    return 0;
}

PS: class understand or do not understand ah ah ah ah ah ah ah! ! !

Guess you like

Origin www.cnblogs.com/ache/p/12535993.html