• 综合练习题【C++】


    前言

    以下内容源自pta
    仅供学习交流使用
    请您阅读文章声明,默认同意该声明

    综合练习题

    判断题

    R1-1 F
    预定义的插入符从键盘上接收数据是不带缓冲区的。

    R1-2- F
    函数模板的友元函数必须是模板函数。

    R1-3- T
    重载函数可以带有默认值参数,但是要注意二义性。

    R1-4 F
    对每个可重载的运算符来讲,它既可以重载为友元函数,又可以重载为成员函数,还可以重载为非成员函数。

    R1-5 T
    在protected保护继承中,对于垂直访问等同于公有继承,对于水平访问等同于私有继承。

    R1-6 T
    操纵符本身是一个对象,它可以直接被提取符或插入符操作。

    R1-7 T
    使用打开文件函数open()之前,需要定义一个流类对象,使用open()函数来操作该对象。(。

    R1-8- T
    将构造函数说明为纯虚函数是没有意义的。

    R1-9 T
    预定义的提取符和插入符是可以重载的。

    R1-10- F
    记录流的当前格式化状态标志字中的每一位用于记录一种格式,这种格式是不能被设置或清除的。

    单选题

    R2-1 D
    所有类都应该有:
    A.析构函数
    B.构造函数
    C.以上答案都不对
    D.构造函数和析构函数

    R2-2 A
    关于delete运算符的下列描述中,()是错误的。
    A.对一个指针可以使用多次该运算符;
    B.使用它删除对象时要调用析构函数;
    C.它必须用于new返回的指针;
    D.指针名前只有一对方括号符号,不管所删除数组的维数。

    R2-3 A
    下面说法正确的是()。
    A.内联函数在编译时是将该函数的目标代码插入每个调用该函数的地方
    B.内联函数在运行时是将该函数的目标代码插入每个调用该函数的地方
    C.类的内联函数必须在类体外通过加关键字inline定义
    D.类的内联函数必须在类体内定义

    R2-4 B
    对定义重载函数的下列要求中,( )是错误的。
    A.要求参数个数相同时,参数类型不同
    B.要求函数的返回值不同
    C.要求参数的个数不同
    D.要求参数中至少有一个类型不同

    R2-5- A
    命名空间应用于:
    A.避免各个不同函数、变量等的名称冲突
    B.以上答案都正确
    C.在类外定义类的成员函数
    D.提高代码的执行速度

    R2-6- C
    在派生类对基类继承的传递性中,__是错误的。
    A.不管是私有继承还是公有继承,基类中的私有成员在派生类的作用域内都是不可能见的。
    B.在公有继承方式下,间接派生类对象可以直接调用基类中的公有成员函数,去访问基类的私有数据成员
    C.在私有继承方式下,间接派生类对象可以直接调用基类中的公有成员函数,去访问基类的私有数据成员
    D.在公有继承方式下,直接派生类对象可以直接调用基类中的公有成员函数,去访问基类的私有数据成员

    R2-7- D
    在以下哪种情形,复制构造函数会被调用。
    A.以上答案都不对
    B.当一个对象赋值给另一个对象
    C.当一个对象采用引用方式,作为参数传递给一个函数
    D.当一个函数采用值方式,返回一个对象

    R2-8 A
    在C++语言中设置虚基类的目的是( ) 。
    A.解决多继承造成的二义性问题
    B.缩短程序的目标代码
    C.简化程序代码
    D.提高程序的运行效率

    R2-9 D
    cout 是由I/O 流库预定义的( )。
    A.包含文件
    B.类
    C.常量
    D.对象

    R2-10 D
    设int x;,则经过()后,语句*px=0;可将x值置为0。
    A.const int * px=&x;
    B.int * px;
    C.int const * px=&x;
    D.int * const px=&x;

    R2-11 B
    关于纯虚函数和抽象类的描述中,( )是错误的。
    A.抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出
    B.一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类
    C.纯虚函数是一种特殊的虚函数,它没有具体的实现
    D.抽象类是指具有纯虚函数的类

    R2-12 A
    对于类之间的友元关系:
    A.以上答案都不对。
    B.如果类A是类B的友元,则B也是A的友元。
    C.如果类A是类B的友元,则B的成员函数可以访问A的私有成员
    D.如果类A是类B的友元,并且类B是类C的友元,则类A也是类C的友元。

    R2-13 C
    关于动态绑定的下列描述中,( )是错误的。
    A.动态绑定在运行时确定所调用的函数代码
    B.动态绑定调用函数操作是通过指向对象的指针或对象引用来实现的
    C.动态绑定是在编译时确定操作函数的
    D.动态绑定是以虚函数为基础的

    R2-14 D
    在C++中,关于下列设置缺省参数值的描述中,()是正确的。
    A.只能在函数的定义性声明中指定参数的缺省值;
    B.不允许设置缺省参数值;
    C.设置缺省参数值时,必须全部都设置;
    D.在指定了缺省值的参数右边,不能出现没有指定缺省值的参数;

    R2-15- D
    在C++中,函数原型不能标识()。
    A.函数参数类型
    B.函数参数的个数
    C.函数的返回类型
    D.函数的功能

    填空题

    R4-1
    The name of an operator function is the keyword operator
    followed by the operator itself, for example, operator<<. An operator function is declared and can be called like any other function. A use of the operator is only a shorthand for an object call of the operator function.

    R4-2
    A constructor’s job is to initialize an object of its class.

    R4-3
    A resource is anything we need to acquire and eventually (explicitly or
    directly) close once we are finished with it. Examples of resources are memory, locks, file handles, and thread handles.

    R4-4
    A binary operator can be defined by either a non-static member function taking one argument or a nonmember function taking two arguments.

    程序填空题

    R5-1
    打印餐厅菜单
    阅读下列说明和 C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
    【说明】某饭店在不同的时段提供多种不同的餐饮,其菜单的结构图如图1所示。
    在这里插入图片描述

    现在采用组合(Composition)模式来构造该饭店的菜单,使得饭店可以方便地在其中增加新的餐饮形式,得到如图2所示的类图。

    在这里插入图片描述

    其中MenuComponent为抽象类,定义了添加(add)新菜单和打印饭店所有菜单信息(print)的方法接口。类Menu表示饭店提供的每种餐饮形式的菜单,如煎饼屋菜单、咖啡屋菜单等。每种菜单中都可以添加子菜单,例如图1中的甜点菜单。类MenuItem表示菜单中的菜式。

    #include <iostream>
    #include <list>
    #include <string>
    using namespace std;
    class MenuComponent{
    protected:  string  name;
    public :
        MenuComponent (string name){this->name=name;}
    	string getName()  {  return name;}  
        void add(MenuComponent*  menuComponent){};                        //添加新菜单
        virtual void print( )  = 0;                //打印新菜单
    };
    class Menultem : public MenuComponent{
    private:  double price;
    public :
        Menultem (string name, double price) : MenuComponent (name) { this->price = price; }
        double getPrice( ){  return price;  }
        void add(MenuComponent* menuComponent) { return ; }  //添加新菜单
        void print( )  { cout << "   " << getName( ) << ", " << getPrice ( )<< endl; }
    };
    class Menu: public MenuComponent {
    private: list<MenuComponent*> menuComponents;
    public :  
        Menu (string name): MenuComponent (name){ }
        void add(MenuComponent*  menuComponent)  //添加新菜单
        {  
    	 	menuComponents.push_back(menuComponent);
      	}
        void print( ){
            cout << "\n" << getName( ) << "\n----------"<<endl;
            std::list<MenuComponent  *>::iterator iter;
            for (iter = menuComponents.begin( ); iter!= menuComponents.end( ); iter++)
                (*iter)->print( );
        }
    };
    int main( ){
        MenuComponent*  allMenus=new Menu("ALL MENUS");
        MenuComponent*  dinerMenu=new Menu("DINER MENU");
        //......创建更多的Menu对象,此处代码省略
        allMenus->add(dinerMenu);    //将dinerMenu添加到餐厅菜单中
        //......为餐厅增加更多的菜单,此处代码省略
            
      	allMenus->print( );    // 打印饭店所有菜单的信息
           return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    R5-2
    文件目录树
    阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

    【说明】现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下所示
    在这里插入图片描述

    【C++代码】

    #include <list>
    #include <iostream>
    #include <string>
    using namespace std; 
    
    class AbstractFile {
    protected :    
       string name;  // 文件或目录名称
    public:
       void printName(){cout << name;}  // 打印文件或目录名称
       virtual void addChild(AbstractFile  *file)=0;   // 给一个目录增加子目录或文件
       virtual void removeChild(AbstractFile *file)=0;// 删除一个目录的子目录或文件
       virtual list<AbstractFile*> *getChildren()=0;// 获得一个目录的子目录或文件
    };
    class File : public AbstractFile {
    public :
       File(string name) { this->name = name; }
       void addChild(AbstractFile  *file) { return ; }
       void removeChild(AbstractFile *file) { return ; }
       list<AbstractFile*> * getChildren() { return NULL ; }
    };
    class Folder :public AbstractFile {
    private :
       list <AbstractFile*> childList;  // 存储子目录或文件
    public :
       Folder(string name) { this->name = name; }
       void addChild(AbstractFile *file) { childList.push_back(file); }
       void removeChild(AbstractFile *file) { childList.remove(file);}
       list<AbstractFile*> *getChildren() { return  &childList; }
    };
    
    int main( ) {
       // 构造一个树形的文件/目录结构
       AbstractFile *rootFolder = new Folder("c:\\");
       AbstractFile *compositeFolder = new Folder("composite");
       AbstractFile *windowsFolder = new Folder("windows");
       AbstractFile *file = new File("TestComposite.java");
       rootFolder->addChild(compositeFolder);
       rootFolder->addChild(windowsFolder);
       compositeFolder->addChild(file);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    函数题

    R6-1 Max函数模板

    R6-1 Max函数模板
    创建一个函数模板Max( ),该模板返回两个参数中的最大值,例如:
    
    Max(3,4)返回4
    Max('c','a')返回'c'
    裁判测试程序样例:
    #include <iostream>
    #include <string>
    using namespace std;
    //你提交的代码将被嵌入到这里
    
    
     int main( )
    {
      int m,n;
      char c,d;
      string s1,s2;
      cin>>m>>n;
      cin>>c>>d;
      cin>>s1>>s2;
      cout<<Max(m,n)<<endl;
      cout<<Max(c,d)<<endl;
      cout<<Max(s1,s2)<<endl;
        return 0 ;
    }
    
    
    
    /* 请在这里填写答案 */
    输入样例:
    在这里填写一组输入
    -21252 3
    输出样例:
    在这里填写相应的输出
    3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    template<typename T>  
    T Max(T t1,T t2){
        if(t1>t2){
            return t1;
        }else{
            return t2;
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    R6-2 求平面几何形状的面积(圆形和矩形)

    R6-2 求平面几何形状的面积(圆形和矩形)C++
    分数 10
    作者 张德慧
    单位 浙江大学
    下列shape类是一个表示形状的基类,该类有一个计算形状面积的方法area( )。请以形状类"Shape"为基类派生出圆形类"Circle"和矩形类"Rectangle",要求保持求面积的接口不变。从键盘输入圆形的半径和矩形的长和宽,在主函数中创建一个圆形类"Circle"的对象和一个矩形类"Rectangle"的对象,计算并输出这个圆形和矩形的面积。圆周率PI=3.14
    
    class shape{
    
    public:
    
    double area( ){ }
    
    };
    
    圆形类名:Circle   注意:圆周率PI=3.14
    矩形类名:Rectangle
    裁判测试程序样例:
    #include <iostream>
    using namespace std;
    class Shape{
    public:
        double area ( ){ return -1; }
    };
    //你提交的代码将嵌入到这里。 
     
    int main()
    {
        double r,h,w;
        cin>>r>>h>>w;
        Circle c(r); Rectangle rect(h,w);
     
        cout<<"The area of the Circle is "<<c.area( )<<endl;
        cout<<"The area of the Rectangle is "<<rect.area( )<<endl;
     
        return 0;
    }
    输入样例:
    8.8
    16.8  9.8
    输出样例:
    The area of the Circle is 243.162
    The area of the Rectangle is 164.64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    const double PI=3.14;
    class Circle:public Shape{
        
    public:
        Circle(double a){
            r=a;
        }
        double area ( ){ 
            return PI*r*r;
        }
    private:
        double r;
    };
    
    class Rectangle:public Shape{
        
    public:
        Rectangle(double a,double b){
            h=a;
            w=b;
        }
        double area ( ){ 
            return h*w;
        }
    private:
        double h;
        double w;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    R6-3 将学生对象按照成绩降序排序

    R6-3 将学生对象按照成绩降序排序
    分数 10
    作者 张德慧
    单位 西安邮电大学
    请阅读程序并补全源代码:先从键盘录入5个学生的数据,保存到向量vector容器对象v中,然后按照成绩score从高到低排序之后输出。
    
    裁判测试程序样例:
    本程序将下面的学生类Student对象按照成绩score从高到低排序:
    
        #include <iostream>
        #include <algorithm> 
        #include <vector>
        #include <string>
        using namespace std;
        class Student {
            string number;
            string name;
            float score;
        public:
            // Constructor
            Student(string number1, string name1, float score1) {
                number = number1;
                name = name1;
                score = score1;
            }
            float get_score(){
                return score;
            }
        
            // Used to print student details in main()
             void print() {
                cout<<number << " " <<name << " " << score<<endl;
            }
    };
        bool cmp_score(Student s1,Student s2);//比较成绩的函数 ,需要考生实现该函数
        int main() 
        {
            vector<Student> v;
    
    /* 请在这里填写答案 */
    输入样例:
    在这里给出一组输入。例如:
    
    04031021 Zhang3 84
    04031013 Li4    73
    04031018 Wang5  98
    04031038 Ma6    65
    04031029 Chen7  96
    输出样例:
    在这里给出相应的输出。例如:
    
    04031018 Wang5 98
    04031029 Chen7 96
    04031021 Zhang3 84
    04031013 Li4 73
    04031038 Ma6 65
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    	for(int i=0;i<5;i++){
    	        string number1;
    	        string name1;
    	        float score1;
    	        cin>>number1>>name1>>score1;
    	        Student s(number1, name1, score1);
    	        v.push_back(s);
    		}
    		sort(v.begin(),v.end(), cmp_score);
    		vector<Student>::iterator iter=v.begin();
    		while(iter!=v.end()){
    			
    			(*iter).print();
    			iter++;
    		}
    		return 0;
        
        }
    bool cmp_score(Student s1,Student s2){
    	if(s1.get_score()>s2.get_score()){	 
    	 		return true;
    	}else{
    	 	return false; 
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    R6-4 对学生对象按照成绩降序排序

    R6-4 对学生对象按照成绩降序排序
    下面这段代码要实现对学生对象按照成绩降序排序。 仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。
    
    裁判测试程序样例:
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    
    class Student {
       string name;
       char sex;
       int score;
       string grade;
    
    public:
       Student(string name, char sex, int score, string grade);
       friend ostream &operator<< (ostream& os, Student st) ;
       friend bool operator<(Student &st1, Student &st2);    
    };
    //你提交的代码将被嵌入到这里
    
    Student::Student(string name, char sex, int score, string grade) {
       this->name = name;
       this->sex = sex;
       this->score = score;
       this->grade = grade;
    }
    
    int main() {
       list<Student> st;
       string name, grade;
       char sex;      int score;
    
       for(int i = 0; i < 5; i++) {
          cin>>name;      cin>>sex;
          cin>>score;       cin>>grade;
          st.push_back(Student(name, sex, score, grade));
       }
    
       st.sort();
    
       list<Student>::iterator p = st.begin();
       while(p != st.end()) {
          cout<<*p;
          p++;
       }
       return 0;
    }
    输入样例:
    在这里给出一组输入。例如:
    
    Bill M 86 JK1501
    David M 98 JK1502
    Mary F 78 JK1503
    Adam M 83 JK1504
    Rose F 96 JK1505
    输出样例:
    在这里给出相应的输出。例如:
    
    David M 98 JK1502
    Rose F 96 JK1505
    Bill M 86 JK1501
    Adam M 83 JK1504
    Mary F 78 JK1503
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    ostream &operator<< (ostream& os, Student st) {
        os<<st.name<<" "<<st.sex<<" "<<st.score<<" "<<st.grade<<endl;
        return os;
    }
    bool operator<(Student &st1, Student &st2){
        if(st1.score>st2.score){
            return true;
        }else{
            return false;
        }
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    R6-5 求正9边形的面积和周长

    R6-5 求正9边形的面积和周长
    求正9边形的面积和周长
    
    在一个正n边形(Regular Polygon)中,所有边的边长都相等,且所有角的度数相同(即这个多边形是等边、等角的)。我们已经从下列抽象类shape实现了一个正n边形类RegularPolygon。其构造方法为:RegularPolygon(int n,double side); 其中n为边数,side为边长。
    
    从键盘输入正9边形的边长s,请补充下列程序计算该正9边形的面积和周长。
    
    class shape {// 抽象类
    public:
     virtual double getArea()=0;// 求面积
     virtual double getPerimeter()=0; // 求周长
    };
    裁判测试程序样例:
    #include <iostream>
    #include <cmath> 
    using namespace std;
    
    class shape {// 抽象类
    public:
     virtual double getArea()=0;// 求面积
     virtual double getPerimeter()=0; // 求周长
    };
    //这里实现了正n边形类RegularPolygon,其构造函数为:RegularPolygon(int n,double side);
    
    int main()
    {
        double s;
        cin>>s;
        
    /* 请在这里填写答案 */
    
        return 0;
    }
    输入样例:
    在这里给出一组输入。例如:
    
    6.1828
    输出样例:
    在这里给出相应的输出。例如:
    
    236.313
    55.6452
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    RegularPolygon rr(9,s);
    cout<<rr.getArea()<<endl;
    cout<<rr.getPerimeter()<<endl;
    
    • 1
    • 2
    • 3

    编程题

    R7-1 求平方根函数mySqrt的异常处理

    R7-1 求平方根函数mySqrt的异常处理
    分数 10
    作者 张德慧
    单位 西安邮电大学
    改造下面的求平方根函数mySqrt,当x小于0时,输出错误信息:"Error: Can not take sqrt of negative number";当x不小于0时,输出x的平方根。要求在main函数中采用C++的异常处理方法。
    
    double mySqrt(double x)
    {
    return sqrt(x);
    }
    
    输入格式:
    4
    
    输出格式:
    The sqrt of 4 is 2
    
    输入样例:
    -9
    输出样例:
    Error: Can not take sqrt of negative number
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    #include<iostream>
    #include<cmath>
    using namespace std;
    double mySqrt(double x)
    {
         if(x<0)
             throw ("Error: Can not take sqrt of negative number");
        
         return sqrt(x);
    
        
    }
    int main(){
        double x;
        cin>>x;
        try{
            double y=mySqrt(x);
             cout<<"The sqrt of "<<x<<" is "<<y;
        }catch(const char* msg){
            cout<<msg;
        }
       
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    R7-2 分离目录路径和文件名

    R7-2 分离目录路径和文件名
    分数 10
    作者 张德慧
    单位 西安邮电大学
    输入文件目录路径和文件名,要求分离成目录路径和文件名分别输出
    
    输入格式:
    例如:输入
    
    c:\windows\winhelp.exe
    
    输出格式:
    c:\windows (目录路径)
    
    winhelp.exe (文件名)
    
    输入样例:
    /usr/bin/man
    输出样例:
    /usr/bin
    man
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    #include <iostream>
    #include<string>
    using namespace std;
    
    void filename(const string & str)
    {
        size_t found=str.find_last_of("/\\");//Point
        cout<<str.substr(0,found)<<endl;
        cout<<str.substr(found+1)<<endl;
    }
    int main()
    {
        string str1;
        getline(cin,str1);
        filename(str1);
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    R7-3 电话号码同步

    R7-3 电话号码同步
    文件phonebook1.txt和phonebook2.txt中有若干联系人的姓名和电话号码。请你设计一个程序,将这两个文件中的电话号码同步。(所谓同步,就是将两个文件中的电话号码合并后剔除相同的人名和电话号码。请将同步后的电话号码按照姓名拼音顺序排序后保存到文件phonebook3.txt中。)
    
    由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的单词为end时,表示文件结束。将同步后的电话号码按照姓名拼音顺序排序后输出。
    
    输入格式:
    张三 13012345678
    
    李四 13112340000
    
    王五 13212341111
    
    马六 13312342222
    
    陈七 13412343333
    
    孙悟空 13512345555
    
    end (表示文件phonebook1.txt结束)
    张三 13012345678
    
    孙悟空 13512345555
    
    王五 13212341111
    
    陈七 13412343333
    
    唐三藏 13612346666
    
    猪悟能 13712347777
    
    沙悟净 13812348888
    
    end (表示文件phonebook2.txt结束)
    
    输出格式:
    陈七 13412343333
    
    李四 13112340000
    
    马六 13312342222
    
    沙悟净 13812348888
    
    孙悟空 13512345555
    
    唐三藏 13612346666
    
    王五 13212341111
    
    张三 13012345678
    
    猪悟能 13712347777
    
    输入样例:
    Zhang3 13012345678
    Li4 13112340000
    Wang5 13212341111
    Ma6 13312342222
    Chen7 13412343333
    SunWuKong 13512345555
    end
    Zhang3 13012345678
    SunWuKong 13512345555
    Wang5 13212341111
    Chen7 13412343333
    TangSanZang 13612346666
    ZhuWuneng 13712347777
    ShaWuJing 13812348888
    end
    输出样例:
    Chen7 13412343333
    Li4 13112340000
    Ma6 13312342222
    ShaWuJing 13812348888
    SunWuKong 13512345555
    TangSanZang 13612346666
    Wang5 13212341111
    Zhang3 13012345678
    ZhuWuneng 13712347777
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    #include<iostream>
    #include<set>
    #include<string>
    #include<vector>
    using namespace std;
    int main()
    {
    	set <string> A;
    	string a;
    	
    	for(int i=0;i<2;i++)
    	{
    		getline(cin,a);
    		while(a!="end")
    		{
    			A.insert(a);
    			getline(cin,a);
    		}
    	}
    	set <string>::iterator p=A.begin();
    	while(p!=A.end())
    	{
    		cout<<*p++<<endl;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    最后

    请您阅读文章声明,默认同意该声明
    打赏通道
    请添加图片描述

  • 相关阅读:
    oracle -- 表操作
    Java字节码学习笔记(一):Java字节码是什么?
    Python变量与常量
    【无标题】
    leetcode:650. 只有两个键的键盘【二维dp】
    3DTiles 1.0 数据规范详解[4.1] b3dm瓦片二进制数据文件结构
    webpack知识点整理
    java计算机毕业设计基于springboo+vue的人事管理系统
    ip地址范围不是整体范围的子集
    OpenRoads导出FBX体积为0kb问题解决
  • 原文地址:https://blog.csdn.net/qq_51625007/article/details/125485590