網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在大家學(xué)完C語(yǔ)言后,經(jīng)常會(huì)被學(xué)校要求做很多管理系統(tǒng),主流做法當(dāng)選鏈表,但是很多問(wèn)題就來(lái)了,在學(xué)習(xí)階段寫(xiě)過(guò)最多基本上就是50行代碼左右了,一下子做個(gè)小型管理系統(tǒng)問(wèn)題就比較多。小編當(dāng)然也是在學(xué)完初階C語(yǔ)言之后過(guò)了半年才完整做出來(lái)。
所以分享一下經(jīng)驗(yàn)。
1:首先數(shù)據(jù)結(jié)構(gòu):鏈表要會(huì),而且要熟練掌握其增刪查改;
2:調(diào)試代碼要會(huì),很多代碼一行一行的看到最后真的是很讓人頭大;
不會(huì)調(diào)試和監(jiān)控變量是個(gè)很痛苦的過(guò)程,就算寫(xiě)出來(lái)bug多到炸也是很繁瑣的;
3:最關(guān)鍵的就是這個(gè)框架問(wèn)題了,如果框架不好,這個(gè)代碼量會(huì)數(shù)倍上升,最后就是很多重疊的語(yǔ)句,很不美觀,并且代碼質(zhì)量不是很好,在寫(xiě)系統(tǒng)之前一定要多看點(diǎn)小型管理系統(tǒng)的代碼,主要是學(xué)習(xí)框架如何建立,然后再根據(jù)自己的系統(tǒng)情況進(jìn)行改進(jìn)。
首先我們來(lái)看頭文件,先將能夠用到的頭文件一一羅列出來(lái)創(chuàng)建在?
common.h文件里面。
#define _CRT_SECURE_NO_WARNINGS?
#define Space 1000
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<assert.h>//斷言
#include<stdbool.h>
#include<string.h>
將定義宏等包含進(jìn)去。
其次就是這個(gè)框架了
#include"system.h"
int main()
{
?? ?CycleList myCycleList;
?? ?CycleListInit(&myCycleList);
?? ?int input = 0;
?? ?do
?? ?{
?? ??? ?menu();
?? ??? ?printf("請(qǐng)選擇->");
?? ??? ?scanf("%d", &input);
?? ??? ?switch (input)
?? ??? ?{
?? ??? ?case 1:
?? ??? ??? ?system("cls");
?? ??? ??? ?LoginCycle(&myCycleList);
?? ??? ??? ?break;
?? ??? ?case 0:
?? ??? ??? ?system("cls");
?? ??? ??? ?printf("自行車(chē)管理系統(tǒng)已經(jīng)退出\n");
?? ??? ??? ?break;
?? ??? ?case 2:
?? ??? ??? ?system("cls");
?? ??? ??? ?SearchCycle(&myCycleList);
?? ??? ??? ?break;
?? ??? ?case 3:
?? ??? ??? ?system("cls");
?? ??? ??? ?ChangeCycle(&myCycleList);
?? ??? ??? ?break;
?? ??? ?case 4:
?? ??? ??? ?system("cls");
?? ??? ??? ?PickUpCycle(&myCycleList);
?? ??? ??? ?_flushall();
?? ??? ??? ?FileWrite(&myCycleList);
?? ??? ??? ?_flushall();
?? ??? ??? ?break;
?? ??? ?case 5:
?? ??? ??? ?system("cls");
?? ??? ??? ?PrintResSpace(&myCycleList);
?? ??? ??? ?break;
?? ??? ?default:
?? ??? ??? ?system("cls");
?? ??? ??? ?printf("輸入有誤請(qǐng)重新選擇\n");
?? ??? ??? ?break;
?? ??? ?}
?? ?} while (input);
?? ??? ?system("pause");
?? ??? ?return 0;
?? ?}
再看各個(gè)功能的實(shí)現(xiàn)
為了避免函數(shù)代碼在一個(gè)文件里面十分的繁瑣,可以先創(chuàng)建一個(gè).h文件將函數(shù)名和節(jié)點(diǎn)定義放進(jìn)去,看代碼。
#include"common.h"
typedef struct Time
{
?? ?int year;
?? ?int month;
?? ?int day;
?? ?int hour;
}Time;
typedef struct Cycle
{
?? ?char CycleHoster[20];//自行車(chē)主名字
?? ?int Sex;
?? ?int Age;
?? ?char IDcard[18];//身份證號(hào)
?? ?Time partingTime; ? ? ?//存放時(shí)間
?? ?int number;//自行車(chē)車(chē)位號(hào)
?? ?struct Cycle* next;
}CycleNode;
typedef struct CycleList
{
?? ?CycleNode *first;
?? ?CycleNode *last;
?? ?size_t ?space;
}CycleList;
void menu();
void LoginCycle(CycleList *plist);//登記自行車(chē)函數(shù)
bool SearchCycle(CycleList* myBycycleList);//按照車(chē)位號(hào)查詢(xún)自行車(chē)
void ChangeCycle(CycleList* myBycycleList);
void PickUpCycle(CycleList* myBycycleList);
void PrintResSpace(CycleList* myBycycleList);
void CycleListInit(CycleList *plist);
CycleNode*_Buynode(char* p, int sex, int age, char* number, int Pnumber, int year, int month, int day, int hour);
bool CycleListIsFull(CycleList *plist);
CycleNode* SearchCycle1(CycleList* myBycycleList);
void FileWrite(CycleList* myBycycleList);
void DeleteCyNode(CycleList* myBycycleList,int key);
最后就是代碼函數(shù)功能的實(shí)現(xiàn)了。
#include"system.h"
void menu()
{
?? ?system("color 4");
?? ?printf(" ? ? *********************************************************************** ? ? \n");
?? ?printf(" ?************************歡迎使用自行車(chē)存放管理系統(tǒng)******************************* ? \n");
?? ?printf("| ? ? ? ? ? ? ? ? ? ? ? ? ?自行車(chē)停車(chē)收費(fèi)標(biāo)準(zhǔn)2元一小時(shí) ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("|\t1.--登記自行車(chē)停放位信息 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("|\t2.--查詢(xún)自行車(chē)停車(chē)位信息 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("|\t3.--修改自行車(chē)信息 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("|\t4.--用戶(hù)取車(chē)管理 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("|\t5.--顯示當(dāng)前空余自行車(chē)位信息 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("|\t0.--退出自行車(chē)管理系統(tǒng) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|\n");
?? ?printf("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |\n");
?? ?printf("|*******************************************************************************|\n");
?? ?printf("|********************>>>>楊天喆-計(jì)科(02)班-20190101134<<<<********************|\n");
?? ?printf("|*******************************************************************************|\n\n");
}
void LoginCycle(CycleList *plist)
{
?? ?
?? ?if (!CycleListIsFull(plist))
?? ?{
?? ??? ?char name[20];
?? ??? ?int age;
?? ??? ?int sex;
?? ??? ?char IDcard[18];
?? ??? ?int hour;
?? ??? ?int Number;
?? ??? ?int year;
?? ??? ?int month;
?? ??? ?int day;
?? ??? ?printf("請(qǐng)輸入自行車(chē)主姓名\n");
?? ??? ?scanf("%s",name);
?? ??? ?_flushall();
?? ??? ?printf("請(qǐng)輸入性別(0代表男,1代表女)");
?? ??? ?scanf("%d", &sex);
?? ??? ?printf("請(qǐng)輸入車(chē)主年齡");
?? ??? ?scanf("%d", &age);
?? ??? ?printf("請(qǐng)輸入身份證號(hào)");
?? ??? ?scanf("%s", IDcard);
?? ??? ?_flushall();
?? ??? ?printf("請(qǐng)輸入停車(chē)時(shí)間(年,月,日,時(shí))");
?? ??? ?scanf("%d,%d,%d,%d",&year,&month,&day,&hour);
?? ??? ?printf("請(qǐng)輸入要停的位號(hào)(位號(hào)為1到2000)");
?? ??? ?scanf("%d", &Number);
?? ??? ?plist->space++;
?? ??? ?plist->last->next = _Buynode(name, sex, age, IDcard, Number,year,month,day,hour);
?? ??? ?plist->last = _Buynode(name, sex, age, IDcard, Number, year, month, day, hour);
?? ??? ?FILE *fp = fopen("Test.txt", "a");
?? ??? ?fprintf(fp, "%s %d %d %s %d %d %d %d %d %d\n", name,age,sex,IDcard,hour,Number,year,month,day,hour);
?? ??? ?fclose(fp);
?? ??? ?printf("登記成功\n");
?? ??? ?printf("\n");
?? ?}
?? ?else
?? ?{
?? ??? ?printf("自行車(chē)位已滿(mǎn),不能繼續(xù)停車(chē)");
?? ?}
}
CycleNode* _Buynode(char* p, int sex, int age, char* number, int Pnumber, int year, int month, int day, int hour)
{
?? ?CycleNode *s = (CycleNode*)malloc(sizeof(CycleNode));
?? ?size_t sz = strlen(p)+1;
?? ?size_t sz1 = strlen(number)+1;
?? ?if (s == NULL)
?? ??? ?return NULL;
?? ?else
?? ?{
?? ??? ?memcpy(s->CycleHoster,p,sz);
?? ??? ?s->Age = age;
?? ??? ?s->Sex = sex;
?? ??? ?s->partingTime.year = year;
?? ??? ?s->partingTime.month = month;
?? ??? ?s->partingTime.hour = hour;
?? ??? ?s->partingTime.day = day;
?? ??? ?memcpy(s->IDcard,number,sz1);
?? ??? ?s->number = Pnumber;
?? ??? ?s->next = NULL;
?? ??? ?return s;
?? ?}
}
void CycleListInit(CycleList *plist)
{
?? ?char qname[20] = "姓名初始化";
?? ?char qnumber[18] = "身份證初始化";
?? ?CycleNode* p = _Buynode(qname, 0, 0, qnumber, 0, 0,0,0,0);
?? ?plist->first = plist->last = p;
?? ?plist->space = 0;
}
bool CycleListIsFull(CycleList *plist)
{
?? ?if (plist->space > Space)
?? ??? ?return true;
?? ?return false;
}
bool SearchCycle(CycleList* myBycycleList)
{
?? ?int num;
?? ?printf(" ? ? ?查詢(xún)客房信息\n");
?? ?printf("請(qǐng)輸入要查詢(xún)自行車(chē)位號(hào):\n");
?? ?scanf("%d", &num);
?? ?CycleNode* p = myBycycleList->first->next;
?? ?while ((p) != NULL)
?? ?{
?? ??? ?if (p->number == num)
?? ??? ?{
?? ??? ??? ?printf("車(chē)主姓名:%s\n", p->CycleHoster);
?? ??? ??? ?(p)->Sex == 0 ? printf("男") : printf("女");
?? ??? ??? ?printf("車(chē)主年齡 %d\n", p->Age);
?? ??? ??? ?printf("車(chē)主的身份證號(hào)為%s \n", p->IDcard);
?? ??? ??? ?printf("停車(chē)時(shí)間是 %d 年 %d ?月 ?%d 日 ?%d時(shí)\n", p->partingTime.year,p->partingTime.month,p->partingTime.day,p->partingTime.hour);
?? ??? ??? ?printf("停車(chē)位號(hào)為%d \n", p->number);
?? ??? ??? ?printf("查詢(xún)成功\n");
?? ??? ??? ?printf("\n");
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?p = p->next;
?? ?}
?? ?system("cls");
?? ?printf("查詢(xún)失敗,此車(chē)位還未被停車(chē)\n");
?? ?return false;
}
void ChangeCycle(CycleList* myBycycleList)
{
?? ?int num;
?? ?int temp;
?? ?char Name[20];
?? ?char Number[18];
?? ?size_t sz1;
?? ?size_t sz2;
?? ?FILE *fp = fopen("Test.txt", "wt");
?? ?int SEX;
?? ?int AGE;
?? ?printf("請(qǐng)輸入你要更改的信息的自行車(chē)位號(hào)\n");
?? ?scanf("%d", &num);
?? ?CycleNode *p = myBycycleList->first;
?? ?while (p != NULL)
?? ?{
?? ??? ?if (p->number == num)
?? ??? ?{
?? ??? ??? ?printf("您所查詢(xún)的信息如下\n");
?? ??? ??? ?SearchCycle(myBycycleList);
?? ??? ??? ?printf("請(qǐng)輸入你要更改的信息\n");
?? ??? ??? ?printf("1:更改姓名\n");
?? ??? ??? ?printf("2:更改性別\n");?? ??? ?
?? ??? ??? ?printf("3:更改年齡\n");
?? ??? ??? ?printf("4:更改身份證號(hào)\n");
?? ??? ??? ?scanf("%d", &temp);
?? ??? ??? ?switch (temp)
?? ??? ??? ?{
?? ??? ??? ?case ?1:
?? ??? ??? ??? ?_flushall();
?? ??? ??? ??? ?printf("請(qǐng)輸入新的姓名\n");
?? ??? ??? ??? ?scanf("%s", Name);
?? ??? ??? ??? ?_flushall();
?? ??? ??? ??? ?sz1 = strlen(Name)+1;
?? ??? ??? ??? ?memcpy(p->CycleHoster, Name, sz1);
?? ??? ??? ??? ?printf("更改成功,返回主菜單");
?? ??? ??? ??? ?printf("\n");
?? ??? ??? ??? ?return;
?? ??? ??? ?case 2:
?? ??? ??? ??? ?printf("請(qǐng)輸入性別:(0代表男)\n");
?? ??? ??? ??? ??? ?scanf("%d", &SEX);
?? ??? ??? ??? ??? ?p->Sex = num;
?? ??? ??? ??? ??? ?printf("更改成功,返回主菜單");
?? ??? ??? ??? ??? ?printf("\n");
?? ??? ??? ??? ??? ?return;
?? ??? ??? ?case 3:
?? ??? ??? ??? ?printf("請(qǐng)輸入年齡\n");
?? ??? ??? ??? ?scanf("%d", &AGE);
?? ??? ??? ??? ?p->Age = AGE;
?? ??? ??? ??? ?printf("更改成功,返回主菜單");
?? ??? ??? ??? ?printf("\n");
?? ??? ??? ??? ?return;
?? ??? ??? ?case 4:
?? ??? ??? ??? ?printf("請(qǐng)輸入身份證號(hào)\n");
?? ??? ??? ??? ?scanf("%s", Number);
?? ??? ??? ??? ?_flushall();
?? ??? ??? ??? ?sz2 = strlen(Number)+1;
?? ??? ??? ??? ?memcpy(p->IDcard, Number, sz2);
?? ??? ??? ??? ?printf("更改成功,返回主菜單");
?? ??? ??? ??? ?printf("\n");
?? ??? ??? ??? ?return;
?? ??? ??? ?default:
?? ??? ??? ??? ?printf("輸入有誤,返回主菜單\n");
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?p = p->next;
?? ?}
?? ?if(p == NULL)
?? ?{
?? ?printf("輸入有誤,返回主菜單");
?? ?printf("\n");
?? ?}
?? ?fclose(fp);
}
void PickUpCycle(CycleList* myBycycleList)//取車(chē)
{
?? ?int Year;
?? ?int Month;
?? ?int Day;
?? ?int Hour;
?? ?int FEE;
?? ?CycleNode*p = SearchCycle1(myBycycleList);
?? ?CycleNode* q = p->next;
?? ?assert(p != NULL);
?? ?printf("請(qǐng)輸入取車(chē)(年,月,日,時(shí))");
?? ?scanf("%d,%d,%d,%d", &Year, &Month, &Day, &Hour);
?? ?printf("您存車(chē)的時(shí)間是 %d年 %d月 %d日 %d時(shí)", (*p).partingTime.year, (*p).partingTime.month, (*p).partingTime.day, (*p).partingTime.hour);
?? ?FEE = (((Year - (*p).partingTime.year) * 365 * 24 + (Month - (*p).partingTime.month) * 30 + (Day - (*p).partingTime.day) * 24 + (Hour - (*p).partingTime.hour)))*2;
?? ?printf("您需要繳納的費(fèi)用為%d\n\n", FEE);
?? ?DeleteCyNode(myBycycleList, p->number);
?? ?printf("取車(chē)成功,返回主菜單\n");
?? ?printf("\n");
}
CycleNode* SearchCycle1(CycleList* myBycycleList)
{
?? ?int num;
?? ?printf("請(qǐng)輸入要查詢(xún)自行車(chē)位號(hào):\n");
?? ?scanf("%d", &num);
?? ?CycleNode* p = myBycycleList->first;
?? ?while (p != NULL)
?? ?{
?? ??? ?if (p->number == num)
?? ??? ?{
?? ??? ??? ?return p;
?? ??? ?}
?? ??? ?p = p->next;
?? ?}
?? ?return NULL;
}
void PrintResSpace(CycleList* myBycycleList)
{
?? ?printf("=============查詢(xún)剩余車(chē)位===================\n");
?? ?CycleNode* p = myBycycleList->first->next;
?? ?assert(p != NULL);
?? ?while (p != NULL)
?? ?{
?? ??? ?printf("當(dāng)前使用中的車(chē)位號(hào)是 %d\n", p->number);
?? ??? ?p = p->next;
?? ?}
?? ?printf("剩余車(chē)位數(shù)為: %zu \n",Space - myBycycleList->space);
?? ?printf("=============================================\n");
}
void FileWrite(CycleList* myBycycleList)
{
?? ?FILE *fp = fopen("Test.txt", "w");
?? ?CycleNode *p = myBycycleList->first;
?? ?assert(p != NULL);
?? ?while (p != NULL)
?? ?{
?? ??? ?fprintf("%s %d %d %s %d %d %d %d %d ",p->CycleHoster, p->Age,p->Sex,p->IDcard,p->number,p->partingTime.year,p->partingTime.month,p->partingTime.day, p->partingTime.hour);
?? ??? ?p = p->next;
?? ?}
?? ?fclose(fp);
}
void DeleteCyNode(CycleList* myBycycleList, int key)
{
?? ?CycleNode *s;//q
?? ?CycleNode *t = myBycycleList->first;//p
?? ?while (t->next != NULL && t->next->number != key)
?? ??? ?t = t->next;
?? ?if (t->next == NULL)
?? ??? ?return;
?? ?s = t->next;
?? ?if (t->next == myBycycleList->last)
?? ??? ?myBycycleList->last = t;
?? ?t->next = s->next;
?? ?free(t);
?? ?myBycycleList->space--;
?? ?return;
}
實(shí)際上用C寫(xiě)出來(lái)的這種系統(tǒng)只能說(shuō)是應(yīng)付學(xué)校檢測(cè),實(shí)際上沒(méi)有人愿意用這種系統(tǒng),操作性不好,界面不美觀,魯棒性也不好,但是C真的是基礎(chǔ)編程學(xué)科,掌握這些方法的底層實(shí)現(xiàn)有助于很多其他編程語(yǔ)言的學(xué)習(xí),所以,底層的方法我們還是要學(xué)一學(xué)。
原文鏈接:https://blog.csdn.net/flf1234567898/article/details/104660142
相關(guān)推薦
- 2023-01-21 python?flask自定義404錯(cuò)誤頁(yè)面方式_python
- 2022-07-21 python:實(shí)現(xiàn)balanced parentheses平衡括號(hào)表達(dá)式算法(附完整源碼)
- 2022-09-25 服務(wù)器ftp上傳失敗的原因有什么
- 2022-09-06 一文詳解Python如何優(yōu)雅地對(duì)數(shù)據(jù)進(jìn)行分組_python
- 2022-05-27 Python/R語(yǔ)言分別實(shí)現(xiàn)斐波那契數(shù)列的示例詳解_python
- 2023-11-11 tensorflow分布式報(bào)錯(cuò):tensorflow.python.framework.errors
- 2023-07-15 express 連接 MongoDb
- 2022-06-28 C++詳解多線(xiàn)程中的線(xiàn)程同步與互斥量_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支