北理计算机学院2005年机试真题

北京理工大学计算机学院复试上机题目

  由于编者水平有限,如有错误,请多多包涵。欢迎各位指正,转载请注明,谢谢合作!

1.给定一个程序,关于字符串的,要求输入并调试,说出此程序的意图。意图是按字

母顺序对两个字符串比较排序。第二问要求用尽可能少的语句对该程序进行修改,使其能够

对两个字符串比较长度排序。本题满分 20。

#include<iostream>
#include<string>
using namespace std;
void main()
{
string str1,str2;
cout<<"请输入两个字符串:";
cin>>str1>>str2;
if(str1.length()>str2.length())
cout<<str1<<"is long than"<<str2<<endl;
else if(str1.length()==str1.length())
cout<<str1<<" and "<<str2<<" is equal long"<<endl;
else
cout<<str2<<"is long than "<<str1<<endl;
}

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{
public:
	int year;
	int month;
	int day;
	Date(){}
	Date(int y,int m,int d){
		year=y;
		month=m;
		day=d;
	}
	void nextDate(){
		day++;
		if(day>30){
			day=1;
			month++;
			if(month>12){
				month=1;
				year++;
			}
		}
	}
	void show(){
		cout<<year<<"-"<<month<<"-"<<day<<endl;
	}
};

int main(){
	int year,month,day;
	while(1){
		cout<<"请输入日期:";
		cin>>year>>month>>day;
		if(year<1||month<1||month>12||day<1||day>30){
			cout<<"时间不合法,请重新输入!"<<endl;
			continue;
		}
		Date date(year,month,day);
		date.nextDate();
		date.show();
	}
	
	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 Complex{
public:
	double a;
	double b;
	Complex(){}
	Complex(double at,double bt){
		a=at;
		b=bt;
	}
	// 实现两个复数相加
	Complex add(Complex B){
		return Complex(a+B.a,b+B.b);
	}
	friend ostream& operator <<(ostream&,Complex&);
	Complex operator +(Complex A){
		return Complex(a+A.a,b+A.b);
	}
	// 类型转换函数,实现double类型=Comple类型的赋值运算 
	operator double(){
		return a+b;
	}
};

ostream& operator <<(ostream& output,Complex& A){
	output <<A.a<<"+"<<"i"<<A.b;
	return output;
}

int main(){
	Complex a(1,2),b(2,3),d(3,2),c;
	cout<<"复数a,b,d分别为:a:"<<a<<" b:"<<b<<" d:"<<d<<endl;
	c=a+b+d;
	cout<<"c=a+b+d的值为:"<<c<<endl;
	cout<<"复数a+b等于:"<<a.add(b)<<endl;
	double t=a;
	cout<<"double t=a有效,赋值后t的值为:"<<t<<endl;
	
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值