This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreightlib.h
105 lines (100 loc) · 2.47 KB
/
freightlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef __FREIGHTLIB_H__
#define __FREIGHTLIB_H__
#include "struct.h"
#include <stdio.h>
#include <stdlib.h>
#include "ui_terminal.h"
int load_stock_data(freight *stock_data, int *length)
{
FILE *fp;
fp = fopen("stock.dat", "rb");
if (fp == NULL)
{
fp = fopen("stock.dat", "wb");
if (fp == NULL)
{
system("cls");
SetColor(4, 0);
printf("磁盘读写错误,请检查程序是否有相应目录的读写权限!\n");
SetColor(15, 0);
system("pause");
return -1;
}
return 0;
}
while (1)
{
freight *stock_data_temp = (freight *)malloc(sizeof(freight));
if (fread(stock_data_temp, sizeof(freight), 1, fp) == 1)
{
(*length)++;
*stock_data = *stock_data_temp;
stock_data++;
}
else
{
break;
}
free(stock_data_temp);
}
fclose(fp);
return 0;
}
int save_stock_data(freight *stock_data_head, int *length)
{
FILE *fp2;
fp2 = fopen("stock.dat", "wb");
if (fp2 == NULL)
{
system("cls");
SetColor(4, 0);
printf("磁盘读写错误,请检查程序是否有相应目录的读写权限!\n");
SetColor(15, 0);
system("pause");
return -1;
}
fwrite(stock_data_head, sizeof(freight), *length, fp2);
fclose(fp2);
system("cls");
SetColor(10, 0);
printf("保存成功!\n");
SetColor(15, 0);
system("pause");
}
int is_in_stock(freight *stock, int length, unsigned long long EAN)
{
for (int i = 0; i < length; i++)
{
if (stock[i].EAN == EAN)
{
return i;
}
}
return -1;
}
void stock_msg(char *msg, freight *stock, int serch_id)
{
int length = strlen(msg);
char *tail = msg + length;
sprintf(tail, "\n该货物的登记信息如下:\n\n"
"EAN:%lld\n"
"货物名称:%s\n"
"库存量:%d\n"
"进货价格:%.2lf¥\n"
"售出价格:%.2lf¥\n"
"盈利价格:%.2lf¥\n",
stock[serch_id].EAN,
stock[serch_id].name,
stock[serch_id].stock,
stock[serch_id].purchase_price,
stock[serch_id].sale_price,
stock[serch_id].margins);
}
int silent_save_stock_data(freight *stock_data_head, int *length)
{
FILE *fp2;
fp2 = fopen("stock.dat", "wb");
fwrite(stock_data_head, sizeof(freight), *length, fp2);
fclose(fp2);
}
#endif