知識社群
登入
中文(台灣)
English(US)
Q&A
線上人數:
137
回首頁
最新活動
(34)
公佈欄
最新公告
近期課程
技術專欄
Android
嵌入式Linux
韌體設計
FPGA / Verilog
站上資源
線上教學
文件總覽
廠商徵才
專業論壇
(1315)
推廣成果
學員專題
Android I/O Workshop
Android 企業產訓專班
活動花絮
(58)
專班學員資訊平台
102年嵌入式軟韌體設計工程師養成班
102年JAVA程式設計班
102Linux網路工程師-LPI認證養成班
嵌入式Linux就業班第二梯次
嵌入式Linux就業班第三梯次
101年 Android設計班(2)
101年 Android設計班(1)
公告
艾鍗學院成立滿十週年了!十年一遇的超值回饋,獻給堅持學習的你~
(03-12)
【2019下半年課程行事曆】政府+企業雙重補助,AI人才養成計劃開跑
(07-10)
IT TRAINING 2019上半年最新技術培訓一覽
(03-13)
[
more
]
最新消息
文件分類
[
總覽
]
廠商徵才資訊
(215)
學員專題成果
(31)
研討會活動專區
(5)
Android 專欄
(19)
活動簡報檔
(2)
線上教學 Training Video
(24)
韌體設計相關
(17)
Embedded LInux 專欄
(54)
Raspberry Pi
(4)
FPGA/Verilog 專欄
(14)
小品文章
(37)
未分類文件
(28)
101年 Android 應用軟體設計班
(4)
基礎C程式&資料結構
(8)
嵌入式Linux就業班第二梯次
(20)
101年臺北市政府勞工局職業訓練中心-Android APP 軟體開發人才培訓就業班
(16)
102年Linux網路工程師-LPI認證養成班
(9)
102年JAVA程式設計班
(2)
102年嵌入式軟韌體設計工程師養成班
(6)
Access VBA
(5)
Excel VBA
(4)
產業訊息
(2)
PCB Layout
(7)
電子電路
(6)
專班文件區
嵌入式Linux就業班第三梯次
(13)
社群資訊
訪客: 3181513
文章: 557
討論: 1315
公告: 31
容量: 剩餘
2.4 GB
(4 GB)
閱讀權限: 開放
分類:
教育學習 / 軟體系統
版主: 艾鍗學院
副版主: 無
位置:
艾鍗學院 Blog
>
專業論壇
>
討論
訂位系統
(
C程式設計作業--for 就業班同學
)
(naruto,
naruto@hotmail.com
, 2011-03-17 14:35)
1樓
seatingReservation.h
-----------------------------------
#ifndef SEATING_RESERVATION_DIANZHANG
#define SEATING_RESERVATION_DIANZHANG
#define DATA_FILENAME "seat_information.bin"
#define SEATING_CAPACITY 30
typedef enum {empty, assigned} seatStatus;
typedef struct seat_information {
int seatID;
seatStatus marker;
char lastName[20];
char firstName[20];
struct seat_information *next;
} SeatInfo;
typedef struct {
SeatInfo *head;
} Seat;
//Read seating infomation from a data file.
int seat_readInfo(Seat *seat);
//Write seating infomation from a data file.
int seat_writeInfo(Seat *seat);
//Add an assigned seating information to the list.
void seat_addSeatInfo(Seat *seat, SeatInfo *seatinfo);
//Delete an assigned seating information from the list.
void seat_delSeatInfo(Seat *seat, SeatInfo *seatinfo);
//Get number of empty seats.
int seat_getNumEmptySeat(Seat *seat);
//Print a list of empty seats.
void seat_printEmptySeat(Seat *seat);
//Print a list of assigned seats.
void seat_printAssignedSeat(Seat *seat);
//Assign a customer to a seat assignment.
void seat_assignSeat(Seat *seat);
//Delete an assigned seat.
void seat_delSeat(Seat *seat);
//Initialize a seating infomation.
SeatInfo* seat_init(void);
//Check if a seat id had been assigned.
int seat_isNode(Seat *seat, int num);
//Get a seat information by seat id.
SeatInfo* seat_getSeatInfo(Seat *seat, int num);
#endif
seatingReservation.c
--------------------------------
#include "seatingReservation.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Read seating infomation from a data file.
int seat_readInfo(Seat *seat) {
int i=0;
int check=0;
SeatInfo buffer;
SeatInfo *seatinfo;
FILE *fp=NULL;
fp=fopen(DATA_FILENAME, "rb");
if(!fp) {
printf("Failed to open a data file!\n");
return 1;
}
printf("Data file reading...\n\n");
for(i=0; i<SEATING_CAPACITY; i++) {
check=fread(&buffer, sizeof(SeatInfo), 1, fp);
if(1==check) {
seatinfo=seat_init();
seatinfo->seatID=buffer.seatID;
seatinfo->marker=buffer.marker;
strcpy(seatinfo->lastName, buffer.lastName);
strcpy(seatinfo->firstName, buffer.firstName);
seat_addSeatInfo(seat, seatinfo);
} else {
break;
}
}
fclose(fp);
return 0;
}
//Write seating infomation from a data file.
int seat_writeInfo(Seat *seat) {
SeatInfo *trace=NULL;
FILE *fp=NULL;
fp=fopen(DATA_FILENAME, "wb");
if(!fp) {
printf("Failed to open a data file!\n");
return 1;
}
printf("Data file writing...\n\n");
trace=seat->head;
while(trace!=NULL) {
fwrite(trace, sizeof(SeatInfo), 1, fp);
trace=trace->next;
}
fclose(fp);
return 0;
}
//Add an assigned seating information to the list by ascending.
void seat_addSeatInfo(Seat *seat, SeatInfo *seatinfo) {
SeatInfo *trace=NULL, *pre=NULL;
//No seat information.
if(seat->head==NULL) {
seat->head=seatinfo;
seat->head->next=NULL;
return;
}
trace=seat->head;
//Only one seat information.
if(trace->next==NULL) {
if(trace->seatID > seatinfo->seatID) {
seatinfo->next=trace;
seat->head=seatinfo;
return;
} else {
trace->next=seatinfo;
seatinfo->next=NULL;
return;
}
}
//More than one seat information.
while(trace!=NULL) {
if(trace->seatID>seatinfo->seatID) {
seatinfo->next=trace;
if(trace==seat->head) {
seat->head=seatinfo;
return;
}
pre->next=seatinfo;
return;
} else {
pre=trace;
trace=trace->next;
}
}
pre->next=seatinfo;
seatinfo->next=NULL;
}
//Delete an assigned seating information from the list.
void seat_delSeatInfo(Seat *seat, SeatInfo *seatinfo) {
SeatInfo *trace=NULL;
//No seat information.
if(seat->head==NULL) {
return;
}
trace=seat->head;
//Only one seat information.
if(trace->next==NULL) {
seat->head=NULL;
free(seatinfo);
return;
}
//More than one seat information.
if(trace==seatinfo) {
seat->head=trace->next;
free(seatinfo);
return;
} else {
while(trace->next!=seatinfo) {
trace=trace->next;
}
trace->next=seatinfo->next;
free(seatinfo);
}
}
//Get number of empty seats.
int seat_getNumEmptySeat(Seat *seat) {
int count=0;
SeatInfo *trace=NULL;
trace=seat->head;
while(trace!=NULL) {
count++;
trace=trace->next;
}
return SEATING_CAPACITY-count;
}
//Print a list of empty seats.
void seat_printEmptySeat(Seat *seat) {
int i=0, count=0;
SeatInfo *trace=NULL;
trace=seat->head;
for(i=1; i<=SEATING_CAPACITY; i++) {
if(trace!=NULL && trace->seatID==i) {
trace=trace->next;
continue;
}
count++;
printf("%2d ", i);
if(count%5==0) {
printf("\n");
}
}
}
//Print a list of assigned seats.
void seat_printAssignedSeat(Seat *seat) {
SeatInfo *trace=NULL;
if(seat->head==NULL) {
printf("0\n");
} else {
trace=seat->head;
while(trace!=NULL) {
printf("ID:%-2d Last Name:%-15s First Name:%-15s\n", \
trace->seatID, trace->lastName, trace->firstName);
trace=trace->next;
}
}
}
//Assign a customer to a seat assignment.
void seat_assignSeat(Seat *seat) {
SeatInfo *seatinfo=NULL;
int num=0;
char check;
seatinfo=seat_init();
printf("Empty seat list:\n");
seat_printEmptySeat(seat);
printf("\n");
printf("Please input a seat number you want to reserve:");
scanf("%d", &num);
if(num<1 || num>SEATING_CAPACITY) {
printf("%d is an error seat id!\n", num);
return;
} else {
if(seat_isNode(seat, num)) {
printf("Sorry! The seat had been reserved.\n");
return;
}
seatinfo->seatID=num;
seatinfo->marker=assigned;
printf("Please input your last name:");
scanf("%s", seatinfo->lastName);
printf("Please input your first name:");
scanf("%s", seatinfo->firstName);
printf("\n");
printf("Please check your information again.\n");
printf("Seat ID:%d\n", seatinfo->seatID);
printf("Your last name:%s\n", seatinfo->lastName);
printf("Your first name:%s\n", seatinfo->firstName);
while(1) {
printf("Do you want to reserve the seat ? (y or n)\n");
scanf(" %c", &check);
switch(check) {
case 'y':
case 'Y':
seat_addSeatInfo(seat, seatinfo);
return;
case 'n':
case 'N':
return;
default:
printf("Input error!\n");
}
}
}
}
//Delete an assigned seat.
void seat_delSeat(Seat *seat) {
SeatInfo *seatinfo=NULL;
int num=0;
char check;
printf("Assigned seat list:\n");
seat_printAssignedSeat(seat);
printf("Please input a seat number you want to delete:");
scanf("%d", &num);
if(num<1 || num>SEATING_CAPACITY) {
printf("%d is an error seat id!\n", num);
return;
} else {
if(!seat_isNode(seat, num)) {
printf("Sorry! The seat had not been reserved.\n");
return;
}
seatinfo=seat_getSeatInfo(seat, num);
printf("\n");
printf("Please check the information again.\n");
printf("Seat ID:%d\n", seatinfo->seatID);
printf("Your last name:%s\n", seatinfo->lastName);
printf("Your first name:%s\n", seatinfo->firstName);
while(1) {
printf("Do you want to delete the seat ? (y or n)\n");
scanf(" %c", &check);
switch(check) {
case 'y':
case 'Y':
seat_delSeatInfo(seat, seatinfo);
return;
case 'n':
case 'N':
return;
default:
printf("Input error!\n");
}
}
}
}
//Initialize a seating infomation.
SeatInfo* seat_init(void) {
SeatInfo *seatinfo=NULL;
seatinfo=(SeatInfo*)malloc(sizeof(SeatInfo));
if(!seatinfo) {
printf("Failed to allocate a memory block!\n");
exit(1);
} else {
return seatinfo;
}
}
//Check if a seat id had been assigned.
int seat_isNode(Seat *seat, int num) {
SeatInfo *trace=NULL;
if(seat->head==NULL) {
return 0;
} else {
trace=seat->head;
while(trace!=NULL) {
if(trace->seatID==num) {
return 1;
} else {
trace=trace->next;
}
}
return 0;
}
}
//Get a seat information by seat id.
SeatInfo* seat_getSeatInfo(Seat *seat, int num) {
SeatInfo *trace=NULL;
trace=seat->head;
while(trace!=NULL) {
if(trace->seatID==num) {
return trace;
} else {
trace=trace->next;
}
}
return NULL;
}
main.c
------------------
#include "seatingReservation.h"
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char fun;
Seat seat;
seat.head=NULL;
if(seat_readInfo(&seat)) {
printf("Error!\n");
exit(1);
}
while(1) {
printf("\n");
printf("To choose a function, enter its letter label:\n");
printf("a) Show number of empty seats.\n");
printf("b) Show list of empty seats.\n");
printf("c) Show alphabetical list of seats.\n");
printf("d) Assign a customer to a seat assignment.\n");
printf("e) Delete a seat assignment.\n");
printf("f) Quit.\n");
scanf(" %c", &fun);
switch(fun) {
case 'a':
printf("Number of empty seats now is: %d\n", seat_getNumEmptySeat(&seat));
break;
case 'b':
printf("Empty seat list:\n");
seat_printEmptySeat(&seat);
break;
case 'c':
printf("Assigned seat list:\n");
seat_printAssignedSeat(&seat);
break;
case 'd':
seat_assignSeat(&seat);
break;
case 'e':
seat_delSeat(&seat);
break;
case 'f':
seat_writeInfo(&seat);
return 0;
default:
printf("Input error!\n");
}
}
}
(joseph, 2011-03-18 17:22)
2樓
char user_select (void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}