北理工计算机复试上机 2008

1.存储一组姓名,如Apple,Tom,Green,Jack 要求能排序、按字母顺序插入、并显示。

 1 /**
 2 1.存储一组姓名,如Apple,Tom,Green,Jack
 3 要求能排序、按字母顺序插入、并显示。
 4 */
 5 #include<iostream>
 6 #include<string>
 7 #include<vector>
 8 #include<algorithm>
 9 
10 using namespace std;
11 bool ins(string a,string b){
12     return a<b;
13 }
14 
15 int main(){
16 vector<string> l;
17 cout<<"请输入姓名,00结束"<<endl;
18 string s;
19 vector<string>::iterator i;
20 while(cin>>s){
21     if(s=="00")break;
22     l.push_back(s);
23     sort(l.begin(),l.end(),ins);
24     for(i=l.begin();i!=l.end();i++){
25         cout<<(*i)<<endl;
26     }
27 }
28 
29 return 0;
30 }

2.输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,
最后将该文件的路径、该文件名及文件中的内容输出到屏幕

 1 // 2008_2.cpp : Defines the entry point for the console application.
 2 /**
 3 2.输入文件名及路径创建该文件,并把从键盘输入的内容保存到该文件,
 4 最后将该文件的路径、该文件名及文件中的内容输出到屏幕
 5 */
 6 #include<iostream>
 7 #include<string>
 8 #include<fstream>
 9 using namespace std;
10 int main(int argc, char* argv[])
11 {
12     string filename;
13     string filepath;
14     string content;
15     string sourcepath;
16     cout<<"请输入文件完整路径"<<endl;//F:\zz.txt
17     cin>>sourcepath;
18     cout<<"请输入要保持的内容,00结束"<<endl;
19     int p;
20 
21     for(int pos=0;pos<sourcepath.length();pos++){
22         if(sourcepath[pos]=='\')p=pos;
23     }
24     
25 
26     filename=sourcepath.substr(p+1);
27     filepath=sourcepath.substr(0,p-1);
28     ofstream out(sourcepath.c_str());
29     while(cin>>content){
30         if(content=="00")break;
31         out<<content<<endl;
32     }
33     cout<<"路径:"<<filepath<<endl;
34     cout<<"名称:"<<filename<<endl;
35     cout<<"内容:";
36     out.close();
37     ifstream in(sourcepath.c_str());
38     while(!in.eof()){
39         in>>content;
40         cout<<content<<" ";
41     }
42     cout<<endl;
43     in.close();
44     return 0;
45 }
3.设计捕获两种不同类型的异常,一个是被0除,另一个是数组越界(不会,抄的)
 1 // 2008_3.cpp : Defines the entry point for the console application.
 2 //
 3 /**
 4 3.设计捕获两种不同类型的异常,一个是被0除,另一个是数组越界
 5 */
 6 #include<iostream>
 7 #include<list>
 8 
 9 using namespace std;
10 
11 class A{};
12 class B{};
13 
14 int main(int argc, char* argv[])
15 {
16     int a,b,length;
17     cout<<"请输入除数和被除数"<<endl;
18     cin>>a>>b;
19     try{
20         if(b==0)throw A();
21         else cout<<"正常"<<a/b<<endl;
22         cout<<"请输入数组长度"<<endl;
23         cin>>length;
24         int i=1;
25         list<int> arr;
26 
27         while(cin>>a){
28             if(a==0)break;
29             arr.push_back(a);
30             i++;
31         }
32         if(i>length)throw B();
33         else {
34             list<int>::iterator x;
35             for(x=arr.begin();x!=arr.end();x++)cout<<(*x)<<endl;
36         }
37     }catch(A){
38         cout<<"/0"<<endl;
39     }catch(B){
40         cout<<"越界"<<endl;
41     }catch(...){
42         cout<<"未知错误"<<endl;
43     }
44     
45 
46     return 0;
47 }
原文地址:https://www.cnblogs.com/PPWEI/p/8460792.html