C++鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)
用數(shù)據(jù)結(jié)構(gòu)里面線性結(jié)構(gòu)的鏈表實(shí)現(xiàn),供大家參考,具體內(nèi)容如下
文件操作未寫(xiě)
有登錄操作,復(fù)制源碼需要更改登錄模塊的密碼文件存放位置
使用VS2017編譯器需要保留開(kāi)頭: #define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include "cstdio"
#include "fstream"
#include "stdlib.h"
#include "String"
#include "iomanip"
#include "windows.h"
#define LEN 100
using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ios;
using std::cerr;
using std::string;
using std::setw;
typedef struct LNode {
char num[10];
char name[20];
char telNum[12];
char qq[10];
struct LNode *next;
}LNode,*LinkList;
int n = 0;
void InitList(LinkList &L);//初始化表
void InsertLNode(LinkList &L,LNode *s);//前插法插入新結(jié)點(diǎn)
LinkList SearchName(LinkList L);//按姓名查找
LinkList SearchNum(LinkList L);//按學(xué)號(hào)查找
void DelLNode(LinkList &L,LinkList p);//刪除p結(jié)點(diǎn)
void PrintLNode(LinkList p);//打印結(jié)點(diǎn)
void PrintList(LinkList L);//打印表
/*----------------系統(tǒng)函數(shù)----------------*/
void CreateLinkList(LinkList &L);//創(chuàng)建鏈表
void DelName(LinkList &L);//按姓名刪除通訊錄成員
void DelNum(LinkList &L);//按學(xué)號(hào)刪除通訊錄成員
void saveRecord(LinkList L);//存儲(chǔ)信息
void loadRecord(LinkList &L);//加載信息
/*--------------------------------------*/
void Secret();
void fun();
void ver();
void yanshi(char *p);
void clear();
void header();
void menu() {
LinkList L=NULL;
int select;
do {
system("cls");
printf("\t\t\t Welcome to the address book information management system!\n\n\n");
printf("\t\t\t\t***************************************************\n");
printf("\t\t\t\t * │1.InitList 2.Add Message │ *\n");
printf("\t\t\t\t * │ │ *\n");
printf("\t\t\t\t * │3.Search Message 4.Save File │ *\n");
printf("\t\t\t\t * │ │ *\n");
printf("\t\t\t\t * │5.Sort Static 6.Load Message │ *\n");
printf("\t\t\t\t * │ │ *\n");
printf("\t\t\t\t * │7.Display Message 8.Delete Message│ *\n");
printf("\t\t\t\t * │ │ *\n");
printf("\t\t\t\t * │9.Save Message 0.Exit System │ *\n");
printf("\t\t\t\t***************************************************\n");
cout << endl;
yanshi((char *)"\t\tPlease choose the mode of operation(1~8):\n");
/* cout << "\t\tPlease choose the mode of operation(1~8):" << endl;*/
cin >> select;
switch (select) {
case 8:
cout << "Please select the deletion method:\n1.Delete by student number 2.Delete by name\n" << endl;
int x;
cin >> x;
switch (x) {
case 1:
DelNum(L);
break;
case 2:
DelName(L);
break;
}
case 6:
loadRecord(L);
break;
case 5:
break;
case 4:
saveRecord(L);
break;
case 3:
clear();
cout << "Please select a search method:\n1.Find by student number 2.Find by name\n" << endl;
int a;
cin >> a;
switch (a) {
case 1:
clear();
{
LinkList aa = SearchNum(L);
header();
PrintLNode(aa);
cout << "\n\n\n成功!" << endl;
system("pause");
menu();
}
break;
case 2:
clear();
{
LinkList b = SearchName(L);
header();
PrintLNode(b);
cout << "\n\n\n成功!" << endl;
system("pause");
menu();
break;
}
}
break;
case 1:
InitList(L);
break;
case 9:
break;
case 7:
PrintList(L);
break;
case 2:
CreateLinkList(L);
break;
case 0:
cout << endl << endl << endl;
cout << "The programe is over!" << endl << endl << endl;
Sleep(2000);
exit(0);
break;
}
} while (select != 8);
}
int main() {
fun();
ver();//版本信息
Secret();//密碼登錄
menu();
return 0;
}
//初始化表
void InitList(LinkList & L)
{
L = new LNode;//申請(qǐng)頭結(jié)點(diǎn)
L->next= NULL;
}
//插入一條信息
void InsertLNode(LinkList & L, LNode *s)
{
s->next = L->next;
L->next = s;
}
//按姓名查找
LinkList SearchName(LinkList L)
{
char name[20];
cout << "請(qǐng)輸入要查找的姓名:" << endl;
cin >> name;
LinkList p = L->next;
while (p) {
//如果找到,退出循環(huán),返回p
if (strcmp(p->name, name) == 0)
break;
else
p = p->next;
}
return p;
}
//按學(xué)號(hào)查找
LinkList SearchNum(LinkList L)
{
char num[10];
cout << "請(qǐng)輸入要查找的學(xué)號(hào):" << endl;
cin >> num;
LinkList p = L->next;
while (p) {
//如果找到,退出循環(huán),返回p
if (strcmp(p->num, num) == 0)
break;
else
p = p->next;
}
return p;
}
//刪除節(jié)點(diǎn)
void DelLNode(LinkList &L,LinkList p)
{
LinkList s=NULL, q;
q = L->next;
//將s指向p前面的一個(gè)結(jié)點(diǎn)
while (q&&q!=p) {
s = q;
q = q->next;
}
s->next = q->next;
delete q;
}
//打印一條信息
void PrintLNode(LinkList p)
{
printf("%15s", p->num);
printf("%15s", p->name);
printf("%15s", p->telNum);
printf("%15s\n",p->qq);
}
//打印通訊錄
void PrintList(LinkList L)
{
clear();
header();
LinkList p = L->next;
while (p) {
PrintLNode(p);
p = p->next;
}
system("pause");
}
//添加信息
void CreateLinkList(LinkList & L)
{
char ans = 'y';
n = 0;
while (ans=='y'||ans=='Y') {
system("cls");
LNode *p = new LNode;
cout << "請(qǐng)輸入學(xué)號(hào):" << endl;
cin >> p->num;
cout << "請(qǐng)輸入姓名:" << endl;
cin >> p->name;
cout << "請(qǐng)輸入電話號(hào)碼:" << endl;
cin >> p->telNum;
cout << "請(qǐng)輸入QQ號(hào):" << endl;
cin >> p->qq;
InsertLNode(L,p);
n++;
cout<<"是否繼續(xù)?(Y/N)"<<endl;
getchar();
ans=getchar();
}
system("pause");
}
//按姓名刪除
void DelName(LinkList &L)
{
char name[20];
LinkList p;
cout << "請(qǐng)輸入要?jiǎng)h除的學(xué)生姓名:" << endl;
cin >> name;
p = SearchName(L);
if (p) {
DelLNode(L,p);
}
system("pause");
}
//按學(xué)號(hào)刪除
void DelNum(LinkList & L)
{
char num[20];
LinkList p;
cout << "請(qǐng)輸入要?jiǎng)h除的學(xué)生學(xué)號(hào):" << endl;
cin >> num;
p = SearchName(L);
if (p) {
DelLNode(L, p);
}
system("pause");
}
//存儲(chǔ)信息
void saveRecord(LinkList L)
{
FILE *fp=NULL;
int count = 0;
if ((fp=(fopen("student.dat","wb")))==NULL) {
cout << "Can't open this file!" << endl;
Sleep(3000);
}
LinkList q = L->next;
while (q) {
fwrite(q, sizeof(LNode), 1, fp);
count ++;
q = q->next;
}
fclose(fp);
cout << "Save the file successfully!" << endl;
getchar();
}
//加載信息
void loadRecord(LinkList & L)
{
FILE *fp=NULL;
int count = 0;
if ((fp=(fopen("student.dat", "rb"))) == NULL) {
cout << "Can't open this file!" << endl;
Sleep(3000);
}
LinkList p=NULL;
while(1){
p = new LNode;
if (fread(p, sizeof(LNode), 1, fp) > 0) {
InsertLNode(L,p);
count++;
}
else {
break;
}
}
fclose(fp);
cout << endl << endl << "Load "<<count<<"messages!" << endl;
Sleep(2200);
}
//控制臺(tái)樣式
void fun() {
system("color 2a");
system("title 學(xué)生通訊錄信息管理系統(tǒng)");
}
//版本信息
void ver()
{
yanshi((char*)"\t \3\3\3\3\3\3\3歡迎使用通訊錄信息管理系統(tǒng)\3\3\3\3\3\3\3\n\n\n\n");
cout << "\t 學(xué)生通訊錄信息管理系統(tǒng)\n\n\n\n\n";
cout << "\t\t version 1.0\n\n\n\n\n";
cout << "\t\t xxxxxxxxx 某某某\n\n\n\n\n";
cout << "\t\t Loading......\n\n" << endl;
Sleep(3000);
system("cls");
}
//延時(shí)輸出
void yanshi(char *p)
{
while (1) {
if (*p != 0)
cout << *p++;
else
break;
Sleep(50);
}
}
//清屏
void clear()
{
system("cls");
}
//表頭
void header()
{
printf("%15s%15s%15s%15s\n","學(xué)號(hào)","姓名","電話","QQ");
}
/*--------------------------------登錄模塊----------------------------------*/
/*--------------------------------登錄模塊----------------------------------*/
/*--------------------------------登錄模塊----------------------------------*/
struct UsrInfo//用戶(hù)名的賬戶(hù)和密碼信息
{
char UsrName[20];
char Psword[20];
};
/*
注意我的文件中用戶(hù)名是一行,密碼是一樣。在注冊(cè)的時(shí)候只需要看用戶(hù)名,不需要看密碼是不是相符,所以設(shè)置i為計(jì)數(shù)變量,當(dāng)i不能整除2
的時(shí)候是訪問(wèn)用戶(hù)名的時(shí)候,當(dāng)i整除2的時(shí)候是訪問(wèn)密碼的時(shí)候。讀入密碼這一行直接跳出去就行了。
*/
int regest(struct UsrInfo* usr) {//注冊(cè)程序
char usrname[20];
char psword[20];
strcpy_s(usrname, usr->UsrName);
strcpy_s(psword, usr->Psword);
string temp;
int flag = 0;
int i = 0;
ifstream fin("E:\\Love-Study\\學(xué)生通訊錄管理系統(tǒng)\\static.dat", ios::in);//在這個(gè)路徑下讀入文件
ofstream fout("E:\\Love-Study\\學(xué)生通訊錄管理系統(tǒng)\\static.dat", ios::app);//在同一個(gè)路徑下,如果注冊(cè)成功則寫(xiě)入文件
while (std::getline(fin, temp))//每次讀一行的數(shù)據(jù)進(jìn)入temp中。
{
i++;
if (i % 2 == 0) continue;//訪問(wèn)的是密碼這一行,跳出。
if (!strcmp(usrname, temp.c_str())) flag = 1;//flag=1說(shuō)明用戶(hù)名已經(jīng)被注冊(cè)了
}
fin.close();
if (flag) {
return 0;//之前有重復(fù)的賬戶(hù)名
}
else {//沒(méi)注冊(cè)
fout << usrname << endl;//向文件寫(xiě)入注冊(cè)者的用戶(hù)名,然后換一行
fout << psword << endl;//寫(xiě)入密碼,換行
fout.close();
return 1;//注冊(cè)成功
}
}
int login(struct UsrInfo* usr) {
char usrname[20];
char psword[20];
strcpy_s(usrname, usr->UsrName);
strcpy_s(psword, usr->Psword);
string temp1;
string temp2;
int existname = 0;
int match = 0;
int i = 0;
ifstream fin("E:\\Love-Study\\學(xué)生通訊錄管理系統(tǒng)\\static.dat", ios::in);
while (std::getline(fin, temp1))
{
std::getline(fin, temp2);//一次讀進(jìn)去兩行,分別是用戶(hù)名和密碼
if (!strcmp(usrname, temp1.c_str())) {//有這個(gè)用戶(hù)名了,接下來(lái)看看密碼是不是相符的
existname = 1;
if (!strcmp(psword, temp2.c_str())) {//相符
match = 1;
break;
}
}
}
fin.close();
if (!existname) {
return 2;//沒(méi)有賬戶(hù)名
}
else {
if (match) return 1;
else return 3;//用戶(hù)名和密碼不匹配
}
}
void Secret()
{
clear();
cout << "1.注冊(cè) 2.登錄" << endl;
int c;
cin >> c;
switch (c) {
case 1:
{
clear();
UsrInfo test1;//用于測(cè)試注冊(cè)程序的。
char urr[20], prr[20];
cout << "請(qǐng)輸入用戶(hù)名:" << endl;
cin >> urr;
cout << "請(qǐng)輸入密碼:" << endl;
cin >> prr;
strcpy(test1.UsrName, urr);
strcpy(test1.Psword, prr);
switch (regest(&test1))
{
case 1:
cout << "注冊(cè)成功" << endl;
system("pause");
Secret();
break;
case 0:
cout << "用戶(hù)名已被注冊(cè),請(qǐng)重新選擇用戶(hù)名" << endl;
system("pause");
Secret();
break;
default:
break;
}
break;
}
case 2:
{
clear();
UsrInfo test2;//用于測(cè)試注冊(cè)程序的。
char ur[20], pr[20];
cout << "請(qǐng)輸入用戶(hù)名:" << endl;
cin >> ur;
cout << "請(qǐng)輸入密碼:" << endl;
cin >> pr;
strcpy(test2.UsrName, ur);
strcpy(test2.Psword, pr);
switch (login(&test2))
{
case 1:
cout << "登錄成功" << endl;
system("pause");
menu();
break;
case 3:
cout << "密碼錯(cuò)誤" << endl;
system("pause");
Secret();
break;
case 2:
cout << "沒(méi)有此用戶(hù)名,請(qǐng)注冊(cè)" << endl;
system("pause");
Secret();
break;
default:
break;
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C++ map 根據(jù)value找key的實(shí)現(xiàn)
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言實(shí)現(xiàn)電話簿項(xiàng)目
本文標(biāo)題:C++鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/67.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10深入理解鏈表的各類(lèi)操作詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)


