功能需求:
新增一個更新的功能在選項5,此功能可由使用者輸入欲更新的學號資料,如果串列裡有此學號,則要求使用者輸入新的學號,
姓名,分數,並將新的學號,姓名,分數印出來,若沒有則提示使用者串列裡沒有此筆資料
*提示:可以用strcmp來進行字串的比較,如果相等的話會回傳0,如strcmp(id1,id2)
-----------------從下面開始---------------
#include <stdio.h>
#include <stdlib.h>
struct student{
char id[10];
char name[10];
int score;
struct student *next;
};
struct student *insert_f(struct student *);
struct student *delete_f(struct student *);
void show_f(struct student *);
struct student *update_f(struct student *);
int main(int argc, char *argv[])
{
struct student *head=NULL;
char option;
do{
printf("<<<<Main function>>>>>>>>>\n");
printf("====<1> insert============\n");
printf("====<2> delete============\n");
printf("====<3> show==============\n");
printf("====<4> quit==============\n");
printf("====<5> update============\n");
printf("請輸入您的選擇:");
option=getche();
printf("\n");
switch(option)
{
case '1':
head=insert_f(head);
break;
case '2':
head=delete_f(head);
break;
case '3':
show_f(head);
break;
case '4':
printf("Bye Bye\n");
break;
case '5':
head=update_f(head);
break;
default:
printf("Wrong option\n");
}
}while(option !='4');
system("PAUSE");
return 0;
}
struct student *update_f(struct student *head) {
struct student *find_node;
struct student *ptr;
int checkPoint=0;
char enter[10];
if(head==NULL || enter==NULL)
{
system("CLS");
printf("沒有學生資料存在!!\n\n");
return NULL;
}
system("CLS");
printf("<<<<update function>>>>\n");
printf("請輸入欲更新的學生的學號:");
scanf("%s",enter);
if(head==NULL || enter==NULL)
{
system("CLS");
printf("沒有學生資料存在!!\n\n");
return NULL;
}
ptr=head;
while(ptr!=NULL)
{
if(strcmp(ptr->id,enter)==0)
{
find_node=ptr;
checkPoint=1;
}
ptr=ptr->next;
}
if(checkPoint==0)
{
system("CLS");
printf("查無此學號資料!!\n\n");
}
else
{
system("CLS");
printf("請輸入新的學生的學號:");
scanf("%s",find_node->id);
printf("請輸入新的學生的姓名:");
scanf("%s",find_node->name);
printf("請輸入新的學生的分數:");
scanf("%d",&find_node->score);
printf("資料已更新\n");
printf("====================================\n");
printf("== %-10s %-10s %4s ==\n","學號","姓名","分數");
printf("====================================\n");
printf("== %-10s %-10s %4d ==\n", find_node->id,find_node->name,find_node->score);
printf("====================================\n");
}
return head;
}
struct student *insert_f(struct student *head)
{
struct student *new_node;
struct student *ptr;
system("CLS");
printf("<<<<Insert function>>>>\n");
new_node=(struct student *)malloc(sizeof (struct student));
printf("請輸入學生的學號:");
scanf("%s",new_node->id);
ptr=head;
while(ptr!=NULL)
{
if(strcmp(ptr->id,new_node->id)==0)
{
system("CLS");
printf("輸入的學號重覆!!\n");
new_node=NULL;
return head;
}
ptr=ptr->next;
}
printf("請輸入學生的姓名:");
scanf("%s",new_node->name);
printf("請輸入學生的分數:");
scanf("%d",&new_node->score);
new_node->next=head;
head=new_node;
printf("已插入資料\n\n");
return head;
}
struct student *delete_f(struct student *head)
{
struct student *del_node;
system("CLS");
printf("<<<<Delete function>>>>\n");
if(head==NULL)
{
system("CLS");
printf("沒有資料\n\n");
return NULL;
}
del_node=head;
head=head->next;
printf("學生學號%s 已刪除!\n",del_node->id);
free(del_node);
return head;
}
void show_f(struct student *head)
{
struct student *ptr;
system("CLS");
printf("<<<<Show function>>>>\n");
if(head==NULL)
{
system("CLS");
printf("沒有學生資料存在!!\n\n");
return;
}
ptr=head;
system("CLS");
printf("====================================\n");
printf("== %-10s %-10s %4s ==\n","學號","姓名","分數");
printf("====================================\n");
while(ptr!=NULL)
{
printf("== %-10s %-10s %4d ==\n",
ptr->id,ptr->name,ptr->score);
ptr=ptr->next;
}
printf("====================================\n");
}
-----------------從上面結束---------------
留言列表