历年北京理工大学复试上机题题目汇总:
http://blog.csdn.net/u014552756/article/details/78505845
1、给定一个程序,关于字符串的,要求输入并调试,说出此程序的意图。意图是按字母顺序对两个字符串比较排序。第二问要求用尽可能少的语句对该程序进行修改,使其能够对两个字符串比较长度排序。
#include <iostream>
#include<vector>
#include <string>
#include <algorithm>
using namespace std;
bool length(string a,string b)
{
return (a.length()<b.length());
}
int main()
{
string s;
vector<string> str;
cout<<"请输入字符串,以00结束:"<<endl;
while(cin>>s)
{
if(s=="00")
break;
str.push_back(s);
}
cout<<"按字典排序:"<<endl;
sort(str.begin(),str.end());
vector<string>::const_iterator i;
for(i=str.begin(); i!=str.end(); i++)
cout<<*i<<" ";
cout<<endl;
cout<<"按长度排序:"<<endl;
sort(str.begin(),str.end(),length);
for(i=str.begin(); i!=str.end(); i++)
cout<<*i<<" ";
cout<<endl;
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 assign(int i,int j,int k)
{
year=i;
month=j;
day=k;
}
void show()
{
day=day+1;
if(day==31)
{
month++;
day=1;
}
if(month==13)
{
year++;
month=1;
}
cout<<"加一天后时间为: "<<endl<<year<<"-"<<month<<"-"<<day<<endl;
}
};
int main()
{
int y,m,d;
date a;
cout<<"请输入日期,以0 0 0结束:"<<endl;
while(cin>>y>>m>>d)
{
if(y==0&&m==0&&d==0)
break;
if(d>30||m>12||y<0||m<0||d<0)
cout<<"不存在这样的日期。。。。"<<endl;
else
{
date a=date(y,m,d);
a.show();
}
cout<<"请输入日期,以0 0 0结束:"<<endl;
}
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) {};
void assign(int i,int j)
{
a=i;
b=j;
}
int show()
{
return (a+b);
}
void get()
{
cout<<a<<"+"<<b<<"i"<<endl;
}
fushu operator+(fushu kk)
{
return fushu(a+kk.a,b+kk.b);
}
};
int main()
{
fushu c1(1,2),c2(2,2),c3(5,6),c4;
cout<<"复数 c1,c2,c3:"<<endl;
c1.get();
c2.get();
c3.get();
c4=c1+c2+c3;
cout<<"c1,c2,c3 复数之和为:"<<endl;
c4.get();
double c=c4.show();
cout<<"定义的double类型值为:"<<c<<endl;
return 0;
}