您的位置:首页 > 数据库 > MySQL

mysql POS机的简单vs2013代码 及所遇问题解析

2015-12-06 10:35 736 查看
源码如下:

//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);

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;

}

free(buff);

return *(row + 3);

}

/*输入购买的数量*/

void Domestic::inumber(MYSQL* mysql)

{

float i = 0;

char* p = putin(mysql);

float f = strtof(p, NULL);

cout << "输入购买数量!" << endl;

cin >> i;

sum = sum + i * f;

cout << "需要缴付的金额为:" << i * f << endl;

}

/*判断是否为数字*/

bool is_number(string str)

{

if (str.c_str()[0] != 45)

{

for (int i = 0; i < str.length(); i++)

{

if (str.c_str()[i] < '0' || str.c_str()[i] > '9')

{

return false;

}

}

return true;

}

else

{

for (int i = 1; i < str.length(); i++)

{

if (str.c_str()[i] < '0' || str.c_str()[i] > '9')

{

return false;

}

}

return true;

}

}

/*********************************管理用户************************************/

/*添加普通用户*/

void Manage::add_Domestic(MYSQL* mysql)

{

char *name;

name = (char *)malloc(50);

memset(name, 0, 100);

char* key;

key = (char *)malloc(10);

memset(key, 0, 10);

char* id;

id = (char *)malloc(10);

memset(id, 0, 10);

char *buff;

buff = (char *)malloc(200);

memset(buff, 0, 100);

cout << endl << "输入ID号:";

cin >> id;

cout << endl << "输入姓名:";

cin >> name;

cout << endl << "输入密码:";

cin >> key;

strcat_s(buff, 10 + sizeof("insert into users values("), "insert into users values(");

strcat_s(buff, 20 + sizeof(buff) + sizeof(id), id);

strcat_s(buff, 30 + sizeof(buff)+sizeof(char), ",'");

strcat_s(buff, 40 + sizeof(buff) + sizeof(name), name);

strcat_s(buff, 50 + sizeof(buff)+sizeof(char), "',");

strcat_s(buff, 60 + sizeof(buff) + sizeof(key), key);

strcat_s(buff, 70 + sizeof(buff)+sizeof(")"), ")");

mysql_query(mysql, buff);//mysql写入和读取数据这块不懂

// free(name);

// free(buff);

// free(id);

// free(key);

}

/*删除普通用户*/

void Manage::del_Domestic(MYSQL* mysql)

{

char* id;

id = (char *)malloc(10);

memset(id, 0, 10);

cout << endl << "输入ID号:";

cin >> id;

char *buff;

buff = (char *)malloc(100);

memset(buff, 0, 100);

strcat_s(buff, sizeof("delete from users where ID = "), "delete from users where ID = ");

strcat_s(buff, 20 + sizeof(buff) + sizeof(id), id);

mysql_query(mysql, buff);

free(buff);

free(id);

}

/*增加货物*/

void Manage::add_Goods(MYSQL *mysql)

{

cout << endl << "商品ID:";

cin >> goods->G_ID;

cout << endl << "商品名字:";

cin >> goods->G_name;

cout << endl << "商品价格:";

cin >> goods->G_selling_price;

cout << endl << "商品数量:";

cin >> goods->G_amount;

cout << endl << "商品折扣:";

cin >> goods->discount;

cout << endl << "商品厂商:";

cin >> goods->manufacture;

char *buff;

buff = (char *)malloc(500);

memset(buff, 0, 500);

strcat_s(buff, sizeof("insert into GOODS values("), "insert into GOODS values(");

strcat_s(buff, 20 + sizeof(buff)+sizeof(goods->G_ID), goods->G_ID);

strcat_s(buff, 20 + sizeof(buff)+sizeof(char), ",'");

strcat_s(buff, 30 + sizeof(buff)+sizeof(goods->G_name), goods->G_name);

strcat_s(buff, 40 + sizeof(buff)+sizeof(char), "',");

strcat_s(buff, 40 + sizeof(buff)+sizeof(goods->G_selling_price), goods->G_selling_price);

strcat_s(buff, 50 + sizeof(buff)+sizeof(char), ",");

strcat_s(buff, 50 + sizeof(buff)+sizeof("goods->G_amount"), goods->G_amount);

strcat_s(buff, 60 + sizeof(buff)+sizeof(char), ",'");

strcat_s(buff, 60 + sizeof(buff)+sizeof("goods->manufacture"), goods->manufacture);

strcat_s(buff, 70 + sizeof(buff)+sizeof(char), "',");

strcat_s(buff, 70 +sizeof(buff)+sizeof("goods->discount"), goods->discount);

strcat_s(buff, 70 + sizeof(buff)+sizeof(")"), ")");

mysql_query(mysql, buff);

free(buff);

}

/*删除货物*/

void Manage::del_Goods(MYSQL *mysql)//到底要不要分配内存 要不要初始化

{

// Goods goods;

cout << endl << "商品ID:";

cin >> goods->G_ID;

char *buff;

buff = (char *)malloc(200);//

memset(buff, 0, 200);

strcat_s(buff, sizeof("delete from GOODS where ID = "), "delete from GOODS where ID = ");

strcat_s(buff, 20 + sizeof(buff)+sizeof(goods->G_ID), goods->G_ID);

mysql_query(mysql, buff);

free(buff);

}

/*修改物品*/

void Manage::modify_Goods(MYSQL *mysql)

{

// Goods goods;

cout << endl << "商品ID:";

cin >> goods->G_ID;

cout << endl << "商品名字:";

cin >> goods->G_name;

cout << endl << "商品价格:";

cin >> goods->G_selling_price;

cout << endl << "商品数量:";

cin >> goods->G_amount;

cout << endl << "商品厂商:";

cin >> goods->manufacture;

cout << endl << "商品折扣:";

cin >> goods->discount;

char *buff;

buff = (char *)malloc(500);

memset(buff, 0, 500);

strcat_s(buff, sizeof("delete from GOODS where ID = "), "delete from GOODS where ID = ");

strcat_s(buff, 20 + sizeof(buff)+sizeof(goods->G_ID), goods->G_ID);

mysql_query(mysql, buff);

memset(buff, 0, 500);

strcat_s(buff, sizeof("insert into GOODS values("), "insert into GOODS values(");

strcat_s(buff, 20 + sizeof(buff)+sizeof(goods->G_ID), goods->G_ID);

strcat_s(buff, 20 + sizeof(buff)+sizeof(char), ",'");

strcat_s(buff, 30 + sizeof(buff)+sizeof(goods->G_name), goods->G_name);

strcat_s(buff, 30 + sizeof(buff)+sizeof(char), "',");

strcat_s(buff, 30 + sizeof(buff)+sizeof(goods->G_selling_price), goods->G_selling_price);

strcat_s(buff, 30 + sizeof(buff)+sizeof(char), ",");

strcat_s(buff, 30 + sizeof(buff)+sizeof("goods.G_amount"), goods->G_amount);

strcat_s(buff, 30 + sizeof(buff)+sizeof(char), ",'");

strcat_s(buff, 40 + sizeof(buff)+sizeof("goods.manufacture"), goods->manufacture);

strcat_s(buff, 40 + sizeof(buff)+sizeof(char), "',");

strcat_s(buff, 40 + sizeof(buff)+sizeof("goods.discount"), goods->discount);

strcat_s(buff, 40 + sizeof(buff)+sizeof(")"), ")");

mysql_query(mysql, buff);

free(buff);

}

/*********************************ROOT用户************************************/

/*添加管理用户*/

void Root::add_Manage(MYSQL* mysql)

{

char *name;

name = (char *)malloc(50);

memset(name, 0, 100);

char* key;

key = (char *)malloc(10);

memset(key, 0, 10);

char* id;

id = (char *)malloc(10);

memset(id, 0, 10);

char *buff;

buff = (char *)malloc(200);

memset(buff, 0, 100);

cout << endl << "输入ID号:";

cin >> id;

cout << endl << "输入姓名:";

cin >> name;

cout << endl << "输入密码:";

cin >> key;

strcat_s(buff, 10 + sizeof("insert into users values("), "insert into users values(");

strcat_s(buff, 20 + sizeof(buff)+sizeof(id), id);

strcat_s(buff, 30 + sizeof(buff)+ sizeof(char), ",'");//为什么总是你不对,这一块的值到底是多少?

strcat_s(buff, 40 + sizeof(buff)+sizeof(name), name);

strcat_s(buff, 50 + sizeof(buff)+sizeof(char), "',");

strcat_s(buff, 60 + sizeof(buff)+sizeof(key), key);

strcat_s(buff, 70 + sizeof(buff)+sizeof(char), ")");

strcat_s(buff, 80 + sizeof(buff)+sizeof(char), ";");

mysql_query(mysql, buff);//mysql写入和读取数据这块不懂

// free(name);

// free(buff);

// free(id);

// free(key);

}

