将此文件改为多文件#include<iostream> #include<string.h> using namespace std; class CSTRING { private: char* p; public: CSTRING() { int len = 0; p = new char[len + 1]; p[0] = '\0'; } CSTRING(const char* str) { int len = strlen(str); p = new char[len + 1]; strcpy(p, str); } CSTRING(const CSTRING& str) { int len = strlen(str.p); p = new char[len + 1]; strcpy(p, str.p); } CSTRING operator+=( CSTRING& s) { char* str = new char[strlen(p) + strlen(s.p) + 3]; str[0] = '\0'; strcat(str, p); strcat(str, "--"); strcat(str, s.p); delete[]p; p = str; return *this; } friend ostream& operator<<(ostream& out, CSTRING& str); friend istream& operator>>(istream& in, CSTRING& str); int operator>(const CSTRING& str); }; ostream& operator<<(ostream& out, CSTRING& str) { out << str.p; return out; } istream& operator>>(istream& in, CSTRING& str) { in >> str.p; return in; } int CSTRING:: operator>(const CSTRING& str) { if (strcmp(p, str.p)>0){ return 1; }else {return 0; } } int main() { int n, i, j; while (cin >> n) { CSTRING *c = new CSTRING[n + 2]; for (i = 0; i < n; i++) { cin >> c[i]; } for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) if (c[j] > c[j + 1]) { c[n] = c[j]; c[j] = c[j + 1]; c[j + 1] = c[n]; } for (i = 0; i < n; i++) c[n + 1] += c[i]; cout << c[n + 1] << endl; delete[] c; } return 0; }
时间: 2024-01-18 16:01:48 浏览: 135
这里提供一个多文件的实现,将CSTRING类的定义和实现分别放在CSTRING.h和CSTRING.cpp文件中,主函数放在main.cpp文件中。
CSTRING.h文件内容如下:
```c++
#ifndef CSTRING_H
#define CSTRING_H
#include<iostream>
#include<string.h>
using namespace std;
class CSTRING {
private:
char* p;
public:
CSTRING();
CSTRING(const char* str);
CSTRING(const CSTRING& str);
~CSTRING();
CSTRING operator+=(CSTRING& s);
int operator>(const CSTRING& str);
friend ostream& operator<<(ostream& out, CSTRING& str);
friend istream& operator>>(istream& in, CSTRING& str);
};
#endif
```
CSTRING.cpp文件内容如下:
```c++
#include "CSTRING.h"
CSTRING::CSTRING() {
int len = 0;
p = new char[len + 1];
p[0] = '\0';
}
CSTRING::CSTRING(const char* str) {
int len = strlen(str);
p = new char[len + 1];
strcpy(p, str);
}
CSTRING::CSTRING(const CSTRING& str) {
int len = strlen(str.p);
p = new char[len + 1];
strcpy(p, str.p);
}
CSTRING::~CSTRING() {
delete[] p;
}
CSTRING CSTRING::operator+=(CSTRING& s) {
char* str = new char[strlen(p) + strlen(s.p) + 3];
str[0] = '\0';
strcat(str, p);
strcat(str, "--");
strcat(str, s.p);
delete[]p;
p = str;
return *this;
}
int CSTRING::operator>(const CSTRING& str) {
if (strcmp(p, str.p) > 0) {
return 1;
}
else {
return 0;
}
}
ostream& operator<<(ostream& out, CSTRING& str) {
out << str.p;
return out;
}
istream& operator>>(istream& in, CSTRING& str) {
in >> str.p;
return in;
}
```
main.cpp文件内容如下:
```c++
#include<iostream>
#include"CSTRING.h"
using namespace std;
int main() {
int n, i, j;
while (cin >> n)
{
CSTRING* c = new CSTRING[n + 2];
for (i = 0; i < n; i++)
{
cin >> c[i];
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (c[j] > c[j + 1])
{
c[n] = c[j];
c[j] = c[j + 1];
c[j + 1] = c[n];
}
for (i = 0; i < n; i++)
c[n + 1] += c[i];
cout << c[n + 1] << endl;
delete[] c;
}
return 0;
}
```
阅读全文