源码如下:
//user.h头文件
#include "windows.h"
#include "iostream"
#include "string.h"
#include "mysql.h"
using namespace std;
//货物的属性
struct Goods{//用结构体 还是用类啊??
// Goods();
// ~Goods();
char* G_ID;
char* G_name;
char* G_amount;
char* G_selling_price;
char* manufacture;
char* discount;
};
/*普通用户的类*/
class Domestic{
public:
Domestic();
// ~Domestic();
char* putin(MYSQL*);//输入“ID”或“名字”,来返回一个字符串,
char* find(MYSQL*,int);//根据putin()的返回值 来找商品并显示
void inumber(MYSQL*);//输入数量,并打印总额
};
/*管理用户的类*/
class Manage :public Domestic{
public:
void add_Domestic(MYSQL*);
void del_Domestic(MYSQL*);
void add_Goods(MYSQL *);
void del_Goods(MYSQL *);
void modify_Goods(MYSQL *);
// void inquiry_G_buying_price(char *);//进价就不弄了
// void modify_discount(float);
protected:
// float G_buying_price;
};
class Root :public Manage{
public:
void add_Manage(MYSQL*);
void del_Manage(MYSQL*);
void inquiry_total(MYSQL*);
void inquiry_user(MYSQL*);
};
bool is_number(string str);
//fuction.h头文件
#include "user.h"
void ROOT(MYSQL*);
void MANAGE(MYSQL*);
void DOMESTIC(MYSQL*);
//user.cpp代码
#include "user.h"
Goods *goods;
float sum = 0;
/********************************普通用户**********************************/
/*初始化*/
Domestic::Domestic()//到底需要不??
{
goods = (Goods *)malloc(sizeof(Goods));
goods->G_ID = (char *)malloc(20);
goods->G_name = (char *)malloc(100);
goods->G_amount = (char *)malloc(20);
goods->discount = (char *)malloc(20);
goods->G_selling_price = (char *)malloc(20);
goods->manufacture = (char *)malloc(100);
memset(goods->G_ID, 0, 20);
memset(goods->G_name, 0, 100);
memset(goods->G_amount, 0, 20);
memset(goods->discount, 0, 20);
memset(goods->G_selling_price, 0, 20);
memset(goods->manufacture, 0, 100);
// goods->discount = "1";
}
/*析构函数*//*析构函数这块会有中断,有错误
Domestic::~Domestic()
{
free(goods->manufacture);
free(goods->G_selling_price);
free(goods->discount);
free(goods->G_amount);
free(goods->G_name);
free(goods->G_ID);
free(goods);
}*/
/*输入查找的ID或商品名*/
char * Domestic::putin(MYSQL* mysql)
{
char* str;
str = (char *)malloc(5);
memset(str, 0, 5);
int j = 0;
int result = 0;
cout << "输入商品信息:\n";
cin >> str;
if (is_number(str))
{
goods->G_ID = str;
result = 1;
// return 1;//1 表示输入数据为 ID
}
else
{
goods->G_name = str;
// return 0;//0表示输入为 名字
}
return find(mysql, result);
}
/*查找对应的商品并打印其信息*/
char * Domestic::find(MYSQL* mysql,int f)//f表示查找输入的是ID 还是名字
{
MYSQL_RES *res;
MYSQL_ROW row;
row = (MYSQL_ROW)malloc(sizeof(MYSQL_ROW));
memset(row, 0, sizeof(MYSQL_ROW));
char *buff;
buff = (char *)malloc(500);
memset(buff, 0, 500);
strcat_s(buff, sizeof("select G_ID,G_name,G_amount,G_selling_price,manufacture,discount from Goods where "), "select ID,Name,Amount,Selling_price,Manufacture,Discount from Goods where ");
if (f == 1)
{
strcat_s(buff, 100 + sizeof(buff) + sizeof("G_ID = "), "ID = ");//中间其实是合并后的大小
strcat_s(buff, 100 + sizeof(buff)+sizeof(goods->G_ID), goods->G_ID);
mysql_query(mysql, buff);
res = mysql_store_result(mysql);
row = mysql_fetch_row(res);
if (row == 0)
cout << "没有此商品!" << endl;
else
cout << "货物编号:" << *row << endl << "货物名称:" << *(row + 1) << endl << "货物存货:" << *(row + 2) << endl << "货物单价:" << *(row + 3) << endl << "货物厂家:" << *(row + 4) << endl << "货物折扣:" << *(row + 5) << endl;
}
if (f == 0)
{
strcat_s(buff, sizeof("G_name = Goods->G_name"), "G_name = Goods->G_name");
mysql_query(mysql, buff);