北理工计算机复试上机 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

时间: 2024-08-02 08:34:28

北理工计算机复试上机 2008的相关文章

北理工计算机复试上机 2014

本人也是练习,如果有错误,欢迎指正[email protected],也可留言 1. 系统中有最近打开文件的记录,现用整数表示打开的文件名,且显示最近3个打开的文件,输出文件序列. 示例: 输入:1  输出:1 输入:2  输出:2,1 输入:3  输出:3,2,1 输入:4  输出:4,3,2 输入:1  输出:1,4,3 输入:4  输出:1,4,3 输入:3  输出:1,4,3 1 // 2014_1.cpp : Defines the entry point for the consol

北理工计算机复试上机 2013

1. 求两个数的最大公约数(似乎有个辗转相除法,为什么不用呢,没错,我不会) 示例: 输入:24,18 输出:6 1 // 2013_1.cpp : Defines the entry point for the console application. 2 // 3 #include<iostream> 4 using namespace std; 5 6 int main(int argc, char* argv[]) 7 { 8 int a,b; 9 cout<<"

北理工计算机复试上机 2012

1.输入10个数,从小到大排序 示例: 输入:1,2,5,7,9,10,45,67,24,26 输出:1,2,5,7,9,10,24,26,45,67 1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 int a[10]={0}; 6 int x=0; 7 while(x<10){ 8 cin>>a[x]; 9 x++; 10 } 11 12 for(int i=10;i>0;i--){ 13 i

北理工计算机复试上机 2011

1.输入一组单词(区分大小写),统计首字母相同的单词的个数,相同的单词不累加,输出格式:"字母,个数" 1 // 2011_1.cpp : Defines the entry point for the console application. 2 // 3 4 #include<iostream> 5 #include<vector> 6 #include<string> 7 using namespace std; 8 /** 9 1. 10 输

北理工计算机复试上机 2010

1.输入一串整数,输入命令!要求    1.输入a t在这串整数后添加整数t.    2.输入c \m \n用n替换m.    3.输入d t删除t    4.输入s 排序 1 /** 2 1.输入一串整数,输入命令! 3 要求 4 1.输入a t在这串整数后添加整数t. 5 2.输入c \m \n用n替换m. 6 3.输入d t删除t 7 4.输入s 排序 8 */ 9 #include<iostream> 10 #include<vector> 11 #include<a

北理工计算机复试上机 2009

1.请输入字符串,最多输入4个字符串,要求后输入的字符串排在前面,例如 输入:EricZ 输出:1=EricZ 输入:David 输出:1=David 2=EricZ 1 /** 2 1.请输入字符串,最多输入4个字符串,要求后输入的字符串排在前面,例如 3 输入:EricZ 4 输出:1=EricZ 5 输入:David 6 输出:1=David 2=EricZ 7 */ 8 #include<iostream> 9 #include<list> 10 #include<s

HDU 1234 (浙大计算机研究生复试上机考试-2005年) 开门人和关门人 (水)

开门人和关门人 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11133    Accepted Submission(s): 5667 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一

hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画

浙大计算机研究生复试上机考试-2010年 zoj问题

ZOJ问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2984 Accepted Submission(s): 906 Problem Description 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. zoj能AC: 2. 若字符串形式为xzojx,则也能AC,其中x可以是N