C++ 链表实现通讯录 (关于获取鼠标句柄后,清屏不能再次获取)

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">	</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">1.头文件</span></span>

Address_book.h
#pragma once
#include<string>
#include"Node.h"
#include"cls.h"
#include"Interface.h"
#include<iostream>
#include"List.h"
#include"io.h"
#include<algorithm>
#include"mouse.h"
class Address
{
	
public:
	Address();
	static List list;
	static int falg;
	int static falg2;
	int static falg3;
	void  newlinkman();
	void  initialize();
	void  over();
	void  FindLinkman();
	void  Linkman(int);
	void  ModLinkman(int);
	void  print();
	void  DeleteLinkman(int );
	void  Delete_Linkman();
private:
	int static Pos;
	int static PosDelete;
	
	string static Xinxi;
	HANDLE hStdout;
	Interface face;
	Mouse mouse;
	Cls cl;
	IO file;
};
Cls.h
<pre name="code" class="cpp">#include"mouse.h"
class Cls
{
public:
	void cls(HANDLE hConsole);
};

Interface.h
#pragma once
#include"mouse.h"
#include"List.h"
#include<iostream>
using namespace std;
class Interface
{
	
public:
	void showface(List list);

};

 IO.h 
 
#pragma once
#include<iostream>
#include<fstream>
#include<istream>
#include"List.h"
#include"node.h"
using namespace std;
class IO
{
public:
	void in(const string &str, List &list);
	void out(const string &str,List &list);
};
List.h
#include<iostream>
#include <windows.h> 
#include<algorithm>
using namespace std;
class List
{
public:
	List();
	void AddNode(Node *);
	void Print();
	void PrintNode(int);
	void Print_Node(int);
	void ModNode(int,int,string);
	void Find(string);
	int FindNode(string,int);
	void L_sort();
	void DeleteNode(int);
	void clean();
	Node *head;
	Node *end;
	int Pos;
	int Find_Pos;
};
Mouse.h

#pragma once
#include <windows.h>  
#include <stdio.h>  
using namespace std;
class  Mouse
{
public:
	int shubiao();
};
Node.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Node
{
public:
	string name;
	string number;
	string address;
	string sex;
	string age;
	Node *next;
	bool operator<(Node *n)
	{
		if (this->name < n->name)
			return 1;
		else
			return 0;
	}
};

2.源文件

Address_book.cpp
<pre name="code" class="cpp">#include"Address_book.h"
#include<conio.h>
#include<iostream>
Address::Address()
{
	
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
}
void Address::print()
{
	cl.cls(hStdout);
	list.L_sort();
	face.showface(list);
}
void Address::Linkman(int n)
{
	if (falg == 0 || falg2 == 1)
	{
		cl.cls(hStdout);
		falg = 2;
		Pos = n;
		list.Print_Node(Pos);
		cout << "返回主界面" << endl;
		mouse.shubiao();
	}
	if (falg == 1||falg2 ==2)
	{
		cl.cls(hStdout);
		falg = 2;
		Pos = n;
		list.Print_Node(list.FindNode(Xinxi,Pos));
		cout << "返回主界面" << endl;
		mouse.shubiao();
	}
}
void Address::ModLinkman(int n)
{
	if (falg2 != 2)
	{
		string str;
		cl.cls(hStdout);
		if (n == 0)
		{
			cout << "请输入你要修改的姓名:";
			cin >> str;
		}
		if (n == 1)
		{
			cout << "请输入你要修改的年龄:";
			cin >> str;
		}
		if (n == 2)
		{
			cout << "请输入你要修改的性别:";
			cin >> str;
		}
		if (n == 3)
		{
			cout << "请输入你要修改的电话:";
			cin >> str;
		}
		if (n == 4)
		{
			cout << "请输入你要修改的地址:";
			cin >> str;
		}
		list.ModNode(Pos, n, str);
		Linkman(Pos);
	}
	if (falg2 == 2)
	{
		string str;
		cl.cls(hStdout);
		if (n == 0)
		{
			cout << "请输入你要修改的姓名:";
			cin >> str;
		}
		if (n == 1)
		{
			cout << "请输入你要修改的年龄:";
			cin >> str;
		}
		if (n == 2)
		{
			cout << "请输入你要修改的性别:";
			cin >> str;
		}
		if (n == 3)
		{
			cout << "请输入你要修改的电话:";
			cin >> str;
		}
		if (n == 4)
		{
			cout << "请输入你要修改的地址:";
			cin >> str;
		}
		list.ModNode(list.FindNode(Xinxi,Pos),n, str);
		Linkman(Pos);
	}
}
void Address::initialize()
{
	file.out("address.txt", list);
	face.showface(list);
	mouse.shubiao();
}
List Address::list;
int Address::falg = 0;
int Address::falg3 = 0;
int Address::Pos = 0;
string Address::Xinxi = "";
int Address::falg2 = 0;
void Address::newlinkman()
{
	Node * node;
	node = new Node();
	//system("cls");
	cl.cls(hStdout);
	cout << "请输入联系人姓名:";
	cin >> node->name;
	cout << "请输入联系人年龄:";
	cin >> node->age;
	cout << "请输入联系人性别:";
	cin >> node->sex;
	cout << "请输入联系人电话:";
	cin >> node->number;
	cout << "请输入联系人地址:";
	cin >> node->address;
	list.AddNode(node);
	cl.cls(hStdout);
	//system("cls");
	face.showface(list);
}
void Address::FindLinkman()
{
	falg = 1;
	cl.cls(hStdout);
	cout << "请输入你要查到的联系人的信息:";
	cin >> Xinxi;
	falg3 = 1;
	list.Find(Xinxi);
	cout << "返回通讯录主界面"<<endl;
	mouse.shubiao();
}

