C++,类的初学者,做题目写了个程序编译没报错,但运行会崩,求解
#include<string.h>
class LinearStr
{ int Size;
int Point;
char ** strp;
public:
LinearStr(int n=100)
{ if(n==0)
{ Size=Point=0;
strp=0;
}
else
{ Size=n;
Point=0;
strp=new char *[n];
for(int i=0;i<n;i++) strp[i]=0;
}
}
~LinearStr()
{ for(int i;i<Point;i++)
if(strp[i]) delete[]strp[i];
}
int input(char *);
char * String(unsigned n)
{return strp[n];}
unsigned Elem(void)
{return Point;}
unsigned Max(void)
{return Size;}
void Print(void);
};
int LinearStr::input(char *p)
{ if(Point<Size)
{ strp[Point]=new char[100];
strcpy(strp[Point++],p);
return Point;
}
else
{ char ** Strp;
Strp=new char*[Size+1];
for(int i=0;i<Size;i++)
{ Strp[i]=new char[100];
strcpy(Strp[i],strp[i]);
}
for(i=0;i<Point;i++) delete[]strp[i];
delete []strp;
Size++;
strp=Strp;
strp[Point]=new char[100];
strp[Point++]=p;
return Point;
}
}
void LinearStr::Print(void)
{ for(int i=0;i<Point;i++) cout<<strp[i]<<'\n';
}
void main()
{ LinearStr list;
char *p;
p=new char[100];
cout<<"请输入字符串,以-1结束"<<'\n';
for(int i=0;;i++)
{ cin.getline(p,100);
if(strcmp(p,"-1")==0) break;
list.input(p);
}
cout<<"字符串个数为:"<<list.Elem()<<'\n';
cout<<"list长度为:"<<list.Max()<<'\n';
cout<<"第三个字符串为:"<<list.String(3)<<'\n';
list.Print();
} 展开
修改了你的代码,修改部分都注释了,你看下。
#include<iostream> // 修改了头文件, c++的标准建议不加.h
#include<string>
using namespace std; // 包含std的命名空间,否则下面在用到cout之类而没加std会报错
class LinearStr
{
int Size;
int Point;
char ** strp;
public:
LinearStr(int n=100)
{
if(n==0)
{
Size = 0; // 建议分开写,
Point = 0;
strp = 0;
}
else
{
Size=n;
Point=0;
strp=new char *[n];
for(int i=0;i<n;i++)
strp[i]=0;
}
}
~LinearStr()
{
for(int i = 0;i<Point;i++) // i未赋初值,不安全
if(strp[i])
delete[]strp[i];
}
int input(char *);
char * String(unsigned n){return strp[n];}
unsigned Elem(void){return Point;}
unsigned Max(void){return Size;}
void Print(void);
};
int LinearStr::input(char *p)
{
if(Point<Size)
{
strp[Point]=new char[100];
strcpy_s(strp[Point++], 100, p); // 这里换用strcpy_s
return Point;
}
else
{
char ** Strp;
Strp=new char*[Size+1];
for(int i=0;i<Size;i++)
{
Strp[i]=new char[100];
strcpy_s(Strp[i],100, strp[i]);
}
for(int i=0;i<Point;i++)
delete[]strp[i];
delete []strp;
Size++;
strp=Strp;
strp[Point]=new char[100];
strp[Point++]=p;
return Point;
}
}
void LinearStr::Print(void)
{
for(int i=0;i<Point;i++)
cout<<strp[i]<<'\n';
}
void main()
{
LinearStr list;
char *p;
p=new char[100];
cout<<"请输入字符串,以-1结束"<<'\n';
for(int i=0;;i++)
{
cin.getline(p,100);
if(strcmp(p,"-1")==0)
break;
int m = sizeof(*p);
list.input(p);
}
cout<<"字符串个数为:"<<list.Elem()<<'\n';
cout<<"list长度为:"<<list.Max()<<'\n';
cout<<"第三个字符串为:"<<list.String(2)<<'\n'; // 从0开始计数
list.Print();
system("pause");
}
运行结果:
内存用了乱七八糟,改成string和vector吧。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class LinearStr
{
int Point;
int Size;
vector<string> strp;
public:
explicit LinearStr() {
}
~LinearStr() {
}
int input(string const&);
string const& String(unsigned n) {
return strp[n];
}
unsigned Elem(void) const {
return strp.size();
}
unsigned Max(void) const {
return strp.capacity();
}
void Print(void);
};
int LinearStr::input(string const& p)
{
strp.push_back(p);
return Elem();
}
void LinearStr::Print(void)
{
for(int i=0;i<Elem();i++) {
cout<<strp[i]<<'\n';
}
}
int main()
{
LinearStr list;
cout<<"请输入字符串,以-1结束"<<'\n';
string buf;
getline(cin, buf);
while (buf != "-1"){
list.input(buf);
getline(cin, buf);
}
cout<<"字符串个数为:"<<list.Elem()<<'\n';
cout<<"list长度为:"<<list.Max()<<'\n';
cout<<"第三个字符串为:"<<list.String(3)<<'\n';
list.Print();
return 0;
}