在疫情期间,由于学校通知我们课程设计不受物理环境的限制,于是C语言课设如约而至。。。刚刚结束的C语言课程设计的确非常煎熬(还不是c语言没有学好嘛),想必也会有很多人像我一样在网上到处找代码,或是恶补链表文件的知识点,但最后还是在网上苦苦的寻找符合老师要求的代码,话不多说,直接上代码。
#include "singlelist.h" void medicine_menu()//主菜单 { printf("\t----- Hello! You
have entered the management system of pharmacy -----\n\n"); printf(
"\t\t\t\t\t\t\t\t\tMENU\n\n"); printf("\t\t\t Enter 1 to add the message of
medicine.\n");//添加 printf("\t\t\t Enter 2 to check the message of medicine.\n");
//查看 printf("\t\t\t Enter 3 to revise the message of medicine.\n");//修改 printf(
"\t\t\t Enter 4 to search medicine with its number.\n");//按编号查找 printf("\t\t\t
Enter 5 to sort medicine by price.\n");//按价格排序 printf("\t\t\t Enter 6 to delete
the message of medicine.\n");//删除信息 printf("\t\t\t Enter 7 to exit this
system.\n");//退出 printf(
"\t---------------------------------------------------------------------\n\n");
printf("\t\t\t Please enter the number (1 ~ 7) :"); } drug *list; void input()
//用户输入 { int choice = 0; medicine date; drug *pMove = NULL; scanf("%d",&choice);
switch(choice) { case 1://添加信息 printf("\t\t\t\t\t----------[ADD
MESSAGES]----------\n"); printf("\t\tPlease enter the
number,name,MFD,EXP,price,inventory,brand and function\n"); printf("\t\t");
fflush(stdin); scanf("%s %s %s %s %lf %s %s %s", date.number,date.name,date.MFD,
date.EXP,&date.price,date.inventory,date.brand,date.function); insert(list, date
); break; case 2://查看所有信息 printf("\t\t\t\t\t----------[Check
Information]----------\n"); print(list); break; case 3://修改信息 printf(
"\t\t\t\t\t----------[Revise Information]----------\n"); printf("\t\tPlease
enter the number needed to be revised:"); scanf("%s",date.number); if(search(
list, date.number) == NULL) { printf("Erro\n"); } else { drug *node = search(
list, date.number); printf("\t\tPlease enter the number,name,MFD,EXP,price,
inventory,brand and function\n\t\t"); scanf("%s %s %s %s %lf %s %s %s",node->
date.number,node->date.name,node->date.MFD,node->date.EXP,&node->date.price,node
->date.inventory,node->date.brand,node->date.function); } break; case 4://查找药品信息
printf("\t\t\t\t\t----------[Search Information]----------\n"); printf(
"\tPlease enter the number needed to be searched:"); scanf("%s" , date.number);
pMove= search(list, date.number); if(pMove == NULL) { printf("Erro\n"); system(
"pause"); } else { printf("\t\t\tThe number of the medicine--------%s\n",pMove->
date.number); printf("\t\t\tThe name of the medicine----------%s\n",pMove->date.
name); printf("\t\t\tThe MFD of the medicine-----------%s\n",pMove->date.MFD);
printf("\t\t\tThe EXP of the medicine-----------%s\n",pMove->date.EXP); printf(
"\t\t\tThe price of the medicine---------%.2f\n",pMove->date.price); printf(
"\t\t\tThe inventory of the medicine-----%s\n",pMove->date.inventory); printf(
"\t\t\tThe brand of the medicine---------%s\n",pMove->date.brand); printf(
"\t\t\tThe function of the medicine------%s\n",pMove->date.function); } break;
case 5://排序 printf("The result of the compositor from low to high is :\n"); sort
(list); Display(list); break; case 6://删除信息 printf("\t\t\t\t\t----------[Delete
Information]----------\n"); printf("Please enter the number needed to be
deleted:"); scanf("%s",date.number); delet(list, date.number); break; case 7:
printf("exit"); system("pause"); exit(0); break; default: printf(
"\n\t\t\t\tErro\n"); system("pause"); break; } writeInfoToFile(list,"1.txt"); }
int main(void) { list = creatlist(); printf(
"\t---------------------------------------------------------------------\n");
printf("\t-------------------- Welcome to management
system--------------------\n"); printf("\t------------------ Press any key to
enter the menu-------------------\n"); printf(
"\t---------------------------------------------------------------------\n\n\n")
; getchar(); readInfoFromFile(list,"1.txt"); while (1) { medicine_menu(); input(
); system("pause"); system("cls"); } return 0; }
以上是main.c文件中的内容,再来看看singlelist.h这个头文件中的内容
#ifndef singlelist_h #include <stdio.h> #include <string.h> #include <math.h> #
include <stdlib.h> typedef struct medicine{ char number[10];//药品编号 char name[50]
;//药品名称 char MFD[50];//生产日期 char EXP[50];//保质期 double price;//价格 char inventory[
50];//库存数量 char brand[50];//品牌 char function[100];//药品功能 }medicine; typedef
struct drug{ medicine date; struct drug *next; }drug; drug * creatlist()//创建链表 {
drug*head = (drug*)malloc(sizeof(drug)); head->next = NULL; return head; } drug
* creatNode(medicine date)//创建节点 { drug *newnode = (drug*)malloc(sizeof(drug));
newnode->date = date; newnode->next = NULL; return newnode; } void insert(drug *
head, medicine date)//录入信息(头插法) { drug *newNode = creatNode(date); newNode->next
= head->next; head->next = newNode; } void delet(drug *head , char *number)
//通过编号删除信息 { drug *posNode = head->next; drug *posFrontnode = head; if(posNode
== NULL)//若该链表中没有数据 { printf("Erro\n"); return; } else { while (strcmp(posNode->
date.number,number))//比较原有的数据与输入的数据 { posFrontnode = posNode; posNode =
posFrontnode->next; if( posNode == NULL) { printf("Erro\n"); return; } }
posFrontnode->next = posNode->next; free(posNode); } } drug *search(drug *head,
char *number)//根据编号查找信息 { drug *pMove = head->next; if(pMove == NULL) { return
NULL; } while(strcmp(pMove->date.number , number)) { pMove = pMove->next; }
return pMove; } void sort(drug *L) { drug *p,*pre,*q; p = L->next->next;
//p指向L的第2个数据节点 L->next->next = NULL; while (p != NULL) { q = p->next;
//q保存*p节点后继节点的指针 pre = L; //从表头开始进行比较 while (pre->next != NULL && (pre->next->
date.price < pre->date.price)) { pre = pre->next; //在有序表中找插入*p的前趋节点*pre } p->
next= pre->next; pre->next = p; p = q; //扫描原单链表余下的节点 } } void Display(drug *L) {
drug*p = L->next; //p指向第一个结点(不一定是头结点) while( p!= NULL) { printf("\t\t%s
%.2lf\n",p->date.name,p->date.price); p = p->next; } printf("\n"); } void
readInfoFromFile(drug *headnode, char*fileName) { //1.打开文件 FILE*fp; medicine
date; fp=fopen(fileName,"r"); if(fp == NULL) { fp=fopen(fileName,"w+"); }
//2.读文件 while(fscanf(fp,"%s\t%s\t%s\t%s\t%lf\t%s\t%s\t%s\t\n",date.number,date.
name,date.MFD,date.EXP,&date.price,date.inventory,date.brand,date.function) !=
EOF) { insert(headnode,date); } //3.关闭文件 fclose(fp); } //文件写操作 void
writeInfoToFile(drug *headnode, char*fileName) { FILE*fp; fp = fopen(fileName,
"w"); drug *pMove = headnode->next; while(pMove) { fprintf(fp,
"%s\t%s\t%s\t%s\t%.2f\t%s\t%s\t%s\t\n",pMove->date.number,pMove->date.name,pMove
->date.MFD,pMove->date.EXP,pMove->date.price,pMove->date.inventory,pMove->date.
brand,pMove->date.function); pMove=pMove->next; } fclose(fp); } void print(drug
* head)//打印所有信息 { drug *p = head->next; while (p) { printf("\t\t\tThe number of
the medicine--------%s\n",p->date.number); printf("\t\t\tThe name of the
medicine----------%s\n",p->date.name); printf("\t\t\tThe MFD of the
medicine-----------%s\n",p->date.MFD); printf("\t\t\tThe EXP of the
medicine-----------%s\n",p->date.EXP); printf("\t\t\tThe price of the
medicine---------%.2f\n",p->date.price); printf("\t\t\tThe inventory of the
medicine-----%s\n",p->date.inventory); printf("\t\t\tThe brand of the
medicine---------%s\n",p->date.brand); printf("\t\t\tThe function of the
medicine------%s\n",p->date.function); printf("\n"); p = p->next; } printf("\n")
; } #endif 其实这些管理系统基本上都是一个意思,无非就是要求熟练链表和文件的操作,如果要做的不是药店管理系统而是其他的系统 如:图书管理系统 ,
学生管理系统 , 球星管理系统等等,都可以在这个基础上修改结构体成员,或者自己修改函数功能。
最后祝福大家的课设都顺利通过哦!

技术
©2019-2020 Toolsou All rights reserved,
vs2017,创建C++Win32窗体应用程序 项目跟了一年,客户最后说不做了org.postgresql.util.PSQLException 处理记录pytorch之ResNet18(对cifar10数据进行分类准确度达到94%)非父子组件之间的三种传值办法latex中自定义的命令———\newcommand习题11-5 指定位置输出字符串ElementUI中获取select 的label值(精华)2020年6月26日 C#类库model PageInput(精华)2020年7月21日 ASP.NET Core 全局过滤器的使用