以下程序哪里错了

xyj19820205 2004-06-18 05:29:28


#include <iostream>
#include <cstdlib>
using namespace std;

template <class Type>
int count(Type *s,int sz,const Type &which)
{
int n=0;
for(int i=0;i<sz;++i)
if(a[i]==which)
n++;
return n;
}

int main()
{
double a[]={10.0,20.0,30.0,40.0,50.0,30.0,70.0,80.0,30.0};
cout<<"count(a,9,30.0) should return 3:"
<<count(a,9,30.0)<<endl;
int b[]={10,20,30,40,50,30,70,80,30};
cout<<"count(b,9,30) should return 3:"
<<count(b,9,30)<<endl;
char c[]={'a','a','c','a'};
cout<<"count(c,4,'a') should return 3:"
<<count(c,4,'a')<<endl;

string d[]={"aa","aa","aa"};
cout<<"count(d,3,\"aa\") should return 3:"
<<count(d,3,string("aa"))<<endl;

string e[]={"bb","aa","aa"};
cout<<"count(e,3,\"aa\") should return 2:"
<<count(e,3,string("aa"))<<endl;

system("pause");
return 0;
}
...全文
148 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
o1n 2004-06-20
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

template <class Type>
int count(Type *s,int sz, const Type& which)
{
int n=0;
for(int i=0;i<sz;++i)
if(s[i]==which)
n++;
return n;
}
int count(char*s[],int sz, char* which)
{
int n=0;
for(int i=0;i<sz;i++)
if(strcmp(s[i],which)==0)n++;
return n;
}

int main()
{
double a[]={10.0,20.0,30.0,40.0,50.0,30.0,70.0,80.0,30.0};
cout<<"count(a,9,30.0) should return 3:"
<<count(a,9,30.0)<<endl;
int b[]={10,20,30,40,50,30,70,80,30};
cout<<"count(b,9,30) should return 3:"
<<count(b,9,30)<<endl;
char c[]={'a','a','c','a'};
cout<<"count(c,4,'a') should return 3:"
<<count(c,4,'a')<<endl;

char* d[]={"aa","aa","aa"};
cout<<"count(d,3,\"aa\") should return 3:"
<<count(d,3,"aa")<<endl;

char* e[]={"bb","aa","aa"};
cout<<"count(e,3,\"aa\") should return 2:"
<<count(e,3,"aa")<<endl;

system("pause");
return 0;
}
cenlmmx 2004-06-20
  • 打赏
  • 举报
回复
晕,太不仔细
加上#include <string>
insulator 2004-06-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdlib>
using namespace std;

template <class Type>
int count(Type *s,int sz, const Type& which)
{
int n=0;
for(int i=0;i<sz;++i)
if(s[i]==which)
n++;
return n;
}
int count(char*s[],int sz, char* which)
{
int n=0;
for(int i=0;i<sz;i++)
if(strcmp(s[i],which)==0)n++;
return n;
}

int main()
{
double a[]={10.0,20.0,30.0,40.0,50.0,30.0,70.0,80.0,30.0};
cout<<"count(a,9,30.0) should return 3:"
<<count(a,9,30.0)<<endl;
int b[]={10,20,30,40,50,30,70,80,30};
cout<<"count(b,9,30) should return 3:"
<<count(b,9,30)<<endl;
char c[]={'a','a','c','a'};
cout<<"count(c,4,'a') should return 3:"
<<count(c,4,'a')<<endl;

char* d[]={"aa","aa","aa"};
cout<<"count(d,3,\"aa\") should return 3:"
<<count(d,3,"aa")<<endl;

char* e[]={"bb","aa","aa"};
cout<<"count(e,3,\"aa\") should return 2:"
<<count(e,3,"aa")<<endl;

system("pause");
return 0;
}
steel007 2004-06-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
~~~~~~~~~~~~~~~~~~~~~~此处加入#include
#include <cstdlib>
using namespace std;

template <class Type>
int count(Type *a,int sz,const Type &which)
~~~~~~~改为a,应该是手误写为s了吧?
{
int n=0;
for(int i=0;i<sz;++i)
if(a[i]==which)
n++;
return n;
}

int main()
{
double a[]={10.0,20.0,30.0,40.0,50.0,30.0,70.0,80.0,30.0};
cout<<"count(a,9,30.0) should return 3:"
<<count(a,9,30.0)<<endl;
int b[]={10,20,30,40,50,30,70,80,30};
cout<<"count(b,9,30) should return 3:"
<<count(b,9,30)<<endl;
char c[]={'a','a','c','a'};
cout<<"count(c,4,'a') should return 3:"
<<count(c,4,'a')<<endl;

string d[]={"aa","aa","aa"};
cout<<"count(d,3,\"aa\") should return 3:"
<<count(d,3,string("aa"))<<endl;

string e[]={"bb","aa","aa"};
cout<<"count(e,3,\"aa\") should return 2:"
<<count(e,3,string("aa"))<<endl;

system("pause");
return 0;
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