实验题集三:指针和引用 所有题

本文介绍了使用C++指针进行冒泡排序、数组查找、删除字符、字符串复制和实数排序的实例,展示了实践中的关键技术点。
摘要由CSDN通过智能技术生成

R7-1 冒泡

鸿鸿哥最近学习了指针,感觉这个知识点有点难以理解,于是想要通过编程实践来掌握它。鸿鸿哥以前学习数组(第7章)的时候已经掌握了冒泡排序的一般写法,现在他想用指针来实现排序的功能函数。但是他遇到了困难,你能帮帮他吗?

指针实现冒泡排序函数,函数名统一用void bubbleSort(int *p,int c)。
具体方法自己实现。

输入格式:

一组输入,第一行是待排数据个数n, 第二行是数据的具体值。

输出格式:

输出排序后的数,两个数之间以空格间开,最后一个数字末尾有空格

输入样例:

在这里给出一组输入。例如:

5
503 87 512 61 908

输出样例:

在这里给出相应的输出。例如:

61 87 503 512 908 

#include <iostream>
using namespace std;
void bubbleSort(int *p,int c)
{
    for(int i=0;i<c-1;i++)
    {
        for(int j=0;j<c-1-i;j++)
        {
            if(*(p+j)>*(p+j+1))
            {
                int t;
                t=*(p+j);
                *(p+j)=*(p+j+1);
                *(p+j+1)=t;
                
            }
        }
    }
}
int main()
{
    int *p,n,a[n];
    p=a;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    bubbleSort(p,n);
    for(int j=0;j<n;j++)
    {
        cout<<*(p+j)<<" ";
    }
    return 0;
}

R7-2 在数组中查找指定元素

输入一个正整数repeat (0<repeat<10),做repeat次下列运算:

输入一个正整数 n (1<n<=10),然后输入n个整数存入数组a中,再输入一个整数x,在数组a中查找x,如果找到则输出相应元素的最小下标,否则输出"Not found"。

要求定义并调用函数search(list, n, x),它的功能是在数组list中查找元素x,若找到则返回相应元素的最小下标,否则返回-1,函数形参 list 的类型是整型指针,形参n和x的类型是int,函数的类型是int。

输出格式语句:printf("index = %d\n", );

输入输出示例:括号内为说明,无需输入输出

输入样例:

2              (repeat=2) 
3              (n=3)
1 2 -6		
2              (x=2)
5              (n=5)
1 2 2 5 4
0              (x=0)

输出样例:

index = 1
Not found
#include <iostream>
using namespace std;
int search(int *list,int n,int x)
{
    for(int i=0;i<n;i++)
    {
        if(*(list+i)==x)
        {
            return i;
        }
    }
    return -1;
}
int main()
{
    int repeat,n,x,a[n];
    int *p=a;
    cin>>repeat;
    while(repeat--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        cin>>x;
        int b;
        b=search(p,n,x);
        if(b==-1)
        {
            cout<<"Not found"<<endl;
        }
        else
        {
            printf("index = %d\n",b);
        }
    }
    return 0;
}

R7-3 使用函数删除字符串中的字符

输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:

输入一个字符串 str,再输入一个字符 c,将字符串 str 中出现的所有字符 c 删除。

要求定义并调用函数delchar(str,c), 它的功能是将字符串 str 中出现的所有 c 字符删除,函数形参str的类型是字符指针,形参c的类型是char,函数类型是void。

输入输出示例:括号内为说明,无需输入输出

输入样例:

3               (repeat=3)
happy new year  (字符串"happy new year")
a               (待删除的字符'a')
bee             (字符串"bee")
e               (待删除的字符'e')
111211          (字符串"111211")
1               (待删除的字符'1')

输出样例:

result: hppy new yer    (字符串"happy new year"中的字符'a'都被删除)
result: b               (字符串"bee"中的字符'e'都被删除)
result: 2               (字符串"111211"中的字符'1'都被删除)

#include <iostream>
#include <string>
using namespace std;
void delchar(string &str,char c)
{
    while(str.find(c)!=-1)
    {
        str=str.erase(str.find(c),1);
    }
}
int main()
{
    int repeat;
    string str;
    char c;
    cin>>repeat;
    cin.get();
    while(repeat--)
    {
        getline(cin,str);
        cin>>c;
        delchar(str,c);
        cout<<"result: "<<str<<endl;
    }
    return 0;
}

R7-4 使用函数实现字符串复制

输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:

输入一个字符串 t 和一个正整数 m,将字符串 t 中从第 m 个字符开始的全部字符复制到字符串 s 中,再输出字符串 s。

要求定义并调用函数 strmcpy(s,t,m), 它的功能是将字符串 t 中从第 m 个字符开始的全部字符复制到字符串 s 中,函数形参s和t的类型是字符指针,形参m的类型是int,函数类型是void。

输入输出示例:括号内为说明,无需输入输出

输入样例:

3              (repeat=3)
happy new year
7
happy
1
new
4

输出样例:

