c语言超市管理系统源代码
时间: 2023-06-26 13:05:22 浏览: 92
c语言超市管理系统源码
以下是一个简单的 C 语言超市管理系统的源代码,仅供参考。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct product {
int id;
char name[50];
float price;
int quantity;
};
int main() {
int n, i, choice, id, quantity, flag = 0;
char name[50];
float price;
struct product *products;
printf("Enter the number of products: ");
scanf("%d", &n);
products = (struct product*) malloc(n * sizeof(struct product));
for (i = 0; i < n; i++) {
printf("\nEnter product %d details:\n", i+1);
printf("ID: ");
scanf("%d", &products[i].id);
printf("Name: ");
scanf("%s", products[i].name);
printf("Price: ");
scanf("%f", &products[i].price);
printf("Quantity: ");
scanf("%d", &products[i].quantity);
}
while (1) {
printf("\n\n");
printf("1. Display all products\n");
printf("2. Search for a product by ID\n");
printf("3. Search for a product by name\n");
printf("4. Add a new product\n");
printf("5. Update a product\n");
printf("6. Delete a product\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nAll products:\n");
for (i = 0; i < n; i++) {
printf("ID: %d, Name: %s, Price: %0.2f, Quantity: %d\n",
products[i].id, products[i].name, products[i].price, products[i].quantity);
}
break;
case 2:
printf("\nEnter the ID to search: ");
scanf("%d", &id);
for (i = 0; i < n; i++) {
if (products[i].id == id) {
printf("Product found:\n");
printf("ID: %d, Name: %s, Price: %0.2f, Quantity: %d\n",
products[i].id, products[i].name, products[i].price, products[i].quantity);
flag = 1;
break;
}
}
if (!flag) {
printf("Product not found.\n");
}
break;
case 3:
printf("\nEnter the name to search: ");
scanf("%s", name);
for (i = 0; i < n; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("Product found:\n");
printf("ID: %d, Name: %s, Price: %0.2f, Quantity: %d\n",
products[i].id, products[i].name, products[i].price, products[i].quantity);
flag = 1;
break;
}
}
if (!flag) {
printf("Product not found.\n");
}
break;
case 4:
n++;
products = (struct product*) realloc(products, n * sizeof(struct product));
printf("\nEnter product details:\n");
printf("ID: ");
scanf("%d", &products[n-1].id);
printf("Name: ");
scanf("%s", products[n-1].name);
printf("Price: ");
scanf("%f", &products[n-1].price);
printf("Quantity: ");
scanf("%d", &products[n-1].quantity);
printf("Product added successfully.\n");
break;
case 5:
printf("\nEnter the ID of the product to update: ");
scanf("%d", &id);
flag = 0;
for (i = 0; i < n; i++) {
if (products[i].id == id) {
printf("Enter new details:\n");
printf("Name: ");
scanf("%s", products[i].name);
printf("Price: ");
scanf("%f", &products[i].price);
printf("Quantity: ");
scanf("%d", &products[i].quantity);
printf("Product updated successfully.\n");
flag = 1;
break;
}
}
if (!flag) {
printf("Product not found.\n");
}
break;
case 6:
printf("\nEnter the ID of the product to delete: ");
scanf("%d", &id);
flag = 0;
for (i = 0; i < n; i++) {
if (products[i].id == id) {
for (int j = i; j < n - 1; j++) {
products[j] = products[j+1];
}
n--;
products = (struct product*) realloc(products, n * sizeof(struct product));
printf("Product deleted successfully.\n");
flag = 1;
break;
}
}
if (!flag) {
printf("Product not found.\n");
}
break;
case 7:
free(products);
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
```
注意:此代码仅供参考,可能存在漏洞和不完善之处。建议在实际应用中进行必要的修改和测试。
阅读全文