已結貼√
問題點數:20 回複次數:2
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5SY5kjYmZmZ3MjZ3ADZ5MTNhljZkVmYiNjYyMjM2UTYj9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)
幫忙解釋下面程式裡res什麼意思
#include
#include
#include
typedef struct resisdent
{
long num; // 編号
char adress[50]; // 使用者位址
char name[50]; // 戶名
long last_num; // 上次讀數
long now_num; // 抄表讀數
long cost_remain; // 電費餘額
struct resisdent *next; // 下一個使用者
} res;
void main()
{
// 函數聲明
res *enter(); //就是這裡 res *delete(res *);
res *alter(res *);
void display(res *);
void search(res *);
res *jiaofei(res *);
res *chaobiao(res *);
int menu_select();
// 變量初始化
// head 變量儲存連結清單頭指針
res *head=NULL;
res *p1,*p2,*p3;
int i,j,k;
for(;;)
{
switch(menu_select())
{
case 1: head=enter();break;
case 2: head=delete(head);break;
case 3: head=alter(head);break;
case 4: display(head);break;
case 5: search(head);break;
case 6: head=jiaofei(head);break;
case 7: head=chaobiao(head);break;
case 8: exit(0);
}
}
}
// 菜單選項
int menu_select()
{
int ch;
printf("***************Welcome to the eletronic system**********************************\n");
printf("\t1.Enter the static of the resisdent.\n");
printf("\t2.Delete the static of the resisdent.\n");
printf("\t3.Alter the static of the resisdent.\n");
printf("\t4.Display the static of the resisdent.\n");
printf("\t5.Search the static of the resisdent.\n");
printf("\t6.Hand up the cost.\n");
printf("\t7.Updata the reader.\n");
printf("\t8.Quit.\n");
printf("********************************************************************************\n");
do
{
printf("Make a chioce(1--8):");
scanf("%d",&ch);
} while(ch<1 || ch>8);
return(ch);
}
// 錄入資料,當編号為 0 則退出
res *enter()
{
res *head = NULL;
res *p1 = NULL;
res *p2 = NULL;
res *p3 = NULL;
int n=0;
p1=p2=(res *) malloc (sizeof(res)); //還有這裡 printf("Enter the static of the resisdent:\n");
printf("num:");
scanf("%ld",&p1->num);
if(p1->num==0)
goto end;
printf("adress:");
scanf("%s",p1->adress);
printf("name:");
scanf("%s",p1->name);
printf("The number of last time:");
scanf("%ld",&p1->last_num);
printf("The number of now:");
scanf("%ld",&p1->now_num);
printf("The cost remain:");
scanf("%ld",&p1->cost_remain);
while(p1->num!=0)
{
n=n+1;
// 如果計數為1,則置表頭為p1,
// 否則将建立節點添加至上一節點(此時為p2)尾部
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(res *) malloc (sizeof(res));
printf("num:");
scanf("%ld",&p1->num);
if(p1->num==0)
goto end;
printf("adress:");
scanf("%s",p1->adress);
printf("name:");
scanf("%s",p1->name);
printf("The number of last time:");
scanf("%ld",&p1->last_num);
printf("The number of now:");
scanf("%ld",&p1->now_num);
printf("The cost remain:");
scanf("%ld",&p1->cost_remain);
}
end:
p2->next=NULL;
return(head);
}
// 根據輸入的編号删除對應項
// 傳回更新後的連結清單表頭指針
res *delete(res *head)
{
res *p1,*p2;
long num;
if(head==NULL)
printf("The list is empty.!\n");
else
{
printf("Enter the num to delete:");
scanf("%ld",&num);
p1=p2=head;
while(p1->num!=num && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(head==p1)
head=p1->next;
else
p2->next=p1->next;
}
else
printf("Not found!\n");
}
return(head);
}
// 輸入編号,修改對應項
// 傳回表頭指針
res *alter(res *head)
{
res *p1,*p2;
long num;
if(head==NULL)
printf("Null list.\n");
else
{
printf("Enter the num to alter:");
scanf("%ld",&num);
p1=head;
while(p1->num!=num &&p1->next!=NULL)
p1=p1->next;
if(num==p1->num)
{
printf("new num:");
scanf("%ld",&p1->num);
printf("new adress:");
scanf("%s",p1->adress);
printf("new name:");
scanf("%s",p1->name);
printf("The number of last time:");
scanf("%ld",&p1->last_num);
printf("The number of now:");
scanf("%ld",&p1->now_num);
printf("The cost remain:");
scanf("%ld",&p1->cost_remain);
}
else
printf("Not found!\n");
}
return(head);
}
// 顯示表中所有項目
void display(res *head)
{
res *p1,*p2;
p1=p2=head;
printf("********************************************************************************\n");
printf("\tNUM\tADRESS\tNAME\tNUMBER_LAST\tNUMBER_NOW\tCOST_REMAIN\n");
while(p1!=NULL)
{
printf("\t%-3ld\t%-6s\t%-4s\t%-11ld\t%-10ld\t%-5ld\n",p1->num,p1->adress,
p1->name,p1->last_num,p1->now_num,p1->cost_remain);
p1=p1->next;
}
printf("********************************************************************************\n");
}
// 查詢
void search(res *head)
{
res *p1,*p2;
char ser[50];
int n;
printf("1.search by adress.\n");
printf("2.search by name.\n");
printf("choose 1 || 2:\n");
scanf("%d",&n);
if(head==NULL)
printf("NULL list.\n");
switch(n)
{
case 1:
printf("input the adress:");
scanf("%s",ser);
p1=p2=head;
while(strcmp(p1->adress,ser)!=0 &&p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->adress,ser)==0)
{
printf("\tNUM\tADRESS\tNAME\tLAST_NUM\tNOW_NUM\tCOST_REMAIN\n");
printf("\t%-3ld\t%-6s\t%-4s\t%-11ld\t%-10ld\t%-5ld\n",p1->num,p1->adress,p1->name,
p1->last_num,p1->now_num,p1->cost_remain);
}
else
printf("Not found!\n");break;
case 2:
{
printf("input the name:");
scanf("%s",ser);
p1=p2=head;
while(strcmp(p1->name,ser)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->name,ser)==0)
{
printf("\tNUM\tADRESS\tNAME\tLAST_NUM\tNOW_NUM\tCOST_REMAIN\n");
printf("\t%-3ld\t%-6s\t%-4s\t%-11ld\t%-10ld\t%-5ld\n",p1->num,p1->adress,p1->name,
p1->last_num,p1->now_num,p1->cost_remain);
}
else
printf("Not found!\n");break;
}
default:break;
}
}
res *jiaofei(res *head)
{
res *p1,*p2;
char adress[50];
long cost;
printf("input the adress:");
scanf("%s",adress);
if(head==NULL)
printf("NULL list!\n");
else
{
p1=head;
while(strcmp(p1->adress,adress)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->adress,adress)==0)
{
printf("input the number of the cost:");
scanf("%ld",&cost);
p1->cost_remain+=cost;
}
else
printf("Not found!\n");
}
return(head);
}
res *chaobiao(res *head)
{
res *p1,*p2;
char adress[50];
if(head==NULL)
printf("NULL list!\n");
else
{
printf("input the adress:");
scanf("%s",adress);
p1=head;
while(strcmp(p1->adress,adress)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->adress,adress)==0)
{
p1->last_num=p1->now_num;
printf("input the num:");
scanf("%ld",&p1->now_num);
}
else
printf("Not found!\n");
}
return(head);
}
搜尋更多相關主題的文章:
解釋 res