?? student-manage.c
字號:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <error.h>
//extern int errno;
struct std_info
{
int no;
char name[30];
struct std_info *next;
};
struct std_info *head=NULL,*current=NULL;
int student_num=0;
int edit_flag=0;
int write(void)
{
struct std_info *p;
FILE *fp;
if(head==NULL) {
printf("\nNo data. Please load data.\n");
return;
}
fp=fopen("test.txt","w");
if(!fp) {
printf("Error in writing file \n");
return -1;
}
p=head;
while(p) {
fprintf(fp,"%d,%s\n",p->no,p->name);
p=p->next;
}
fclose(fp);
}
int add_student(struct std_info *st)
{
struct std_info *new_std;
new_std = (struct std_info *)malloc(sizeof(struct std_info));
if(new_std != NULL) {
new_std->no = st->no;
strcpy(new_std->name,st->name);
new_std->next = NULL;
if(head==NULL) {
head=new_std;
current=new_std;
}
else {
current->next = new_std;
current=new_std;
}
return 0;
}
else {
printf("\nError in memory alloc\n");
return -1;
}
}
int append(void)
{
struct std_info student_info;
char key;
int num=0;
do {
printf("please input student number:");
scanf("%d",&student_info.no);
printf("name:");
scanf("%s",student_info.name);
printf("No.\tName\n");
printf("\n%d\t%s\n",student_info.no,student_info.name);
add_student(&student_info);
num++;
do {
printf("Do you need input again?[y/n]");
key=getchar();
} while(key!='n' && key!='N' && key!='y' && key!='Y');
} while(key=='y'|| key=='Y');
printf("\n");
return(num);
}
int load(void)
{
FILE *fp;
int flen,fpn1,num=0;
struct std_info student;
fp=fopen("test.txt","r");
if(!fp) {
printf("Error in loading file : \n");
return -1;
}
fseek(fp,0,2);
flen=ftell(fp);
fseek(fp,0,0);
while(!feof(fp))
{ fscanf(fp,"%d,%s",&student.no,student.name);
num++;
fpn1=ftell(fp);
if(add_student(&student)!=0)
{ num=-1;
break;
}
if((flen-fpn1)<2)
break;
}
fclose(fp);
printf("\nThere are %d students\n",num);
return(num);
}
void display(void)
{
struct std_info *p;
if(head==NULL) {
printf("\nNo data. Please load data.\n");
return;
}
printf("\nNo.\tName\n");
p=head;
while(p) {
printf("%d\t%s\n",p->no,p->name);
p=p->next;
}
}
void modify(void)
{
printf("You can finish this function by yourself\n");
}
void search(void)
{
int w;
struct std_info *l;
if(head==NULL)
{
printf("\nNo data.Please load data.\n");
return;
}
l=head;
printf("Please input the student number:");
scanf("%d",&w);
while(l)
{
if(w==l->no)
break;
l=l->next;
}
printf("%d %s\n",l->no,l->name);
}
void del_all()
{
struct std_info *p,*next;
p=head;
while(p) {
next=p->next;
free(p);
p=next;
}
printf("All data be deleted.\n");
head=NULL;
current=NULL;
}
void help(void)
{
char *cmdstr="\nl: load\t\t\td: display\n"
"w: write\t\ts: search\n"
"a: append\t\tm: modify\n"
"e: empty\t\tq: quit\n";
printf("%s",cmdstr);
}
void print_prompt(void)
{
printf("Input command(? for help):");
}
int menu(void)
{
int kin;
kin=getchar();
printf("\n");
return(kin);
}
int main(void)
{
int key,ctl=1;
while(ctl) {
print_prompt();
key=menu();
switch(key) {
case 'l':
student_num=load();
break;
case 'e':
del_all();
student_num=0;
edit_flag=1;
break;
case 'd':
display();
break;
case 'q':
ctl=0;
break;
case '?':
help();
break;
case 'w':
write();
break;
case 's':
search();
break;
case 'a':
student_num += append();
edit_flag=1;
break;
case 'm':
modify();
break;
}
}
printf("\nbye!\n");
return(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -