#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//-----------------------------------------------//
// 定义图书节点结构体
typedef struct Node{
int id; //图书编码
char name[50]; //图书名称
float price; //价格
struct Node* next; //指针
}Node;
//-----------------------------------------------//
void Menu(){
printf("\n=====图书管理系统=====\n");
printf("0. 退出系统\n");
printf("1. 添加货物\n");
printf("2. 删除货物\n");
printf("3. 修改价格\n");
printf("4. 修改货物名称\n");
printf("5. 插入货物\n");
printf("6. 按价格排序\n");
printf("7. 按编码排序\n");
printf("8. 显示货物\n");
printf("9. 按货名查找货物\n");
printf("10. 按编码查找货物\n");
printf("请输入操作编号:");
}
//-----------------------------------------------//
Node* createNode(int id, char* name, float price){
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->id = id;
strcpy(newNode->name, name);
newNode->price = price;
newNode->next = NULL;
return newNode;
}
//-----------------------------------------------//
void addBook(Node* *head, int id, char* name, float price){
Node* newNode = createNode(id, name, price);
if (*head == NULL){
*head = newNode;
return;
}
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
//-----------------------------------------------//
void deleteBook(Node* *head, int id){
if (*head == NULL) return;
Node* current = *head;
Node* prev = NULL;
while (current != NULL && current->id != id){
prev = current;
current = current->next;
}
if (current == NULL){
printf("未找到编码为%d的货物\n", id);
return;
}
if (prev == NULL){
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
printf("删除成功\n");
}
//-----------------------------------------------//
void ChangeNotePrice(Node* head, int id, float newPrice){
Node* current = head;
while (current != NULL && current->id != id){
current = current->next;
}
if (current == NULL){
printf("未找到编码为%d的货物\n", id);
return;
}
current->price = newPrice;
printf("价格修改成功\n");
}
//-----------------------------------------------//
void ChangeNoteName(Node* head, int id, char* newName){
Node* current = head;
while (current != NULL && current->id != id){
current = current->next;
}
if (current == NULL){
printf("未找到编码为%d的货物\n", id);
return;
}
strcpy(current->name, newName);
printf("名称修改成功\n");
}
//-----------------------------------------------//
void insertBook(Node* *head, int id, char* name, float price){
Node* newNode = createNode(id, name, price);
if (*head == NULL || id < (*head)->id) {
newNode->next = *head;
*head = newNode;
return;
}
Node* current = *head;
while (current->next != NULL && current->next->id < id){
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
//-----------------------------------------------//
void MaopaoPrice(Node* head) {
if (head == NULL) return;
Node* i;
Node* j;
int maopao;
for (i = head; i->next != NULL; i = i->next) {
maopao = 0;
for (j = head; j->next != NULL; j = j->next) {
if (j->price > j->next->price) {
int ChangeId = j->id;
char ChangeName[100];
strcpy(ChangeName, j->name);
float ChangePrice = j->price;
j->id = j->next->id;
strcpy(j->name, j->next->name);
j->price = j->next->price;
j->next->id = ChangeId;
strcpy(j->next->name, ChangeName);
j->next->price = ChangePrice;
maopao= 1;
}
}
if (!maopao) break;
}
}
//-----------------------------------------------//
void InputId(Node** head) {
if (*head == NULL) return;
Node* input = NULL;
Node* current = *head;
while (current != NULL) {
Node* next = current->next;
if (input == NULL || current->id < input->id) {
current->next = input;
input = current;
} else {
Node* temp = input;
while (temp->next != NULL && temp->next->id < current->id) {
temp = temp->next;
}
current->next = temp->next;
temp->next = current;
}
current = next;
}
*head = input;
}
//-----------------------------------------------//
Node* SearchByName(Node* head, char* name) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
//-----------------------------------------------//
Node* SearchById(Node* head, int id) {
Node* current = head;
while (current != NULL) {
if (current->id == id) {
return current;
}
current = current->next;
}
return NULL;
}
//-----------------------------------------------//
void OutputBooks(Node* head) {
if (head == NULL) {
printf("没有货物信息");
return;
}
printf("\n=====货物列表=====\n");
Node* current = head;
while (current != NULL) {
printf("编码: %d, 名称: %s, 价格: %.2f\n", current->id, current->name, current->price);
current = current->next;
}
}
//-----------------------------------------------//
//主函数
int main(){
Node* head=NULL;
Node* found;
int choice;
int id;
char name[50];
float price;
int pos;
while (1) {
Menu();
scanf("%d",&choice);
switch (choice) {
case 10:
printf("请输入要查找的货物编码:");
scanf("%d", &id);
found =SearchById(head, id);
if (found != NULL){
printf("找到货物: 编码: %d, 名称: %s, 价格: %.2f\n", found->id, found->name, found->price);
} else {
printf("未找到货物\n");
}
break;
default:
printf("无效的选择\n");
case 0:
printf("退出系统\n");
exit(0);
case 1:
printf("请输入货物编码:");
scanf("%d", &id);
printf("请输入货物名称:");
scanf("%s", name);
printf("请输入货物价格:");
scanf("%f", &price);
addBook(&head, id, name, price);
break;
case 2:
printf("请输入要删除的货物编码:");
scanf("%d", &id);
deleteBook(&head, id);
break;
case 3:
printf("请输入要修改价格的货物编码:");
scanf("%d", &id);
printf("请输入新的价格:");
scanf("%f", &price);
ChangeNotePrice(head, id, price);
break;
case 4:
printf("请输入要修改名称的货物编码: ");
scanf("%d", &id);
printf("请输入新的名称: ");
scanf("%s", name);
ChangeNoteName(head, id, name);
break;
case 5:
printf("请输入货物编码: ");
scanf("%d", &id);
printf("请输入货物名称: ");
scanf("%s", name);
printf("请输入货物价格: ");
scanf("%f", &price);
insertBook(&head, id, name, price);
break;
case 6:
MaopaoPrice(head);
printf("按价格排序成功。\n");
break;
case 7:
InputId(&head);
printf("按编码排序成功。\n");
break;
case 8:
OutputBooks(head);
break;
case 9:
printf("请输入要查找的货物名称:");
scanf("%s", name);
Node* found = SearchByName(head, name);
if (found != NULL){
printf("找到货物: 编码: %d, 名称: %s, 价格: %.2f\n", found->id, found->name, found->price);
} else {
printf("未找到货物\n");
}
break;
}
}
return 0;
}