怎么对文件里面的内容排序后输出啊~~求大神帮忙、、
小弟正在做一个小程序,就是用c同时用文件来编的一个工资管理系统,但是其中的排序部分真的不知道怎么做了。具体内容是根据来基本工资排序,然后输出其他内容,比如说工号,姓名。
程序代码:#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int id;
char name[10];
char Sex[5];
char pos[10];
int basic_sarary;
int reward;
int add_time_sarary;
char tips[10];
};
void js(); // 系 统 介 绍
void cw(); // 从 属 系 统 程 序 介 绍
void add(); // 添 加
void del(); // 删 除
void alter(); // 修 改
void Findin(); // 查 询
void display(); // 显 示
void End(); // 退 出
void menu(); // 菜 单
// 主 函 数
void main()
{
printf("\n\n\n\n");
printf("\t\t\t \4 欢 迎 使 用 本 系 统\n\n\n");
printf("\t\t┏┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┯┓");
printf("\t\t\t\t┣┙\t杰宇广告公司职工工资信息管理系统 ┕┫");
printf("\n\n\n");
printf("\t\t┣┑ 请按任意键进入系统主页面 ┍┫\n");
printf("\t\t┗┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┷┛\n\n\n");
printf("\t\t\4 请 按 任 意 键 继 续 !\n\n");
printf("\t\t\4 亲,进入界面后记得按要求操作噢! !\n\n\n");
getch();
menu();
}
// 主 菜 单
void menu()
{
int choice;
system("cls");
printf("\n");
printf("\n");
printf("\t \t★ ★ ★ ★ ★ 信 息 管 理 系 统 ★ ★ ★ ★ ★\n\n\n");
printf("\t \t ~~~职工信息管理系统菜单如下:~~~\n\n");
printf("\t \t ☆.......1......系 统 程 序 介 绍...........\n\n");
printf("\t \t ☆.......2......添 加 职 工 信 息...........\n\n");
printf("\t \t ☆.......3......删 除 职 工 信 息...........\n\n");
printf("\t \t ☆.......4......修 改 职 工 信 息...........\n\n");
printf("\t \t ☆.......5......查 询 职 工 信 息...........\n\n");
printf("\t \t ☆.......6......显 示 职 工 信 息...........\n\n");
printf("\t \t ☆.......7......退 出 应 用 程 序...........\n\n");
printf("\t \t 请选择相应的功能代号:");
scanf("%d",&choice);
switch(choice)
{
case 1:
js();
break;
case 2:
add();
break;
case 3:
del();
break;
case 4:
alter();
break;
case 5:
Findin();
break;
case 6:
display();
break;
case 7:
End();
break;
default:
printf("\n\t\t\t\t 输入无效,请您重新输入?\n");
printf("\n\t\t\t\t 请选择相应的功能代号:");
scanf("%d",&choice);
getch();
system("cls");
menu();
}
}
// 1 系 统 程 序 介 绍
void js()
{
system("cls");
printf("\n\n\t\t\t\t系统程序介绍\n\n\n");
printf("\t\t\t\3 这是一个职工工资信息管理系统,\n\n\n");
printf("\t\t\t\3 可以对职工的信息进行增,删,改,查!\n\n\n");
printf("\t\t\t\3 它能够快速的统计工资信息,\n\n\n");
printf("\t\t\t\3 节省工作时间,提高工作效率。");
printf("\n\n");
char choice;
printf("\n您想返回主菜单吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
menu();
}
else
{
system("cls");
cw();
}
}
// 从 属 系 统 程 序 介 绍
void cw()
{
system("cls");
printf("\n\n\n\n\n\n\n\t\t\t\t 操作错误啦!\n\n");
printf("\t\t\t\t 不能在继续了!!");
char choice;
printf("\n\n\t\t\t\t返回主菜单请按(Y/y)");
printf("\n\n\t\t\t\t返回系统程序介绍请按(N/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
menu();
}
else
{
system("cls");
js();
}
}
// 2 添 加 职 工 信 息
void add()
{
fflush(stdin);
system("cls");
printf("\n\n\n\n\t\t\t\t添 加 职 工 信 息 模 块\n\n\n");
FILE *fp;
fp=fopen("node.txt","ab");
if(fp==NULL)
{
printf("\t\t\t无工资信息,请重试!");
exit(1);
}
node i;
printf("\n\t请您输入要添加的职工的信息:\n\n");
printf("\t\t\t\t 职工工号:");
scanf("%d",&i.id);
printf("\n");
printf("\t\t\t\t 姓名:");
scanf("%s",i.name);
printf("\n");
printf("\t\t\t\t 性别:");
scanf("%s",i.Sex);
printf("\n");
printf("\t\t\t\t 职位:");
scanf("%s",i.pos);
printf("\n");
printf("\t\t\t\t 基本工资:");
scanf("%d",&i.basic_sarary);
printf("\n");
printf("\t\t\t\t 奖金:");
scanf("%d",&i.reward);
printf("\n");
printf("\t\t\t\t 加班费:");
scanf("%d",&i.add_time_sarary);
printf("\n");
printf("\t\t\t\t 备 注:");
scanf("%s",i.tips);
printf("\n");
fwrite(&i,sizeof(struct node),1,fp);
fclose(fp);
printf("\n\n\t\t\t\t工资信息已成功保存!!!\n");
char choice;
printf("\n\t\t\t\t您想继续添加吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
add();
}
else
{
system("cls");
menu();
}
}
// 3 删 除 职 工 信 息
void del()
{
system("cls");
printf("\n\n\n\t\t\t删 除 职 工 信 息 模 块\n\n\n");
FILE *fp;
fp=fopen("node.txt","r");
if((fp=fopen("node.txt","r"))==NULL)
{
printf("\n\n\n\n");
printf("\t\t\t没有记录!\n\n");
printf("\t\t\t请按任意健返回!!");
menu();
}
node i[10];
node temp;
int index=0;
fread(&temp,sizeof(struct node),1,fp);
while(!feof(fp))
{
i[index]=temp;
index++;
fread(&temp,sizeof(struct node),1,fp);
}
fclose(fp);
int delID;
printf("\n\t请输入要删除的职工的工号:");
scanf("%d",&delID);
fopen("node.txt","w");
if(fp==NULL)
{
printf("\t\t\t无工资信息,请您重试!");
exit(1);
}
for(int j=0;j<index;j++)
{
if(i[j].id!=delID)
{
fwrite(&i[j],sizeof(struct node),1,fp);
}
}
fclose(fp);
printf("\t\t\t\t\t工资信息已清除!!!\n");
char choice;
printf("\n\t\t\t\t 您,想继续吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
system("cls");
del();
}
else
{
system("cls");
menu();
}
}
// 4 修 改 职 工 信 息
void alter()
{
system("cls");
printf("\n\n\n\t\t\t\t修 改 职 工 信 息 模 块\n\n\n");
FILE *fp;
fp=fopen("node.txt","r");
if(fp==NULL)
{
printf("\t\t\t无工资信息,请您重试!");
exit(1);
}
node i[10];
node temp;
int index=0;
fread(&temp,sizeof(struct node),1,fp);
while(!feof(fp))
{
i[index]=temp;
index++;
fread(&temp,sizeof(struct node),1,fp);
}
fclose(fp);
fp=fopen("node.txt","w");
if(fp==NULL)
{
printf("\\t\t\t无工资信息,请您重试!");
exit(1);
}
int ID,j;
printf("\n\t请输入要修改的职工工号:");
scanf("%d",&ID);
for(j=0;j<=index-1;j++)
{
if(i[j].id!=ID)
{
fwrite(&i[j],sizeof(struct node),1,fp);
}
else
{
printf("\n\n");
printf("\t\t姓 名:");
scanf("%s",i[j].name);
printf("\t\t性 别:");
scanf("%s",i[j].Sex);
printf("\t\t职 位:");
scanf("%s",i[j].pos);
printf("\t\t基本工资:");
scanf("%d",&i[j].basic_sarary);
printf("\t\t奖 金:");
scanf("%s",&i[j].reward);
printf("\t\t加班费:");
scanf("%d",&i[j].add_time_sarary);
printf("\t\t备 注:");
scanf("%s",i[j].tips);
fwrite(&i[j],sizeof(struct node),1,fp);
}
}
fclose(fp);
printf("\t\t\t\t工资信息已修改!");
char choice;
printf("\n\t\t\t\t您想继续修改吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
alter();
}
else
{
system("cls");
menu();
}
}
// 5 查 询 职 工 信 息
void Findin()
{
system("cls");
findID:
printf("\n\n\n\t\t\t\t查 询 职 工 信 息 模 块\n\n\n");
FILE *fp;
fp=fopen("node.txt","r");
if(fp==NULL)
{
printf("\t\t\t无工资信息,请重试!\n");
exit(1);
}
int findID;
printf("\n\t请输入要查询的工资工号:");
scanf("%d",&findID);
node i;
fread(&i,sizeof(struct node),1,fp);
char choice;
while(!feof(fp))
{
if(i.id==findID)
{
printf("\n\t\t\t **********职 工 信 息***********\n\n");
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 工 号:%10d",i.id);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 姓 名:%10s",i.name);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 性 别:%10s",i.Sex);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 职 位:%10s",i.pos);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 基 本 工 资:%10d",i.basic_sarary);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 职 工 奖 金:%10d",i.reward);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 加 班 费:%10d",i.add_time_sarary);
printf("\n\t\t\t --------------------------------");
printf("\n\t\t\t 备 注:%10s",i.tips);
printf("\n\t\t\t --------------------------------");
printf("\n\n\t\t您还想继续吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
Findin();
}
else
{
system("cls");
menu();
}
}
fread(&i,sizeof(struct node),1,fp);
}
fclose(fp);
printf("\t\t\t\t\t输入的工号不存在,请您重新输入!");
printf("\n\t\t\t\t您还想继续吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
Findin();
}
else
{
system("cls");
menu();
}
goto findID;
}
// 6 显 示 职 工 信 息
void display()
{
fflush(stdin);
system("cls");
FILE *fp;
fp=fopen("node.txt","rb");
if((fp=fopen("node.txt","rb"))==NULL)
{
fclose(fp);
printf("\t\t\t无工资信息,请重试!");
exit(1);
}
node i;
fread(&i,sizeof(struct node),1,fp);
printf("\n\n\n\t\t\t显 示 职 工 信 息 模 块\n\n");
int index=0;
while(!feof(fp))
{
fflush(stdin);
index++;
printf("\n\t\t * * * * * * * 职 工 信 息 * * * * * * * \n\n",index);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t工 号:%10d\n",i.id);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t姓 名:%10s\n",i.name);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t性 别:%10s\n",i.Sex);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t职 位:%10s\n",i.pos);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t基 本 工 资:%10.1d\n",i.basic_sarary);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t奖 金:%10d\n",i.reward);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t加 班 费:%10d\n",i.add_time_sarary);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t\t备 注:%10s\n",i.tips);
printf("\n\t\t-----------------------------------------");
getch();
fread(&i,sizeof(struct node),1,fp);
}
fclose(fp);
char choice;
printf("\n\t\t\t\t 您还想继续吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
system("cls");
display();
}
else
{
system("cls");
menu();
}
}
// 8 退 出 应 用 程 序
void End()
{
system("cls");
char choice;
printf("\n\n\n\n\n\n\n");
printf("\t\t\3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3
\t\t");
printf("\n\t\t\3\t\t你确定要退出系统吗? \3\n");
printf("\n\t\t\3\t 如果你想退出请按'y',其它键将返回主菜单! \3\n\n");
printf("\t\t\3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3 \3
\t\t");
printf("\n");
printf("\n\t\t\t\t 您还想继续吗?(y/n)");
scanf(" %c",&choice);
if(choice=='Y'||choice=='y')
{
system("cls");
printf("\n\n\n\n\n\n\n\t\t\t 感 谢 您 的 使 用 !!!\n\n");
printf("\n\n\t\t\t ¤¤ !欢 迎 再 次 使 用 !¤¤\n\n\n\n");
}
else
{
menu();
}
}
时间有点紧,所以请各位帮帮小弟吧。。






