第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[] 所以不需要返回 看来你要在看看指针和数组这块
=========================================
很遗憾 你写的两个都错了 而且都是逻辑错误