8.8编程练习

  1. 第一题
#include <iostream>
#include <string>

using namespace std;
void show(const string & str, int n=1);


int main() {

    string temp;
    getline(cin, temp);
    for (int i=0;i<10;i++)
    {
        show(temp, i);
    }

    return 0;
}

void show(const string & str, int n)
{
    cout << str << endl;
}

运行结果:

abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
  1. 第二题
#include <iostream>
#include <cstring>
using namespace std;
const int Max = 20;
struct CandyBar
{
    char name[Max]; // char * 和 char []在参数是相同,但在其他地方含义不同
    double weight;
    int hot;
};
void fill(CandyBar & cb, const char * n, double w, int h);
void show(const CandyBar & cb);


int main() {

    CandyBar cb {};
    fill(cb, "Millennium Munch", 2.85, 350);
    show(cb);

    return 0;
}
void fill(CandyBar & cb, const char * n, double w, int h)
{
    strcpy(cb.name, n);
    cb.weight = w;
    cb.hot = h;
}
void show(const CandyBar & cb)
{
    cout << cb.name << endl;
    cout << cb.weight << endl;
    cout << cb.hot << endl;
}

运行结果:

Millennium Munch
2.85
350
  1. 第三题
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void change(string & str);


int main() {

    string str;
    cout << "Enter a string (q to quit):";
    while (getline(cin, str) && str != "q")
    {
        change(str);
        cout << str << endl;
        cout << "Next string (q to quit):";
    }
    cout << "Bye.\n";

    return 0;
}
// toupper函数只可以一个一个转换
void change(string & str)
{
    for (int i = 0; str[i] != '\0' ; ++i)
    {
        str[i] = toupper(str[i]);
    }
}

运行结果:

Enter a string (q to quit):go away
GO AWAY
Next string (q to quit):good grief!
GOOD GRIEF!
Next string (q to quit):q
Bye.
  1. 第四题
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
    char * str;
    int ct;
};
void set(stringy & be, const char * test);
void show(const stringy & be, int n = 1);
void show(const char * test, int n = 1);


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!");
    delete [] beany.str;

    return 0;
}

void set(stringy & be, const char * test)
{
    char * x = new char [strlen(test)+1];
    strcpy(x, test); // 赋值时第一个参数最好分配好大小
    be.str = x;
    be.ct = strlen(test);
}
void show(const stringy & be, int n)
{
    for (int i = 0; i < n; ++i)
    {
        cout << be.str << endl;
    }
}
void show(const char * test, int n)
{
    for (int i = 0; i < n; ++i)
    {
        cout << test << endl;
    }
}

运行结果:

Reality isn't what it used to be.
Reality isn't what it used to be.
Reality isn't what it used to be.
Duality isn't what it used to be.
Duality isn't what it used to be.
Duality isn't what it used to be.
Duality isn't what it used to be.
Done!
  1. 第五题
#include <iostream>
#include <cstring>
using namespace std;
template<class T>
T max5(const T arr[]);


int main() {

    int arr[5];
    for (int & i : arr)
    {
        cin >> i;
    }
    cout << "int数组最大值为:" << max5(arr) << endl;
    double arr1[5];
    for (double & i : arr1)
    {
        cin >> i;
    }
    cout << "double数组最大值为:" << max5(arr) << endl;

    return 0;
}

template<class T>
T max5(const T arr[])
{
    T temp = arr[0];
    for (int i = 1; i < 5; ++i)
    {
        if (temp < arr[i])
            temp = arr[i];
    }
    return temp;
}

运行结果:

1 2 3 4 5
int数组最大值为:5
1.0 2.0 3.0 4.0 5.0
double数组最大值为:5
  1. 第六题
    该题较5题少了const才能正常运行,原因暂时未知。
#include <iostream>
#include <cstring>
using namespace std;
template <class T>
T maxn(T arr[], int n);

template <> char * maxn(char * arr[], int n)
{
    int max=0; //用于记录最大的字符串长度
    int temp=0; //用于记录字符串长度
    int place=0; //用于记录所在位置
    for(int i=0;i<n;i++)
    {
        temp = strlen(arr[i]);
        if(temp>max)
        {
            max=temp;
            place=i; //记录字符串所在位置
        }
    }

    return arr[place];
}

int main() {

    int arr[6];
    for (int & i : arr)
    {
        cin >> i;
    }
    cout << "int数组最大值为:" << maxn(arr, 6) << endl;
    double arr1[4];
    for (double & i : arr1)
    {
        cin >> i;
    }
    cout << "double数组最大值为:" << maxn(arr, 4) << endl;

    char * pd[5];
    char a[] = "abc";
    char b[] = "bc";
    char c[] = "a";
    char d[] = "degf";
    char e[] = "ab";
    pd[0] = a;
    pd[1] = b;
    pd[2] = c;
    pd[3] = d;
    pd[4] = e;
    cout << "最长字符串为:" << maxn(pd, 5) << endl;
    cout << "最长字符串地址为:" << (int *) maxn(pd, 5) << endl; // 由于cout的重载,char*会自动显示所指向的字符串,所以需要int *转换

    return 0;
}

template<class T>
T maxn(T arr[], int n)
{
    T temp = arr[0];
    for (int i = 1; i < n; ++i)
    {
        if (temp < arr[i])
            temp = arr[i];
    }
    return temp;
}

运行结果:

1 2 3 4 5 6
int数组最大值为:6
1.0 2.0 3.0 4.0
double数组最大值为:4
最长字符串为:degf
最长字符串地址为:0x61fd52
  1. 第七题
#include <iostream>
using namespace std;

template <typename T>  // template A
T SumArray(T arr[], int n);

template <typename T>  // template B
T SumArray(T * arr[], int n);

struct debts
{
    char name[50];
    double amount;
};

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 << "Listing Mr.E's counts of things:\n";
    cout << "things数组总和为:" << SumArray(things, 6) << endl;
    cout << "Listing Mr.E's debts:\n";
    cout << "pd所指数组总和为:" << SumArray(pd, 3);

    return 0;
}

template <typename T>
T SumArray(T arr[], int n)
{
    cout << "template A\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)
{
    cout << "template B\n";
    T sum = 0;
    for (int i = 0; i < n; ++i)
    {
        sum += *arr[i];
    }
    return sum;
}

运行结果:

Listing Mr.E's counts of things:
things数组总和为:template A
888
Listing Mr.E's debts:
pd所指数组总和为:template B
5500
posted @ 2021-02-21 19:42  小Aer  阅读(114)  评论(0编辑  收藏  举报