首先
#include
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
using std::ostream;
using std::istream;
class String
{
private:
char*str;
int len;
static int num_string;
static const int CILIM = 80;
public:
String(const char *s);
String();
String(const String&);
~String();
int length() { return len; }
String & operator=(const String&);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String&st, const String&st2);
friend bool operator>(const String&st1, const String&st2);
friend bool operator==(const String&st, const String&st2);
friend ostream& operator<<(ostream&os, const String&st);
friend istream&operator>>(istream&is, String &st);
static int HowMany();
};
#endif
其次
#define _CRT_SECURE_NO_WARNINGS
#include “strngbad.h”
#include
int String::num_string = 0;
using std::cin;
using std::cout;
String::String(const char * s)
{
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_string++;
std::cout << num_string << “:”"<
}
String::String()
{
len = 4;
str = new char[1];
str[0] = ‘\n’;
num_string++;
}
String::String(const String &st)
{
num_string++;
len =st.len;
str = new char[len+1];
std::strcpy(str, st.str);
std::cout << num_string << “:”" << str
<< “” object created\n";
}
String::~String()
{
std::cout << “”" << str << "“object delected,”;
–num_string;
std::cout << num_string << “left\n”;
delete[]str;
}
String & String::operator=(const String &st)
{
if (this == &st)
return *this;
delete[]str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
// TODO: 在此处插入 return 语句
}
String & String::operator=(const char *s)
{
delete[]str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
// TODO: 在此处插入 return 语句
}
char & String::operator[](int i)
{
return str[i];
// TODO: 在此处插入 return 语句
}
const char & String::operator[](int i) const
{
return str[i];
// TODO: 在此处插入 return 语句
}
int String::HowMany()
{
return num_string;
}
bool operator<(const String & st1, const String & st2)
{
return (std::strcmp(st1.str,st2.str)<0);
}
bool operator>(const String & st1, const String & st2)
{
return st2 < st1;
}
bool operator==(const String & st1, const String & st2)
{
return (std::strcmp(st1.str, st2.str)==0);
}
ostream &operator<<(ostream & os, const String & st)
{
os << st.str;
return os;
// TODO: 在此处插入 return 语句
}
istream & operator>>(istream & is, String & st)
{
char temp[String::CILIM];
is.get(temp, String::CILIM);
if (is)
st = temp;
while (is&&is.get() != ‘\n’)
continue;
return is;
// TODO: 在此处插入 return 语句
}
最后
#include
using std::cout;
#include"strngbad.h"
const int Arsize = 10;
const int maxlen = 81;
int main()
{
using std::cin;
using std::cout;
using std::endl;
String name;
cout << "hi,what's your name";
cin >> name;
cout << name << ",please enter up to " << Arsize
<< "short saying " << "empty line to quit:\n";
String saying[Arsize];
char temp[maxlen];
int i;
for (i = 0;i < Arsize;++i)
{
cout << i + 1 << ":";
cin.get(temp, maxlen);
while (cin&&cin.get() != '\n')
continue;
if (!cin || temp[0] == '\0')break;
else
saying[i] = temp;
}
int total = i;
if(total>0)
{
cout << "here are you saying:\n";
for (i = 0;i < total;i++)
cout << saying[i][0] << ":" << saying[i] << endl;
int shortest=0;
int first = 0;
for (i = 0;i < total;++i)
{
if (saying[i].length() < saying[shortest].length())
shortest = i;
if (saying[i] < saying[first])
first = i;
}cout << "shortest saying:\n" <
return 0;
}