读书人

帮小弟我看看错在哪了高分悬赏

发布时间: 2012-03-15 11:50:38 作者: rapoo

帮我看看,错在哪了,高分悬赏
//开始了
#include <stdio.h>
#include <conio.h>


//定义一个结构
struct stu
{
float Math , English , Chinese;//学生的学号和成绩
int Num;
char* name;//学生的名字
stu *next;//指向下一个节点的指针
};
struct stu *head , *tail , *ptr1 , *ptr2;//声明变量
int renshu,a;

void input (void )
{
FILE *fp1,*fp2;
int i,j,k;

fp1=fopen( "C:\\学生.txt ", "r+ ");
fprintf(fp1, "学号| 名字| 语文| 数学| 英语\n ");
fclose(fp1);
fp1=fopen( "C:\\学生.txt ", "a ");
ptr1=head;

printf( "请输入要输入的人数: ");
scanf( "%d ",&renshu);
for (a = 1 ;a <= renshu; a++ )//给分配内存空间
{
if (1 == a)
{
ptr1 = new struct stu;
head = tail = ptr2 = ptr1;
}

ptr1=new struct stu;
ptr2-> next=ptr1;
ptr2=tail=ptr1;
}
tail-> next=NULL;

for (i=1;i <=renshu;i++)
{
printf( "请分别输入第%d位学生的语数外成绩,用逗号隔开: ",i);
scanf( "%f,%f,%f ",&ptr1-> Chinese,&ptr1-> Math,&ptr1-> English);
printf( "请输入这位学生的姓名: ");
scanf( "%s ",&ptr1-> name);
printf( "请输入这位学生的学号: ");
scanf( "%d ",&ptr1-> Num);
printf( "\n\n ");

fprintf(fp1, "%4d|%6s|%4.2f|%4.2f|%4.2f\n ",ptr1-> Num,&ptr1-> name,ptr1-> Chinese,ptr1-> Math,ptr1-> English);
if (i == renshu)
ptr1=ptr1-> next;
}

delete head;
fclose (fp1);

}
void paixu(void)//正在制作中
{
char ch;
char ch1[10];
FILE* fp1;
int i,j,a,r,c;
char* tempname;
float tempChinese,tempMath,tempEnglish;
int tempNum;
printf( "\n\n\t\t\t1.按语文成绩排序\n\t\t\t2.按数学成绩排序\n\t\t\t3.按英语成绩排序\n\t\t\t4.按学号排序\n\n ");


fp1=fopen( "C:\\学生.txt ", "r+ ");
a=-1;
while (feof(fp1) == 0)
{
ch=fgetc(fp1);
if (ch == '\n ')
a++;
}

for (i = 1 ;i <= a; i++ )//给分配内存空间
{
if (1 == a)
{
ptr1 = new struct stu;
head = tail = ptr2 = ptr1;
}

ptr1=new struct stu;
ptr2-> next=ptr1;


ptr2=tail=ptr1;
}

while (feof(fp1)==0)
{
ptr1-> Num=fscanf(fp1, "%4d ");
ch=fgetc(fp1);
for (c=0;c < 6;c++)
{
ch1[c]=fgetc(fp1);
}
ptr1-> name=ch1;
ch=fgetc(fp1);
ptr1-> Chinese=fscanf(fp1, "%4d ");
ch=fgetc(fp1);
ptr1-> Math=fscanf(fp1, "%4d ");
ch=fgetc(fp1);
ptr1-> English=fscanf(fp1, "%4d ");
ch=fgetc(fp1);
ptr1=ptr1-> next;

}


tail-> next=NULL;

printf( "请选择: ");
b:
scanf( "%d ",&r);

switch(r)
{
case 1:
for(i = 0;i < a;i++ )
{
for(j=a-1-i,ptr2=head; j < a;j++ )
{
if (ptr1-> Chinese < ptr2-> Chinese)
{
tempChinese=ptr2-> Chinese;
tempname=ptr2-> name;
tempMath=ptr2-> Math;
tempEnglish=ptr2-> English;
tempNum=ptr2-> Num;


ptr2-> Chinese=ptr1-> Chinese;
ptr2-> English=ptr1-> English;
ptr2-> Math=ptr1-> Math;
ptr2-> name=ptr1-> name;
ptr2-> Num=ptr1-> Num;

ptr1-> Chinese=tempChinese;
ptr1-> Math=tempMath;
ptr1-> English=tempEnglish;
ptr1-> Num=tempNum;
ptr1-> name=tempname;


}

if (ptr2-> next!=NULL)
ptr2=ptr2-> next;

}
ptr1=ptr1-> next;

}
case 2:
for(i = 0;i < a;i++ )


{
for(j=a-1-i,ptr2=head; j < a;j++ )
{
if (ptr1-> Math < ptr2-> Math)
{
tempChinese=ptr2-> Chinese;
tempname=ptr2-> name;
tempMath=ptr2-> Math;
tempEnglish=ptr2-> English;
tempNum=ptr2-> Num;


ptr2-> Chinese=ptr1-> Chinese;
ptr2-> English=ptr1-> English;
ptr2-> Math=ptr1-> Math;
ptr2-> name=ptr1-> name;
ptr2-> Num=ptr1-> Num;

ptr1-> Chinese=tempChinese;
ptr1-> Math=tempMath;
ptr1-> English=tempEnglish;
ptr1-> Num=tempNum;
ptr1-> name=tempname;


}

if (ptr2-> next!=NULL)
ptr2=ptr2-> next;

}
ptr1=ptr1-> next;

}
case 3:
for(i = 0;i < a;i++ )
{
for(j=a-1-i,ptr2=head; j < a;j++ )
{
if (ptr1-> English < ptr2-> English)
{
tempChinese=ptr2-> Chinese;
tempname=ptr2-> name;
tempMath=ptr2-> Math;
tempEnglish=ptr2-> English;


tempNum=ptr2-> Num;


ptr2-> Chinese=ptr1-> Chinese;
ptr2-> English=ptr1-> English;
ptr2-> Math=ptr1-> Math;
ptr2-> name=ptr1-> name;
ptr2-> Num=ptr1-> Num;

ptr1-> Chinese=tempChinese;
ptr1-> Math=tempMath;
ptr1-> English=tempEnglish;
ptr1-> Num=tempNum;
ptr1-> name=tempname;


}

if (ptr2-> next!=NULL)
ptr2=ptr2-> next;

}
ptr1=ptr1-> next;

}
for(i = 0;i < a;i++ )
{
for(j=a-1-i,ptr2=head; j < a;j++ )
{


[解决办法]
上来就贴大段的代码, 没有很多人会耐心的看的,
你还是把你问题的关键点写出来,讨论嘛, 这样的话好的多

[解决办法]
什么错了,这么长看起来n麻烦哦
[解决办法]
paixu()里申请内存空间的时候有问题,自己看看吧,学着debug。
[解决办法]
你应该把你觉得有问题的片段拿出来就ok了
太多了,一般没人愿意看
我本来想看看的,结果....
[解决办法]
同楼上的楼上
[解决办法]
同意上上上楼的。
[解决办法]
同意楼上的楼上的楼上
[解决办法]
这么长
谁会去看啊
楼主
[解决办法]
没兴趣看代码堆~

读书人网 >C语言

热点推荐