#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <windows.h>
#include<iostream>
#include<string>
#include<conio.h>
#include<fstream>
#include<vector>
#include<stdio.h>
#include<ctime>
#include<WinBase.h>
#include <iomanip>
#include<map>
#include<algorithm>
#include"myFunctions.h"
using namespace std;
#define AD_LOGIN 49
#define USER_SIGN_UP 50
#define USER_LOGIN 51
#define EXIT 52
int adLoginTimes = 0;
// int userLoginTimes = 0;
string END = "END";
void appGetXY(int* res) {
HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO pBuffer;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &pBuffer);
res[0] = pBuffer.dwCursorPosition.X;
res[1] = pBuffer.dwCursorPosition.Y;
}
void gotoxy(int x, int y) {
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
int digit(int a) {
int res = 0;
while (a) {
res++;
a /= 10;
}
return res;
}
int string2ascii(string& str) {
int res = 0;
for (int i = 0; i < str.length(); i++) {
int k = digit(int(str[i]));
res = pow(10, k) * res + int(str[i]);
}
return res;
}
// print a line of "="
void printDisplay() {
for (int i = 0; i < 10; i++) {
cout << " ";
}
for (int i = 0; i < 120; i++) {
cout << "=";
}
}
// print a line of "*"
void printDisplay2() {
for (int i = 0; i < 10; i++) {
cout << " ";
}
for (int i = 0; i < 120; i++) {
cout << "*";
}
}
int home() {
printDisplay();
cout << endl;
string str = "1. 管理员登录 2.用户注册 3.用户登录 4.退出程序";
for (int i = 0; i < 20; i++) {
cout << " ";
}
cout << str;
cout << endl;
printDisplay();
cout << endl << endl;
for (int i = 0; i < 10; i++) {
cout << " ";
}
cout << "请输入操作:";
string operation;
cin >> operation;
int res = string2ascii(operation);
return res;
}
void administratorLogin() {
if (adLoginTimes == 0) {
adLoginTimes++;
int res1[2] = { 0 };
int res2[2] = { 0 };
string info1 = "请输入管理员姓名:";
printInfo(info1);
appGetXY(res1);
string info2 = "请输入管理员密码:";
printInfo(info2);
appGetXY(res2);
string ans1, ans2;
gotoxy(res1[0], res1[1]);
cin >> ans1;
gotoxy(res2[0], res2[1]);
cin >> ans2;
if (ans1 == "admin" && ans2 == "123456") {
string info3 = "-----登录成功-----";
printInfo(info3);
}
else {
string info4 = "-----登录失败-----";
printInfo(info4);
string info6 = "是否退出登录?[y/n] ";
cout << endl;
printInfo(info6);
string ans = handleInvalidInput_yn();
if (ans == "n") {
string info5 = "请重新登录";
cout << endl;
printInfo(info5);
administratorLogin();
}
else if (ans == "y") {
Sleep(100);
system("cls");
marketSystem();
}
}
}
else {
int res1[2] = { 0 };
int res2[2] = { 0 };
string info1 = "请再次输入管理员姓名:";
printInfo(info1);
appGetXY(res1);
for (int i = 0; i < 10; i++) {
cout << " ";
}
string info2 = "请再次输入管理员密码:";
printInfo(info2);
appGetXY(res2);
string ans1, ans2;
gotoxy(res1[0], res1[1]);
cin >> ans1;
gotoxy(res2[0], res2[1]);
cin >> ans2;
if (ans1 == "admin" && ans2 == "123456") {
string info3 = "-----登录成功-----";
printInfo(info3);
}
else {
string info4 = "-----登录失败-----";
printInfo(info4);
string info6 = "是否退出登录?[y/n] ";
cout << endl;
printInfo(info6);
string ans = handleInvalidInput_yn();
if (ans == "n") {
string info5 = "请重新登录";
cout << endl;
printInfo(info5);
administratorLogin();
}
else if (ans == "y") {
Sleep(100);
system("cls");
marketSystem();
}
}
}
}
string handleInvalidInput_y() {
while (1) {
string tmpAns;
cin >> tmpAns;
if (tmpAns == "y") {
return tmpAns;
break;
}
else {
string info2 = "输入不合法,请重新输入:";
printInfo(info2);
int res3[2] = { 0 };
appGetXY(res3);
gotoxy(res3[0], res3[1]);
}
}
}
string handleInvalidInput_yn() {
while (1) {
string tmpAns;
cin >> tmpAns;
if (tmpAns == "y") {
return tmpAns;
break;
}
else if (tmpAns == "n") {
return tmpAns;
break;
}
else {
cout << endl;
for (int i = 0; i < 10; i++) {
cout << " ";
}
cout << "输入不合法,请重新输入:";
int res4[2] = { 0 };
appGetXY(res4);
gotoxy(res4[0], res4[1]);
}
}
}
string handleInvalidInput_12() {
while (1) {
string tmpAns;
cin >> tmpAns;
if (tmpAns == "1") {
return tmpAns;
break;
}
else if (tmpAns == "2") {
return tmpAns;
break;
}
else {
cout << endl;
for (int i = 0; i < 10; i++) {
cout << " ";
}
cout << "输入不合法,请输入1或2来选择一个操作:";
int res4[2] = { 0 };
appGetXY(res4);
gotoxy(res4[0], res4[1]);
}
}
}
string handleInvalidInput_123() {
while (1) {
string tmpAns;
cin >> tmpAns;
if (tmpAns == "1") {
return tmpAns;
break;
}
else if (tmpAns == "2") {
return tmpAns;
break;
}
else if (tmpAns == "3") {
return tmpAns;
break;
}
else {
cout << endl;
for (int i = 0; i < 10; i++) {
cout << " ";
}
cout << "输入不合法,请输入1或2或3来选择一个操作:";
int res4[2] = { 0 };
appGetXY(res4);
gotoxy(res4[0], res4[1]);
}
}
}
void back2Ad() {
int res2[2] = { 0 };
cout << endl;
string info1 = "按y返回管理员操作界面:";
printInfo(info1);
appGetXY(res2);
gotoxy(res2[0], res2[1]);
string ans;
ans = handleInvalidInput_y();
if (ans == "y") {
Sleep(100);
system("cls");
administrator();
}
}
void back2AdSearch() {
int res2[2] = { 0 };
cout << endl;
string info1 = "按y返回管理员搜索界面:";
printInfo(info1);
appGetXY(res2);
gotoxy(res2[0], res2[1]);
string ans;
ans = handleInvalidInput_y();
if (ans == "y") {
Sleep(100);
system("cls");
adSearch();
}
}
void back2manageUser(string uid) {
int res2[2] = { 0 };
cout << endl;
string info1 = "按y返回个人信息管理界面:";
printInfo(info1);
appGetXY(res2);
gotoxy(res2[0], res2[1]);
string ans;
ans = handleInvalidInput_y();
if (ans == "y") {
Sleep(100);
system("cls");
manageUserInformation(uid);
}
}
void back2AdSearchOrder() {
int res2[2] = { 0 };
cout << endl;
string info1 = "按y返回订单查找界面:";
printInfo(info1);
appGetXY(res2);
gotoxy(res2[0], res2[1]);
string ans;
ans = handleInvalidInput_y();
if (ans == "y") {
Sleep(100);
system("cls");
adSearchOrder();
}
}
void back2message(string uid) {
int res2[2] = { 0 };
cout << endl;
string info1 = "按y返回个人信息管理界面:";
printInfo(info1);
appGetXY(res2);
gotoxy(res2[0], res2[1]);
string ans;
ans = handleInvalidInput_y();
if (ans == "y") {
Sleep(100);
system("cls");
message(uid);
}
}
void adSearch() {
printDisplay();
cout << endl;
string str = "1.按商品名称搜索 2.按商品价格区间搜索 3.按商品描述搜索 4.返回管理员操作界面";
for (int i = 0; i < 20; i++) {
cout << " ";
}
cout << str;
cout << endl;
printDisplay();
cout << endl << endl;
string info1 = "请输入操作:";
printInfo(info1);
int res1[2] = { 0 };
appGetXY(res1);
string tmpOperation;
gotoxy(res1[0], res1[1]);
cin >> tmpOperation;
int operation = string2ascii(tmpOperation);
switch (operation) {
case 49: {
//TODO
string info = "请输入商品名称:";
printInfo(info);
int res2[2] = { 0 };
appGetXY(res2);
gotoxy(res2[0], res2[1]);
string cmdName;
cin >> cmdName;
vector<cmd> cmds, res;
readDatasCmd(cmds);
searchComplex(cmdName, cmds, res);
if (res.size() > 0) {
printCommodities(res);
back2AdSearch();
}
else {
printSearchFailed();
cout << endl;
back2AdSearch();
}
break;
}
case 50: {
string info1 = "请输入最低价格,商品价格不可以0开头(01之间的小数除外):";
string info2 = "请输入最高价格,商品价格不可以0开头(01之间的小数除外):";
int res1[2] = { 0 };
int res2[2] = { 0 };
printInfo(info1);
appGetXY(res1);
printInfo(info2);
appGetXY(res2);
string price1, price2;
gotoxy(