void Address::over()
{
	file.in("address.txt", list);
}
int Address::PosDelete = 0;
void Address::DeleteLinkman(int n)
{
	cl.cls(hStdout);
	falg = 3;
	PosDelete = n;
	cout << "确定删除此联系人?" << endl;
	cout << "   是     否" << endl;
}
void Address::Delete_Linkman()
{
	if (!falg3)
	{
		list.DeleteNode(PosDelete);
		falg = 0;
		print();
	}
	if (falg3)
	{
		//int a = list.FindNode(Xinxi, PosDelete+1);
		list.DeleteNode(list.FindNode(Xinxi,PosDelete+1));
		falg = 0;
		falg3 = 0;
		print();
	}
}
cls.cpp
#include"cls.h"
#include <windows.h>

void Cls::cls(HANDLE hConsole)
{
	COORD coordScreen = { 0, 0 };    // home for the cursor 
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD dwConSize;

	// Get the number of character cells in the current buffer. 

	if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
	{
		return;
	}

	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

	// Fill the entire screen with blanks.

	if (!FillConsoleOutputCharacter(hConsole,        // Handle to console screen buffer 
		(TCHAR) ' ',     // Character to write to the buffer
		dwConSize,       // Number of cells to write 
		coordScreen,     // Coordinates of first cell 
		&cCharsWritten))// Receive number of characters written
	{
		return;
	}

	// Get the current text attribute.

	if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
	{
		return;
	}

	// Set the buffer's attributes accordingly.

	if (!FillConsoleOutputAttribute(hConsole,         // Handle to console screen buffer 
		csbi.wAttributes, // Character attributes to use
		dwConSize,        // Number of cells to set attribute 
		coordScreen,      // Coordinates of first cell 
		&cCharsWritten)) // Receive number of characters written
	{
		return;
	}

	// Put the cursor at its home coordinates.

	SetConsoleCursorPosition(hConsole, coordScreen);
}
Interface.cpp
#include"Interface.h"
void Interface::showface(List list)
{
	cout <<"菜单: "<< "新建联系人" <<"   "<<"搜索联系人"<<"   退出"<<endl;
	for (int i = 0; i < 15; i++)
	{
		cout << "*-*";
	}
	cout << endl;
	list.Print();
}
io.cpp
 
 
<pre name="code" class="cpp">#include"io.h"
void IO::in(const string &str, List &list)
{
	ofstream file(str,ios::out);
	Node *p;
	for (p = list.head->next; !p == NULL; p = p->next)
	{
		if (p->next == NULL)
		{
			file << p->name << endl << p->sex << endl << p->age << endl << p->number << endl << p->address;
		}
		else
		{
			file << p->name << endl << p->sex << endl << p->age << endl << p->number << endl << p->address << endl;
		}

	}
	file.close();
}
void IO::out(const string &str, List &list)
{
	ifstream file(str);
	file.seekg(0, ios::beg);
	Node *p;
	while (!file.eof())
	{
		p = new Node;
		getline(file, p->name);
		getline(file, p->sex);
		getline(file, p->age);
		getline(file, p->number);
		getline(file, p->address);
		list.AddNode(p);
	}
}
List.cpp
 
 
<pre name="code" class="cpp">#include"List.h"
List::List()
{
	head = new Node;
	head->name = "";
	head->sex = "";
	head->number = "";
	head->address = "";
	head->next=NULL;
	Pos = 0;
	Find_Pos = 0;
}
void List::AddNode(Node* node)
{
	Node *p = head;
	while (!p->next == NULL)
	{
		p = p->next;
	}
	p->next = node;
	end = p->next;
	Pos++;
	L_sort();
}
void List::Print()
{
	Node *p = head->next;
	while (!p == NULL)
	{
		cout << p->name << " " << p->age << " " << p->sex << " " << p->number << " " << p->address << endl;
		p = p->next;
	}
}
void List::PrintNode(int n)
{
	int pos = 0;
	Node *p = head;
	while (pos < n)
	{
		p = p->next;
		pos++;
	}
	cout << p->name << " " << p->sex << " " << p->age << " " << p->number << " " << p->address << endl;
}
void List::L_sort()
{
	Node* p = head->next;
	Node* p_pre = p;
	//bool flag = false;   //用于标记是否有交换,当数组有序的时候,提高判断效率

	while (p_pre->next != NULL)
	{
		string temp = p_pre->name;
		p = p->next;
		while (p)
		{
			if (temp <= (p->name))
			{
				p = p->next;
				continue;
			}
			else
			{
				string temp_change1;
				temp_change1 = p->name;
				p->name = p_pre->name;
				p_pre->name = temp_change1;

				string temp_change2;
				temp_change1 = p->age;
				p->age = p_pre->age;
				p_pre->age = temp_change1;

				string temp_change3;
				temp_change1 = p->sex;
				p->sex = p_pre->sex;
				p_pre->sex = temp_change1;

				string temp_change4;
				temp_change1 = p->number;
				p->number = p_pre->number;
				p_pre->number = temp_change1;

				string temp_change5;
				temp_change1 = p->address;
				p->address = p_pre->address;
				p_pre->address = temp_change1;
				p = p->next;
				//flag = true;
			}
		}
		p_pre = p_pre->next;
		p = p_pre;
	}
	
}
void List::clean()
{
	Node *s;
	Node *p = head->next;
	while (p != NULL)
	{
		s = p;
		p = p->next;
		delete s;
	}
}
void List::Find(string str)
{
	Node *p = head->next;
	int pos = 1;
	Find_Pos = 0;
	while (!p == NULL)
	{
		bool key = false;
		if (p->name.find(str) != string::npos)
			key = true;
		if (p->sex.find(str) != string::npos)
			key = true;
		if (p->age.find(str) != string::npos)
			key = true;
		if (p->number.find(str) != string::npos)
			key = true;
		if (p->address.find(str) != string::npos)
			key = true;
		if (key)
		{
			PrintNode(pos);
			Find_Pos++;
		}
		p = p->next;
		pos++;
	}
}
int List::FindNode(string str,int n)
{
	Node *p = head->next;
	int iINum = 1;
	int pos = 0;
	while (pos != n)
	{
		if (p->name.find(str) != string::npos)
			pos++;
		if (p->sex.find(str) != string::npos)
			pos++;
		if (p->age.find(str) != string::npos)
			pos++;
		if (p->number.find(str) != string::npos)
			pos++;
		if (p->address.find(str) != string::npos)
			pos++;
		iINum++;
		p = p->next;
	}
	return iINum-1;
}
void List::Print_Node(int n)
{
	int pos = 1;
	Node * p = head->next;
	while (pos < n)
	{
		p = p->next;
		pos++;
	}
	cout << "姓名:" << p->name<<endl;
	cout << "年龄: " << p->age << endl;
	cout << "性别:" << p->sex << endl;
	cout << "电话号:" << p->number << endl;
	cout << "地址: " << p->address << endl;
}
void List::ModNode(int n,int falg,string str)
{
	int pos = 1;
	Node * p = head->next;
	while (pos < n)
	{
		p = p->next;
		pos++;
	}
	if (falg == 0)
	{
		p->name = str;
	}
	if (falg == 1)
	{
		p->age = str;
	}
	if (falg == 2)
	{
		p->sex = str;
	}
	if (falg == 3)
	{
		p->number = str;
	}
	if (falg == 4)
	{
		p->address = str;
	}
}
void List::DeleteNode(int n)
{
	int pos = 1;
	Node * p = head->next;
	if (n != 1)
	{
		while (pos < n - 1)
		{
			p = p->next;
			pos++;
		}
		Node *s = p->next;
		p->next = s->next;
		delete s;
	}
	else
	{
		p = head;
		Node *s = p->next;
		p->next = s->next;
		delete s;
	}
}
mouse.cpp
 
 
<pre name="code" class="cpp">#include"mouse.h"
#include"Address_book.h"
int Mouse::shubiao()
{	
	Address a;
	// 获取标准输入输出设备句柄  
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO bInfo;
	INPUT_RECORD	mouseRec;
	DWORD			res;
	COORD			crPos, crHome = { 0, 0 };
	//printf("[Cursor Position] X: %2lu  Y: %2lu\n", 0, 0);	// 初始状态
	while (1)
	{
		ReadConsoleInput(hIn, &mouseRec, 1, &res);
		if (mouseRec.EventType == MOUSE_EVENT)
		{
			if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
			{
				if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
				{
					//if (a.falg==0)
				    	//break;	// 左键双击 退出循环
				}
			}
			crPos = mouseRec.Event.MouseEvent.dwMousePosition;
			GetConsoleScreenBufferInfo(hOut, &bInfo);
			SetConsoleCursorPosition(hOut, crHome);
			//printf("[Cursor Position] X: %2lu  Y: %2lu", crPos.X, crPos.Y);
			SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition);

			switch (mouseRec.Event.MouseEvent.dwButtonState)
			{
			case FROM_LEFT_1ST_BUTTON_PRESSED:// 左键 
				if (a.falg == 0)
				{
					if (crPos.Y==0)
					{
						if (6 <= crPos.X&&crPos.X <= 16)
						{
							a.newlinkman();
						}
						if (20 <= crPos.X&&crPos.X <= 29)
						{
							a.FindLinkman();
						}
						if (30 <= crPos.X&&crPos.X <= 36)
							return 0;
						break;
					}
					else if (1 <= crPos.Y&&crPos.Y <= a.list.Pos + 1)
					{
						a.falg2 = 1;
						a.Linkman(crPos.Y - 1);
					}
				}
				else if (a.falg==1)
				{
					if (crPos.Y == a.list.Find_Pos + 1)
					{
						a.print();
						a.falg = 0;
					}
					else if (crPos.Y >= 1 && crPos.Y <= a.list.Find_Pos)
					{
						a.falg2 = 2;
						a.Linkman(crPos.Y);
					}
				}
				else if (a.falg == 2)
				{
					if (crPos.Y == 5)
					{
						a.print();
						a.falg = 0;
						a.falg2 = 0;
					}
					else if (0 <= crPos.Y&&crPos.Y <= 4)
					{
						a.ModLinkman(crPos.Y);
					}
				}
				else if (a.falg == 3)
				{
					if (crPos.Y == 1)
					{
						if (3 <= crPos.X&&crPos.X <= 4)
							a.Delete_Linkman();
						if (9 <= crPos.X&&crPos.X <= 11)
						{
							a.print();
							a.falg = 0;
							a.falg2 = 0;
						}
					}
				}
			break;
			case RIGHTMOST_BUTTON_PRESSED:	
				if (a.falg == 0)
				{
					if (1 <= crPos.Y&&crPos.Y <= a.list.Pos + 1)
					{
						a.DeleteLinkman(crPos.Y - 1);
					}
				}
				if (a.falg == 1)
				{
					if (1 <= crPos.Y&&crPos.Y <= a.list.Find_Pos)
					{
						a.DeleteLinkman(crPos.Y - 1);
					}
				}
				//FillConsoleOutputCharacter(hOut, 'a', 1, crPos, &res);
				break;

			default:
				break;
			}
		}
	}
    CloseHandle(hOut);  // 关闭标准输出设备句柄  
	CloseHandle(hIn);   // 关闭标准输入设备句柄  
	return 0;
}


 
 
Main.cpp
<pre name="code" class="cpp">#include"Address_book.h"
int main()
{
	Address a;
	a.initialize();
	a.over();
}

在编写代码是遇到了 获取鼠标句柄后,清屏不能再次获取
 
 
是清屏 system('cls')
会把控制台鼠标句柄清除
所有我用了微软提供的第二种清屏方式





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值