当前位置:   article > 正文

8.8编程练习_编程运行结果是reality isn't what it used to be啥意思

编程运行结果是reality isn't what it used to be啥意思
  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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

运行结果:

abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

运行结果:

Millennium Munch
2.85
350
  • 1
  • 2
  • 3
  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]);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

运行结果:

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
  • 2
  • 3
  • 4
  • 5
  • 6
  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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

运行结果:

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  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
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

运行结果:

1 2 3 4 5
int数组最大值为:5
1.0 2.0 3.0 4.0 5.0
double数组最大值为:5
  • 1
  • 2
  • 3
  • 4
  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
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

运行结果:

1 2 3 4 5 6
int数组最大值为:6
1.0 2.0 3.0 4.0
double数组最大值为:4
最长字符串为:degf
最长字符串地址为:0x61fd52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

运行结果:

Listing Mr.E's counts of things:
things数组总和为:template A
888
Listing Mr.E's debts:
pd所指数组总和为:template B
5500
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/202261
推荐阅读
相关标签
  

闽ICP备14008679号