回复 2楼 宝宝想你
#include"head.h"
void menu()
{
printf("
主菜单
\n");
printf("
1.请输入学生信息
\n");
printf("
2.学生信息显示
\n");
printf("
3.按姓名查找
\n");
printf("
4.删除学生信息
\n");
printf("
5.增加学生信息
\n");
printf("
6.修改学生信息
\n");
printf("
7.保存到文件
\n");
printf("
8.读取文件
\n");
printf("
9.拷贝文件
\n");
printf("
10.排序
\n");
printf("
11.退出
\n");
}
void menu_system()
{
menu();
int a,lenth=0;
while (1){
do{
printf("请输入所选功能号:\n");
scanf("%d", &a);
} while (a < 1 || a>11);
switch (a)
{
case 1:lenth=enter_info(stu); break;
case 2:out_info(stu,lenth); break;
case 3:search_name(stu,lenth); break;
case 4:lenth=delet(stu,lenth); break;
case 5:lenth=add(stu,lenth); break;
case 6: modify(stu, lenth); break;
case 7:write(stu, lenth); break;
case 8:read(stu, lenth); break;
case 9:copy(); break;
case 10:sort(stu, lenth); break;
case 11:exit(0);
}
menu();
}
}
int enter_info(students stu[])
{
int i,n;
system("cls");
printf("please input how many people's info that you want input!\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
system("cls");
printf("input %dth xuehao!\n", i + 1);
scanf_s("%s", stu[i].num, 20);
printf("input %dth name!\n", i + 1);
scanf_s("%s", stu[i].name, 30);
printf("input %dth sex!\n", i + 1);
scanf_s("%s", stu[i].sex, 4);
printf("input %dth age!\n", i + 1);
scanf_s("%d", &stu[i].age);
}
return n;
}
void out_info(students stu[],int n)
{
system("cls");
int i;
printf("--------------students list----------------------\n");
printf("NO.
name
sex
age\n");
for (i = 0; i < n; i++)
{
printf("%s
%s
%s
%d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
printf("\n");
}
printf("------------------------------------------------\n");
}
int find_name(students stu[],int n,char* name)
{
int i;
for (i = 0; i < n; i++)
{
if (strcmp(name, stu[i].name) == 0)
return i;
}
return -1;
}
int find_num(students stu[], int n,char* num)
{
int i;
for (i = 0; i < n; i++)
{
if (strcmp(num, stu[i].num) == 0)
return i;
}
return -1;
}
int by_which()
{
int a;
printf("------------------------------------------------\n");
printf("
1.by student name
\n");
printf("
2.by student number
\n");
printf("------------------------------------------------\n");
do{
printf("
choise:");
scanf("%d", &a);
} while (a < 1 || a>2);
system("cls");
return a;
}
void search_name(students stu[],int n)
{
system("cls");
int i;
switch (by_which())
{
case 1:
char name[30];
printf("please enter name that you want search!\n");
scanf("%s", name, 30);
i = find_name(stu, n, name);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
printf("--------------students search list----------------------\n");
printf("NO.
name
sex
age\n");
printf("%s
%s
%s
%d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
printf("\n");
}break;
default:
char num[20];
printf("please enter num that you want search!\n");
scanf("%s", num, 20);
i = find_num(stu, n, num);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
printf("--------------students search list----------------------\n");
printf("NO.
name
sex
age\n");
printf("%s
%s
%s
%d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
printf("\n");
}break;
}
}
int delet(students stu[],int n)
{
int i,j;
system("cls");
switch (by_which())
{
case 1:
char name[30];
printf("please enter name that you want delet!\n");
scanf("%s", name, 30);
i = find_name(stu, n, name);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
printf("deleting......!\n");
for (j = i; j <n; j++)
{
strcpy_s(stu[j].num, strlen(stu[j + 1].num) + 1, stu[j + 1].num);
strcpy_s(stu[j].name, strlen(stu[j + 1].name) + 1, stu[j + 1].name);
strcpy_s(stu[j].sex, strlen(stu[j + 1].sex) + 1, stu[j + 1].sex);
stu[j].age = stu[j + 1].age;
}
printf("delet succuse");
n--;
}
return n;
break;
default:
char num[20];
printf("please enter num that you want delet!\n");
scanf("%s", num, 20);
i = find_num(stu, n, num);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
printf("deleting......!\n");
for (j = i; j <n; j++)
{
strcpy_s(stu[j].num, strlen(stu[j + 1].num) + 1, stu[j + 1].num);
strcpy_s(stu[j].name, strlen(stu[j + 1].name) + 1, stu[j + 1].name);
strcpy_s(stu[j].sex, strlen(stu[j + 1].sex) + 1, stu[j + 1].sex);
stu[j].age = stu[j + 1].age;
}
printf("delet succuse");
n--;
}
return n;
break;
}
}
int add(students stu[],int n)
{
int i, j;
system("cls");
switch (by_which())
{
case 1:
char name[30];
printf("please enter name that you want add!\n");
scanf("%s", name, 30);
i = find_name(stu, n, name);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
printf("adding......!\n");
for (j = n; j >i; j--)
{
strcpy_s(stu[j].num, strlen(stu[j - 1].num) + 1, stu[j - 1].num);
strcpy_s(stu[j].name, strlen(stu[j - 1].name) + 1, stu[j - 1].name);
strcpy_s(stu[j].sex, strlen(stu[j - 1].sex) + 1, stu[j - 1].sex);
stu[j].age = stu[j - 1].age;
}
printf("input %dth xuehao!\n", i + 1);
scanf_s("%s", stu[i].num, 20);
printf("input %dth name!\n", i + 1);
scanf_s("%s", stu[i].name, 30);
printf("input %dth sex!\n", i + 1);
scanf_s("%s", stu[i].sex, 4);
printf("input %dth age!\n", i + 1);
scanf_s("%d", &stu[i].age);
n++;
printf("add succuse");
}
return n;
break;
default:
char num[20];
printf("please enter num that you want add!\n");
scanf("%s", num, 20);
i = find_num(stu, n, num);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
printf("adding......!\n");
for (j = n; j >i; j--)
{
strcpy_s(stu[j].num, strlen(stu[j - 1].num) + 1, stu[j - 1].num);
strcpy_s(stu[j].name, strlen(stu[j - 1].name) + 1, stu[j - 1].name);
strcpy_s(stu[j].sex, strlen(stu[j - 1].sex) + 1, stu[j - 1].sex);
stu[j].age = stu[j - 1].age;
}
printf("input %dth xuehao!\n", i + 1);
scanf_s("%s", stu[i].num, 20);
printf("input %dth name!\n", i + 1);
scanf_s("%s", stu[i].name, 30);
printf("input %dth sex!\n", i + 1);
scanf_s("%s", stu[i].sex, 4);
printf("input %dth age!\n", i + 1);
scanf_s("%d", &stu[i].age);
n++;
printf("add succuse");
}
return n;
break;
}
}
void modify(students stu[], int n)
{
int i;
switch (by_which()){
case 1:
char name[30];
printf("please enter name that you want search!\n");
scanf("%s", name, 30);
i = find_name(stu, n, name);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
int a;
printf("------------------------------------------------\n");
printf("
1.modify name
\n");
printf("
2.modify number
\n");
printf("
3.modify sex
\n");
printf("
4.modify age
\n");
printf("------------------------------------------------\n");
do{
printf("
choise:");
scanf("%d", &a);
} while (a < 1 || a>4);
system("cls");
switch (a)
{
case 1:
char name[30];
printf("please input the name you want modify!\n");
scanf("%s", name, 30);
strcpy_s(stu[i].name, strlen(name) + 1, name);
printf("modify success!\n");
break;
case 2:
char num[30];
printf("please input the num you want modify!\n");
scanf("%s", num, 30);
strcpy_s(stu[i].num, strlen(num) + 1, num);
printf("modify success!\n");
break;
case 3:
char sex[4];
printf("please input the sex you want modify!\n");
scanf("%s", sex, 4);
strcpy_s(stu[i].sex, strlen(sex) + 1, sex);
printf("modify success!\n");
break;
default:
int age;
printf("please input the age you want modify!\n");
scanf("%d", &age);
stu[i].age = age;
printf("modify success!\n");
break;
}
}break;
default:
char num[20];
printf("please enter num that you want search!\n");
scanf("%s", num, 20);
i = find_num(stu, n, num);
if (i == -1)
{
printf("--------------students search list----------------------\n");
printf("查无此人!\n");
}
else
{
int a;
printf("------------------------------------------------\n");
printf("
1.modify name
\n");
printf("
2.modify number
\n");
printf("
3.modify sex
\n");
printf("
4.modify age
\n");
printf("------------------------------------------------\n");
do{
printf("
choise:");
scanf("%d", &a);
} while (a < 1 || a>4);
system("cls");
switch (a)
{
case 1:
char name[30];
printf("please input the name you want modify!\n");
scanf("%s", name, 30);
strcpy_s(stu[i].name, strlen(name) + 1, name);
printf("modify success!\n");
break;
case 2:
char num[30];
printf("please input the num you want modify!\n");
scanf("%s", num, 30);
strcpy_s(stu[i].num, strlen(num) + 1, num);
printf("modify success!\n");
break;
case 3:
char sex[4];
printf("please input the sex you want modify!\n");
scanf("%s", sex, 4);
strcpy_s(stu[i].sex, strlen(sex) + 1, sex);
printf("modify success!\n");
break;
default:
int age;
printf("please input the age you want modify!\n");
scanf("%d", &age);
stu[i].age = age;
printf("modify success!\n");
break;
}
}
}
}
void write(students stu[],int n)
{
system("cls");
int i;
FILE *file;
if ((file = fopen("F:system.txt", "w")) == NULL)
{
printf("write error!");
}
else
{
printf("write success!");
fprintf(file, "--------------students list----------------------\n");
fprintf(file, "NO.
name
sex
age\n");
for (i = 0; i < n; i++)
{
fprintf(file, "%s
%s
%s
%d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
fprintf(file, "\n");
}
fprintf(file, "------------------------------------------------\n");
fclose(file);
}
}
void read(students stu[], int n)
{
system("cls");
char buf[500] ={NULL};
FILE *file;
if ((file = fopen("F:system.txt", "r")) == NULL)
{
printf("read error!\n");
}
else
{
printf("read success!\n");
while (!feof(file))
{
printf("%s", buf);
fgets(buf, 500, file);
}
fclose(file);
}
}
void copy()
{
char outfile[20];
char buf[100] = {NULL};
FILE*sfp,*fp;
sfp = (fopen("F:system.txt", "r"));
if (sfp ==NULL)
{
printf("read error!\n");
}
else
{
printf("read success!\n");
printf("请输入要拷贝到的文件:形式如D:\\text\\t.txt:\n");
scanf("%s",outfile,20);
fp = (fopen(outfile, "w"));
if (fp == NULL)
{
printf("write error!\n");
}
else
{
while (!feof(sfp))
{
fputs(buf, fp);
fgets(buf, 100, sfp);
}
fclose(sfp);
fclose(fp);
}
}
}
void sort(students stu[], int n)
{
struct students
{
char num[20];
char name[30];
char sex[4];
int age;
}t[N];
int i, j,min;
switch (by_which())
{
case 1:
for (i = 0; i < n; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (strcmp(stu[min].name, stu[j].name) > 0)
{
min = j;
}
}
strcpy_s(t[1].num, strlen(stu[i].num) + 1, stu[i].num);
strcpy_s(stu[i].num, strlen(stu[min].num) + 1, stu[min].num);
strcpy_s(stu[min].num, strlen(t[1].num) + 1, t[1].num);
strcpy_s(t[1].name, strlen(stu[i].name) + 1, stu[i].name);
strcpy_s(stu[i].name, strlen(stu[min].name) + 1, stu[min].name);
strcpy_s(stu[min].name, strlen(t[1].name) + 1, t[1].name);
strcpy_s(t[1].sex, strlen(stu[i].sex) + 1, stu[i].sex);
strcpy_s(stu[i].sex, strlen(stu[min].sex) + 1, stu[min].sex);
strcpy_s(stu[min].sex, strlen(t[1].sex) + 1, t[1].sex);
}
break;
default:
for (i = 0; i < n; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (strcmp(stu[min].num, stu[j].num) > 0)
{
min = j;
}
}
strcpy_s(t[1].num, strlen(stu[i].num) + 1, stu[i].num);
strcpy_s(stu[i].num, strlen(stu[min].num) + 1, stu[min].num);
strcpy_s(stu[min].num, strlen(t[1].num) + 1, t[1].num);
strcpy_s(t[1].name, strlen(stu[i].name) + 1, stu[i].name);
strcpy_s(stu[i].name, strlen(stu[min].name) + 1, stu[min].name);
strcpy_s(stu[min].name, strlen(t[1].name) + 1, t[1].name);
strcpy_s(t[1].sex, strlen(stu[i].sex) + 1, stu[i].sex);
strcpy_s(stu[i].sex, strlen(stu[min].sex) + 1, stu[min].sex);
strcpy_s(stu[min].sex, strlen(t[1].sex) + 1, t[1].sex);
}
break;
}
}
void main(){
menu_system();
system("pause");
}