new year        (从"happy new year"第7个字符开始组成的新字符串为"new year")
happy           (从"happy"第1个字符开始组成的新字符串为"happy")
error input     ("new"的长度小于4)
#include <iostream>
using namespace std;
#include <string>
void strmcpy(string &s,const string &t,int m)
{
    if(m>=0&&m<=t.length())
    {
        s=t.substr(m-1,t.length()-m+1);
    }
    else{
        s="";
    }
}
int main()
{
    int repeat,m;
    cin>>repeat;
    string t,s;
    while(repeat--)
    {        
        cin.get();
        getline(cin,t);        
        cin>>m;
        strmcpy(s,t,m);
        if(s=="") cout<<"error input"<<endl;
        else cout<<s<<endl;
    }
    return 0;
}

R7-5 找最大的字符串

输入5个字符串,输出其中最大的字符串。

输出格式: printf("Max is: %s\n", );

输入输出示例:括号内为说明,无需输入输出

输入样例:

peach 
pear
melon
orange
berry

输出样例:

Max is: pear
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char a[100];
    char Max[100]={0};
    for(int i=0;i<5;i++)
    {
        scanf("%s",a);
        if(strcmp(a,Max)>0)
        {
            strcpy(Max,a);
        }
    }
    printf("Max is: %s\n",Max);
    return 0;
}

R7-6 组织星期信息

输入一个正整数repeat (0<repeat<10),做repeat次下列运算:

定义一个指针数组将下面的星期信息组织起来,输入一个字符串,在表中查找,若存在,输出该字符串在表中的序号,否则输出-1。

Sunday Monday Tuesday Wednesday Thursday Friday Saturday

输入输出示例:括号内为说明,无需输入输出

输入样例 (repeat=3) :

3
Tuesday
Wednesday
year

输出样例:

3
4
-1

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int repeat;
    const string a[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    string b[10];
    cin>>repeat;
    cin.get();
    for(int i=0;i<repeat;i++)
    {
        int A=-1;
        cin>>b[i];
        for(int j=0;j<7;j++)
        {
            if(b[i]==a[j])
            {
                A=j+1;
                break;
            }
            
        }
        cout<<A<<endl;
        
    }
    return 0;
}

R7-7 查找奥运五环色的位置

奥运五环的5种颜色的英文单词按一定顺序排列{"red", "blue", "yellow", "green", "black" },定义指针数组并初始化,输入任意一个颜色的英文单词,从已有颜色中查找并输出该颜色的位置值,若没有找到,则输出"Not Found"。

输入格式:

输入一个代表颜色的单词。

输出格式:

输出单词对应的位置值,如果未找到,输出Not Found。

输入样例:

yellow

输出样例:

3

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    const char *a[5]={"red","blue","yellow","green","black"};
    char c[20];
    cin>>c;
    int i;
    for( i=0;i<5;i++)
    {
        if(strcmp(c,a[i])==0)
        {
            cout<<i+1<<endl;
            break;
        }
    }
        if(i==5)
        {
            cout<<"Not Found";
        }
    return 0;
}

R7-8 字符串的连接

本题要求编写程序,使用指针方式实现两个字符串的连接(不能使用strcat函数),并将连接后的字符串输出。

输入格式:

输入一行以回车结束的非空字符串(不超过40个字符),再输入一行以回车结束的非空字符串(不超过40个字符)。

输出格式:

一行输出俩字符串连接后新的字符串。

输入样例:

Beijing_
China

输出样例:

Beijing_China
#include <iostream>
using namespace std;
int main()
{
    string a,b;
    getline(cin,a);
    getline(cin,b);
    cout<<a+b<<endl;
    return 0;
}

R7-9 实数排序

本题要求编写程序,输入n个实数,使用指针引用的方式将它们按从大到小的顺序排列。

输入格式:

输入第一行给出一个正整数n(2≤n≤10),输入第二行给出n个实数,其间以空格分隔。

输出格式:

输出从大到小排好序的n个数(保留2位小数),每个数之间空一格,行末没有空格。

输入样例:

在这里给出一组输入。例如:

5
3.2 5.4 6.12 2.51 4.23

输出样例:

在这里给出相应的输出。例如:

6.12 5.40 4.23 3.20 2.51

#include <iostream>
#include <iomanip>
using namespace std;
void bigtosmall (double *p,int c)
{
     for(int i=0;i<c-1;i++)
     {
         for(int j=0;j<c-1-i;j++)
         {
             if(*(p+j)<*(p+j+1))
             {
                 double t;
                 t=*(p+j);
                 *(p+j)=*(p+j+1);
                 *(p+j+1)=t;
             }
         }
     }
}
int main()
{
    int n;
    double a[10];
    cin>>n;
    for(int i=0;i<n;i++)
    {
         cin>>a[i];
    }
    bigtosmall(a,n);
    for(int i=0;i<n;i++)
    {
        cout<<fixed<<setprecision(2)<<*(a+i);
        if(i<n-1)
        {
            cout<<" ";
        }
    }
    cout<<endl;
    return 0;
} 

  • 34
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值