谁能帮我?编译提示Runtime Error:Segmentation fault,却不知道到底哪里出问题?在线等! 20
#include<iostream>#include<string>usingnamespacestd;voidinsert(intl,strings,stringstr...
#include<iostream>
#include<string>
using namespace std;
void insert(int l,string s,string str[],int &sum)
{
int i;
if(sum==0)
str[0]=s;
else
{
for(i=sum;i>=l;--i)
str[i]=str[i-1];
str[i]=s;
}
++sum;
}
void search(string s,string str[],int sum)
{
int i;
for(i=0;i<sum;++i)
if(str[i]==s)
cout<<i+1;
}
void show(string str[],int sum)
{
int i;
for(i=0;i<sum;++i)
if(i)
cout<<" ";
cout<<str[i];
}
void del(string s,string str[],int &sum)
{
int i,j;
for(i=0;i<sum;++i)
if(str[i]==s)
for(j=i;j<sum-1;++j)
str[j]=str[j+1];
--sum;
}
int main()
{
int i,sum=0;
string str[200];
string d,name;
while(scanf("%s",&d)!=EOF)
{
cin>>d;
if(d=="insert")
{
cin>>i>>name;
insert(i,name,str,sum);
}
else if(d=="search")
{
cin>>name;
search(name,str,sum);
}
else if(d=="show")
show(str,sum);
else if(d=="delete")
{
cin>>name;
del(name,str,sum);
}
}
return 0;
}
输入为:
insert 1 Stuart
insert 2 Bernadette
show
search Stuart
delete Stuart
show
insert 2 Stuart
show
insert 1 Amy
insert 2 Leslie
insert 3 Stephanie
show
delete Leslie
show
search Stuart 展开
#include<string>
using namespace std;
void insert(int l,string s,string str[],int &sum)
{
int i;
if(sum==0)
str[0]=s;
else
{
for(i=sum;i>=l;--i)
str[i]=str[i-1];
str[i]=s;
}
++sum;
}
void search(string s,string str[],int sum)
{
int i;
for(i=0;i<sum;++i)
if(str[i]==s)
cout<<i+1;
}
void show(string str[],int sum)
{
int i;
for(i=0;i<sum;++i)
if(i)
cout<<" ";
cout<<str[i];
}
void del(string s,string str[],int &sum)
{
int i,j;
for(i=0;i<sum;++i)
if(str[i]==s)
for(j=i;j<sum-1;++j)
str[j]=str[j+1];
--sum;
}
int main()
{
int i,sum=0;
string str[200];
string d,name;
while(scanf("%s",&d)!=EOF)
{
cin>>d;
if(d=="insert")
{
cin>>i>>name;
insert(i,name,str,sum);
}
else if(d=="search")
{
cin>>name;
search(name,str,sum);
}
else if(d=="show")
show(str,sum);
else if(d=="delete")
{
cin>>name;
del(name,str,sum);
}
}
return 0;
}
输入为:
insert 1 Stuart
insert 2 Bernadette
show
search Stuart
delete Stuart
show
insert 2 Stuart
show
insert 1 Amy
insert 2 Leslie
insert 3 Stephanie
show
delete Leslie
show
search Stuart 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询