版权声明:作者:weizu_cool https://blog.csdn.net/qq_26460841/article/details/88361963
1、存储一组姓名,如Apple,Tom,Green,Jack要求能排序、按字母顺序插入、并显示。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
/*
1、存储一组姓名,如Apple,Tom,Green,Jack要求能排序、按字母顺序插入、并显示。
*/
int main(){
vector<string> vc;
cout<<"请输入一组学生姓名(以00结束):"<<endl;
string name;
while(cin>>name){
if(name=="00")
break;
vc.push_back(name);
sort(vc.begin(), vc.end());
for(int i=0;i<vc.size();i++){
cout<<vc[i]<<" ";
}
}
return 0;
}
2、输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,最后将该文件的路径、该文件名及文件中的内容输出到屏幕。
参考:C++ Error no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::string&)
参考:读取文件
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
/*
2、输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,最后将该文件的路径、该文件名及文件中的内容输出到屏幕。
*/
int main(){
cout<<"输入文件名及路径以创建该文件,如:E:/a.txt"<<endl;
string fileName;
cin>>fileName;
ofstream fout(fileName.c_str());
cout<<"请从键盘输入的内容保存到该文件,文件内容以'空格#'结束"<<endl;
string content;
while(cin>>content){
if(content=="#")
break;
fout<<content<<endl;
}
//截取字符串
int j=0;
for(int i=0;i<fileName.length();i++){
if(fileName[i]=='/')
j=i;
}
fout.close();
cout<<j;
cout<<"该文件的路径: "<<fileName.substr(0,j+1)<<"、该文件名: "<<fileName.substr(j+1)<<endl;
ifstream in(fileName.c_str());
string line;
cout<<"件中的内容:"<<endl;
while(!in.eof()){
in>>line; //读取数据后,就判断流是否‘坏掉’,以避免最后一次数据重复读写
if(in.fail())
break;
cout<<line<<endl;
}
in.close();
return 0;
}
3、设计捕获两种不同类型的异常,一个是被0除,另一个是数组越界。参考: C++ 异常处理(try catch)
#include <iostream>
#include <string>
using namespace std;
class A {
private:
string m_msg;
public:
A(string msg):m_msg(msg){}
void showMsg(){
cout<<m_msg<<endl;
}
};
class B {
private:
string m_msg;
public:
B(string msg):m_msg(msg){}
void showMsg(){
cout<<m_msg<<endl;
}
};
int main()
{
while(1){
try{
cout<<"请输入除数和被除数:"<<endl;
int a, b;
cin>>a>>b;
if(b==0)
throw A("被除数不能为0");
else cout<<a<<"/"<<b<<"计算的结果是:"<<a/b<<endl;
cout<<"请输入数组的长度:";
int length;
cin>>length;
int arr[length], num, i=0;
cout<<"请输入数组的元素(int),以777结束:"<<endl;
while(cin>>num){
if(num==777)
break;
arr[i]=num;
i++;
if(i==length+1) throw B("数组越界");
}
}catch(A e){
e.showMsg();
}catch(B e){
e.showMsg();
}
}
return 0;
}
4、设计一个程序能计算日期的间隔,如输入两个日期分别为2008-2-3和2008-3-9计算相隔多少天,或2008-2-3加上100天后的日期是多少。
#include<exception>
#include<iostream>
using namespace std;
/*
4、设计一个程序能计算日期的间隔,如输入两个日期分别为2008-2-3和2008-3-9计算相隔多少天,或2008-2-3加上100天后的日期是多少。
*/
bool leapYear(int year){
if(year%400==0 || (year%4==0 and year%100!=0))//置闰法则【四年一闰;百年不闰,四百年再闰】
return true;
else return false;
}
int months[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
class date{
private:
int year;
int month;
int day;
public:
date(int y=0,int m=0, int d=0):year(y), month(m), day(d){}
int operator-(date& d);
friend date& operator+(date& a, int d);
int calcDay(){
//2005 2 1
int sum = 0;
for(int i=1;i<this->month;i++){
if(i==2 and leapYear(this->year)){
sum++;
}
sum+=months[i-1];
}
sum+=this->day;
return sum;
}
void show(){
cout<<this->year<<"年"<<this->month<<"月"<<this->day<<"日";
}
};
date& operator+(date& da, int d){
date a(da.year, da.month, da.day);
//+100
while(d){
if(leapYear(a.year) and a.month==2){
if(a.day==29){
a.month++;
a.day=1;
cout<<"29"<<endl;
}else{
a.day++;
}
}else{
if(a.day==months[a.month-1]){
if(a.month==12){
a.year++;
a.month=1;
a.day=1;
}else{
a.month++;
a.day=1;
}
}else{
a.day++;
}
}
d--;
// cout<<"a.day="<<a.day<<" || months["<<a.month<<"]"<<months[a.month-1]<<endl;
}
return a;
}
int date::operator-(date& d){////2002 9 2 2005 2 1
int days=0;
if(year>d.year) {
for(int i=d.year;i<this->year;i++){
if(leapYear(i)) days+=366;
else days+=365;
}
days-=d.calcDay();
days+=this->calcDay();
}else{//d.year>year
for(int i=this->year;i<d.year;i++){
if(leapYear(i)) days+=366;
else days+=365;
}
days-=this->calcDay();
days+=d.calcDay();
}
return days;
}
bool check(int year, int month, int day){
if(year<=0 or month <=0 or day<=0 or month>12)
return false;
if(leapYear(year) and month==2){
if(day>29)
return false;
}else{
if(months[month-1]<day)
return false;
}
return true;
}
int main()
{
cout<<"输入第一个日期(2008 2 3):";
int year, month, day;
cin>>year>>month>>day;
if(!check(year, month, day)){
cout<<"请输入有效日期"<<endl;
exit(0);
}
date d1(year, month, day);
cout<<"输入第一个日期(2008 3 3):";
cin>>year>>month>>day;
if(!check(year, month, day)){
cout<<"请输入有效日期"<<endl;
exit(0);
}
date d2(year, month, day);
d1.show();
cout<<" vs ";
d2.show();
cout<<"相隔"<<d1-d2<<"天"<<endl;
cout<<"计算某个日期加上100天后的日期是多少。请输入日期:"<<endl;
cin>>year>>month>>day;
if(!check(year, month, day)){
cout<<"请输入有效日期"<<endl;
exit(0);
}
date d3(year, month, day);
date d4 = d3+100;
d4.show();
return 0;
}
作者:无涯明月
上篇: 北理复试上机题2007年