先建立一个包含学号、姓名、年龄、性别、成绩的学生信息文件,然后按照菜单完成各类操作,结果在屏幕上显示:
(1)用归并排序算法对成绩排序;
(2)用顺序查找算法对性别进行查询(要求找到所有符合条件的记录);
(3)用二分法查找算法对学号进行查询;
(4)退出
1.建立一个txt文件
2.存入数据
注意编码格式,不然会乱码。
3.结果截图
-
输入读入的数据个数
-
菜单选择
-
用归并排序算法对成绩排序;
-
用顺序查找算法对性别进行查询(要求找到所有符合条件的记录)
-
用二分法查找算法对学号进行查询
-
退出
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#define MAX 16
int n=0;
typedef struct
{
//学号、姓名、年龄、性别、成绩
long Sno;
char Sname[10];
int Sage;
char Ssex[4];
float score;
}rectype;
void main()
{
void read_file(rectype R[]);
void print_data(rectype R[]);
void ShellSort(rectype R[]);
void QuickSort(rectype R[],int low,int high);
void HeapSort(rectype R[]);
void menu(rectype R[]);
void SeqFind(rectype R[],char* sex);
rectype R[MAX];
read_file(R);
//SeqFind(R,"女");
menu(R);
//print_data(R);
}
void read_file(rectype R[])
{
FILE *fp1;
int i;
fp1=fopen(".\\data1.txt","r");
printf("Please input select numbers(n<=15):");
scanf("%d",&n);
//fscanf(fp1,"%ld,%s,%d");
for(i=1;i<=n;i++)
{
//学号、姓名、年龄、性别、成绩
fscanf(fp1,"%d %s %d %s %f",&R[i].Sno,R[i].Sname,&R[i].Sage,R[i].Ssex,&R[i].score);
}
fclose(fp1);
}
void print_data(rectype R[])
{
int i;
//printf("\nSorted datas is:\n");
printf(" 学号 姓名 成绩\n");
for(i=1;i<=n;i++)
{
printf("%d %s %d %s %f\n",R[i].Sno,R[i].Sname,R[i].Sage,R[i].Ssex,R[i].score);
}
printf("\n");
}
void Merge(rectype R[],int low,int high,int mid)
{
int i,j,k;
rectype T[MAX+1];
i=low;
j=mid+1;
k=low;
while(i<=mid && j<=high)
{
if(R[i].score<=R[j].score)
T[k++]=R[i++];
else
T[k++]=R[j++];
}
while(i<=mid)
{
T[k++]=R[i++];
}
while(j<=high)
{
T[k++]=R[j++];
}
for(i=low;i<=high;i++)
{
R[i]=T[i];
}
}
void SeqFind(rectype R[],char *sex)
{
int i;
int flag=0;
printf("\n");
for(i=1;i<=n;i++)
{
if(strcmp(R[i].Ssex,sex) == 0)
{
printf("%d %s %d %s %f\n",R[i].Sno,R[i].Sname,R[i].Sage,R[i].Ssex,R[i].score);
flag=1;
}
}
if(flag==0)
{
printf("没有找到!\n");
}
}
void MergeSort(rectype R[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
MergeSort(R,low,mid);
MergeSort(R,mid+1,high);
Merge(R,low,high,mid);
}
}
int Partion(rectype R[],int low,int high)
{
int i,j;
i=low;
j=high;
R[0]=R[i];
do
{
if((R[j].Sno>=R[0].Sno)&&(j>i))
j--;
R[i]=R[j];
if((R[low].Sno<=R[0].Sno)&&(i<j))
i++;
R[j]=R[i];
}while(i!=j);
R[i]=R[0];
return i;
}
void QuickSort(rectype R[],int low,int high)
{
int i;
if(low<high)
{
i=Partion(R,low,high);
QuickSort(R,low,i-1);
QuickSort(R,i+1,high);
}
}
void BinSearch(rectype R[],int sno)
{
int low,high,mid;
low=1;
high=n;
while(low<=high)
{
mid=(low+high)/2;
if(R[mid].Sno>sno)
high=mid-1;
else
low=mid+1;
}
if(R[high].Sno==sno)
{
printf("%d %s %d %s %f\n",R[high].Sno,R[high].Sname,R[high].Sage,R[high].Ssex,R[high].score);
}
else
{
printf("没有找到!\n");
}
}
void gotoxy(int x,int y)
{
COORD Pos;
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
Pos.X = x;
Pos.Y = y;
SetConsoleCursorPosition(hCon,Pos);
}
void menu(rectype R[])
{
int i;
int j;
int sno;
//System("cls");
do
{
system("cls");
gotoxy(30,3);
printf("1.用归并排序算法对成绩排序");
gotoxy(30,5);
printf("2.用顺序查找算法对性别进行查询");
gotoxy(30,7);
printf("3.用二分法查找算法对学号进行查询");
gotoxy(30,9);
printf("4.退出");
printf("\n");
printf("请输入您的选择:\n");
scanf("%d",&i);
getchar();
switch(i)
{
case 1:
print_data(R);
MergeSort(R,1,n);
printf("##");
//write_file(R,".//after_shellsort.txt");
printf("用归并排序算法对成绩排序:\n");
print_data(R);
getchar();
break;
case 2:
print_data(R);
printf("用顺序查找算法对性别进行查询:\n");
printf("请输入性别1(女)2(男)");
scanf("%d",&j);
if(j==1)
SeqFind(R,"女");
if(j==2)
SeqFind(R,"男");
//print_data(R);
getchar();
getch();
break;
case 3:
print_data(R);
QuickSort(R,1,n);
printf("用快速排序对学号进行排序:\n");
print_data(R);
printf("用二分法查找算法对学号进行查询:\n");
printf("请输入查找的学号:\n");
scanf("%d",&sno);
BinSearch(R,sno);
getch();
break;
case 4:
//cls();
printf("bye-bye");
getchar();
exit(1);
default:
printf("again!");
}
}while(i!=0);
}