问题:
解答:
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char* str;
int ct;
};
void show(const stringy& s, int n=0)
{
if (n == 0)n++;
for (int i = 0; i < n; i++)
{
cout << s.str << endl;
}
}
void show(const char *c, int n = 0)
{
if (n == 0)n++;
for (int i = 0; i < n; i++)
{
cout <<c<< endl;
}
}
void set(stringy& s, char* test)
{
int len = strlen(test);
char* c = new char[len];
c = test;
s.str = new char[len];
s.ct = len;
strcpy(s.str, c);
}
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
cout << endl;
show(beany, 2);
cout << endl;
testing[0] = 'D';
testing[1] = 'u';
show(testing);
cout << endl;
show(testing, 3);
cout << endl;
show("Done!");
cout << endl;
return 0;
}
运行结果:
考查点:
- 函数重载
- strcpy()
- strlen()
- 动态内存分配
2024年9月1日21:21:15