2023/9/10
第八章编程题
1.
void show(const char *h, int n = 0)
{
static int cnt = 0; // 静态声明,不会重新定义
cnt++;
if (n == 0)
cout << h << endl;
else
{
for (int i = 1; i <= cnt; i++)
cout << h << endl;
}
}
2.
typedef struct CandyBar
{
char name[40];
double weight;
int heat;
} CandyBar;
void set(CandyBar &c, const char *n = "Millennium", const double w = 2.85, const int h = 350);
void set(CandyBar &c, const char *n, const double w, const int h)
{
strcpy(c.name, n); // 用字符串拷贝
c.weight = w;
c.heat = h;
return;
}
void show(const CandyBar &c)
{
cout << "name: " << c.name << endl
<< "weight: " << c.weight << endl
<< "heat: " << c.heat << endl;
return;
}
3.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
string str;
static int cnt = 0;
bool f = true;
void UpperStr(string &str);
int main()
{
while (f)
{
if (cnt == 0)
cout << "Enter a string(q to quit):";
else
cout << "Next string(q to quit):";
getline(cin, str);
cnt++;
UpperStr(str);
}
}
void UpperStr(string &str)
{
if (str == "q")
{
cout << "Bye";
f = false;
}
else
for (unsigned int i = 0; i < str.size(); i++)
cout << (char)toupper(str[i]);
cout << endl;
return;
}
4.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct stringy
{
char *str;
int ct;
};
void set(stringy &s, char *c);
void show(stringy &s, int i = 1);
void show(char *c, int i = 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!");
return 0;
}
void set(stringy &s, char *c)
{
s.str = new char[strlen(c) + 1]; // 用new开辟空间
strcpy(s.str, c);
}
void show(stringy &s, int i)
{
for (int j = 1; j <= i; j++)
cout << s.str << endl;
}
void show(char *c, int i)
{
for (int j = 1; j <= i; j++)
cout << c << endl;
}
5.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <class T>
T max5(T a[]);
int main()
{
int a[5] = {4, 2, 9, 3, 1};
double b[5] = {14.32, 52.1, 9.5, 31.5, 98.24};
cout << "int arr:" << max5(a) << endl;
cout << "double arr:" << max5(b) << endl;
return 0;
}
template <class T>
T max5(T a[])
{
T max = a[0];
for (int i = 1; i < 5; i++)
if (a[i] > max)
max = a[i];
return max;
}
6.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <class T>
T maxn(T a[], int n);
char *maxn(char *[], int n);
int main()
{
int a[6] = {4, 2, 9, 3, 1, 7};
double b[4] = {14.32, 52.1, 31.5, 98.24};
char *c[5] = {"asdasdasdasdd", "asdsaadavc", "1145141919810", "safeeww", "mizuki"};
cout << "int arr:" << maxn(a, 6) << endl;
cout << "double arr:" << maxn(b, 4) << endl;
cout << "char* arr:" << maxn(c, 5) << endl;
return 0;
}
template <class T>
T maxn(T a[], int n)
{
T max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
char *maxn(char *c[], int n)
{
int max = 0;
int i;
for (i = 1; i < n; i++)
{
if (strlen(c[i]) > strlen(c[max]))
max = i;
}
return c[max];
}
关于第六题warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]的解决:可以把char*全改为string