C语言源代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>//增加Windows头文件为使界面更加美观
const char* file_name = "record2.txt";
//实验设备结构体信息
struct Information{
long int id;//仪器id
char name[200];//姓名
char type[200];//类名
double price;//价格
unsigned int number;
char company[200];//出产公司
char comment[400];//备注
};
//链表结构体——将设备放入链表(此处增添文件读取功能:将文件中信息输出到链表)
struct Info_list{
struct Information*node;
struct Info_list *next;//链接下处结点
};
//由于含有多个功能简化书写,使用枚举enum
enum InfoType{
NONE = 0,
ID =1,
NAME = 2,
TYPE = 3,
PRICE = 4,
NUMBER = 5,
COMPANY = 6,
COMMENT = 7,
ALL = 8
};
char* trimmed(char * c)
{
char* end = NULL;
if (NULL == c)
return c;
end = c + strlen(c) - 1;
while (*c && (*c == ' ' || *c == '\t' || *c == '\n')) {
c++;
}
while (*end && end >= c && end >= c && (*end == ' ' || *end == '\t' || *end == '\n')) {
*end-- = '\0';
}
return c;
}
void show_help()
{
printf("\n\n");
printf("\t\t\t YouYoung---实验室管理系统V7.28.1\n");
printf("\t\t\t*********************************************************************************\n");
// printf("\t\t\t\t\t\t-----ENTER THE FIRST CHAR-----\n");
printf("\t\t\t* -----ENTER THE FIRST CHAR----- *\n");
printf("\t\t\t* *\n");
printf("\t\t\t*\t 1. q\t\t退出\t\t\t\t\t*\n");
printf("\t\t\t*\t 2. i\t\t增添设备信息\t\t\t\t*\n");
printf("\t\t\t*\t 3. d\t\t删除设备\t\t\t\t*\n");
printf("\t\t\t*\t 4. m\t\t设备排序\t\t\t\t*\n");
printf("\t\t\t*\t 5. l\t\t按顺序输出\t\t\t\t*\n");
printf("\t\t\t*\t 6. lid\t\t按id输出\t\t\t\t*\n");
printf("\t\t\t*\t 7. ltype\t按type输出\t\t\t\t*\n");
printf("\t\t\t*\t 8. lprice\t按price输出\t\t\t\t*\n");
printf("\t\t\t*\t 9. lnumber\t按number输出\t\t\t\t*\n");
printf("\t\t\t*\t 10. lcompany\t按company输出\t\t\t\t*\n");
printf("\t\t\t*\t 11. lcomment\t按comment输出\t\t\t\t*\n");
printf("\t\t\t* *\n");
printf("\t\t\t*********************************************************************************\n");
}
//以下属于链表的文件操作 包含链表在文件中的保存和删除
//向文件末尾添加结构体信息
int write_info_to_file(struct Information* info){
FILE *file;
file = fopen(file_name,"a");
if(!file){
printf("\nError! Cannot Open File %s\n", file_name);
return 0;
}
fputs("\n",file);
fprintf(file, "%ld\n", info->id);
fprintf(file, "%s\n", info->name);
fprintf(file, "%s\n", info->type);
fprintf(file, "%f\n", info->price);
fprintf(file, "%d\n", info->number);
fprintf(file, "%s\n", info->company);
fprintf(file, "%s\n", info->comment);
fputs("\n",file);
fclose(file);
return 1;
}
//链表的文件保存 -wirte操作
int write_list_to_file(struct Info_list* list_head)
{
struct Info_list *list_iter = list_head->next;//类似于迭代器iterator
//文件内容的删除操作,先将原文件中内容删除再重新保存,类似于Python的w操作
FILE *file;
file = fopen(file_name,"w");
if(!file){
printf("\nError! Cannot Open File %s\n", file_name);
return 0;
}
fclose(file);
while(list_iter && list_iter->node){
write_info_to_file(list_iter->node);
list_iter = list_iter->next;
}
return 1;
}
//去除两边空白
//文件的读取操作,保存到链表
struct Info_list* read_from_file(struct Info_list* list_first)
{
FILE *file;
struct Information *info = NULL;
struct Info_list *list_next = NULL;
struct Info_list *list_temp = NULL;
char str[400];
int flag = 0;
file = fopen(file_name,"r");
if(!file){
printf("\nError! Cannot Open File %s\n",file_name);
return NULL;
}
list_temp = list_first;
while((fgets(str, 400, file))!=NULL)//从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内
{
if(strlen(str)>1){
++flag;
}
else{
continue;
}
if(flag > 7){
flag = 1;
}
switch(flag)
{
case 0:
break;
case 1:
list_next = (struct Info_list*)malloc(sizeof(struct Info_list));
info = (struct Information*)malloc(sizeof(struct Information));
if(NULL == info||NULL == list_first){
printf("\nError! Cannot malloc memory\n");
return NULL;
}
list_next->next = NULL;
list_next->node = NULL;
list_temp->node = info;
list_temp->next = list_next;
list_temp = list_next;
info->id = atol(str);//字符串转换成长整型
break;
case 2:
strncpy(info->name,str,200);//char*strncpy(char*dest,const char*src,size_t n)把 src 所指向的字符串复制到dest,最多复制 n 个字符。
trimmed(info->name);//trim()函数用于去除字符串两端的空白字符
break;
case 3:
strncpy(info->type, str,200);
trimmed(info->type);
break;
case 4:
info->price = atof(str);
break;//将字符串转成float型
case 5:
info->number = atoi(str);//转成int型
break;
case 6:
strncpy(info->company,str,200);
trimmed(info->company);
break;
case 7:
strncpy(info->comment,str,400);
trimmed(info->comment);
break;
default:
break;
}
}
fclose(file);
return list_first;
}
//插入数据命令
void insert_information(struct Info_list *list_head)
{
char id_char[200];
char price_char[200];
char number_char[200];
struct Information *info = NULL;
struct Info_list* list_next = NULL;
struct Info_list* list_temp = NULL;
/*
if (!list_head) {
list_head = (struct Info_list*)malloc(sizeof(struct Info_list));
if (NULL == list_head) {
printf("\nError! Cannot malloc memory\n");
return ;
}
list_head->next = NULL;
list_head->node = NULL;
}
*/
list_temp = list_head->next;
while(list_temp != NULL && list_temp->node != NULL){
list_temp = list_temp->next;
}
list_next = (struct Info_list*)malloc(sizeof(struct Info_list));
info = (struct Information*)malloc(sizeof(struct Information));
if(NULL == info || NULL == list_next){
printf("\nError! Cannot malloc memory\n");
return;
}
list_next->next = NULL;
list_next->node = NULL;
list_temp->next=list_next;
list_temp->node = info;
printf("\nNow Insert a New Data of Devcie.\n");
printf("Please Enter ID, only numeral support, for example: \"123456\"\n");
printf(">>>> ");
fgets(id_char,200,stdin);
info->id = atol(id_char);
printf("You Typed %ld\n",info->id);
printf("Please Enter Name, for example: \"Kobe Bryant\"\n");
printf(">>>> ");
fgets(info->name, 200, stdin);
trimmed(info->name);
printf("You Typed %s\n",info->name);
printf("Please Enter Type, for example: \"A\"\n");
printf(">>>> ");
fgets(info->type, 200, stdin);
trimmed(info->type);
printf("You Typed %s\n",info->type);
printf("Please Enter Price, only numeral support, example: \"123.456\"\n");
printf(">>>>");
fgets(price_char,200,stdin);
info->price = atof(price_char);
printf("You Typed %f\n", info->price);
printf("Please Enter Number, only numeral support, example: \"543210\"\n");
printf(">>>>");
fgets(number_char,200,stdin);
info->number = atof(number_char);
printf("You Typed %u\n", info->number);
printf("Please Enter Company, for example: \"Red Had\"\n");
printf(">>>>");
fgets(info->company,200,stdin);
trimmed(info->company);
printf("You Typed %s\n",info->company);
printf("Please Enter Comment,for example: \"This is Comment\"\n");
printf(">>>> ");
fgets(info->comment, 400,stdin);
printf("You Typed %s\n",info->comment);
trimmed(info->comment);
write_info_to_file(info);
}
void list_information(struct Info_list *list_head)
{
struct Info_list *list_temp = list_head->next;
struct Information *temp = NULL;
while(list_temp !=NULL && list_temp->node !=NULL){
temp = list_temp->node;
printf("\n ID: %ld", temp->id);
printf("\t Name: %s", temp->name);
printf("\t Type %s",temp->type);
printf("\t Price: %.2f", temp->price);
printf("\t Number: %u",temp->number);
printf("\t Company: %s", temp->company);
printf("\t Comment: %s\n",temp->comment);
list_temp = list_temp->next;
}
}
//按相应的排序方法显示数据
void list_information_order(struct Info_list *list_head,const char* which)
{
struct Info_list *list_temp = list_head->next;
struct Info_list *swap_temp = NULL;
struct SortHelp
{
struct Info_list* node;
int flag;
struct SortHelp *next;
};
struct SortHelp *sort_head = NULL;
struct SortHelp *sort_iter = NULL;
struct SortHelp *sort_one = NULL;
struct SortHelp *sort_min = NULL;
struct SortHelp *sort_temp = NULL;
struct Information * info_temp = NULL;
int sort_num = 0;
int i = 0;
int j = 0;
//新建链表保存原链表的信息
//copy Info_list to SortHelp
while(list_temp&&list_temp->node!=NULL){
sort_one = (struct SortHelp*)malloc(sizeof(struct SortHelp));
if(NULL == sort_one){
printf("\nError! Cannot malloc memory\n");
sort_iter = sort_head;
while(sort_iter) {
sort_temp = sort_iter;
sort_iter = sort_iter->next;
free(sort_temp);
}
return;
}
sort_one->node = list_temp;
sort_one->flag = 0;
sort_one->next = NULL;
if(NULL == sort_head){
sort_head = sort_one;
sort_iter = sort_head;
} else{
sort_iter->next = sort_one;
sort_iter = sort_iter->next;
}
list_temp = list_temp->next;
++sort_num;
}
//冒泡排序
for(i = sort_num; i >0; --i){
sort_iter = sort_head;
//跳过之前已经排好序的结点
for(j=0;j<sort_num - i;++j){
sort_iter = sort_iter->next;
}
sort_temp = sort_iter;
//找到数值最小的结点
sort_min = sort_iter;
while(sort_iter) {
if (0 == sort_iter->flag) {
if (0 == strcmp(which, "id")) {
if (sort_iter->node->node->id < sort_min->node->node->id)
sort_min = sort_iter;
} else if (0 == strcmp(which, "name")) {
if (strcmp(sort_iter->node->node->name , sort_min->node->node->name) < 0)
sort_min = sort_iter;
} else if (0 == strcmp(which, "price")) {
if (sort_iter->node->node->price < sort_min->node->node->price)
sort_min = sort_iter;
} else if (0 == strcmp(which, "type")) {
if (strcmp(sort_iter