考试 2022 5 20 代码

倒数第一

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long int ll;
#define f(i,a,n) for(int i=a;i<n;++i)
#define ff(i,a,n) for(int i=a;i<=n;++i)
#define fre freopen("1.txt","r",stdin)
int main(){
    multiset<char>q;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++ ){
        q.insert(s[i]);
    }
    for(auto z:q){
        cout<<z<<" ";
    }

return 0;
}

倒数第二


//class String {
//public:
//    String( const char *str = NULL );   /// 构造函数
//    String( const String &other );  /// 拷贝构造函数
//    String& operator = ( const String &other ); /// 赋值函数
//    void output( void );   /// 输出字符串
//    ~String( void );   /// 析构函数
//private:
//    int length;  /// 用于保存字符串长度
//    char *m_data; ///用于保存字符。注意:无需以'\0'作为字符串结束标志
//};

/*

   请在此处编写类代码

*/#include<iostream>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
typedef long long int ll;
#define f(i,a,n) for(int i=a;i<n;++i)
#define ff(i,a,n) for(int i=a;i<=n;++i)
#define fre freopen("1.txt","r",stdin)
class  String{
   char *data;int length;
public:

    String(){
        data = NULL;
        length = 0;
    }
    String(char a[]){/// 构造函数
        length = strlen(a);
        data=new char[length];

       for(int i=0; i<length;i++){
            if(a[i]=='\0'||a[i]=='\t'||a[i]=='\r')break;
             else   data [i]=a[i];
       }
    }
    ~String(){
        if(data != NULL)delete []data;
        data =NULL;
    }
    int size(){
        int siz=0;
        f(i,0,length)siz++;
        return siz;
    }
    void output(){/// 输出字符串
            for(int i=0; i<length;i++)cout<<data[i];
    }


     String( String & r){ // 拷贝构造函数
        length=r.length;
        if(data)delete[]data,data=NULL;
        data =  new char [r.length];
        for(int i=0;i<length;i++)data[i]=r.data[i];
    }

    String &operator= ( const String &b ) {/// 赋值函数
        if ( this != &b ) {  // 如果不是自己赋值给自己,如 a = a;
            if ( length > 0 ) {
                delete []data;
                data = NULL;
                length = 0;
            }

            length = b.length;

            if ( length > 0 ) {  // b不空
                data = new char[length];
                for ( int i = 0; i < length; i++ )
                    data[i] = b.data[i];
            }
        }

        return *this;
    }


String& operator + ( String& str ){
    char* s=new char[length + str.length-1] ;
    length=length + str.length;
    strcpy(s,data);
    strcat(s,str.data);
    delete[] data;

    data=s;
    return *this;

}



};


int main(){
    String s1( "apple" );  /// 创建一个字符串对象s1
    s1.output();
    printf( "\n" );

    String s2;  /// 创建一个字符串对象s2
    s2.output();  /// 输出s2。 由于s2是空的,所以没有输出任何内容
    printf( "\n" ); /// 只输出了一个空行

    s2 = s1; /// 对象赋值
    s2.output();
    printf( "\n" );

    String s3( s1 ); /// 创建一个字符串对象s3,测试拷贝构造函数
    s3.output();
    printf( "\n" );

    String s4( "pear" );  /// 创建一个字符串对象s1
    s2 = s4;  /// 对象赋值
    s2.output();
    printf( "\n" );

    return 0;
}

倒数第三,第一题 

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

class CWin    // 定义类 CWin
{
private:
    char id, *title;

public:
    CWin( char i = 'D', char *text = "Default window" ) : id( i )
    {
        title = new char[50];
        strcpy( title, text );
    }

    void set_data( char i, char *text )
    {
        id = i;
        strcpy( title, text );
     }

     CWin &operator =(CWin &win)      // 定义重载运算符「=」
    {
        id = win.id;
        strcpy ( this->title, win.title );
        return *this;
     }

    void show( void )
    {
        cout << "Window " << id << ": " << title << endl;
    }

    ~CWin()
    {
        delete [] title;
    }

    CWin (CWin &win)    // 拷贝构造函数
    {
        id = win.id;
        strcpy( title, win.title );
     }
};

int main( )
{
    CWin win1( 'A', "Main window" );
    CWin win2( 'B', "Big window" );
    CWin win3;

    win1.show();
    win2.show();
    win3.show();

    win1 = win2 = win3;   // 设定 win1 = win2 = win3
    win1.set_data( 'A', "Hello window" );     // 修改win1的内容

    cout << "设定 win1=win2=win3, 并更改win1的成员之后 ..." << endl;
    win1.show();
    win2.show();
    win3.show();

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值