如何将庞大程序多文件编译!此程序是多功能的学生系统!就是将他的函数分开!

NYNYC 2009-01-03 06:56:30
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
struct student/*结构体*/
{char name[10];
int num,xuefen;
int score[3];
float ave;
}stud[SIZE],work;
void main()

{void sortA(void);/*按平均分升序排列并存档*/
void sortB(void);/*按平均分降序排列并存档*/
void sortC(void);/*按学号升序排列并存档*/
void sortD(void);/*按学号降序排列并存档*/
void save(void);/*存档*/
int i,xuefen;
float sum[SIZE];
FILE *fp1;
for(i=0;i<SIZE;i++)
{scanf("%d %s %d %d %d",&stud[i].num,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
stud[i].ave=sum[i]/3;
if(stud[i].score[0]>=60) stud[i].xuefen+=1;
if(stud[i].score[1]>=60) stud[i].xuefen+=1;
if(stud[i].score[2]>=60) stud[i].xuefen+=1;
}
save();
printf("please me number 0-3! 0:up by ave! 1:down by ave! 2up by NO! down NO!");
scanf("%d",&i);
switch(i)
{ case0:sortA();break;
case1:sortB();break;
case2:sortC();break;
case3:sortD();break;
}
fp1=fopen("scort.txt","rb");
printf("\n NO name score1 score2 score3 ave xuefen\n");
printf("-----------------------------------------------------------\n");
for(i=0;i<SIZE;i++)
{fread(&stud[i],sizeof(struct student),1,fp1);
printf("%3d %-10s %7d %7d %7d %8.2f %d\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave,stud[i].xuefen);

}

fclose(fp1);
}

=================================/*以下是SOVE SORTA B C D的定义*/================

void save(void)
{FILE *fp;
int i;
if((fp=fopen("student.txt","wb"))==NULL)
{printf("The file can't open\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)
{printf("file write error\n");
return;
}
fclose(fp);
}

void sortA(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file cant open\n\n");
exit(0);
}
if((fp2=fopen("score.txt","wb"))==NULL)
{printf("the file cant open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("file read error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].ave<stud[j].ave)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
void sortB(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("file read error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].ave>stud[i].ave)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);

}
void sortC(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("the file is error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].num<stud[j].num)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
void sortD(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("the file read error\n");
exit(0);
}
for(i=0;i>SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].num>stud[j].num)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
NYNYC 2009-01-04
  • 打赏
  • 举报
回复
最好能具体的帮我拆开下!我已经想了,试了好久了!还是弄不出!劳烦一下。。。谢谢
jingzhongrong 2009-01-03
  • 打赏
  • 举报
回复
此贴应该发到C++区吧
在工程中添加头文件比如func.h
#ifndef FUNC_H_
#define FUNC_H_
void sortA(void);
void sortB(void);
void sortC(void);
#endif


添加对应的cpp或c文件,添加上面函数的实现:
#include "func.h"

void sortA(void)
{
...
}
void sortB(void)
{
...}
void sortC(void)...
qufo 2009-01-03
  • 打赏
  • 举报
回复
看不太懂,我觉得你这样写也可以。
就这么一小东西,搞那么多更没意思。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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