/*删除管理用户*/

void Root::del_Manage(MYSQL* mysql)

{

char* id;

id = (char *)malloc(10);

memset(id, 0, 10);

cout << endl << "输入ID号:";

cin >> id;

char *buff;

buff = (char *)malloc(100);

memset(buff, 0, 100);

strcat_s(buff, sizeof("delete from users where ID = "), "delete from users where ID = ");

strcat_s(buff, 20 + sizeof(buff)+sizeof(id), id);

mysql_query(mysql, buff);

free(buff);

free(id);

}

/*收入总和*/

void Root::inquiry_total(MYSQL* mysql)

{

cout <<"今日营业额为:"<< sum << endl;

}

/*收入总和*/

void Root::inquiry_user(MYSQL* mysql)

{

MYSQL_RES *res;

MYSQL_ROW row;

row = (MYSQL_ROW)malloc(sizeof(MYSQL_ROW));

memset(row, 0, sizeof(MYSQL_ROW));

mysql_query(mysql, "select * from users");

res = mysql_store_result(mysql);

// row = mysql_fetch_row(res);

//下面的这个while语句 不知道能偶行得通

while (row = mysql_fetch_row(res))//一次读一行

{

for (int t = 0; t < (int)mysql_num_fields(res); t++)

{

printf("%16s ", row[t]);

}

printf("\n");

}

}

//fuction.cpp文件

#ifndef _FUCTION_H

#define _FUCTION_H

#include "fuction.h"

//root 用户的操作部分

int quit = 0;//用来 确定是否退出当前循环,0表示不退出,1表示退出

Root root;

Manage manage;

Domestic domestic;

void func(int u, MYSQL* mysql)

{

switch (u)

{

case 1:

ROOT(mysql);

break;

case 2:

MANAGE(mysql);

break;

case 3:

DOMESTIC(mysql);

break;

}

}

void ROOT(MYSQL* mysql)

{

int choice = 0;

while (!quit)

{

cout << "请选择如下所需的操作:" << endl;

cout << "0、退出root用户操作。" << endl;

cout << "1、添加管理用户。" << endl;

cout << "2、删除管理用户。" << endl;

cout << "3、收额查询。" << endl;

cout << "4、用户查询。" << endl;

cin >> choice;

switch (choice)

{

case 1:root.add_Manage(mysql); break;

case 2:root.del_Manage(mysql); break;

case 3:root.inquiry_total(mysql); break;

case 4:root.inquiry_user(mysql); break;

case 0:quit = 1; break;

default:cout << "输入有误,请重新输入!" << endl; break;

}

}

quit = 0;

}

//manage用户的操作部分

void MANAGE(MYSQL* mysql)

{

int choice = 0;

while (!quit)

{

cout << "请选择如下所需的操作:" << endl;

cout << "0、退出manage用户操作。" << endl;

cout << "1、添加普通用户。" << endl;

cout << "2、删除普通用户。" << endl;

cout << "3、添加商品。" << endl;

cout << "4、删除商品。" << endl;

cout << "5、修改商品。" << endl;

cin >> choice;

switch (choice)

{

case 1:manage.add_Domestic(mysql); break;

case 2:manage.del_Domestic(mysql); break;

case 3:manage.add_Goods(mysql); break;

case 4:manage.del_Goods(mysql); break;

case 5:manage.modify_Goods(mysql); break;

case 0:quit = 1; break;

default:cout << "输入有误,请重新输入!" << endl; break;

}

}

quit = 0;

}

//domestic用户的操作部分

void DOMESTIC(MYSQL* mysql)

{

int choice = 0;

while (!quit)

{

cout << "请选择如下所需的操作:" << endl;

cout << "0、退出domestic用户操作。" << endl;

cout << "1、查找商品。" << endl;

cout << "2、购买商品。" << endl;

cin >> choice;

switch (choice)

{

case 1:domestic.putin(mysql); break;//完成查找功能

case 2:domestic.inumber(mysql); break;//完成购买功能

case 0:quit = 1; break;

default:cout << "输入有误,请重新输入!" << endl; break;

}

}

quit = 0;

}

#endif

//源.cpp文件

#include "winsock.h"

#include "fuction.h"

using namespace std;

#define MAX 512

MYSQL * mysql;

char* Code;//用户密码

char* Name;

int whitch = 0;

//创建数据库,用户表、商品表

void createdb()

{

mysql = (MYSQL *)malloc(sizeof(MYSQL));

mysql_init(mysql);

mysql_real_connect(mysql, "localhost", "root", "666666", 0, 3306, NULL, 0);

mysql_query(mysql, "create database caigaojian");

mysql_select_db(mysql, "caigaojian");

mysql_query(mysql, "create table GOODS(ID int(4) not null primary key auto_increment,Name varchar(20) not null,Amount int,Selling_price float,Manufacture char(30),Discount float)");

mysql_query(mysql, "create table users(ID int(4) not null primary key auto_increment,Name varchar(20) not null,code int)");

mysql_query(mysql, "insert into users value(1,'root',1)");

}

//比较存储的密码、输入的密码是否相等

int compare(char *codec, char *codes)

{

return strcmp(codec, codes);

}

//匹配用户是哪一个

int marry()

{

int result = 0;

MYSQL_RES *res;

MYSQL_ROW row;

row = (MYSQL_ROW)malloc(sizeof(MYSQL_ROW));

memset(row, 0, sizeof(MYSQL_ROW));

res = mysql_store_result(mysql);

row = mysql_fetch_row(res);

/* while (row = mysql_fetch_row(res))

{

for (int t = 0; t < mysql_num_fields(res); t++)

{

printf("!%s!", row[t]);

}

printf(".............\n");

}

cin >> Name;

*/

if (row == 0)

cout << "用户名错误" << endl;

else if (compare(*row, Code) == 0)

{

cout <<"当前用户是:"<< *(row+1) << endl;

result = 1;

}

else

cout << "密码错误!" << endl;

return result;

}

//根据编号取回密码,和用户名

void get_code(char *ID)

{

char* buffer;//用来 传递 数据库 指令的

buffer = (char *)malloc(MAX);

memset(buffer, 0, MAX);

strcat_s(buffer, sizeof("select code,name from users where ID = "), "select code,name from users where ID = ");//指令

strcat_s(buffer, MAX, ID);

mysql_query(mysql, buffer);

// free(buffer);

}

//用户输入

char* putin()

{

char* Id;//获取输入的用户编号

Id = (char *)malloc(100);

Code = (char *)malloc(100);

Name = (char *)malloc(100);

memset(Id, 0, 100);

memset(Code, 0, 100);

memset(Name, 0, 100);

cout << "输入用户编号:" << endl;

cin >> Id;

cout << "输入用户密码:" << endl;

cin >> Code;

return Id;

}

void func(int u, MYSQL* mysql);

int main()

{

createdb();//创建数据库 和 表

char *c;

int result = 1;

char s;

while (result)

{

c = putin();// 输入编号 和密码

int i = (int)(c[0] - '0');//字符‘3’的ASCII为51

get_code(c);//根据编号取回密码

if (marry())//匹配用户

func(i, mysql);

cout << "是否退出?(Y/N)" << endl;

cin >> s;

if (s == 'Y')

result = 0;

}

mysql_close(mysql);

// system("pause");

return 0;

}

以上为全部代码,能够执行所有功能。代码很简单,初学mysql者用。

注:使用时,将 源.cpp中的 mysql_real_connect(mysql, "localhost", "root", "666666", 0, 3306, NULL, 0);中“666666”改为你自己的mysql密码即可。

我所遇问题的归纳。

1、在运行后,按F5会出现上一次从键盘输入的内容。如linux中的history的(↑)。

2、strcat_s(x,x,x);

这个函数在使用时一直出现中断。因为我用了很多这个函数,所有后一个我直接加了很大的数据长度,代码中很容易找到。

3、在row = mysql_fetch_row(res);中

通过vs单步执行时,分析row、res其结构数据时,有很多不明白。

4、在每次定义一个char *buff;时,分配内存、初始化、初始化的大小,得注意。否则易出现中断。

5、代码中出现了一次goods->discount处的中断,这个值只是用来接收外部输入的,即键盘输入。

问题在于我分配内存并memset后,再给他赋了一个值‘1’。这就是原因,至于为什么,还未研究。

这博客第一是给我自己记录用的,然后才是供大家参考,所以我会记录一些我自己要解决的问题,自己不足的地方。读者可以略过!!

我自己欠缺的地方

1. goods 到底是用结构体还是用类?

2.类的构造函数要不要自己定义、还是直接用默认的?

3.构造函数怎么写,写几个,内容写什么?

4.析构函数怎么写,些什么?free内存分配到底是在析构中写,还是在变量用完之后?

5.在写了多个头文件后,反复包含会出现重复定义。那怎么写、规范是什么?

此代码时间 2015.12.2 开始-- 2015.12.5完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: