第八章编程题

1

#include <iostream>
using namespace std;
int times = 0;
void pr(char *str, int n = 0)
{
    times++;
    if (!n)
        cout << str << endl;
    else
    {
        for (int i = 0; i < times; i++)
            cout << str;
        cout << endl;
    }
}
int main()
{
    char *str = "HELLO_";
    pr(str);
    pr(str, 2);
    pr(str, 5);
    pr(str);
    pr(str, 9);
    return 0;
}

在这里插入图片描述

2

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
struct CandyBar
{
    char name[30];
    double weight;
    int heat;
};
char st[] = "Millennium Munch";
void setup(CandyBar &bar, char *str = st, double wei = 2.85, int he = 350)
{
    strcpy(bar.name, str);
    bar.weight = wei;
    bar.heat = he;
}
void display(const CandyBar &bar)
{
    cout << bar.name << endl;
    cout << bar.weight << endl;
    cout << bar.heat << endl;
}
int main()
{
    CandyBar bar1 = {"dove", 9.2, 456};
    CandyBar bar2 = {"k", 0, 0};
    display(bar1);
    display(bar2);
    char str[] = "kinder";
    setup(bar1, str, 7.7, 666);
    setup(bar2);
    display(bar1);
    display(bar2);
    return 0;
}

在这里插入图片描述
3

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
void func(string &str)
{
    int i = 0;
    while (str[i])
    {
        str[i] = toupper(str[i]);
        i++;
    }
}
int main()
{
    string str;
    cout << "Enter a string (q to quit): ";
    getline(cin, str);
    while (str != "q")
    {
        func(str);
        cout << str << endl;
        cout << "Next string (q to quit): ";
        getline(cin, str);
    }
    cout << "Bye.";
    return 0;
}

4

#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
    char *str;
    int ct;
};
void show(stringy y, unsigned int n = 1)
{
    while (n--)
        cout << y.str;
    cout << endl;
}
void show(const char *ch, unsigned int n = 1)
{
    while (n--)
        cout << ch;
    cout << endl;
}
void set(stringy &y, char *test)
{
    y.ct = strlen(test);
    char *str = new char[y.ct + 1];
    strcpy(str, test);
    y.str = str;
}
int main()
{
    stringy beany;
    char testing[] = "Reality isn't what it used to be.";
    set(beany, testing);
    show(beany);
    show(beany, 2);
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);
    show(testing, 3);
    show("Done!");
    return 0;
}

在这里插入图片描述
5

#include <iostream>
using namespace std;

template <typename T>
T max5(T arr[5])
{
    T maxi = arr[0];
    for (int i = 1; i < 5; i++)
        maxi = (maxi > arr[i]) ? maxi : arr[i];
    return maxi;
}

int main()
{
    int iarr[5] = {4, 2, 5, 7, 1};
    double darr[5] = {1.3, 3.4, -3.4, 55.6, 3.1};
    cout << "int max: " << max5(iarr) << endl;
    cout << "double max: " << max5(darr) << endl;
    return 0;
}

在这里插入图片描述
6

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

template <typename T>
T maxn(T arr[], int n)
{
    T maxi = arr[0];
    for (int i = 1; i < n; i++)
        maxi = (maxi > arr[i]) ? maxi : arr[i];
    return maxi;
}
template <>
char *maxn<char *>(char *ch[], int n)
{
    int len = 0;
    int temp = 0;
    char *p;
    for (int i = 0; i < n; i++)
    {
        temp = strlen(ch[i]);
        if (temp > len)
        {
            p = ch[i];
            len = temp;
        }
    }
    return p;
}
int main()
{
    int iarr[6] = {7, 2, 5, 66, 1, 8};
    double darr[4] = {21.3, -43.4, 505.6, 13.1};
    char a[] = "abcd";
    char b[] = "sfifjeo";
    char c[] = "fjsasddfasfiodfj";
    char d[] = "fjis";
    char e[] = "wieoquredn";
    char *arr[5] = {a, b, c, d, e};
    cout << "int max: " << maxn(iarr, 6) << endl;
    cout << "double max: " << maxn(darr, 4) << endl;
    cout << "string max: " << maxn(arr, 5) << endl;
    return 0;
}

在这里插入图片描述
7

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

struct debts
{
    char name[50];
    double amount;
};
template <typename T>
T SumArray(T arr[], int n)
{
    T sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    return sum;
}
template <typename T>
T SumArray(T *arr[], int n)
{
    T sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += *arr[i];
    }
    return sum;
}
int main()
{
    int things[6] = {13, 31, 103, 301, 310, 130};
    struct debts mr_E[3] = {
        {"Ima Wolfe", 2400.0},
        {"Ura Foxe", 1300.0},
        {"Iby Stout", 1800.0}};
    double *pd[3];
    for (int i = 0; i < 3; i++)
    {
        pd[i] = &mr_E[i].amount;
    }
    cout << "things sum: " << SumArray(things, 6) << endl;
    cout << "things sum: " << SumArray(pd, 3) << endl;
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值