
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct room {
char state[100];
int price;
int number;
char level[100];
struct room* next;
};
struct room *head;
struct room *tail;
struct room *createlist();
struct room *search(struct room *head);
struct room *change(struct room *head,int a);
struct room *del(struct room *head,int a);
void print(struct room *head);
struct room* createlist()//錄入
{struct room *p;
char state[40];
int price;
int number;
char level[15];
int size=sizeof(struct room);
printf("輸入房間編號,等級,價格,狀態 注:房間編號為0結束:\n");
scanf("%d",&number);
while(number!=0)
{scanf("%s%d%s",&level,&price,&state);
p=(struct room*)malloc(size);
p->number=number;
strcpy(p->level,level);
p->price=price;
strcpy(p->state,state);
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&number);}
return head;
system("pause");}
void print(struct room * head)//顯示
{struct room *ptr;
if(head==NULL)
{
printf("\n no records \n");
return;
}
printf("\n 房間資訊為:\n");
printf(" 房間編號\t");
printf(" 房間等級\t");
printf(" 房間價格\t");
printf(" 房間狀態\n");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{printf(" %d\t %s\t %d\t %s\n",ptr->number,ptr->level,ptr->price,ptr->state);
}
system("pause");}
struct room *change(struct room *head,int a)//修改
{struct room *ptr;
char state[40];
int price;
int number;
char level[15];
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{if(ptr->number==a)
{printf("輸入房間編號,等級,價格,狀態:\n");
scanf("%d%s%d%s",&number,&level,&price,&state);
ptr->number=number;
strcpy(ptr->level,level);
ptr->price=price;
strcpy(ptr->state,state);}
}
system("pause");
return head;}
struct room *del(struct room *head,int a)//洗掉
{struct room *ptr;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{if(ptr->number==a);
{strcpy(ptr->level,"初始化,請使用修改功能重新錄入");
ptr->price=0;
strcpy(ptr->state,"初始化,請使用修改功能重新錄入");}
printf("初始化完成");
}system("pause");
return head;}
struct room *search(struct room *head)//查詢
{int a=0;
struct room *ptr;
printf("\n 空閑房間資訊為:\n");
printf(" 房間編號\t");
printf(" 房間等級\t");
printf(" 房間價格\t");
printf(" 房間狀態\n");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{if (strcmp(ptr->state,"空閑")==0)
{a++;
printf(" %d\t %s\t %d\t %s\n",ptr->number,ptr->level,ptr->price,ptr->state);
}}
if(a==0)
printf("當前無空閑房間");
system("pause");
return head;}
//讀入
struct room * read() {
FILE * fp;
int count=0;
struct room *head,*p,*q;
head=p=q=NULL;
p=(struct room *)malloc(sizeof(struct room));
head=(struct room *)malloc(sizeof(struct room));
printf("讀取中.....\n");
if((fp = fopen("room.txt", "r")) == NULL )
{
printf("載入失敗……未找到存檔資料!\n\n");
return head;
}
while(!feof(fp))
{count++;
p=(struct room *)malloc(sizeof(struct room));
fread(p,sizeof(struct room),1,fp);
printf("%d %s %d %s \n",p->number,p->level,p->price,p->state);
q->next=p;
q=p;
if(count==1)//標記,第一次回圈結束將首地址賦給head;
head=p;
}
fclose(fp);
return head;
}
void write(struct room * head)
{
FILE * fp;
struct room * ptr;
fp = fopen("room.txt", "a+");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
fprintf(fp,"%d %s %d %s\n",ptr->number,ptr->level,ptr->price,ptr->state);
}
fclose(fp);
}
//選單
void menu() {
int choice=0;
int a=0;
int b=0;
printf("********************************************************************\n");
printf("* 0. 退出系統 *\n");
printf("* 1. 錄入資訊;*\n");
printf("* 2. 瀏覽資訊;*\n");
printf("* 3. 修改資訊; *\n");
printf("* 4. 洗掉(初始化)資訊; *\n");
printf("* 5. 查詢資訊;*\n");
printf("* 6. 讀取資訊;*\n");
printf("********************************************************************\n");
printf("\n");
while(1)
{ scanf("%d", &choice);
switch (choice) {
case 0:
write(head);
printf("退出成功\n");
system("pause");
exit(0);
break;
case 1:
head=createlist();
printf("-------錄入完成\n");
break;
case 2:
printf("-------瀏覽資訊\n");
print(head);
break;
case 3:
printf("------修改資訊\n");
printf("請輸入原來的房間編號:");
scanf("%d", &a);
change(head, a);
break;
case 4:
printf("------洗掉資訊\n");
printf("請輸入洗掉的房間編號:");
scanf("%d", &b);
del(head, b);
break;
case 5:
printf("------查詢空閑房間\n");
search(head);
break;
case 6:
head=read();
printf("讀取成功");
break;
default:
printf("輸入錯誤,重新輸入\n");
system("pause");
break;
}}
system("cls");
}
int main() {
while (1) {
menu();
}
return 0;}
主要遇到問題就是一旦呼叫read函式直接程式結束和囤囤囤
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct room {
char state[100];
int price;
int number;
char level[100];
struct room* next;
};
struct room *head;
struct room *tail;
struct room *createlist();
struct room *search(struct room *head);
struct room *change(struct room *head,int a);
struct room *del(struct room *head,int a);
void print(struct room *head);
struct room* createlist()//錄入
{
struct room *p;
char state[40];
int price;
int number;
char level[15];
int size=sizeof(struct room);
printf("輸入房間編號,等級,價格,狀態 注:房間編號為0結束:\n");
scanf("%d",&number);
while(number!=0)
{
//scanf("%s%d%s",&level,&price,&state);
scanf("%s%d%s", level, &price, state);
p=(struct room*)malloc(size);
p->number=number;
strcpy(p->level,level);
p->price=price;
strcpy(p->state,state);
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&number);
}
return head;
system("pause");
}
void print(struct room * head)//顯示
{
struct room *ptr;
if(head==NULL)
{
printf("\n no records \n");
return;
}
printf("\n 房間資訊為:\n");
printf(" 房間編號\t");
printf(" 房間等級\t");
printf(" 房間價格\t");
printf(" 房間狀態\n");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
printf(" %d\t %s\t %d\t %s\n",ptr->number,ptr->level,ptr->price,ptr->state);
}
system("pause");
}
struct room *change(struct room *head,int a)//修改
{
struct room *ptr;
char state[40];
int price;
int number;
char level[15];
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{if(ptr->number==a)
{printf("輸入房間編號,等級,價格,狀態:\n");
//scanf("%d%s%d%s",&number,&level,&price,&state);
scanf("%d%s%d%s",&number, level,&price,state);
ptr->number=number;
strcpy(ptr->level,level);
ptr->price=price;
strcpy(ptr->state,state);}
}
system("pause");
return head;
}
struct room *del(struct room *head,int a)//洗掉
{
struct room *ptr;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
//if(ptr->number==a); //多一個分號
if(ptr->number==a)
{
strcpy(ptr->level,"初始化,請使用修改功能重新錄入");
ptr->price=0;
strcpy(ptr->state,"初始化,請使用修改功能重新錄入");
}
printf("初始化完成");
}system("pause");
return head;
}
struct room *search(struct room *head)//查詢
{
int a=0;
struct room *ptr;
printf("\n 空閑房間資訊為:\n");
printf(" 房間編號\t");
printf(" 房間等級\t");
printf(" 房間價格\t");
printf(" 房間狀態\n");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{if (strcmp(ptr->state,"空閑")==0)
{a++;
printf(" %d\t %s\t %d\t %s\n",ptr->number,ptr->level,ptr->price,ptr->state);
}}
if(a==0)
printf("當前無空閑房間");
system("pause");
return head;
}
//讀入
struct room * read()
{
FILE * fp;
int count=0;
struct room *head,*p,*q;
head=p=q=NULL;
p=(struct room *)malloc(sizeof(struct room));
head=(struct room *)malloc(sizeof(struct room));
printf("讀取中.....\n");
if((fp = fopen("room.txt", "r")) == NULL )
{
printf("載入失敗……未找到存檔資料!\n\n");
free(p);
free(head); //不用了就應該釋放
return NULL;
}
while(!feof(fp))
{
count++;
p=(struct room *)malloc(sizeof(struct room));
fscanf(fp,"%d %s %d %s\n", &p->number, p->level, &p->price, p->state);
//fread(p,sizeof(struct room),1,fp);
printf("%d %s %d %s \n",p->number,p->level,p->price,p->state);
q->next=p;
q=p;
if(count==1)//標記,第一次回圈結束將首地址賦給head;
head=p;
}
fclose(fp);
return head;
}
void write(struct room * head)
{
FILE * fp;
struct room * ptr;
fp = fopen("room.txt", "a+");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
fprintf(fp,"%d %s %d %s\n",ptr->number,ptr->level,ptr->price,ptr->state);
}
fclose(fp);
}
//選單
void menu() {
int choice=0;
int a=0;
int b=0;
printf("********************************************************************\n");
printf("* 0. 退出系統 *\n");
printf("* 1. 錄入資訊;*\n");
printf("* 2. 瀏覽資訊;*\n");
printf("* 3. 修改資訊; *\n");
printf("* 4. 洗掉(初始化)資訊; *\n");
printf("* 5. 查詢資訊;*\n");
printf("* 6. 讀取資訊;*\n");
printf("********************************************************************\n");
printf("\n");
while(1)
{ scanf("%d", &choice);
switch (choice) {
case 0:
write(head);
printf("退出成功\n");
system("pause");
exit(0);
break;
case 1:
head=createlist();
printf("-------錄入完成\n");
break;
case 2:
printf("-------瀏覽資訊\n");
print(head);
break;
case 3:
printf("------修改資訊\n");
printf("請輸入原來的房間編號:");
scanf("%d", &a);
change(head, a);
break;
case 4:
printf("------洗掉資訊\n");
printf("請輸入洗掉的房間編號:");
scanf("%d", &b);
del(head, b);
break;
case 5:
printf("------查詢空閑房間\n");
search(head);
break;
case 6:
head=read();
printf("讀取成功");
break;
default:
printf("輸入錯誤,重新輸入\n");
system("pause");
break;
}}
system("cls");
}
int main() {
while (1) {
menu();
}
return 0;
}
供參考~
讀寫不匹配。fscanf/fprintf fread/fwrite分別是讀寫對
另外,read函式回傳的head和你添加上去的都指向head就會導致資料丟失
其他問題詳見代碼,建議對比找出自己的問題~
uj5u.com熱心網友回復:
推薦讀一下https://blog.csdn.net/zhanghaiyang9999/article/details/107032563
uj5u.com熱心網友回復:
謝謝大佬,解決了一部分問題,可改完后,只能讀取一行資料了,之后的都讀取不了了,而且我其實最想問的是,不管改前改后,一呼叫read,程式就非正常結束了,選單無法正常使用了
uj5u.com熱心網友回復:
謝謝大佬,解決了一部分問題,可改完后,只能讀取一行資料了,之后的都讀取不了了,而且我其實最想問的是,不管改前改后,一呼叫read,程式就非正常結束了,選單無法正常使用了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/32531.html
標籤:C語言
上一篇:7·3計劃 不多說了 懂得都懂
下一篇:海鷗計劃
