[自用3.]C++双色球系统练习题

1.实现含有中文字符的字符串逆转,如: “我是小萌新转换成“新萌小是我

#include <iostream>
#include <string>

using namespace std;

int main(){
    string line = "wsxmx";
    int len = line.length();
    for(int i=0;i<len;i++){
        cout<<line[len-i-1];
    }
    cout<<endl;

    system("pause");
    return 0;

}

2.有一个整形数组, a[3] = {7,2,5}, 要求使用指针实现数组成员由小到大的顺序排列,即结果为:a[3] = {2,5,7};

#include <iostream>
#include <string>

using namespace std;

#define num 3

void paixu(int a[num]);//排序函数声明

int main(){
    int a[num]={7,2,5};
    for(int i=0;i<num;i++){//打印原数组
        cout<<a[i]<<"\t";
    }
    cout<<endl;
    paixu(a);//排序
    for(int i=0;i<num;i++){//打印排序后数据
        cout<<a[i]<<"\t";
    }
    cout<<endl;

}

void paixu(int a[num]){
    //冒泡排序
    for(int p=0;p<num;p++){
        for(int i=num-1;i>=0;i--){
            if(a[i]<a[i-1]){
                int temp=a[i];
                a[i]=a[i-1];
                a[i-1]=temp;
            }
        }
    }
}

3.实现一个函数,函数完成如下功能:

1). 函数的输入为一个数组,数组的成员个数不定(即:可能为 0 个,也可能为多个)
2). 函数找到成员的最大元素和最小元素,并能让函数的调用者知道最大元素和最小元素
是哪一个
#include <iostream>
#include <string>

using namespace std;

bool MaxMin(int a[],int len,int *max,int *min);

int main(){
    int a[10]={23,41,35,2,45,68,32,15,75,70};
    int max=0,min=a[0];
    MaxMin(a,10,&max,&min);
    cout<<"min:"<<min<<endl;
    cout<<"max:"<<max<<endl;

    system("pause");
    return 0;

}

bool MaxMin(int a[],int len,int *max,int *min){//*min是指指针变量,表示存储着一个整型变量的地址
    if(len<1) return false;
    for(int i=0;i<len;i++){
        if(a[i]<*min){//*min是取min这个地址里的值
            *min=a[i];
        }else if(a[i]>*max){
            *max=a[i];
        }
    }
    return true;
}

 其中,函数定义中的*min是指定义了一个指针变量,在上面这段代码中是int *min代表这是一个指向整型变量的指针,也就是说其中保存的是一个整型变量的地址,也就是main函数中min这个变量的地址。

在函数内部的*min代表着,取这个地址中的值,也就是说MaxMin函数中min是一个地址,*min就是取这个地址中的值,也就是main函数中min 整型变量的值。

main函数中和MaxMin函数中的min是不一样的,main函数中的min是一个整型变量,MaxMin函数中,min是一个指向整型变量的指针。

4. 实现一个函数,使用指针连接两个字符串。 函数输入: 两个源字符串的指针,目的字符串的指针

#include <iostream>
#include <string>

using namespace std;

//string PingJie(string a,string b,string c);//拼接函数声明
int PingJie(char a[],char b[],char c[],int alen,int blen);

int main(){
    //string a="Hello!";
    char a[]={'H','e','l','l','o','!'};
    int alen=size(a);
    //string b="World!";
    char b[]={'W','o','r','l','d','!'};
    int blen=size(b);
    //string c;
    char c[alen+blen];

    //char c[alen+blen]={0};
    PingJie(a,b,c,alen,blen);
    cout<<"================"<<endl;
    for(int i=0;i<alen+blen;i++){
        cout<<c[i];
    }
    cout<<endl;

    system("pause");
    return 0;

}

// string PingJie(string a,string b,string c){
//     int alen=a.length();
//     int blen=b.length();
//     for(int i=0;i<alen;i++){
//         c[i]=a[i];
//     }
//     for(int i=0;i<blen;i++){
//         c[i+alen]=b[i];
//     }
//     return c;
// }

int PingJie(char a[],char b[],char c[],int alen,int blen){
    for(int i=0;i<alen;i++){
        c[i]=a[i];
    }
    for(int i=0;i<blen;i++){
        c[i+alen]=b[i];//将b的字符接在a字符串的后面
    }

    return 0;
}

5.

编写一个程序,初始化一个 double 类型的数组,然后把该数组的内容 拷贝至 3 个其他数组
中(在 main() 中声明这 4 个数组)。使用带数组表示法的 函数进行第 1 份拷贝。使用带指
针表示法和指针递增的函数进行第 2 份拷贝。 把目标数组名、源数组名和待拷贝的元素个
数作为前两个函数的参数。第 3 个函数以目标数组名、源数组名和指向源数组最后一个元
素后面的元素的指 针。也就是说,给定以下声明,则函数调用如下所示:
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);
#include <iostream>
#include <string>

using namespace std;

// 编写一个程序,初始化一个 double 类型的数组,然后把该数组的内容 拷贝至 3 个其他数组
// 中(在 main()中声明这 4 个数组)。使用带数组表示法的 函数进行第 1 份拷贝。使用带指
// 针表示法和指针递增的函数进行第 2 份拷贝。 把目标数组名、源数组名和待拷贝的元素个
// 数作为前两个函数的参数。第 3 个函数以目标数组名、源数组名和指向源数组最后一个元
// 素后面的元素的指 针。也就是说,给定以下声明,则函数调用如下所示:
// double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
// double target1[5];
// double target2[5];
// double target3[5];
// copy_arr(target1, source, 5);
// copy_ptr(target2, source, 5);
// copy_ptrs(target3, source, source + 5);

void copy_arr(double target[],double source[],int len);
void copy_ptr(double *target,double *source,int len);
void copy_ptrs(double *target,double *source,double *sourceback);

int main(){
    double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    double target3[5];
    copy_arr(target1, source, 5);
    cout<<"1."<<endl;
    for(int i=0;i<5;i++){
        cout<<target1[i]<<"\t";
    }
    cout<<endl;
    copy_ptr(target2, source, 5);
    cout<<"2."<<endl;
    for(int i=0;i<5;i++){
        cout<<target1[i]<<"\t";
    }
    cout<<endl;
    copy_ptrs(target3, source, source + 5);
    cout<<"3."<<endl;
    for(int i=0;i<5;i++){
        cout<<target1[i]<<"\t";
    }
    cout<<endl;

    system("pause");
    return 0;

}

void copy_arr(double target[],double source[],int len){
    for(int i=0;i<len;i++){
        target[i]=source[i];
    }
}

void copy_ptr(double *target,double *source,int len){
    for(int i=0;i<len;i++){
        // *target=source[i];
        // target+=sizeof(source[0]);
        *target=*source;
        target++;
        source++;
    }
}

void copy_ptrs(double *target,double *source,double *sourceback){
    //int i=0;
    do{
        // *target=source[i];
        // target+=sizeof(source[0]);
        // i++;
        *target=*source;
        target++;
        source++;
    }while(source<sourceback);//应该使用source数据的指针与source结束的指针进行比较
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值