北京理工大学计算机学院复试上机题目
由于编者水平有限,如有错误,请多多包涵。欢迎各位指正,转载请注明,谢谢合作!
1.存储一组姓名,如Apple,Tom,Green,Jack 要求能排序、按字母顺序插入、并显示。
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
string str;
vector<string> v;
vector<string>::iterator it;
while(1){
cout<<"请输入人名,以#结束:";
cin>>str;
while(str!="#"){
v.push_back(str);
cin>>str;
}
sort(v.begin(),v.end());
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
cout<<endl;
}
return 0;
}
2.输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,最后将该文件的
路径、该文件名及文件中的内容输出到屏幕。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
string path,fpath="",name="",data;
cout<<"请输入文件路径(格式为C:\\xx\\xx\\xx.txt):";
cin>>path;
// c_str()函数返回一个指向正规C字符串的指针
ofstream outfile(path.c_str());
cout<<"输入文件内容,以#结束:";
while(cin>>data){
if(data=="#")
break;
// 必须是endl结尾
outfile<<data<<endl;
}
// 提取文件名
int k=path.length()-1;
while(path[k]!='\\')
name=path[k--]+name;
// 提取文件路径
while(k>=0)
fpath=path[k--]+fpath;
// 输出
cout<<"文件路径为:"<<fpath<<endl;
cout<<"文件名为:"<<name<<endl;
ifstream infile(path.c_str());
cout<<"文件内容是:";
while(infile>>data)
cout<<data<<" ";
cout<<endl;
return 0;
}
3. 设计捕获两种不同类型的异常,一个是被 0 除,另一个是数组越界。
#include<iostream>
#include<vector>
using namespace std;
// 用来标记抛出异常的特殊类型,自带无参构造函数
class A{};
class B{};
int main(){
vector<int> v;
vector<int>::iterator it;
while(1){
try{
// 0做除数
double a,b;
cout<<"请输入被除数与除数:";
cin>>a>>b;
if(b==0)
throw A();
cout<<"无异常,结果为:"<<a/b<<endl;
// 数组越界
int n,data;
cout<<"输入数组长度:";
cin>>n;
cout<<"输入数组数值(int),以-100结束:";
while(cin>>data){
if(data==-100)
break;
v.push_back(data);
}
if(v.size()>n)
throw B();
cout<<"无异常,刚刚输入的数组为:";
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
cout<<endl;
v.clear();
}catch(A){
cout<<"0做除数!"<<endl;
}catch(B){
v.clear();
cout<<"数组越界!"<<endl;
}
}
return 0;
}
4. 设计一个程序能计算日期的间隔,如输入两个日期别为 2008-2-3 和 2008-3-9 计算相隔
多少天,或 2008-2-3 加上 100 天后的日期是多少。
#include<iostream>
#include<math.h>
using namespace std;
/*
* 思路:采取一种以空间换取时间的方法,
* 即:将0-5000年的日期,都计算出其与0年1月1日的相隔天数
* 求两个日期的差值时,直接做差
*/
// 定义宏,判断是否是闰年
#define ISYEAR(x) x%100!=0 && x%4==0 || x%400==0?1:0
// Note:buf数组必须声明为全局变量,因为分配空间过大,声明为局部变量会报错
int buf[5001][13][32];
int dayOfMonth[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};
class Date{
public:
int year;
int month;
int day;
// 产生接下来的一天的日期,并存入属性值当中
void nextDate(){
day++;
// 如果日期超过了当前月份最大天数
if(day>dayOfMonth[month][ISYEAR(year)]){
day=1;
month++;
if(month>12){
month=1;
year++;
}
}
}
};
int main(){
// 预处理,产生所有日期与0年1月1日的相隔天数
Date date;
int sub=0; // 与0年1月1日的相隔天数
date.year=0; date.month=1; date.day=1;
while(date.year<5001){
buf[date.year][date.month][date.day]=sub;
date.nextDate();
sub++;
}
// 输入操作
while(1){
int choose;
cout<<"1.两个日期的间隔 2.n天后的日期:";
cin>>choose;
if(choose==1){
int year1,year2,month1,month2,day1,day2;
cout<<"请输入两个日期,空格隔开(格式为2008 8 1): ";
cin>>year1>>month1>>day1>>year2>>month2>>day2;
cout<<"相隔天数为:"<<abs(buf[year1][month1][day1]-buf[year2][month2][day2])<<"天。"<<endl;
}
else if(choose==2){
int year,month,day,n;
cout<<"请输入日期及间隔天数,日期用空格隔开(格式为2008 8 1 n): ";
cin>>year>>month>>day>>n;
for(int i=year;i<5001;i++)
for(int j=month;j<13;j++)
for(int k=day;k<32;k++)
if(buf[i][j][k]==buf[year][month][day]+n)
cout<<n<<"天后的日期为:"<<i<<"-"<<j<<"-"<<k<<endl;
}
else{
cout<<"输入有误!"<<endl;
}
}
return 0;
}