?? slist.c
字號:
/* file name: slist.c */
/* 單向鍵結鏈表,插入、刪除使用排序 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void read_func(void);
void write_func(void);
void insert_func(void);
void sort_func(void);
void delete_func(void);
void display_func(void);
void modify_func(void);
void anykey_func(void);
struct student {
char name[20];
int score;
struct student *next;
};
struct student *ptr, *head, *current, *prev;
void main(void)
{
char option1;
system("cls");
read_func();
while(1)
{
printf("****************************************\n");
printf(" 1.insert\n");
printf(" 2.delete\n");
printf(" 3.display\n");
printf(" 4.modify\n");
printf(" 5.quit\n");
printf("****************************************\n");
printf(" Please enter your choice (1-5)...");
option1=getche();
printf("\n");
switch(option1)
{
case '1':
insert_func();
break;
case '2':
delete_func();
break;
case '3':
display_func();
break;
case '4':
modify_func();
break;
case '5':
write_func();
exit(0);
}
}
}
void read_func(void)
{
FILE *fptr;
head=(struct student *) malloc(sizeof(struct student));
head->next = NULL;
/* 開始時,若表中不存在數據,則要求輸入第一筆數據 */
if((fptr=fopen("slist.dat","r")) == NULL)
{
printf(" Data file not exist\n");
printf(" Press any key to edit first record...\n");
getch();
insert_func();
}
else
{
ptr=(struct student *) malloc(sizeof(struct student));
while(fscanf(fptr, "%s %d", ptr->name, &ptr->score) != EOF)
{
sort_func();
ptr=(struct student *) malloc(sizeof(struct student));
}
fclose(fptr);
}
}
void write_func(void)
{
FILE *fptr;
fptr=fopen("slist.dat","w");
current=head->next;
while(current != NULL)
{
fprintf(fptr, "%s %d\n", current->name, current->score);
current = current->next;
}
fclose(fptr);
}
void insert_func(void)
{
char s_temp[4];
ptr=(struct student *) malloc(sizeof(struct student));
printf(" Student name : ");
gets(ptr->name);
printf(" Student score: ");
gets(s_temp);
ptr->score = atoi(s_temp);
sort_func();
}
/*以分數高低由大到小排列*/
void sort_func(void)
{
//插入數據
prev = head;
current = head->next;
while ((current != NULL) && (current->score > ptr->score))
{
prev = current;
current = current->next;
}
ptr->next = current;
prev->next = ptr;
}
void delete_func(void)
{
char del_name[20];
printf(" Delete student name: ");
gets(del_name);
prev = head;
current = head->next;
while ((current != NULL) && (strcmp(current->name , del_name)!=0))
{
prev = current;
current = current->next;
}
if (current != NULL)
{
prev->next = current->next;
free(current);
printf(" %s student record deleted\n",del_name);
}
else
printf(" Student %s not found\n",del_name);
anykey_func();
}
void modify_func(void)
{
char n_temp[20],s_temp[4];
printf(" Modify student name: ");
gets(n_temp);
current=head->next;
while ((current != NULL) && (strcmp(current->name , n_temp)!=0))
{
prev = current;
current = current->next;
}
if (current != NULL)
{
printf(" **************************\n");
printf(" Student name : %s\n",current->name);
printf(" Student score: %d\n",current->score);
printf(" **************************\n");
printf(" Please enter new score: ");
gets(s_temp);
current->score = atoi(s_temp);
printf(" %s student record modified\n",n_temp);
}
else
printf(" Student %s not found\n",n_temp);
anykey_func();
}
void display_func(void)
{
int count=0;
system("cls");
if(head->next == NULL)
{
printf(" No student record\n");
}
else
{
printf(" NAME SCORE\n");
printf(" ---------------------------\n");
current=head->next;
while(current != NULL)
{
printf(" %-20s %3d\n", current->name, current->score);
count++;
current=current->next;
if(count % 20 == 0) getch();
}
printf(" ---------------------------\n");
printf(" Total %d record(s) found\n", count);
}
anykey_func();
}
void anykey_func(void)
{
printf(" Press any key to continue...");
getch();
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -