c语言编写一个简单的商店销售系统的程序
时间: 2023-08-07 09:05:04 浏览: 42
好的,下面是一个简单的商店销售系统的程序:
```c
#include <stdio.h>
int main()
{
// 定义变量
int product_id, quantity;
float price, total_price = 0;
// 循环输入商品信息
while (1)
{
printf("请输入商品编号(输入0结束):");
scanf("%d", &product_id);
if (product_id == 0)
break;
printf("请输入商品单价:");
scanf("%f", &price);
printf("请输入商品数量:");
scanf("%d", &quantity);
// 计算总价
total_price += price * quantity;
}
// 输出总价
printf("总价为:%.2f\n", total_price);
return 0;
}
```
以上程序可以循环输入商品信息,直到输入商品编号为0时结束,并计算出所有商品的总价并输出。你可以根据自己的需求进行修改和完善。
相关问题
c语言编写一个商店销售系统的程序
这是一个比较大的项目,需要设计数据库、用户界面、交互逻辑等多个方面,我可以给你提供一个简单的C语言程序示例,作为参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20]; // 商品名称
int price; // 商品单价
int count; // 商品数量
} Item;
typedef struct {
Item items[100]; // 商品列表
int count; // 商品个数
} Cart;
void show_menu() {
printf("======= 欢迎使用商店销售系统 =======\n");
printf("1. 添加商品\n");
printf("2. 查看购物车\n");
printf("3. 结算\n");
printf("4. 退出\n");
}
void add_item(Cart* cart) {
Item item;
printf("请输入商品名称:");
scanf("%s", item.name);
printf("请输入商品单价:");
scanf("%d", &item.price);
printf("请输入商品数量:");
scanf("%d", &item.count);
cart->items[cart->count++] = item;
printf("添加成功!\n");
}
void show_cart(Cart* cart) {
printf("======= 购物车清单 =======\n");
printf("商品名称\t单价\t数量\n");
for (int i = 0; i < cart->count; i++) {
Item item = cart->items[i];
printf("%s\t\t%d\t%d\n", item.name, item.price, item.count);
}
}
void checkout(Cart* cart) {
int total = 0;
for (int i = 0; i < cart->count; i++) {
Item item = cart->items[i];
total += item.price * item.count;
}
printf("您的购物车总价为:%d元\n", total);
}
int main() {
Cart cart = {0};
int choice = 0;
while (choice != 4) {
show_menu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item(&cart);
break;
case 2:
show_cart(&cart);
break;
case 3:
checkout(&cart);
break;
case 4:
printf("谢谢使用!\n");
break;
default:
printf("无效的选择,请重新输入。\n");
break;
}
}
return 0;
}
```
这个程序实现了一个简单的商店销售系统,用户可以添加商品、查看购物车和结算。你可以根据这个示例,结合具体的需求和实际情况,进行进一步的开发和优化。
用c语言编写商店销售管理系统
商店销售管理系统的开发需要使用C语言的基本语法、数据结构和文件操作等知识。以下是一个简单的商店销售管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 100
struct item {
char name[50];
int quantity;
float price;
};
struct item inventory[MAX_ITEMS];
int num_items = 0;
void add_item() {
struct item new_item;
printf("Enter name of item: ");
scanf("%s", new_item.name);
printf("Enter quantity: ");
scanf("%d", &new_item.quantity);
printf("Enter price: ");
scanf("%f", &new_item.price);
inventory[num_items] = new_item;
num_items++;
}
void delete_item() {
char name[50];
printf("Enter name of item to delete: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < num_items; i++) {
if (strcmp(inventory[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Item not found.\n");
} else {
for (int i = index; i < num_items - 1; i++) {
inventory[i] = inventory[i + 1];
}
num_items--;
printf("Item deleted.\n");
}
}
void display_inventory() {
printf("Name\tQuantity\tPrice\n");
for (int i = 0; i < num_items; i++) {
printf("%s\t%d\t\t%.2f\n", inventory[i].name, inventory[i].quantity, inventory[i].price);
}
}
void save_inventory() {
FILE *f = fopen("inventory.txt", "w");
if (f == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < num_items; i++) {
fprintf(f, "%s,%d,%.2f\n", inventory[i].name, inventory[i].quantity, inventory[i].price);
}
fclose(f);
printf("Inventory saved.\n");
}
void load_inventory() {
FILE *f = fopen("inventory.txt", "r");
if (f == NULL) {
printf("Error opening file.\n");
return;
}
char line[100];
while (fgets(line, 100, f) != NULL) {
char *token = strtok(line, ",");
strcpy(inventory[num_items].name, token);
token = strtok(NULL, ",");
inventory[num_items].quantity = atoi(token);
token = strtok(NULL, ",");
inventory[num_items].price = atof(token);
num_items++;
}
fclose(f);
printf("Inventory loaded.\n");
}
int main() {
int choice = 0;
while (choice != 6) {
printf("\n1. Add item\n");
printf("2. Delete item\n");
printf("3. Display inventory\n");
printf("4. Save inventory\n");
printf("5. Load inventory\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item();
break;
case 2:
delete_item();
break;
case 3:
display_inventory();
break;
case 4:
save_inventory();
break;
case 5:
load_inventory();
break;
case 6:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
该程序通过结构体数组存储库存信息,可以添加、删除、显示、保存和加载库存数据。在程序中,库存数据被保存在名为“inventory.txt”的文本文件中。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)