#pragma once
#ifndef STRING_H_
#define STRING_H_
#include<string>
class String1
{
private:
char *str; //字符串
int len; //字符串长度
static int num_strings; //String1对象的监控
static const int CINLIM = 80; //cin 输入的最大限制
public:
String1();
~String1();
String1(const char * s);
String1(const String1 &);
int length()const { return len; }; //返回字符串的长度
/************************************************************************************/
//友元函数 主要重载部分运算符
/************************************************************************************/
friend bool operator<(const String1 &st1, const String1 &st);
friend bool operator>(const String1 &st1, const String1 &st);
friend bool operator==(const String1 &st1, const String1 &st);
friend std::istream & operator>>(std::istream & is, String1 &st);
friend std::ostream & operator<<(std::ostream & is, String1 &st);
/************************************************************************************/
//静态成员变量,字符串数目
static int Howmany() { return num_strings; };
//重载[]运算符,方便使用 str[i]
char & operator[](int i);
const char & operator[](int i)const;
//重载=运算符
String1 & operator=(const char *s);
String1 & operator=(const String1 &st);
};
#endif // !STRING!_H_
#define _CRT_SECURE_NO_WARNINGS
#include "String1.h"
#include<iostream>
#include<string>
//静态变量,对字符串数量进行统计
int String1::num_strings = 0;
//默认构造函数
String1::String1()
{
len = 0;
str = new char[1];//采用char[] 使其与析构函数兼容
str[0] = '\0';
num_strings++;
}
//从string 构造函数
String1::String1(const char * s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
num_strings++;
}
//从现有的String1 构建函数
String1::String1(const String1 &st)
{
num_strings++;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
}
//析构函数
String1::~String1()
{
--num_strings;
delete[]str;
}
/************************************************************************************/
//重载部分运算符
/************************************************************************************/
//重载[]运算符
//对非const 对象实现 读写char
char & String1::operator[](int i)
{
return str[i];
}
//对const 对象实现读
const char & String1::operator[](int i)const
{
return str[i];
}
//重载=运算符
//按照char 对象实现复制
String1 & String1::operator=(const char *s)
{
delete[]str; //释放原有str指向内存
len = std::strlen(s); //重新分配内存
str = new char[len + 1];
std::strcpy(str, s); //拷贝复制
return *this; //返回
}
//按照String1对象实现复制
String1 & String1::operator=(const String1 &st)
{
//深度复制
if (this == &st)
{
return *this;
}
delete[]str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
/************************************************************************************/
//友元函数 主要重载部分运算符
/************************************************************************************/
//比较字符串大小
bool operator<(const String1 &st1, const String1 &st2)
{
if (std::strcmp(st1.str, st2.str) < 0)
{
return true;
}
else
return false;
}
bool operator>(const String1 &st1, const String1 &st2)
{
if (std::strcmp(st1.str, st2.str) > 0)
{
return true;
}
else
return false;
}
bool operator==(const String1 &st1, const String1 &st2)
{
if (std::strcmp(st1.str, st2.str) == 0)
{
return true;
}
else
return false;
}
//重载输入与输出符号
std::istream & operator>>(std::istream & is, String1 &st)
{
char temp[String1::CINLIM];
is.get(temp, String1::CINLIM);
if (is)
st = temp;
while (is&&is.get() != '\n')
continue;
return is;
}
std::ostream & operator<<(std::ostream & os, String1 &st)
{
os << st.str;
return os;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
//#include<string>
#include"String1.h"
using namespace std;
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using namespace std;
String1 name;
cout << "What's your name?\n";
cin >> name;
cout << name << ",please enter up to " << ArSize << " short sayings<empty line to quit>:\n";
String1 sayings[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
sayings[i] = temp;
}
int total = i;
if (total > 0)
{
//显示所有的sayings
cout << "Here are your sayings: \n";
for (i = 0; i < total; i++)
{
cout << sayings[i][0] << ": " << sayings[i] << endl;
}
int shortest = 0;
int first = 0;
//找到最短的sayings 和最小的sayings
for (i = 0; i < total; i++)
{
if (sayings[i].length() < sayings[shortest].length())
shortest = i;
if (sayings[i] < sayings[first])
first = i;
}
cout << "Shortest sayings: " << sayings[shortest] << endl;
cout << "First sayings: " << sayings[first] << endl;
cout << "This program used " << String1::Howmany() << " String1 object. Bye.\n";
}
else
cout << "No input! Bye.\n";
system("pause");
return 0;