#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<graphics.h>
#include <assert.h>
#pragma comment(lib,"winmm.lib")
#define INITSIZE 40
typedef struct drink {
char drink[20];//饮料名字
int count;//数量
double price;//金额
int date;//日期
int size;//有效数据个数
}drink;
drink arr[INITSIZE];
int size;
void Initdrink(drink* pdrink, drink* pcount, drink* pprice, drink* pdate);
void Insertdrink(drink* array);
void DeleteBydrink(drink* array);
void ChangeByPrice(drink* array);
void SearchBydrinkRange(drink* array);
void Show(drink* array);
drink yh;
void Initdrink(drink* pyh)//初始化当前变量,对数据类型获取
{
assert(pyh != NULL);
InputBox(pyh->drink, 10, "饮料名字", "饮料名字信息录入");//输入数据 ---字符串接受
char buff[10];
memset(buff, 0, 10);
InputBox(buff, 10, "数量", "饮料数量信息录入");
pyh->count = atoi(buff);
memset(buff, 0, 10);//初始化清空buff里的数据
InputBox(buff, 10, "金额", "饮料金额信息录入");
pyh->price = atof(buff);
memset(buff, 0, 10);
InputBox(buff, 10, "日期", "饮料日期信息录入");
pyh->date = atoi(buff);
memset(buff, 0, 10);
InputBox(buff, 10, "大小", "饮料大小信息录入");
pyh->size = atoi(buff);
memset(buff, 0, 10);
}
void Insertdrink(drink* parr) {
assert(parr != NULL);//debug 断言 assert.h
drink gs;
Initdrink(&gs);
arr[size] = gs;
size++;
MessageBox(NULL, "饮料信息录入成功", "录入结果", IDOK);
}
void DeleteBydrink(drink* arr)
{
assert(arr != NULL);
char buff[10] = { 0 };
InputBox(buff, 10, "待删除的饮料数量", "删除数据");
int count = atoi(buff);
//在数组中找到元素,将数据从数组删除
int flag = 0;
for (int i = 0; i < size; i++) {
if (arr[i].count == count) {//strcmp(arr[i].name,name)==0 string.h
flag = 1;
//数据移动
for (int j = i; j < size; j++) {
arr[j] = arr[j + 1];
}
size--;
}
}
if (flag == 1)//若删除成功
MessageBox(NULL, "删除饮料成功!", "删除饮料信息", IDOK);
else
MessageBox(NULL, "不存在该饮料!", "删除饮料信息", IDOK);
}
int SearchByPrice(drink* arr) {
char buff[20] = { 0 };
InputBox(buff, 20, "待查找饮料", "通过价格查找饮料");
int price = atoi(buff);
for (int i = 0; i < size; i++) {
if (arr[i].price == price) {
MessageBox(NULL, "查找成功!", "结果展示", MB_OKCANCEL);
return i;
}
}
MessageBox(NULL, "不存在!", "结果展示", MB_OKCANCEL);
return -1;
}
void ChangeByPrice(drink* arr) {
//先查找数据input,后进行元素修改
int index = -1;
if ((index = SearchByPrice(arr)) == -1) {
MessageBox(NULL, "不存在该饮料", "修改结果", MB_OKCANCEL);//不存在该学生
return;
}
char buff[20] = { 0 };
int flag = 0;
InputBox(buff, 20, "待修改饮料属性:price,count,size,date,drink", "更新"); //提示 请输入待修改的属性 : price 库存量
if (strcmp(buff, "price") == 0 || strcmp(buff, "count") == 0 || strcmp(buff, "size") == 0 || strcmp(buff, "date") == 0 || strcmp(buff, "drink") == 0) {//比较 属性名 ==
if (strcmp(buff, "price") == 0) {
InputBox(buff, 20, "价格", "更新价格");//修改的新价格为
arr[index].price = atof(buff);
flag = 1;
}
if (strcmp(buff, "count") == 0) {
InputBox(buff, 20, "库存数量", "更新库存数量");//修改的新价格为
arr[index].count = atoi(buff);
flag = 1;
}
if (strcmp(buff, "size") == 0) {
InputBox(buff, 20, "尺寸", "更新尺寸");//修改的新价格为
arr[index].size = atoi(buff);
flag = 1;
}
if (strcmp(buff, "date") == 0) {
InputBox(buff, 20, "日期", "更新日期");//修改的新价格为
arr[index].date = atoi(buff);
flag = 1;
}
if (strcmp(buff, "drink") == 0) {
InputBox(buff, 20, "饮料名称", "更新名称");//修改的新价格为
strcpy(arr[index].drink, buff);
flag = 1;
}
}
if (flag == 0)
MessageBox(NULL, "更新失败!", "修改结果", MB_OKCANCEL);
else
MessageBox(NULL, "更新成功!", "修改结果", MB_OKCANCEL);
}
void Show(drink* arr) {
char buff[256] = { 0 };
char res[1024] = { 0 };
strcat(res, "名称 日期 库存数量 价格 大小\n");//连接
for (int i = 0; i < size; i++) {
sprintf(buff, "%s %d %d %lf %d \n", arr[i].drink, arr[i].date, arr[i].count, arr[i].price, arr[i].size);
strcat(res, buff);
memset(buff, 0, 128);
}
MessageBox(NULL, res, "结果展示", MB_OKCANCEL);
}
int main() {
initgraph(1000, 1000, SHOWCONSOLE);
IMAGE img;
loadimage(&img, "D:/10.jpg", 1000, 1000, true);
putimage(0, 0, &img);
MOUSEMSG msg;
while (1) {
msg = GetMouseMsg();//监听
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 700 && msg.x <= 845 && msg.y >= 688 && msg.y <=821) {
//printf("x:%d y:%d\n", msg.x, msg.y);
//printf("")
mciSendString("open D:/2.mp3 alias s", 0, 0, 0);
mciSendString("play s repeat", 0, 0, 0);
}
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 149 && msg.x <= 633 && msg.y >= 351 && msg.y <= 747) {
//printf("x:%d y:%d\n", msg.x, msg.y);
//printf("")
loadimage(&img, "D:/300.jpg", 1000, 900, true);
putimage(0, 0, &img);
break;
}
}
while (1) {
msg = GetMouseMsg();//监听
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 829 && msg.x <= 936 && msg.y >= 682 && msg.y <= 752) {
//printf("x:%d y:%d\n", msg.x, msg.y);
mciSendString("close s", 0, 0, 0);
}
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 590 && msg.x <= 785 && msg.y >= 400 && msg.y <= 449) {
//printf("x:%d y:%d\n", msg.x, msg.y);
loadimage(&img, "D:/10.jpg", 1000, 1000, true);
putimage(0, 0, &img);
}
//添加
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 57 && msg.x <= 329 && msg.y >= 59 && msg.y <= 143) {
//printf("x:%d y:%d\n", msg.x, msg.y);
Insertdrink(arr);
}
//删除
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 588 && msg.x <= 826 && msg.y >= 71 && msg.y <= 142) {
//printf("x:%d y:%d\n", msg.x, msg.y);
DeleteBydrink(arr);
}
//修改
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 590 && msg.x <= 785 && msg.y >= 400 && msg.y <= 449) {
//printf("x:%d y:%d\n", msg.x, msg.y);
ChangeByPrice(arr);
}
//查找
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >=132 && msg.x <= 276 && msg.y >= 244 && msg.y <= 287) {
//printf("x:%d y:%d\n", msg.x, msg.y);
SearchByPrice(arr);
}
//展示
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 620 && msg.x <= 700 && msg.y >= 232 && msg.y <= 295) {
printf("x:%d y:%d\n", msg.x, msg.y);
Show(arr);
}
//退出系统
if (msg.uMsg == WM_LBUTTONDOWN && msg.x >= 844 && msg.x <= 913 && msg.y >= 828 && msg.y <= 875) {
//printf("x:%d y:%d\n", msg.x, msg.y);
exit(0);
}
}
system("pause");
closegraph();
return 0;
}