//通过菜单选择管理结构体
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
struct STU
{
char n[20];
int a;
char s;
char t[20];
};
int menu()
{
system("clear");
int choise;
printf("***********welcome******************\n");
printf("*1.input ********** \n");
printf("*2.show list ********** \n");
printf("*3.find name ********** \n");
printf("*4.delete list num ********** \n");
printf("*5.change list menber ********** \n");
printf("*6.sort list menber ********** \n");
printf("*7.exit ********** \n");
printf("***********welcome******************\n");
printf("please input you choise \n");
scanf("%d",&choise);
return choise;
}
int inputlist(struct STU *p[],int j)
{
int n,i,num;
printf("please input num you want input : \n");
scanf("%d",&n);
for(i = j; i < n + j ; i++)
{
printf("please input name age sex and phone\n");
scanf("%s%d %c%s",p[i]->n, &p[i]->a, &p[i]->s, p[i]->t);
}
printf("input finish\n");
getchar();
num = j + n;
return num;
}
int showlist(struct STU *p[],int n)
{
int i;
printf("show list \n");
for(i = 0; i < n; i++)
{
printf("%s %d %c %s\n",p[i]->n, p[i]->a, p[i]->s, p[i]->t);
}
printf(" \n");
getchar();
getchar();
return 0;
}
int findlist(struct STU *p[], int n)
{
int i;
char name[20];
printf("please input name you want find \n");
scanf("%s",name);
for(i = 0; i < n; i++)
{
if(strcmp(name,p[i]->n) == 0)
{
printf("you want find %s informations is: \n" , name);
printf("%s %d %c %s\n",p[i]->n, p[i]->a, p[i]->s, p[i]->t);
}
}
getchar();
getchar();
return 0;
}
int deletelist(struct STU *p[], int n)
{
int i,j;
char name[20];
printf("please input name you want del \n");
scanf("%s",name);
for(i = 0; i < n; i++)
{
if(strcmp(name,p[i]->n) == 0)
{
for(j = i; j < n; j++)
{
strcpy(p[j]->n,p[j + 1]->n);
p[j]->a = p[j + 1]->a;
p[j]->s = p[j + 1]->s;
strcpy(p[j]->t,p[j + 1]->t);
}
printf("delete finish \n");
n--;
}
}
getchar();
getchar();
return n;
}
int changelist(struct STU *p[], int n)
{
int i;
char name[20];
printf("please input name you want change \n");
scanf("%s",name);
for(i = 0; i < n; i++)
{
if(strcmp(name,p[i]->n) == 0)
{
//strcpy(p[i]->n,'\0');
// p[i]->a = 0;
// p[i]->s = 0;
// strcpy(p[i]->t,'\0');
printf("please input name you want change : \n");
scanf("%s",p[i]->n);
printf("please input age you want change : \n");
scanf("%d",&p[i]->a);
printf("please input sex you want change : \n");
getchar();
scanf("%c",&p[i]->s);
printf("please input phone you want change : \n");
scanf("%s",p[i]->t);
}
printf("change finish \n");
}
getchar();
getchar();
return 0;
}
int sortlist(struct STU *p[], int n)
{
int i,j,m;
char x;
char a[20];
char b[20];
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(strcmp(p[j]->n,p[j+1]->n) > 0)
{
strcpy(a , p[j+1]->n);
strcpy(p[j+1]->n , p[j]->n);
strcpy(p[j]->n , a);
strcpy(b , p[j+1]->t);
strcpy(p[j+1]->t , p[j]->t);
strcpy(p[j]->t , b);
m=p[j+1]->a;
p[j+1]->a=p[j]->a;
p[j]->a = m;
x=p[j+1]->s;
p[j+1]->s=p[j]->s;
p[j]->s = x;
}
}
}
getchar();
getchar();
printf("sort finished \n");
return 0;
}
int main()
{
struct STU *stu[SIZE];
int i;
for(i = 0 ; i < SIZE; i++)
{
stu[i] = (struct STU*)malloc(sizeof(struct STU));
}
int num=0,n;
while(1)
{
n = menu();
switch(n)
{
case 1:
num = inputlist(stu,num);
break;
case 2:
showlist(stu,num);
break;
case 3:
findlist(stu,num);
break;
case 4:
num = deletelist(stu,num);
break;
case 5:
changelist(stu,num);
break;
case 6:
sortlist(stu,num);
break;
case 7:
exit(0);
break;
default:
printf("please input right choise \n");
}
}
return 0;
}
//通过文件的读写I/O操作管理文本的查找输入输出删除以及排序。
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"
#ifndef BUFF_SIZE
#define BUFF_SIZE 1024
#endif
struct ADDRESS
{
char name[15];
char unit[15];
char phone[15];
};
int
main(int argc, char *argv[])
{
int n,i;
struct ADDRESS add[50];
// struct ADDRESS *v = NULL;
if(strcmp(argv[1],"input") == 0)
{
FILE * fp;
printf("please input record num you want to input \n");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
printf("input a name a unit a phonenum\n");
scanf("%s%s%s",add[i].name, add[i].unit, add[i].phone);
}
fp = fopen("./tfile","a");
for(i = 0; i < n; i++)
{
fprintf(fp, "%s%15s%15s \n",add[i].name , add[i].unit , add[i].phone);
}
fclose(fp);
}
if(strcmp(argv[1],"show") == 0)
{
FILE * fp;
int len;
char buffer[100];
fp = fopen("./tfile","r");
printf("show address list \n");
fseek(fp , 0L , SEEK_END);
len = ftell(fp);
rewind(fp);
fread(buffer , 1 , len ,fp);
printf("%s",buffer);
fclose(fp);
}
if(strcmp(argv[1],"sort") == 0)
{
struct ADDRESS *v = NULL;
int n = 0,num = 0;
int i,j, x;
char flag;
char a[15];
char b[15];
char c[15]
FILE *fp = fopen("./tfile","r");
while(!feof(fp))
{
flag = fgetc(fp);
if(flag == '\n')
{
num++;
}
}
n = num;
fseek(fp,0,SEEK_SET);
v = (struct ADDRESS*)malloc(sizeof(*v) * n);
for(i = 0; i < n; i++)
{
if(fscanf(fp,"%15s%15s%15s", &v[i].name, &v[i].unit, &v[i].phone) != 3)
{
break;
}
}
fclose(fp);
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(strcmp(v[j].name,v[j+1].name) > 0)
{
strcpy(a , v[j+1].name);
strcpy(v[j+1]->name , v[j]->name);
strcpy(v[j]->name , a);
strcpy(b , v[j+1]->unit);
strcpy(v[j+1]->unit , v[j]->unit);
strcpy(v[j]->unit , b);
strcpy(c , v[j+1]->phone);
strcpy(v[j+1]->phone , v[j]->phone);
strcpy(v[j]->phone , c);
}
}
}
FILE *fq = fopen("./tfile","w+");
for(i = 0; i < n - 1 ; i++)
{
printf("%s%15s%15s\n",v[i].name, v[i].unit, v[i].phone);
}
for(i = 0; i < n - 1 ; i++)
{
fprintf(fq,"%s%15s%15s\n",v[i].name, v[i].unit, v[i].phone);
}
fclose(fq);
free(v);
}
if(strcmp(argv[1],"find") == 0)
{
struct ADDRESS *v = NULL;
int n = 0,num = 0;
int i,j, x;
char flag;
char a[15];
FILE *fp = fopen("./tfile","r");
while(!feof(fp))
{
flag = fgetc(fp);
if(flag == '\n')
{
num++;
}
}
n = num;
fseek(fp,0,SEEK_SET);
v = (struct ADDRESS*)malloc(sizeof(*v) * n);
for(i = 0; i < n; i++)
{
if(fscanf(fp,"%15s%15s%15s", &v[i].name, &v[i].unit, &v[i].phone) != 3)
{
break;
}
}
fclose(fp);
printf("please input name you want find : \n");
scanf("%s",a);
for(i = 0; i < n; i++)
{
if(strcmp(v[i].name, a) == 0)
{
printf("%s%15s%15s\n",v[i].name, v[i].unit, v[i].phone);
printf("this information is in line: %d \n",i + 1);
}
}
free(*v);
}
if(strcmp(argv[1],"del") == 0)
{
struct ADDRESS *v = NULL;
int n = 0,num = 0;
int i,j, x;
char flag;
printf("please input record num you want to delete \n");
scanf("%d",&x);
FILE *fp = fopen("./tfile","r");
fseek(fp,0,SEEK_SET);
while(!feof(fp))
{
flag = fgetc(fp);
if(flag == '\n')
{
num++;
}
}
n = num;
fseek(fp,0,SEEK_SET);
v = (struct ADDRESS*)malloc(sizeof(*v) * n);
for(i = 0; i < n; i++)
{
if(fscanf(fp,"%15s%15s%15s", &v[i].name, &v[i].unit, &v[i].phone) != 3)
{
break;
}
}
fclose(fp);
for(j = x; j < n; j++)
{
strcpy(v[j - 1].name,v[j].name);
strcpy(v[j - 1].unit,v[j].unit);
strcpy(v[j - 1].phone,v[j].phone);
}
FILE *fq = fopen("./tfile","w+");
printf("input n = %d \n",n);
for(i = 0; i < n - 1 ; i++)
{
printf("%s%15s%15s\n",v[i].name, v[i].unit, v[i].phone);
}
for(i = 0; i < n - 1 ; i++)
{
fprintf(fq,"%s%15s%15s\n",v[i].name, v[i].unit, v[i].phone);
}
fclose(fq);
free(v);
}
}
7.30学习日志 学生管理系统
猜你喜欢
转载自blog.csdn.net/sinat_39440759/article/details/81290361
今日推荐
周排行