北京理工大学2008年计算机考研复试上机试题

在这里插入图片描述

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	vector<string>st;
	string s;
	vector<string>::const_iterator it;
	cout<<"请输入单词,以00结束"<<endl;
	while(cin>>s)
	{
		if(s=="00")
		break;
		st.push_back(s);
		sort(st.begin(),st.end());
		for(it=st.begin();it!=st.end();it++)
		cout<<*it<<" ";
		cout<<endl;
		cout<<"请输入单词,以00结束"<<endl;
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
	string path,fpath="",name="",data;
	cout<<"请输入文件路径(格式为C:\\xx\\xx\\xx.txt):"<<endl;
	cin>>path;
	//c_str()函数返回一个指向正规C字符串的指针
	ofstream outf(path.c_str());
	cout<<"输入文件的内容,以#结束:"<<endl;
	while(cin>>data)
	{
		if(data=="#")
		break;
		outf<<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 inf(path.c_str()) ;
	cout<<"文件内容是:";
	while(inf>>data)
	{
		cout<<data<<' ';
	}
	cout<<endl;
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int>v;
	vector<int>::const_iterator it;
	while(1)
	{
		try
		{
			//0做除数
			double a,b;
			cout<<"请输入除数和被除数"<<endl;
			cin>>a>>b;
			if(b==0)
			throw A();
			cout<<"无异常,结果为:"<<a/b<<endl;
			//数组越界
			int n,data;
			cout<<"请输入数组长度:"<<endl;
			cin>>n;
			cout<<"请输入数组数值,以-100结束"<<endl;
			while(cin>>data)
			{
				if(data==-100)
				break;
				v.push_back(data);
			}
			if(v.size()>n)
			throw B();
			cout<<"无异常,输入的数组为:"<<endl;
			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;
}

在这里插入图片描述

#include<iostream>
using namespace std;
int daytab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
bool IsLeapYear(int year)
{
	return (year%4==0&&year%100!=0)||(year%400==0);
}
int sum(int year,int month,int day)
{
	int count=0;
	for(int i=1;i<year;i++)
	{
		if(IsLeapYear(i))
		count+=366;
		else
		count+=365;
	}
	int row=IsLeapYear(year);
	for(int j=1;j<month;j++)
	count+=daytab[row][j];
	count+=(day-1);
	return count;
}
int main()
{
	int op;
	cout<<"请输入要选择的功能:1,计算两个日期间隔;2,一个日期加上多少天后是哪个日期。0表示退出程序"<<endl;
	while(cin>>op)
	{
		if(op==0)
		break;
		if(op==1)
		{
			int year1,month1,day1,year2,month2,day2;
			cout<<"请输入第一个日期和第二个日期"<<endl;
			cin>>year1>>month1>>day1>>year2>>month2>>day2;
			cout<<"两个日期相差:"<<abs(sum(year1,month1,day1)-sum(year2,month2,day2))<<"天"<<endl;
		} 
		else if(op==2)
		{
			int year,month,day,aday;
			cout<<"请输入日期和要加上的天数"<<endl;
			cin>>year>>month>>day>>aday;
			int count=sum(year,month,day);
			count+=aday;
			int y=1;
			while(count>=0)
			{
				if(IsLeapYear(y))
				{
					count-=366;
					y++;
				}
				else
				{
					count-=365;
					y++;
				}
			}
			year=y-1;
			if(IsLeapYear(y-1))
			count+=366;
			else
			count+=365;
			int m=1;
			int row=IsLeapYear(year);
			while(count>=0)
			{
				count-=daytab[row][m++];
			}
			month=m-1;
			count+=daytab[row][m-1];
			day=count+1;
			cout<<"该日期加上"<<aday<<"后的日期为:"<<year<<" "<<month<<" "<<day<<endl;
		}
		else
		cout<<"请重新输入要选择的功能。"<<endl;
		cout<<"请输入要选择的功能:1,计算两个日期间隔;2,一个日期加上多少天后是哪个日期。0表示退出程序"<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值