面向对象程序设计-实验2

news/2025/2/8 15:01:18 标签: 算法, c++, 数据结构

题目1

6-1 使用动态内存分配的冒泡排序

代码清单:

#include <iostream>

using namespace std;

int* bubble_sort(int n);/* 对长度为n的数组执行冒泡排序 */

int main()

{

    int n;

    cin >> n;

    int* a = bubble_sort(n);

    for (int i = 0; i < n; i++) {

        cout << a[i];

        if (i < n - 1)cout << " ";

    }

    cout << endl;

    return 0;

}

int* bubble_sort(int n)

{

int* r = new int[n];

for (int i = 0; i < n; i++)

{

cin >> r[i];

}

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n-i-1; j++)

{

if (r[j] > r[j + 1])

{

int temp = r[j];

 r[j]= r[j+1];

r[j + 1] = temp;

}

}

}

return r;

}

运行结果截图

题目2

7-1 用引用变量作为形参,实现3个整数由小到大输出

代码清单:

#include<iostream>

using namespace std;

void swap(int &a,int &b,int &c)

{

    int tamp;

    if(a>b)

    {

        tamp=a;

        a=b;

        b=tamp;

    }

    if(b>c)

    {

        tamp=b;

        b=c;

        c=tamp;

    }

    if(a>c)

    {

        tamp=a;

        a=c;

        c=tamp;

    }

        if(a>b)

    {

        tamp=a;

        a=b;

        b=tamp;

    }

}

int main()

{

    int a,b,c;

    cin>>a>>b>>c;

    swap(a,b,c);

    cout<<a<<" "<<b<<" "<<c<<endl;

    return 0;

}

运行结果截图

题目3

7-2 函数重载(数据类型不同)

代码清单:

#include<iostream>

using namespace std;

void px(int n,int a[])

{

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<n-i-1;j++)

        {

            int t;

            if(a[j]>a[j+1])

            {

                t=a[j];

                a[j]=a[j+1];

                a[j+1]=t;

            }

        }

    }

}

void px(int n,double b[])

{

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<n-i-1;j++)

        {

            double t;

            if(b[j]>b[j+1])

            {

                t=b[j];

                b[j]=b[j+1];

                b[j+1]=t;

            }

        }

    }

}

int main()

{

    int n;

    cin>>n;

    int a[n];

    for(int i=0;i<n;i++)

        cin>>a[i];

    double b[n];

    for(int i=0;i<n;i++)

        cin>>b[i];

    

    

    px(n,a);

    px(n,b);

    

    

    for(int i=0;i<n;i++)

    {

        cout<<a[i]<<" ";

    }

    cout<<endl;

    

    

    for(int i=0;i<n;i++)

        cout<<b[i]<<" ";

    return 0;

}

运行结果截图

题目4

7-3 字符统计

代码清单:

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    int n1=0,n2=0,n3=0,n4=0,n5=0;

    char arr[100];

    int i=0,n=0;

    scanf("%c",&arr[i]);

    while(arr[i]!='\n')

    {

        if(arr[i]>='0'&&arr[i]<='9')

            n1++;

        else if(arr[i]>='A'&&arr[i]<='Z')

            n2++;

        else if(arr[i]>='a'&&arr[i]<='z')

            n3++;

        else if(arr[i]==' ')

            n4++;

        else

            n5++;

        i++;

        scanf("%c",&arr[i]);

    }

    printf("n1=%d  n2=%d  n3=%d  n4=%d  n5=%d",n1,n2,n3,n4,n5);

        return 0;

    

}

运行结果截图

题目5

7-5 设计一个矩形类Rectangle并创建测试程序(C++)

代码清单:

#include<iostream>

using namespace std;

class Rectangle

{

    double width,height;

    public:

    Rectangle(double a=1,double b=1)

    {

        width=a;

        height=b;

    }

    double getArea()

    {

        return width*height;

    }

    double get()

    {

        return 2*(width+height);

    }

};

int main()

{  

    double w,h;

    cin>>w>>h;

    Rectangle c1(w,h);

    double zc;

    double mj;

    zc=c1.getArea();

    mj=c1.get();

    cout<<zc<<endl;

    cout<<mj;

    return 0;

}

运行结果截图


http://www.niftyadmin.cn/n/5844994.html

相关文章

1.31-子序列问题

Code-1.31-子序列问题 300. 最长递增子序列 题目分析 1. 状态表示 dp[i]表示&#xff1a;以i结尾的所有子序列中&#xff0c;最长递增子序列的长度。 2. 状态转移方程 dp[i] 长度为1 -> 1长度大于1 -> nums[j] < nums[i] -> max(dp[j] 1) 3. 初始化 把表…

『python爬虫』获取免费IP代理 搭建自己的ip代理池(保姆级图文)

目录 1. 环境搭建2. 获取爬虫ip3. 启动本地flask api接口服务4. 封装方法例子代码5. 自定义抓取免费ip的代理站规则6. 自定义规则示例总结欢迎关注 『python爬虫』 专栏,持续更新中 欢迎关注 『python爬虫』 专栏,持续更新中 1. 环境搭建 这边建议python3.7-3.11版本,redis …

数据加载器--不同文档数据格式的加载方法

文章目录 CSVHTMLJSONMarkdownPDF嵌入模型包装器 LangChain有很强的数据加载能力&#xff0c;而且它可以处理各种常见的数据格式&#xff0c;例如CSV、文件目录、HTML、JSON、Markdown及PDF等。下面&#xff0c;分别介绍这些不同的文档格式数据的加载方法。CSV逗号分隔值(Comma…

力扣.sql.1484.按日期分组销售产品

题目&#xff1a; 表 Activities&#xff1a; ---------------------- | 列名 | 类型 | ---------------------- | sell_date | date | | product | varchar | ---------------------- 该表没有主键(具有唯一值的列)。它可能包含重复项。 此表的每一行都…

jmeter逻辑控制器9

1&#xff0c;简单控制器2&#xff0c;录制控制器3&#xff0c;循环控制器4&#xff0c;随机控制器5&#xff0c;随机顺序控制器6&#xff0c;if控制器7&#xff0c;模块控制器8&#xff0c;Include控制器9&#xff0c;事物控制器本文永久更新地址: 1&#xff0c;简单控制器 不…

【创建模式-单例模式(Singleton Pattern)】

赐萧瑀 实现方案饿汉模式懒汉式&#xff08;非线程安全&#xff09;懒汉模式&#xff08;线程安全&#xff09;双重检查锁定静态内部类 攻击方式序列化攻击反射攻击 枚举(最佳实践)枚举是一种类 唐 李世民 疾风知劲草&#xff0c;板荡识诚臣。 勇夫安识义&#xff0c;智者必怀仁…

MQTT:物联网时代的数据桥梁

探秘PcVue系列&#xff1a;E8 MQTT&#xff1a;物联网时代的数据桥梁 什么是MQTT&#xff1f; MQTT&#xff08;Message Queuing Telemetry Transport&#xff09;是一种基于发布 / 订阅模式的轻量级消息传输协议&#xff0c;专为资源受限设备和低带宽、高延迟或不稳定的网络环…

openai库 及LangChain 跟ChatGPT对话的主要接口

在 OpenAI 的官方 Python 库 (openai) 中&#xff0c;提交提示词&#xff08;Prompt&#xff09;等内容给 ChatGPT 进行处理的主要函数/接口如下&#xff1a; 1️⃣ openai.ChatCompletion.create() 用于与 GPT-4 / GPT-3.5 Turbo 进行多轮对话交互&#xff08;适用于 ChatGPT…