C++习题06_模板

C++习题06_模板

习题06(01)函数模板

题目描述
编写一个函数模板,求数组中的最大元素,并写出调用此函数模板的主函数,使得函数调用时,数组的类型可以是int型、double型和string类型。数组中元素个数3≤n≤20
主函数中,先从键盘输入各种类型数组的长度,再输入数组元素的值,调用函数求出最大值,再输出。 
输入描述
输入共分6行
int型数组元素的个数
int型数组元素的值
double型数组元素的个数
double型数组元素的值
string类型数组元素的个数
string数组元素的值 
输出描述
三行
int型数组中元素的最大值
double型数组中元素的最大值
string型数组中元素的最大值 
输入样例
5
78 96 -12 52 856
6
3.2 5.6 89.2 -3.2 46.2 63.47
5
hello world example virtual char 
输出样例
856
89.2
world
#include <iostream>
#include <string>
using namespace std;

template <typename T>
T Max(T *a, int n)
{
    int j = 0;
    //T maxnum = { 0 };//这里十分重要,如果是0,string 就不能用了
    //maxnum = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > a[j])
            j = i; //找到最大值的下标
    }
    return a[j];
}

int main()
{
    int n1, n2, n3;
    int a[20];
    double b[20];
    string c[20];
    cin >> n1;
    for (int i = 0; i < n1; i++)
        cin >> a[i];
    cin >> n2;
    for (int i = 0; i < n2; i++)
        cin >> b[i];
    cin >> n3;
    for (int i = 0; i < n3; i++)
        cin >> c[i];
    cout << Max(a, n1) << endl;
    cout << Max(b, n2) << endl;
    cout << Max(c, n3) << endl;
    return 0;
}

习题06(02)类与对象及静态数据成员

题目描述
某商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时均以重量为单位,各箱的重量不一样,因此,商店需要记录下库存货物的总重量,现要求用C++编程,模拟商店货物购进和卖出的情况。
主函数中,先输出商店货物的初始重量(初始为0),再增加两箱货物,货物重量由键盘输入,输出增加后商店货物总重量。
提示:需要定义静态数据成员存储库存货物的总重量。 
输入描述
两箱货物的重量 
输出描述
商店货物原始总重量
增加两箱货物后的总重量 
输入样例
56
54 
输出样例
货物的初始重量:0
此时货物的重量:110
#include <iostream>

using namespace std;

class Goods
{
public:
    Goods(int w);
    void add(int n);
    int GettotalWeight();

private:
    int weight;
    static int totalWeight;
};

int Goods::totalWeight = 0;

Goods::Goods(int w)
{
    this->weight = w;
}
int Goods::GettotalWeight()
{
    return totalWeight;
}
void Goods::add(int n)
{
    totalWeight = totalWeight + n;
}

int main()
{
    Goods goods1(0);
    int n1, n2;
    cin >> n1 >> n2;
    goods1.add(n1);
    goods1.add(n2);
    cout << "货物的初始重量:0" << endl;
    cout << "此时货物的重量:" << goods1.GettotalWeight() << endl;
    return 0;
}

习题06(03)Point3D运算符重载函数

题目描述
编程:定义描述三维坐标点的类Point3D,重载”++”、”--”、”+”运算符,要求用成员函数实现后置++运算符,用友元运算符实现前置--运算和加法运算符重载。
编写主函数,定义Point3D类对象p1、p2(p1、p2的值均从键盘输入)、p(使用默认值,默认值为0,0,0),执行
p = p1++;   输出p、p1的值
p = --p2;  输出p、p2的值
p = p1 + p2; 输出p的值 
输入描述
两行
第一个三维点的坐标
第二个三维点的坐标 
输出描述
执行以下操作后:
p = p1++;   输出p、p1的值
p = --p2;  输出p、p2的值
p = p1 + p2; 输出p的值 
输入样例
3 5 4
7 1 5 
输出样例
p1=(4,6,5)
p=(3,5,4)
p2=(6,0,4)
p=(6,0,4)
p=(10,6,9)
#include <iostream>

using namespace std;

class Point3D
{
public:
    Point3D(int a, int b, int c);
    Point3D operator++(int);
    friend Point3D operator--(Point3D &op);
    friend Point3D operator+(Point3D &op1, Point3D &op2);
    void print();

private:
    int x, y, z;
};
Point3D::Point3D(int a, int b, int c)
{
    x = a;
    y = b;
    z = c;
}
Point3D Point3D ::operator++(int)
{
    Point3D t(*this);
    x++;
    y++;
    z++;
    return t;
}
Point3D operator--(Point3D &op)
{
    --op.x;
    --op.y;
    --op.z;
    return op;
}
Point3D operator+(Point3D &op1, Point3D &op2)
{
    Point3D t(0, 0, 0);
    t.x = op1.x + op2.x;
    t.y = op1.y + op2.y;
    t.z = op1.z + op2.z;
    return t;
}
void Point3D::print()
{
    cout << "(" << x << "," << y << "," << z << ")" << endl;
}
int main()
{
    Point3D p(0, 0, 0);
    int x1, x2, y1, y2, z1, z2;
    cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
    Point3D p1(x1, y1, z1);
    Point3D p2(x2, y2, z2);
    p = p1++;
    cout << "p1=";
    p1.print();
    cout << "p=";
    p.print();
    p = --p2;
    cout << "p2=";
    p2.print();
    cout << "p=";
    p.print();

    p = p1 + p2;
    cout << "p=";
    p.print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bmNkotc2AECynaY6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值