| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:程序有点错误 就是找不出来 请帮帮忙
只看楼主 加入收藏
tianxiaogang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-10-23
收藏
 问题点数:0 回复次数:3 
程序有点错误 就是找不出来 请帮帮忙

#include "string.h"
class A
{
public:
A(int,int);
~A();
bool panduan(char*)const;
void Add(char*);
void display();
private:
int m;
//static int m;
char**s;
int _count;
};
//int A::m=0;
A::A(int count,int m1)
{
m=m1;
_count=count;
s=new char*[_count];
}
A::~A()
{
for(int i=0;i<_count;i++)
delete s[i];
delete[] s;
}
bool A::panduan(char*p)const
{
bool h=false;
for(int k=0;k<_count;k++)
{
if(s[k]==p)
{
h=true;
break;
}
}
return h;

}
void A::Add(char*p)
{
if(!panduan(p))
{
if(m<=_count)
{
s[m]=new char[strlen(p)];
strcpy(s[m],p);
m++;
}
else
cout<<"已经满了"<<endl;
}
}
void A::display()
{
for(int j=0;j<_count;j++)
cout<<s[j]<<endl;
}

这是 头文件

#include <iostream.h>
#include "tou.h"
int main()
{
A a(3,0);
a.Add("abcd");
a.Add("lp");
a.Add("qqqqoooo");
//a.Add("ttt");
a.display();
return 0;
}

当运行时 可以输出但是 跳出来一个窗口说什么

图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: private display include public count 
2005-12-11 11:20
minima
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-12-13
收藏
得分:0 

#include "string.h"
#include <iostream.h>
class A
{
public:
A(int,int);
~A();
bool panduan(char*)const;
void Add(char*);
void display();
private:
int m;
static int t;
char**s;
int _count;
bool b;
};
int A::t=-1;
A::A(int count,int m1)
{
m=m1;
_count=count;
s=new char*[_count];
}
A::~A()
{
delete s;
}
bool A::panduan(char*p)const
{
bool h=false;
for(int k=0;k<_count;k++)
{
if(strlen(s[k])==strlen(p))
for(int i=0;i<(int)strlen(p);i++)
if(*(s[k]+i)==*(p+i))
{
h=true;t=k+1;
}
else
{
h=false;
break;
}
}
return h;
}
void A::Add(char*p)
{
if(m<_count)
{
s[m]=new char[strlen(p)];
strcpy(s[m],p);
m++;
}
else b=!panduan(p);
}
void A::display()
{
for(int j=0;j<_count;j++)
cout<<s[j]<<endl;
if(b)
{
cout<<"已经满了"<<endl;
}
else
{
cout<<"已经存在于第"<<t<<"个位置"<<endl;
}
}


int main()
{
A a(3,0);
a.Add("abcd");
a.Add("lp");
a.Add("qqqqoooo");
a.Add("a");
a.display();
return 0;
}

自己看吧 就是这样 中间你犯了太多的逻辑错误


2005-12-15 17:29
minima
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-12-13
收藏
得分:0 

#include "string.h"
class A
{
public:
A(int,int);
~A();
bool panduan(char*)const;
void Add(char*);
void display();
private:
int m;
//static int m;
char**s;
int _count;
};
//int A::m=0;
A::A(int count,int m1)
{
m=m1;
_count=count;
s=new char*[_count];
}
A::~A()
{
for(int i=0;i<_count;i++)
delete s[i];//delete不是这么用的 只有下面的就够了
delete[] s;
}
bool A::panduan(char*p)const
{
bool h=false;
for(int k=0;k<_count;k++)
{
if(s[k]==p)//地址比较有意义吗?要用值进行比较
{
h=true;
break;
}
}
return h;

}
void A::Add(char*p)
{
if(!panduan(p))//这个判断的位置有问题 第一个元素怎么也能判断??
{
if(m<=_count)
{
s[m]=new char[strlen(p)];
strcpy(s[m],p);
m++;
}
else
cout<<"已经满了"<<endl;
}
}
void A::display()
{
for(int j=0;j<_count;j++)
cout<<s[j]<<endl;
}

这是 头文件

#include <iostream.h>
#include "tou.h"
int main()
{
A a(3,0);
a.Add("abcd");
a.Add("lp");
a.Add("qqqqoooo");
//a.Add("ttt");
a.display();
return 0;
}


2005-12-15 17:32
快速回复:程序有点错误 就是找不出来 请帮帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021562 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved