一道关于“删除”的C++题目 应定要帮我看一下,我很认真的研究过,有点想不通

fun函数的功能是,从s所指的字符串中删除给定的字符。同一字母的大小写按不同字符处理。
我编的程序(一)是:
#include<stdio.h>
#include<string.h>
void fun(char s[ ],char c)
{
int i;
char t[ ];
for(i=0;i<strlen(s);i++)
{
if (s[i]==c) t[i]=s[i++];
}
return t; 这里用不用写RETURN呀?是不是t代表数组首地址就不用写了?
}
main()
{
static char str[ ]="turbo c and borland c++";
char ch;
printf("原始字符串:%s\n",str);
printf("输入一个字符:");
scanf("%c",&ch);
fun(str,ch);
printf("str[ ]=%s\n",str);
}

我编的程序(第二)是:
#include<stdio.h>
#include<string.h>
void fun(char s[ ],char c)
{
int i;
while (s[ i ])
{
if (s[i]==c) s[i]=s[i++];
else i++;
}

}
main()
{
static char str[ ]="turbo c and borland c++";
char ch;
printf("原始字符串:%s\n",str);
printf("输入一个字符:");
scanf("%c",&ch);
fun(str,ch);
printf("str[ ]=%s\n",str);
}

但是正确答案是如下的,我看了一下觉得自己写的程序也能表达同样的意思:
void fun(char s[ ],char c)
{
char *p=s;
int i;
while(*p)
{
if (*p!=c) s[i++]=*p; 这里是重建了新的s[i]数组,我觉得不清楚。
不如新建个数组t.
p++;
}
s[i]='\0';
}
main()
{
static char str[ ]="turbo c and borland c++";
char ch;
printf("原始字符串:%s\n",str);
printf("输入一个字符:");
scanf("%c",&ch);
fun(str,ch);
printf("str[ ]=%s\n",str);
}
其实这只是一道程序编写题,自需要编写fun 函数部分
我不理解为什么不用加return,能解释一下原因吗?
十分感谢及时解答我的题目,我最想问的是我编写的两个程序究竟对不对?请赐教。这是一道2级机试题目。

你好,那个你说的正确答案也有一个小问题,就是他int i没有初始化为0.
你的两个函数都不太对,我帮你把你的第一个函数稍微改了一下,保持你的初衷。你想返回值,我在主函数里帮你也改了一下,fun函数可以返回值了,因为一开始你第一个函数你将返回值设成了void(无返回值),所以不能返回数组t。
现在这样就好了:
你要运行那个题目给的正确答案就把第一个函数的注释去掉,把第二个函数注释掉就好了,在把主函数main里的两行注释掉的改回来,把最后一行注释掉。第二个函数是我帮你改的你第一个函数:
#include<stdio.h>
#include<string.h>

char t[20]; //声明成全局变量

/*
void fun(char s[],char c)
{
char *p=s;
int i=0;
while(*p)
{
if(*p!=c)
s[i++]=*p;
p++;
}
s[i]='\0';
}
*/

char *fun(char s[],char c)
{
int i,j;
//char t[] = "";
for(i=0,j=0;i<strlen(s);i++,j++)
{
if (s[i]==c)
{ t[j]= s[i+1];
j--;}
else
t[j]=s[i];

}
return t; //用return &(t[0]); 也可以
}

/*
void fun(char s[],char c)
{
int i=0;
while (s[i])
{
if (s[i]==c)
s[i]=s[i++];
else
i++;
}
}*/

main()
{
static char str[]="turbo c and borland c++";
char ch;
printf("原始字符串:%s\n",str);
printf("输入一个字符:");
scanf("%c",&ch);
//fun(str,ch);
//printf("str[]=%s\n",str);
printf("str[]=%s\n", fun(str,ch));
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-02-10
前几天刚好帮朋友写过个类似的 贴出来给你参考吧
#include<iostream.h>
#include<malloc.h>
#include<iomanip.h>
#include<string.h>
#define LEN sizeof(struct student)
#define NULL 0

struct student
{ long num;
float english; //结构体定义学生的数据 包括学号 和各科成绩
float math;
float computer;
struct student *next;
};

int n; //全局变量 表示记录学生的个数

struct student *creat(void)
{ struct student *head; //构造函数 链表由head开始 到NULL结束
struct student *p1,*p2; //构造函数接收数据 直到输入的NUM=0
n=0;
p1=p2=(struct student *) malloc(LEN);

cin>>p1->num>>p1->english>>p1->math>>p1->computer;

head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *) malloc(LEN);

cin>>p1->num>>p1->english>>p1->math>>p1->computer;
}

p2->next=NULL;

return(head);
}

void print(struct student *head)
{ struct student *p;
cout<<endl<<"目前所记录的"<<n<<"位学生的成绩分别是(学号 英语 数学 计算机)"<<endl; //输出 函数
p=head;
if(head!=NULL) //输出链表的内容
do
{ cout<<p->num<<" "<<p->english<<" "<<p->math<<" "<<p->computer<<endl;
p=p->next;
}
while(p!=NULL);

}

struct student *del(struct student *head,long num)
{ struct student *p1,*p2;
if(head==NULL)
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next; //删除结点 函数 按指定学生学号(num)删除结点
}
if(num==p1->num)
{if(p1==head) head=p1->next;else p2->next=p1->next;
cout<<endl<<num<<"已被删除"<<endl;
n=n-1;
}

else cout<<endl<<num<<" 无法找到 "<<endl;

return(head);
}

struct student *insert(struct student *head,struct student *std)
{ struct student *p0,*p1,*p2;
p1=head;
p0=(struct student *) malloc(LEN);
p0=std;
if(head==NULL)
else
{ while( (p0->num>p1->num) && (p1->next!=NULL) )
{p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num) //插入结点 函数 在已排序情况下 选择合适位置插入(按学号从小到大)
{if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}

else

}
n=n+1;

return(head);
}

int serch(struct student *head,long serch_num)
{ struct student *p1;
p1=head;
while(p1!=NULL) //寻找指定学号的学生的信息 并输出
{if(p1->num==serch_num)
{ cout<<"学生"<<serch_num<<"的成绩(英语 数学 计算机)为:"<<endl;
cout<<p1->english<<" "<<p1->math<<" "<<p1->computer<<endl; return 0;
}
else p1=p1->next;

}

cout<<"学号"<<serch_num<<",没有这个学生的记录"<<endl;
return 0;

}

int std1(struct student *head,long std1_num)
{ struct student *p1;
p1=head;
float total;
float ave;
while(p1->next!=NULL) // 寻找指定学号的学生 输出其总分和平均分
{if(p1->num==std1_num)
{ cout<<"学生"<<std1_num<<"的总分(英语 数学 计算机)为:"<<endl;
total=p1->english+p1->math+p1->computer;
ave=total/3;
cout<<p1->english<<"+"<<p1->math<<"+"<<p1->computer<<"="<<total<<endl;
cout<<"平均分为"<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<ave<<endl;
return 0;
}
else p1=p1->next;

}

cout<<"学号"<<std1_num<<",没有这个学生的记录"<<endl;
return 0;

}

int total_ave(struct student *head,int std1_chose2)
{ struct student *p1;
float total=0.0;
if(std1_chose2==1)
{for(p1=head;p1!=NULL;p1=p1->next) //求指定科总平均分
total=total+p1->english;
cout<<"英语总平均分为:"<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<total/n<<endl;return 0;
}
else if(std1_chose2==2)
{for(p1=head;p1!=NULL;p1=p1->next)
total=total+p1->math;
cout<<"数学总平均分为:"<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<total/n<<endl;return 0;
}
else if(std1_chose2==3)
{for(p1=head;p1!=NULL;p1=p1->next)
total=total+p1->computer;
cout<<"计算机总平均分为:"<<endl;
}cout<<setiosflags(ios::fixed)<<setprecision(2)<<total/n<<endl;return 0;

}

/*
struct student *paixu(struct student *head)
{ struct student *p1;
int k=n,j;

struct student *a=new struct student[k];
for(j=0,p1=head; j<k; j++,p1=p1->next) //排序函数 一直无法通过~~~~还需要继续调试
a[j]=*p1;

for(p1=head;p1!=NULL;p1=p1->next)
del(head,p1->num);

for(j=0 ; j<k; j++)
insert(head,&a[j]);

return(head);

}

*/

void main()
{ struct student *head,std;
cout<<"请根据题目要求,按组输入3名学号,及英语、数学、计算机成绩 回车完毕 输入0 0 0 0结束:"<<endl;
head=creat();
print(head);
long del_num;
long serch_num;
long std1_num;
int std1_chose2;
int z,z1;

cout<<"请按数字键选择需要的功能:"<<endl;
cout<<"1.插入(输入一个学生的信息将它插入链表中,假定链表按学号有序)"<<endl;
cout<<"2.查找(输入一个学生学号,输出其各科成绩)"<<endl;
cout<<"3.删除(从链表中按输入的学号删除该学生)"<<endl;
cout<<"4.统计(之后 若按1,则输入学生的学号统计该生的总分及平均分;若按2,则输入课程求该门课程的总平均分)"<<endl;
cout<<"5.输出所有已存信息"<<endl;
cout<<"6.结束"<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cin>>z;
while(z!=6)
{ if(z==1){
cout<<"输入要插入的学号和成绩(英语 数学 计算机):"<<endl;
cin>>std.num>>std.english>>std.math>>std.computer;
head=insert(head,&std);
print(head);
cout<<endl;
cout<<endl;
cout<<endl;
}
if(z==2){
cout<<"请输入要搜索的学号:"<<endl; cin>>serch_num;serch(head,serch_num);
cout<<endl;cout<<endl;cout<<endl;
}
if(z==3){
cout<<"请输入要删除的学号"<<endl;cin>>del_num;head=del(head,del_num);
cout<<endl;cout<<endl;cout<<endl;
}
if(z==4){
cout<<"输入需要的功能"<<endl;
cout<<"若按1,则输入学生的学号统计该生的总分及平均分"<<endl;
cout<<"若按2,则输入课程求该门课程的总平均分"<<endl;
cin>>z1;
{ if(z1==1){
cout<<"请输入学号:"<<endl;cin>>std1_num;std1(head,std1_num);
cout<<endl;cout<<endl;cout<<endl;
}
if(z1==2)
{
cout<<"请输入数字选择要统计平均分的科目:(输入1.英语 2.数学 3.计算机)"<<endl;
cin>>std1_chose2;
total_ave(head,std1_chose2);
}
}

}
if(z==5)
cout<<"请按数字键选择需要的功能:"<<endl;
cout<<"1.插入(输入一个学生的信息将它插入链表中,假定链表按学号有序)"<<endl;
cout<<"2.查找(输入一个学生学号,输出其各科成绩)"<<endl;
cout<<"3.删除(从链表中按输入的学号删除该学生)"<<endl;
cout<<"4.统计(之后 若按1,则输入学生的学号统计该生的总分及平均分;若按2,则输入课程求该门课程的总平均分)"<<endl;
cout<<"5.输出所有已存信息"<<endl;
cout<<"6.结束"<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cin>>z;

}

}

100分诶 开玩笑~
第2个回答  2011-02-10
不用加return

还可以这样写

#include<stdio.h>
#include<string.h>

void fun(char s[ ],char c)
{
char *p=s;
while (*p++)
if (c==*p)strcpy(p,p+1);
}

main()
{
static char str[ ]="turbo c and borland c++";
char ch;
printf("原始字符串:%s\n",str);
printf("输入一个字符:");
scanf("%c",&ch);
fun(str,ch);
printf("str[ ]=%s\n",str);
}

=====================================
你定义了void 所以不需要改变
你的函数改变的是参数s[] 所以不需要返回 看来你要在看看指针和数组这块

=========================================
很遗憾 你写的两个都错了 而且都是逻辑错误
第3个回答  2011-02-11
void fun() 中返回值是void 肯定不用返回的,要返回可以用指针函数 如:
char *fun(char s[],char c) 它的返回值是一个指向字符的指针,可以用来返回数组t[]的首地址