首页 > 试题广场 >

下面是一个程序框架: #include using na

[问答题]
下面是一个程序框架:

#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
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 menber 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;
}

请提供其中描述的函数和原型,从而完成该程序。注意,应有两个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 &a,char *b)
      {
          int c=strlen(b);
          a.ct =c;
          a.str =new char (c+1);
          strcpy(a.str ,b);
      }
void show(const string &a,int b=1)
{
    for(;b>=0;b--)
    cout<<a<<endl;
}
void show(const stringy a,int b=1)
{
    for(;b>=0;b--)
    cout<<a.str <<endl;
    
}
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 menber 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;
}
发表于 2021-03-07 18:30:37 回复(0)
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

struct stringy
{
	char* stry;
	int ct;
};

void show(const string &st,int n=0);
void show(const stringy &str,int n=0);
void set( stringy &sty,char* st);

int main()
{
	stringy benny;
	char testing[]="Reality isn't what it is used to be.";
	set(benny,testing);
	show(benny);
	show(benny,3);
	testing[0]='D';
	testing[1]='u';
	show(testing);
	show(testing,3);
	show("Done");
	system("pause");
	return 0;
}

void show(const string &st,int n)
{
	if(n==0) n++;
	for (int i = 0; i < n; i++)
	{
		cout<<st<<endl;
	}
}
void show(const stringy &str,int n)
{
	if(n==0) n++;
	for (int i = 0; i < n; i++)
	{
		cout<<str.stry<<endl;
	}
}
void set( stringy &sty,char* st)
{
	sty.ct=strlen(st);
	sty.stry=new char[sty.ct];
	strcpy(sty.stry,st);
}

发表于 2021-03-06 17:02:02 回复(0)