📄 complete.cpp
字号:
// 4.下面是一个程序框架:
// 请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个
// 都使用默认参数。请尽可能使用const参数。set()使用new分配足够的空间来存储指定
// 的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件
// 的名称,删除using编译指令,这取决于所用的编译器。)
#include <iostream>
using namespace std;
#include <cstring> // for strlen(),strcpy()
struct stringy{
char *str; // points to a string
int ct; // length of string(not counting '\0')
};
// prototypes for set(),show(),and show()go here
void set(stringy & strin, const char str[]);
void show(const stringy & strin, int n=1); //const stringy &
void show(const char * str,int n=1); //const char *
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set (beany,testing); //first argument is a reference,
// allocates space to hold copy of testing,
// sets str member of beany to point to the
// new block,copies testing to new block,
// and sets ct member of beany
show (beany); //prints member string once
show (beany,2); //prints member string twice
testing[0] = 'D';
testing[1] = 'u';
show (testing); //prints testing string once
show (testing,3); //prints testing string thrice
show ("Done!");
return 0;
}
void set(stringy & strin,const char str[])
{
strin.ct=strlen(str);
strin.str=new char[strin.ct+1]; //为什么要这样做呢,不能直接使用参数吗
strcpy(strin.str,str);
}
void show(const stringy & strin, int n) //show版本1
{
int i=0;
for(i;i<n;i++)
{
cout<<strin.str<<endl;
}
}
void show(const char * str,int n) //show版本2
{
int i=0;
for(i;i<n;i++)
{
cout<<str<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -