C++控制台代码实现一定的界面效果Ⅳ

以下为主要的头文件代码,只需要在头文件中添加即可。

使用此头文件开发的实例可以参考:

C++控制台模拟购物程序
C++控制台界面扫雷程序 2021/7/7 版本

此头文件的更老版本可以参考: ICUF_HEAD_05.h.

此头文件的更新版本可以参考: ICUF_HEAD_07.h.

开发环境:Visual Studio 2019


ICUF_HEAD_06.h

//ICUF_HEAD_06.H

#ifndef ICUF_HEAD_06
#define ICUF_HEAD_06
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <iomanip>
using namespace std;
ofstream fout;
ifstream fin;
const char* HeadFile = "HeadFile.txt";
inline void Col(int ColN)//设置自定义行的背景颜色
{
	if (ColN == 0)cout << "\033[0m";//清除颜色
	else
	{
		cout << "\033[4;1;7m";
		if (ColN == 1)cout << "\033[31m";//背景红色
		else if (ColN == 2)cout << "\033[32m";//背景绿色
		else if (ColN == 3)cout << "\033[33m";//背景黄色
		else if (ColN == 4)cout << "\033[34m";//背景蓝色
		else if (ColN == 5)cout << "\033[35m";//背景紫色
		else if (ColN == 6)cout << "\033[36m";//背景淡蓝
		else if (ColN == 7)cout << "\033[37m";//背景白色
		else if (ColN == 8)Col((rand() % 6) + 1);//随机颜色(1-7)
		else if (ColN == 11)cout << "\033[41m";//字体红色
		else if (ColN == 12)cout << "\033[42m";//字体绿色
		else if (ColN == 13)cout << "\033[43m";//字体黄色
		else if (ColN == 14)cout << "\033[44m";//字体蓝色
		else if (ColN == 15)cout << "\033[45m";//字体紫色
		else if (ColN == 16)cout << "\033[46m";//字体淡蓝
		else if (ColN == 17)cout << "\033[47m";//字体白色
		else if (ColN == 18)Col((rand() % 6) + 11);//随机颜色(11-17)
	}
}
class FunctionList
{
public:int Page{ 1 };
public:int Times{};//用于计算某一函数的执行次数,同时用于限定某些代码只执行一次
public:int TempTimes{};
public:int FunctionNumbers = 0;//表示类中含有的字符串数量
public:virtual void SetCol(int ColS, int ColU, int ColD = 7)
{
	this->SelectedCol = ColS;
	this->UnSelectedCol = ColU;
	this->DefaultCol = ColD;
}
public:void RecoverTimes()//复原被转移的Times值
{
	this->Times += ++this->TempTimes;
	this->TempTimes = 0;
}
public:void ResetTimes()//将Times的值转移到TempTimes
{
	this->TempTimes += ++this->Times;
	this->Times = 0;
}
public:void InitList(int Start = 0)//列表初始化函数,默认初始化第一个选项为选定状态
{
	for (int i = 0; i < FunctionNumbers; i++)
	{
		ListStatus[i] = 0;
	}
	for (int i = FunctionNumbers; i < 50; i++)
	{
		ListStatus[i] = -1;
	}
	ListStatus[Start] = 1;
}
public:virtual void DisplayList(int LStart = 0, int LEnd = 9, int Switch = 0)//显示列表 ICUF_H_06中添加两个默认参数,可用于显示指定行
{
	int Start = (Page - 1) * LimitListEachPage;
	int End = Start + LimitListEachPage;
	int UnSeCol = UnSelectedCol;
	if (Switch == 1)//Switch值为1时 显示此页面的各种基本信息
		cout << "FunId: " << CheckCurrent() << "  Limit: " << LimitListEachPage << "  Page: " << Page << "  Start: " << Start << "  End: " << End << endl << endl;
	if (Switch == 2)//Switch值为2时 输出子页面的列表 且使用子页面的颜色
		UnSeCol = TempCol;
	for (int i = Start + LStart; i < End + (LEnd - LimitListEachPage); i++)
	{
		if (ListStatus[i] != -1)
		{
			if (ListStatus[i] == 1)
				Col(SelectedCol);
			else
				Col(UnSeCol);
			cout << setw(23) << List[i] << setw(16) << " " << endl;
			if (Switch != 2)
				cout << endl;
			Col(0);
		}
	}
	if (Switch == 2)
		cout << endl;
}
public:int InsertAction()//输入控制
{
	char Key;
	int Temp;
	Key = _getch();
	if (Key == 'c' || Key == 'C')//如果输入了大小写的C则返回上一级
		return 3;
	if (FunctionNumbers == 0)
		return 0;
	if ((int)(Key - 48) > 0 && (int)(Key - 48) <= 9)//判断是否是从零到九的数字
	{
		if ((int)(Key - 48) > FunctionNumbers)//如果是,且小于等于选项总数则直接指定这个选项
			InitList(FunctionNumbers - 1);
		else
			InitList((int)(Key - 48) - 1);//如果超出了最大值,则指向最大值
		return 4;
	}
	else if (Key == 'w' || Key == 'W' || Key == 72)//如果输入了大小写的W或者上箭头,则执行MoveUp函数
	{
		MoveUp();
		return 0;
	}
	else if (Key == 's' || Key == 'S' || Key == 80)//如果输入了大小写的S或者下箭头,则执行MoveDown函数
	{
		MoveDown();
		return 0;
	}
	else if (Key == 'a' || Key == 'A' || Key == 75)//如果输入了大小写的A或者左箭头,则执行向上翻页函数
	{
		if (Page == GetPages() && GetPages() == 1)
		{
			Temp = CheckCurrent();
			ListStatus[Temp] = 0;
			ListStatus[0] = 1;
		}
		else if (Page != 1)
		{
			Page--;
			Temp = CheckCurrent();
			ListStatus[Temp] = 0;
			ListStatus[Temp - LimitListEachPage] = 1;
		}
		else
		{
			Page = GetPages();
			Temp = CheckCurrent();
			ListStatus[Temp] = 0;
			ListStatus[(Page - 1) * LimitListEachPage] = 1;
		}
		return 0;
	}
	else if (Key == 'd' || Key == 'D' || Key == 77)//如果输入了大小写的D或者右箭头,则执行向下翻页函数
	{
		if (Page == GetPages() && GetPages() == 1)
		{
			Temp = CheckCurrent();
			ListStatus[Temp] = 0;
			ListStatus[FunctionNumbers - 1] = 1;
		}
		else if (Page != GetPages())
		{
			Page++;
			Temp = CheckCurrent();
			ListStatus[Temp] = 0;
			if (Temp + LimitListEachPage >= FunctionNumbers - 1)
				ListStatus[FunctionNumbers - 1] = 1;
			else
				ListStatus[Temp + LimitListEachPage] = 1;
		}
		else
		{
			Page = 1;
			Temp = CheckCurrent();
			ListStatus[Temp] = 0;
			ListStatus[Temp % LimitListEachPage] = 1;
		}
		return 0;
	}
	else if (Key == 'z' || Key == 'Z')//保留原功能的情况下,预留两个用户自定义返回值
	{
		return 1;
	}
	else if (Key == 'x' || Key == 'X')
	{
		return 2;
	}
	else if (Key == '\r')//回车确认
		return 4;
	return 0;
}
public:void MoveUp()//选项上移
{
	int i = CheckCurrent();//找到当前指向的选项
	if (i == 0 && FunctionNumbers == 1)//如果只有一个选项则不变
		ListStatus[i] = 1;
	else if (i == 0 && FunctionNumbers != 1)//如果指向第一个且不止包含一个选项,则改为指向最下方的选项
	{
		ListStatus[i] = 0;
		Page = GetPages();
		ListStatus[FunctionNumbers - 1] = 1;
	}
	else if ((i + 1) % LimitListEachPage == 1)//如果指向某一页面的第一项则翻页,并指向前一页的最后一项
	{
		if (Page != 1)
		{
			Page--;
			ListStatus[i] = 0;
			ListStatus[i - 1] = 1;
		}
		else
		{
			Page = GetPages();
			ListStatus[i] = 0;
			ListStatus[FunctionNumbers - 1] = 1;
		}
	}
	else//正常情况
	{
		ListStatus[i] = 0;
		ListStatus[i - 1] = 1;
	}
}
public:void MoveDown()//选项下移
{
	int i = CheckCurrent();//找到当前指向的选项
	if (i == 0 && FunctionNumbers == 1)//如果只有一个选项则不变
		ListStatus[i] = 1;
	else if (i != 0 && i == FunctionNumbers - 1)//如果指向最后一个且不止包含一个选项,则改为指向最上方的选项
	{
		ListStatus[FunctionNumbers - 1] = 0;
		Page = 1;
		ListStatus[0] = 1;
	}
	else if ((i + 1) % LimitListEachPage == 0)//如果指向某一页面的最后一项则翻页,并指向下一页的第一项
	{
		if (Page != GetPages())
		{
			Page++;
			ListStatus[i] = 0;
			ListStatus[i + 1] = 1;
		}
		else
		{
			Page = 1;
			ListStatus[i] = 0;
			ListStatus[0] = 1;
		}
	}
	else//正常情况
	{
		ListStatus[i] = 0;
		ListStatus[i + 1] = 1;
	}
}
public:int CheckCurrent()//找到当前指向的选项  当找不到时返回-1
{
	for (int j = 0; j < FunctionNumbers; j++)
	{
		if (ListStatus[j] == 1)
			return j;
	}
	return -1;
}
public:void Append(string FunctionName, int Times = 1)//List数组添加String成员
{
	if (Times != 0)
		return;
	List[FunctionNumbers] = FunctionName;
	FunctionNumbers += 1;//成员计数器
}
public:void SetMaxList(int List)
{
	LimitListEachPage = List;
}
public:void RemoveAll()//移除所有成员(在04.h版本中为重要功能,05.h中被Times计数器取代)
{
	for (int i = 0; i < 10; i++)
	{
		List[i] = {};
		FunctionNumbers = 0;
	}
}
public:int GetPages()//获取总页面数
{
	if (FunctionNumbers % LimitListEachPage != 0)
		Pages = FunctionNumbers / LimitListEachPage + 1;
	else
		Pages = FunctionNumbers / LimitListEachPage;
	return Pages;
}
public:int GetLimitList()
{
	return LimitListEachPage;
}
private:int Pages{ 1 };
private:int LimitListEachPage{ 9 };
private:string List[50]{};//存放选项名称
private:int ListStatus[50]{ -1 };//存放选项状态
private:int TempCol{ 6 };
private:int SelectedCol{ 7 };
private:int UnSelectedCol{ 1 };
public:int DefaultCol{ 1 };
};
inline void Free(FunctionList* Fun)//释放指针
{
	delete Fun;
	Fun = NULL;
}
struct ICUF_Function//函数指针
{
	FunctionList Fun_Name;
	void (*Fun)();
};
#endif

更新内容

Line 14~37

内联函数Col增加了字体颜色设置的功能。

inline void Col(int ColN)//设置自定义行的背景颜色
{
	if (ColN == 0)cout << "\033[0m";//清除颜色
	else
	{
		cout << "\033[4;1;7m";
		if (ColN == 1)cout << "\033[31m";//背景红色
		else if (ColN == 2)cout << "\033[32m";//背景绿色
		else if (ColN == 3)cout << "\033[33m";//背景黄色
		else if (ColN == 4)cout << "\033[34m";//背景蓝色
		else if (ColN == 5)cout << "\033[35m";//背景紫色
		else if (ColN == 6)cout << "\033[36m";//背景淡蓝
		else if (ColN == 7)cout << "\033[37m";//背景白色
		else if (ColN == 8)Col((rand() % 6) + 1);//随机颜色(1-7)
		else if (ColN == 11)cout << "\033[41m";//字体红色
		else if (ColN == 12)cout << "\033[42m";//字体绿色
		else if (ColN == 13)cout << "\033[43m";//字体黄色
		else if (ColN == 14)cout << "\033[44m";//字体蓝色
		else if (ColN == 15)cout << "\033[45m";//字体紫色
		else if (ColN == 16)cout << "\033[46m";//字体淡蓝
		else if (ColN == 17)cout << "\033[47m";//字体白色
		else if (ColN == 18)Col((rand() % 6) + 11);//随机颜色(11-17)
	}
}

Line 44~49

增加对选项不同状态下的颜色设置。

public:virtual void SetCol(int ColS, int ColU, int ColD = 7)
{
	this->SelectedCol = ColS;
	this->UnSelectedCol = ColU;
	this->DefaultCol = ColD;
}

Line 72~97

现在DisplayList函数有三个int类型的接受值,其中LStart代表从当前页面列表的第LStart+1个开始输出,LStart从0开始计数且默认值为0。LEnd代表从当前页面列表的第Lend个时结束输出,LEnd默认值为9。Switch目前接收1、2时有效,Switch值为1时会在输出的列表上方输出此页面的各种信息。Switch值为2时,输出模式改为输出子列表的模式(修改颜色、换行)。此功能还有改进的空间。

public:virtual void DisplayList(int LStart = 0, int LEnd = 9, int Switch = 0)//显示列表 ICUF_H_06中添加两个默认参数,可用于显示指定行
{
	int Start = (Page - 1) * LimitListEachPage;
	int End = Start + LimitListEachPage;
	int UnSeCol = UnSelectedCol;
	if (Switch == 1)//Switch值为1时 显示此页面的各种基本信息
		cout << "FunId: " << CheckCurrent() << "  Limit: " << LimitListEachPage << "  Page: " << Page << "  Start: " << Start << "  End: " << End << endl << endl;
	if (Switch == 2)//Switch值为2时 输出子页面的列表 且使用子页面的颜色
		UnSeCol = TempCol;
	for (int i = Start + LStart; i < End + (LEnd - LimitListEachPage); i++)
	{
		if (ListStatus[i] != -1)
		{
			if (ListStatus[i] == 1)
				Col(SelectedCol);
			else
				Col(UnSeCol);
			cout << setw(23) << List[i] << setw(16) << " " << endl;
			if (Switch != 2)
				cout << endl;
			Col(0);
		}
	}
	if (Switch == 2)
		cout << endl;
}

Line 301~305

如果要通过头文件来输出子列表的话,在保证代码尽量简短的情况下可能会大量用到指针,所以临时添加了这个方法。

inline void Free(FunctionList* Fun)//释放指针
{
	delete Fun;
	Fun = NULL;
}

Line 306~310

通过此结构可以非常简单的制作一个菜单管理器(暂时没有用到)。

struct ICUF_Function//函数指针
{
	FunctionList Fun_Name;
	void (*Fun)();
};

实例

ICUF2 0.00.01.61+

// ICUF 2.0 - 0.00.01.61+  2021/6/24 19:05

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include "ICUF_HEAD_06.h"
using namespace std;
const char* SDL = "SkinData_Local.txt";
const char* Deals = "ICUF_Deal_Local.txt";
void BuyCargo(int Id = 0);
void BuyingCargo(int Id);
void ChangeData();
void SearchDeal();
void SearchDealShow();
void SearchDealById();
void Init();
FunctionList MainPage, BuyPage, BuyingPage, ChangeDPage, SearchDPage, SetPage;
ICUF_Function ChangeDataF = { ChangeDPage,ChangeData };
ICUF_Function SearchDealF = { SearchDPage,SearchDeal };
struct ICUF_SkinData
{
	int ICUF_Id{ 0 };
	string ICUF_Name{};
	string ICUF_Float{};
	double ICUF_Price{};
};
ICUF_SkinData ISD[30];
class ICUF_Skin
{
public: FunctionList SetPage2, SetPage4;
public: void SaveSkinData()
{
	fout.open(SDL);
	for (int i = 0; i < 30; i++)
	{
		if (ISD[i].ICUF_Id == 0)
		{
			LengthOfData = i;
			break;
		}
		fout << ISD[i].ICUF_Id << " " << ISD[i].ICUF_Name << " " << ISD[i].ICUF_Float << " " << ISD[i].ICUF_Price << endl;
	}
	fout.close();
}
public: void GetSkinData()
{
	fin.open(SDL);
	for (int i = 0; i < 30; i++)
	{
		fin >> ISD[i].ICUF_Id >> ISD[i].ICUF_Name >> ISD[i].ICUF_Float >> ISD[i].ICUF_Price;
	}
	fin.close();
}
public:int GetLength()
{
	return LengthOfData;
}
public: void RemoveData()
{
	remove(SDL);
	remove(Deals);
}
private:int LengthOfData{};
};
ICUF_Skin skin;
int main()
{
	Init();
	MainPage.Append("购买商品", MainPage.Times);
	MainPage.Append("查询订单", MainPage.Times);
	MainPage.Append("系统管理", MainPage.Times);
	MainPage.Append("退出系统", MainPage.Times++);
	FunctionList* F = &MainPage;
	FunctionList* T = NULL;
	MainPage.InitList();
	int Temp = MainPage.FunctionNumbers;
	while (1)
	{
		system("cls");
		Col(MainPage.DefaultCol); cout << "\n\n\n\n                         欢迎来到ICUF购物平台  WiChG_Trade             \n\n"; Col(0);
		MainPage.DisplayList(0, Temp, 1);
		if (T != NULL)
		{
			T->DisplayList(0, 9, 2);
		}
		MainPage.DisplayList(Temp, MainPage.FunctionNumbers);
		int i = F->InsertAction();
		if (i == 3)
		{
			T = NULL;
			F = &MainPage;
		}
		else if (i == 4)
		{
			int i = F->CheckCurrent();
			if (i == 0 && F == &MainPage)
			{
				skin.GetSkinData();//读取本地文件数据
				skin.SaveSkinData();
				if (BuyPage.TempTimes != 0)
					BuyPage.RemoveAll();
				for (int i = 0; i < skin.GetLength(); i++)//使用头文件方法给列表添加物品
					BuyPage.Append(ISD[i].ICUF_Name + "  (" + ISD[i].ICUF_Float + ")", BuyPage.Times);
				BuyPage.Times++;
				BuyPage.RecoverTimes();
				BuyPage.InitList();
				Temp = i + 1;
				T = &BuyPage;
				F = &BuyPage;
			}
			else if (i == 1 && F == &MainPage)
				SearchDeal();
			else if (i == 2 && F == &MainPage)
				ChangeData();
			else if (i == 3 && F == &MainPage)
				exit(1);
			else if (F == &BuyPage)
			{
				BuyingCargo(i);
			}
		}
	}
	delete F;
}
void BuyCargo(int Id)//购买商品主页面
{
	skin.GetSkinData();//读取本地文件数据
	skin.SaveSkinData();
	if (BuyPage.TempTimes != 0)
		BuyPage.RemoveAll();
	for (int i = 0; i < skin.GetLength(); i++)//使用头文件方法给列表添加物品
		BuyPage.Append(ISD[i].ICUF_Name + "  (" + ISD[i].ICUF_Float + ")", BuyPage.Times);
	BuyPage.Times++;
	BuyPage.RecoverTimes();
	BuyPage.InitList(Id);
	while (1)
	{
		system("cls");
		Col(BuyPage.DefaultCol); cout << "\n\n\n\n                         欢迎来到ICUF购物平台  WiChG_Trade             \n\n"; Col(0);
		BuyPage.DisplayList();
		int i = BuyPage.InsertAction();
		if (i == 3)
			main();
		else if (i == 4)
		{
			int i = BuyPage.CheckCurrent();
			BuyingCargo(i);
		}
	}
}
void BuyingCargo(int Id)
{
	BuyingPage.Append("继续", BuyingPage.Times);
	BuyingPage.Append("返回", BuyingPage.Times++);
	BuyingPage.InitList();
	while (1)
	{
		system("cls");
		Col(8); cout << " ICUF_Id (int) :" << ISD[Id].ICUF_Id << endl; Col(0);
		Col(8); cout << " ICUF_Name (string) :" << ISD[Id].ICUF_Name << endl; Col(0);
		Col(8); cout << " ICUF_Float (string) :" << ISD[Id].ICUF_Float << endl; Col(0);
		Col(8); cout << " ICUF_Price (double) :" << ISD[Id].ICUF_Price << endl; Col(0);
		Col(8); cout << "是否继续购买?" << endl << endl; Col(0);
		BuyingPage.DisplayList();
		int i = BuyingPage.InsertAction();
		if (i == 3)
			main();
		else if (i == 4)
		{
			int i = BuyingPage.CheckCurrent();
			if (i == 1)
				main();
			else
			{
				cout << "请输入购买商品的数量(" << ISD[Id].ICUF_Name << ") :";
				int Number;
				cin >> Number;
				srand(time(NULL));
				int List = ((rand() % 9) + 1) * 100000 + ((rand() % 9) + 1) * 10000 + ((rand() % 9) + 1) * 1000 + ((rand() % 9) + 1) * 100 + ((rand() % 9) + 1) * 10 + ((rand() % 9) + 1);
				fout.close();
				fout.open(Deals, ios::app);
				fout << List << " " << Id << " " << ISD[Id].ICUF_Id << " " << Number << " " << ISD[Id].ICUF_Price * Number << endl;
				fout.close();
				cout << "购买成功  你的购买订单号为 " << List << endl;
				cout << "List ID : " << List << "  Cargo ID : " << Id << "  ICUF ID : " << ISD[Id].ICUF_Id << "  Number : " << Number << "  Price : " << ISD[Id].ICUF_Price * Number << endl;
				system("pause");
				main();
			}
		}
	}
}
void ChangeData()
{
	int LengthOfData{};
	ChangeDPage.Append("1.更改数据", ChangeDPage.Times);
	ChangeDPage.Append("2.添加数据", ChangeDPage.Times);
	ChangeDPage.Append("3.默认数据", ChangeDPage.Times);
	ChangeDPage.Append("4.清理数据", ChangeDPage.Times++);
	ChangeDPage.InitList();
	while (1)
	{
		system("cls");
		Col(ChangeDPage.DefaultCol); cout << "\n\n\n\n                         ICUF购物平台-修改数据功能  WiChG_Trade             \n\n"; Col(0);
		ChangeDPage.DisplayList();
		int i = ChangeDPage.InsertAction();
		if (i == 3)
			main();
		else if (i == 4)
		{
			int i = ChangeDPage.CheckCurrent();
			skin.GetSkinData();
			if (i == 0)//更改单一数据功能
			{
				SetPage.RemoveAll();
				SetPage.ResetTimes();
				for (int i = 0; i < 30; i++)
				{
					if (ISD[i].ICUF_Id == 0)
					{
						LengthOfData = i;
						break;
					}
					cout << ISD[i].ICUF_Id << " " << ISD[i].ICUF_Name << " " << ISD[i].ICUF_Float << " " << ISD[i].ICUF_Price << endl;
				}
				int ISD_Id, ISD_Fun;
				Col(1); cout << "请输入要修改的物品的ICUF_Id :  ";
				cin >> ISD_Id; Col(0);
				SetPage.Append("1.ISD[i].ICUF_Id    (序号)", SetPage.Times);
				SetPage.Append("2.ISD[i].ICUF_Name  (名称)", SetPage.Times);
				SetPage.Append("3.ISD[i].ICUF_Float (磨损)", SetPage.Times);
				SetPage.Append("4.ISD[i].ICUF_Price (价格)", SetPage.Times++);
				SetPage.InitList();
				while (1)
				{
					system("cls");
					Col(SetPage.DefaultCol); cout << "\n\n\n\n                         更改单一数据  WiChG_Trade             \n\n"; Col(0);
					SetPage.DisplayList();
					int i = SetPage.InsertAction();
					if (i == 3)
					{
						ChangeData();
					}
					else if (i == 4)
					{
						int i = SetPage.CheckCurrent();
						ISD_Fun = i + 1;
						break;
					}
				}
				Col(1); cout << "请输入修改后的内容 :  ";
				switch (ISD_Fun)
				{
				case 1:
					/*cin >> ISD[ISD_Id].ICUF_Id;*/
					Col(1); cout << "暂时不提供修改序号的功能 !" << endl; Col(0);
					break;
				case 2:
					cin >> ISD[ISD_Id - 1].ICUF_Name;
					break;
				case 3:
					cin >> ISD[ISD_Id - 1].ICUF_Float;
					break;
				case 4:
					cin >> ISD[ISD_Id - 1].ICUF_Price;
					break;
				}
				Col(0);
				skin.SaveSkinData();
				BuyPage.ResetTimes();
			}
			if (i == 1)//从头开始修改数据(不会删除后面未经修改的数据)
			{
				SetPage.RemoveAll();
				SetPage.ResetTimes();
				int breakword{};
				system("cls");
				for (int i = BuyPage.FunctionNumbers; i < 30; i++)
				{
					Col(8); cout << "ICUF_Id  :" << i + 1 << endl; Col(0);
					ISD[i].ICUF_Id = i + 1;
					Col(8); cout << "请输入 ICUF_Name (string) :";
					cin >> ISD[i].ICUF_Name; Col(0);
					Col(8); cout << "请输入 ICUF_Float (string) :";
					cin >> ISD[i].ICUF_Float; Col(0);
					Col(8); cout << "请输入 ICUF_Price (double) :";
					cin >> ISD[i].ICUF_Price; Col(0);
					SetPage.Append("1.继续", SetPage.Times);
					SetPage.Append("2.退出", SetPage.Times++);
					SetPage.InitList();
					while (1)
					{
						system("cls");
						for (int j = 0; j <= i; j++)
						{
							Col(rand() % 6 + 1); cout << "ICUF_Id  :" << j + 1 << endl; Col(0);
							ISD[j].ICUF_Id = j + 1;
							Col(rand() % 6 + 1); cout << "ICUF_Name (string) :" << ISD[j].ICUF_Name << endl; Col(0);
							Col(rand() % 6 + 1); cout << "ICUF_Float (string) :" << ISD[j].ICUF_Float << endl; Col(0);
							Col(rand() % 6 + 1); cout << "ICUF_Price (double) :" << ISD[j].ICUF_Price << endl; Col(0);
						}
						SetPage.DisplayList();
						int i = SetPage.InsertAction();
						if (i == 3)
						{
							ChangeData();
						}
						else if (i == 4)
						{
							int i = SetPage.CheckCurrent();
							if (i == 0)
							{
								breakword = 0;
								break;
							}
							else if (i == 1)
							{
								LengthOfData = i;
								breakword = 1;
								break;
							}
						}
					}
					if (breakword == 1)
						break;
				}
				skin.SaveSkinData();
				BuyPage.ResetTimes();
			}
			if (i == 2)//系统初始数据
			{
				SetPage.RemoveAll();
				SetPage.ResetTimes();
				skin.RemoveData();
				for (int i = 0; i < 30; i++)
				{
					if (i == 0)
					{
						ISD[0].ICUF_Id = 1;
						ISD[0].ICUF_Name = "地下水";
						ISD[0].ICUF_Float = "久经沙场";
						ISD[0].ICUF_Price = 0.2;
					}
					else if (i == 1)
					{
						ISD[1].ICUF_Id = 2;
						ISD[1].ICUF_Name = "地下水";
						ISD[1].ICUF_Float = "略有磨损";
						ISD[1].ICUF_Price = 0.3;
					}
					else if (i == 2)
					{
						ISD[2].ICUF_Id = 3;
						ISD[2].ICUF_Name = "地下水";
						ISD[2].ICUF_Float = "崭新出厂";
						ISD[2].ICUF_Price = 0.4;
					}
					else if (i == 3)
					{
						ISD[3].ICUF_Id = 4;
						ISD[3].ICUF_Name = "狩猎网格";
						ISD[3].ICUF_Float = "久经沙场";
						ISD[3].ICUF_Price = 0.5;
					}
					else
					{
						ISD[i].ICUF_Id = NULL;
						ISD[i].ICUF_Name = "";
						ISD[i].ICUF_Float = "";
						ISD[i].ICUF_Price = NULL;
					}
				}
				skin.SaveSkinData();
				BuyPage.ResetTimes();
			}
			if (i == 3)
			{
				skin.RemoveData();
				SetPage.ResetTimes();
				for (int i = 0; i < 30; i++)
				{
					ISD[i].ICUF_Id = NULL;
					ISD[i].ICUF_Name = "";
					ISD[i].ICUF_Float = "";
					ISD[i].ICUF_Price = NULL;
				}
				skin.SaveSkinData();
				BuyPage.ResetTimes();
			}
		}
	}
}
void SearchDeal()
{
	SearchDPage.Append("1.查询所有", SearchDPage.Times);
	SearchDPage.Append("2.按ID查询", SearchDPage.Times++);
	SearchDPage.InitList();
	while (1)
	{
		system("cls");
		Col(SearchDPage.DefaultCol); cout << "\n\n\n\n                         ICUF购物平台-查询订单功能  WiChG_Trade             \n\n"; Col(0);
		SearchDPage.DisplayList();
		int i = SearchDPage.InsertAction();
		if (i == 3)
			main();
		else if (i == 4)
		{
			int i = SearchDPage.CheckCurrent();
			switch (i)
			{
			case 0:
				SearchDealShow();
				break;
			case 1:
				SearchDealById();
				break;
			}
		}
	}
}
void SearchDealById()
{
	int list, List[30]{}, Number[30]{}, Id[30]{}, ICUF_Id[30]{}, DataLength{ 30 };
	double price[30]{};
	Col(8); cout << "请输入要查询的订单号 : ";
	cin >> list; Col(0);
	fin.open(Deals);
	for (int i = 0; i < 30; i++)
	{
		fin >> List[i] >> Id[i] >> ICUF_Id[i] >> Number[i] >> price[i];
		if (List[i] == 0)
		{
			DataLength = i;
			break;
		}
	}
	fin.close();
	fout.open(Deals);
	for (int i = 0; i < 30; i++)
	{
		if (List[i] == 0)
			break;
		fout << List[i] << " " << Id[i] << " " << ICUF_Id[i] << " " << Number[i] << " " << price[i] << endl;

	}
	fout.close();
	int flag{};
	Col(8);
	for (int j = 0; j < DataLength; j++)
	{
		if (List[j] == list)
		{
			cout << "List ID : " << List[j] << "  Cargo ID : " << Id[j] << "  ICUF ID : " << ICUF_Id[j] << "  Number : " << Number[j] << "  Price : " << price[j]; Col(0);
			flag = 1;
		}
		if (flag == 0 && j == DataLength - 1)
			cout << "不存在此订单!"; Col(0);
	}
	if (DataLength == 0)
		cout << "当前不存在任何已储存订单" << endl; Col(0);
	system("pause");
}
void SearchDealShow()//用于展示所有已储存的订单
{
	int List[30]{}, Number[30]{}, Id[30]{}, ICUF_Id[30]{}, DataLength{ 30 };
	double price[30]{};
	fin.open(Deals);
	for (int i = 0; i < 30; i++)
	{
		fin >> List[i] >> Id[i] >> ICUF_Id[i] >> Number[i] >> price[i];
		if (List[i] == 0)
		{
			DataLength = i;
			break;
		}
	}
	fin.close();
	fout.open(Deals);
	for (int i = 0; i < 30; i++)
	{
		if (List[i] == 0)
			break;
		fout << List[i] << " " << Id[i] << " " << ICUF_Id[i] << " " << Number[i] << " " << price[i] << endl;
		Col(8); cout << "List ID : " << List[i] << "  Cargo ID : " << Id[i] << "  ICUF ID : " << ICUF_Id[i] << "  Number : " << Number[i] << "  Price : " << price[i] << endl; Col(0);
	}
	fout.close();
	system("pause");
}
void Init()//用于判断执行次数并进行更新
{
	skin.GetSkinData();//读取本地文件数据
	skin.SaveSkinData();
	if (BuyPage.TempTimes != 0)
		BuyPage.RemoveAll();
	for (int i = 0; i < skin.GetLength(); i++)//使用头文件方法给列表添加物品
		BuyPage.Append(ISD[i].ICUF_Name + "  (" + ISD[i].ICUF_Float + ")", BuyPage.Times);
	BuyPage.Times++;
	BuyPage.ResetTimes();
}

运行截图

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WiChP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值