?? train_booking_attach.c
字號:
// train_booking_attach.c -- 包含輔助函數
//
/////////////////////////////////////////////////////////////////////////////
#ifndef TRAIN_BOOKING_ATTACH_C_
#define TRAIN_BOOKING_ATTACH_C_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include "train_booking_declare.h"
#include "train_booking_const.h"
#include "train_booking_unit.h"
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放車次鏈表及相關內存區域(列車標識、列車、車站鏈表)
//
// 函數參數: train_sq_info_head:車次鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_train_sq_info_list(train_sq_info *train_sq_info_head)
{
train_sq_info *train_sq_info_pt;
while (train_sq_info_head->next) // 如果還有下一個結點沒釋放
{
train_sq_info_head = train_sq_info_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
train_sq_info_pt = train_sq_info_head; // 取得要銷毀的內存指針
// 釋放該車次的所有列車標識信息
free_train_item_info_list(&train_sq_info_pt->train_item_list);
// 釋放該車次的所有列車信息
free_train_info_list(&train_sq_info_pt->train_list);
free(train_sq_info_pt); // 釋放內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放列車鏈表
//
// 函數參數: train_info_head:列車鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_train_info_list(train_info *train_info_head)
{
train_info *train_info_pt;
while (train_info_head->next) // 如果還有下一個結點沒釋放
{
train_info_head = train_info_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
train_info_pt = train_info_head->next; // 取得要釋放內存指針
// 釋放該列車的站點信息內存
free_station_info_list(&train_info_pt->station_links);
free(train_info_pt); // 釋放列車內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放車站鏈表
//
// 函數參數: station_info_head:車站鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_station_info_list(station_info *station_info_head)
{
station_info *station_info_pt;
while (station_info_head->next) // 如果還有下一個結點沒釋放
{
station_info_head = station_info_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
station_info_pt = station_info_head; // 取得要銷毀的內存指針
free(station_info_pt); // 釋放車站內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放列車標識鏈表
//
// 函數參數: train_item_head:列車標識鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_train_item_info_list(train_item *train_item_head)
{
train_item *train_item_pt;
while (train_item_head->next) // 如果還有下一個結點沒釋放
{
train_item_head = train_item_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
train_item_pt = train_item_head; // 取得要銷毀的內存指針
free(train_item_pt); // 釋放列車標識內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放用戶信息鏈表
//
// 函數參數: passenger_info_head:用戶信息鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_passenger_info_list(passenger_info *passenger_info_head)
{
passenger_info *passenger_info_pt;
while (passenger_info_head->next) // 如果還有下一個結點沒釋放
{
passenger_info_head = passenger_info_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
passenger_info_pt = passenger_info_head; // 取得要銷毀的內存指針
// 釋放系統消息鏈占用內存
free_msgbox_item_list(&passenger_info_pt->sysmsg_links);
// 釋放訂票信息鏈占用內存
free_booked_list(&passenger_info_pt->booked_links);
free(passenger_info_pt); // 釋放用戶信息占用內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放用戶系統消息鏈表
//
// 函數參數: msg_box_item_head:用戶系統消息鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_msgbox_item_list(msgbox_item *msg_box_item_head)
{
msgbox_item *msg_box_item_pt;
while (msg_box_item_head->next) // 如果還有下一個結點沒釋放
{
msg_box_item_head = msg_box_item_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
msg_box_item_pt = msg_box_item_head; // 取得要銷毀的內存指針
free(msg_box_item_pt); // 釋放內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放用戶已訂票信息鏈表
//
// 函數參數: booked_info_head:用戶已訂票信息鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_booked_list(booked_info *booked_info_head)
{
booked_info *booked_info_pt;
while (booked_info_head->next) // 如果還有下一個結點沒釋放
{
booked_info_head = booked_info_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
booked_info_pt = booked_info_head; // 取得要銷毀的內存指針
free(booked_info_pt); // 釋放內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 釋放用戶標識鏈表
//
// 函數參數: user_item_head:用戶標識鏈表頭結點指針
//
/////////////////////////////////////////////////////////////////////////////
status free_user_item_info_list(user_item *user_item_head)
{
user_item *user_item_pt;
while (user_item_head->next) // 如果還有下一個結點沒釋放
{
user_item_head = user_item_head->next;
// 指針指向下一個結點,第一次運行時跳過頭結點
user_item_pt = user_item_head; // 取得要銷毀的內存指針
free(user_item_pt); // 釋放列車標識內存
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 延遲時間,單位為秒
//
// 函數參數: sec:延遲秒數
//
/////////////////////////////////////////////////////////////////////////////
status delay(int sec)
{
clock_t delay = sec * CLOCKS_PER_SEC; // 換算延遲時間
clock_t start = clock(); // 取得當前時間
while (clock() - start < delay); // 計算時差是否滿足條件
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 字符菜單顯示,序號為數組下標法
//
// 函數參數: itme:菜單條目字符串數組
// warn:附加提示信息(可選)
// start:從第幾項開始顯示
// end:顯示到第幾項
//
/////////////////////////////////////////////////////////////////////////////
key text_menu(char item[][MAX_MENU_LEN], char *warn, int start, int end)
{
int i, t;
char choice;
do {
CLS;
if (*warn) // 如果有附加信息串則輸出
{
puts(warn);
}
printf("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
for (i = start; i <= end; i++)
{
t = i + 1;
if (10 == t)
{
t = 0;
}
printf("【%d】%s \n", t, item[i]);
}
printf("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
printf("【提示】請作出一個選擇:");
choice = getch();
putchar('\n');
choice -= '1'; // 轉換為便于判斷的整數類型
} while ((choice < start) || (choice > end));
return choice + '1'; // 仍然返回字符
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 獲取字符選擇
//
// 函數參數: case:可選項的字符組合(字符串)
//
/////////////////////////////////////////////////////////////////////////////
key get_choice(char *cases)
{
char choice;
int i, len = strlen(cases);
input:
{
printf("\n【提示】請作出一個選擇:");
choice = getch();
for (i = 0; i < len; i++) // 檢測作出的選擇是否在可選范圍內
{
if (choice == cases[i])
{
return choice; // 是則返回該選擇
}
}
goto input;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 屏幕消息函數
//
// 函數參數: head:消息類型:ERROR(錯誤);WARN(警告);INFO(信息);
// TIP(提示)
// body:消息正文
// deal:消息發送完后處理方式:WC(等待確認并清屏);
// W(等待確認);C(清屏);NULL(不做任何動作)
//
/////////////////////////////////////////////////////////////////////////////
status message(char *head, char *body, char *deal)
{
char msg[MAX_MSG_LEN];
// 生成消息串
strcpy(msg, "");
strcat(msg, head);
strcat(msg, body);
printf("%s\n", msg);
if (deal == NULL)
{
return OK; // 如果沒有操作直接返回
}
// 根據參數執行后續操作
if (!strcmp(WC, deal))
{
WAIT;
CLS;
}
else if (!strcmp(W, deal))
{
WAIT;
}
else if (!strcmp(C, deal))
{
CLS;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 一級路徑文件名生成函數
//
// 函數參數: dir_i:第一級目錄名
// filename:要操作的文件名
//
/////////////////////////////////////////////////////////////////////////////
status create_i_dir(char *finalpath, char *dir_i, char *filename)
{
strcpy(finalpath, "");
strcat(finalpath, dir_i);
strcat(finalpath, "\\");
strcat(finalpath, filename);
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 二級路徑文件名生成函數
//
// 函數參數: dir_i:第一級目錄名
// dir_ii:第二級目錄名
// filename:要操作的文件名
//
/////////////////////////////////////////////////////////////////////////////
status create_ii_dir(char *finalpath, char *dir_i, char *dir_ii, char *filename)
{
strcpy(finalpath, "");
strcat(finalpath, dir_i);
strcat(finalpath, "\\");
strcat(finalpath, dir_ii);
strcat(finalpath, "\\");
strcat(finalpath, filename);
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函數功能: 三級路徑文件名生成函數
//
// 函數參數: dir_i:第一級目錄名
// dir_ii:第二級目錄名
// dir_iii:第三級目錄名
// filename:要操作的文件名
//
/////////////////////////////////////////////////////////////////////////////
char *dir_iii, char *filename)
strcpy(finalpath, "");
strcat(finalpath, dir_i);
strcat(finalpath, "\\");
strcat(finalpath, dir_ii);
strcat(finalpath, "\\");
strcat(finalpath, dir_iii);
strcat(finalpath, "\\");
strcat(finalpath, filename);
return OK;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -