C/C++は設計モードに基づいて、多態を利用して同級生の通信録システムを実現する
20584 ワード
コードの要点を掘り起こして展開するにはどうすればいいですか? main.cpp Contact.h Contact.cpp
以前に書かれたC実装 2020-0908通常C++を学ぶ私达はすべていくつかの设计モードを学ぶことができて、最も早く私は设计モードを利用してこの通信録に対して书き直したいと思って、保护性を强化して闭原则を満たして先生に断られて、结局このコードの量は高くなくて、1000行以内は确かに必要ありません.このプロジェクトで最も掘り起こすことができる点は「ダイナミック配列」であるはずですが、STLライブラリにはこの概念がありますが、私はあまり言いません.
2020-0806これは完全なプロジェクトではなく、個人的にはQTインタフェースなどが必要だと感じています.
main.cpp
Contact.h
Contact.cpp
以前書いたC実装次のコードは、単一チェーンテーブルの粗い追加削除を実現するコードです.
前に双方向のチェーンテーブルで通信録を書いたことがありますが、単一のチェーンテーブルを考えて書いて、少し時間をかけてノックしました.今振り返ってみると、やはり評価しないほうがいいでしょう.しかし、削除するつもりはありません.これを黒い歴史にします.やはり基礎知識を補い続けなければならない.
2020-0806これは完全なプロジェクトではなく、個人的にはQTインタフェースなどが必要だと感じています.
main.cpp
#include
#include "Contact.h"
using namespace std;
int main(void)
{
funcClass user;
int choice = 0;
while (true)
{
user.funcMenu();
cout << " :
";
cin >> choice;
switch (choice)
{
case 0: // 0.
user.funcExit();
break;
case 1: // 1.
user.addContacts();
break;
case 2: // 2.
user.DelContacts();
break;
case 3: // 3.
user.modContacts();
break;
case 4: // 4.
user.findContacts();
break;
case 5: // 5.
user.showContacts();
break;
case 6: // 6.
user.sortContacts();
case 7: // 7.
user.cleanFile();
break;
default:
system("cls");
break;
}
}
return 0;
}
Contact.h
#pragma once //
#include
#include
using namespace std;
class Contacts
{
public:
virtual void showInfo() = 0;//
string name; //
string addr; //
string phone; //
string workfor; //
};
class newContacts :public Contacts
{
public:
// ,
newContacts(string name, string addr, string phone, string workfor);
void showInfo(); // ( virtual )
};
//
class funcClass
{
public:
funcClass();
int m_num; //
Contacts** m_cttArray; //
bool m_fileIsEmpty; //
void funcMenu(); //
void funcExit(); //
void addContacts(); //
void fileSave(); //
int getNum(); //
void init(); //
void showContacts(); //
void DelContacts(); //
int IsExit(string name);// , -1
void modContacts(); //
void findContacts(); //
void sortContacts(); //
void cleanFile(); //
~funcClass();
};
Contact.cpp
#include
#include
#include
#include "Contact.h"
using namespace std;
#define FILENAME "phone.txt"
// ,
newContacts::newContacts(string name, string addr, string phone, string workfor)
{
this->name = name;
this->addr = addr;
this->phone = phone;
this->workfor = workfor;
}
//
void newContacts::showInfo()
{
cout << " :" << this->name
<< "\t :" << this->addr
<< "\t :" << this->phone
<< "\t :" << this->workfor << endl;
}
funcClass::funcClass()
{
//1.
ifstream fi;
fi.open(FILENAME, ios::in); //
if (!fi.is_open())
{ //
//
this->m_num = 0;
//
this->m_cttArray = nullptr;
//
this->m_fileIsEmpty = true;
fi.close();
return;
}
//2.
char ch;
fi >> ch;
if (fi.eof())
{
//
this->m_num = 0;
//
this->m_cttArray = nullptr;
//
this->m_fileIsEmpty = true;
fi.close();
return;
}
//3. ,
int num = this->getNum();
this->m_num = num;
//
this->m_cttArray = new Contacts * [this->m_num];
// ,
this->init();
}
//
void funcClass::funcMenu()
{
cout << "0.
";
cout << "1.
";
cout << "2.
";
cout << "3.
";
cout << "4.
";
cout << "5.
";
cout << "6.
";
cout << "7.
";
}
//
void funcClass::funcExit()
{
cout << " !
";
system("pause"); //
exit(0); //
}
void funcClass::addContacts()
{
cout << " :
";
int addNum = 0; //
cin >> addNum;
if (addNum > 0)
{
//
//
int newSize = this->m_num + addNum; // = +
//
Contacts ** newSpace = new Contacts* [newSize];
// ,
if (this->m_cttArray != nullptr)
{
for (int i = 0; i < this->m_num; i++)
{
newSpace[i] = this->m_cttArray[i];
}
}
//
for (int i = 0; i < addNum; i++)
{
string name; //
string addr; //
string phone; //
string workfor; //
cout << " " << i + 1 << " " << endl;
cin >> name;
cout << " " << i + 1 << " " << endl;
cin >> addr;
cout << " " << i + 1 << " " << endl;
cin >> phone;
cout << " " << i + 1 << " " << endl;
cin >> workfor;
Contacts* contact = new newContacts(name, addr, phone, workfor);
// ,
newSpace[this->m_num + i] = contact;
}
//
delete[]this->m_cttArray;
//
this->m_cttArray = newSpace;
//
this->m_num = newSize;
//
this->m_fileIsEmpty = false;
//
cout << " " << addNum << " " << endl;
//
this->fileSave();
}
else
{
cout << " ,
";
}
//
system("pause");
system("cls");
}
//
void funcClass::fileSave()
{
ofstream fo;
fo.open(FILENAME, ios::out);// --
//
for (int i = 0; i < this->m_num; i++)
{
fo << this->m_cttArray[i]->name << " "
<< this->m_cttArray[i]->addr << " "
<< this->m_cttArray[i]->phone << " "
<< this->m_cttArray[i]->workfor << endl;
}
//
fo.close();
}
// :
int funcClass::getNum()
{
ifstream fi;
fi.open(FILENAME, ios::in);//
string name;
string addr;
string phone;
string workfor;
int num = 0;
while (fi >> name && fi >> addr && fi >> phone && fi >> workfor)
{
//
num++;
}
return num;
}
//
void funcClass::init()
{
ifstream fi;
fi.open(FILENAME, ios::in);
string name;
string addr;
string phone;
string workfor;
int index = 0;
while (fi >> name && fi >> addr && fi >> phone && fi >> workfor)
{
Contacts* contact = new newContacts(name, addr, phone, workfor);
this->m_cttArray[index] = contact;
index++;
}
//
fi.close();
}
//
void funcClass::showContacts()
{
//
if (this->m_fileIsEmpty)
{
cout << " !" << endl;
}
else
{
for (int i = 0; i < m_num; i++)
{
//
this->m_cttArray[i]->showInfo();
}
}
//
system("pause");
system("cls");
}
//
void funcClass::DelContacts()
{
if (this->m_fileIsEmpty)
{
cout << " !" << endl;
}
else
{
//
cout << " : " << endl;
string name;
cin >> name;
int index = this->IsExit(name);
if (index != -1)// , index
{
//
for (int i = index; i < this->m_num - 1; i++)
{
this->m_cttArray[i] = this->m_cttArray[i + 1];
}
this->m_num--;//
this->fileSave();
cout << " !" << endl;
}
else
{
cout << " , " << endl;
}
}
//
system("pause");
system("cls");
}
// , -1
int funcClass::IsExit(string name)
{
int index = -1;
for (int i = 0; i < this->m_num; i++)
{
if (this->m_cttArray[i]->name == name)
{
//
index = i;
break;
}
}
return index;
}
//
void funcClass::modContacts()
{
if (this->m_fileIsEmpty)
{
cout << " !" << endl;
}
else
{
cout << " : " << endl;
string name;
cin >> name;
int ret = this->IsExit(name);
if (ret != -1)
{
//
delete this->m_cttArray[ret];
string newName = nullptr;//
string newAddr = nullptr;//
string newPhone = nullptr;//
string newWorkfor = nullptr;//
cout << " " << name << " , : " << endl;
cin >> newName;
cout << " :" << endl;
cin >> newAddr;
cout << " :" << endl;
cin >> newPhone;
cout << " :" << endl;
cin >> newWorkfor;
Contacts* contact = new newContacts(newName, newAddr, newPhone, newWorkfor);
//
this->m_cttArray[ret] = contact;
cout << " !" << endl;
//
this->fileSave();
}
else
{
cout << " , !" << endl;
}
}
system("pause");
system("cls");
}
//
void funcClass::findContacts()
{
if (this->m_fileIsEmpty)
{
cout << " !" << endl;
}
else
{
cout << " :" << endl;
cout << "1. " << endl;
cout << "2. " << endl;
cout << "3. " << endl;
cout << "4. " << endl;
int select = 0;
cin >> select;
if (select == 1)
{
string newName;
cout << " :" << endl;
cin >> newName;
//
bool flag = false; //
for (int i = 0; i < m_num; i++)
{
if (this->m_cttArray[i]->name == newName)
{
cout << " , " << newName << " :" << endl;
flag = true;
//
this->m_cttArray[i]->showInfo();
}
}
if (flag == false)
{
cout << " , !" << endl;
}
}
else if (select == 2)
{
string newAddr;
cout << " :" << endl;
cin >> newAddr;
//
bool flag = false; //
for (int i = 0; i < m_num; i++)
{
if (this->m_cttArray[i]->addr == newAddr)
{
cout << " , " << newAddr << " :" << endl;
flag = true;
//
this->m_cttArray[i]->showInfo();
}
}
if (flag == false)
{
cout << " , !" << endl;
}
}
else if (select == 3)
{
string newPhone;
cout << " :" << endl;
cin >> newPhone;
//
bool flag = false; //
for (int i = 0; i < m_num; i++)
{
if (this->m_cttArray[i]->phone == newPhone)
{
cout << " , " << newPhone << " :" << endl;
flag = true;
//
this->m_cttArray[i]->showInfo();
}
}
if (flag == false)
{
cout << " , !" << endl;
}
}
else if (select == 4)
{
string newWorkfor;
cout << " :" << endl;
cin >> newWorkfor;
//
bool flag = false; //
for (int i = 0; i < m_num; i++)
{
if (this->m_cttArray[i]->workfor == newWorkfor)
{
cout << " , " << newWorkfor << " :" << endl;
flag = true;
//
this->m_cttArray[i]->showInfo();
}
}
if (flag == false)
{
cout << " , !" << endl;
}
}
else
{
cout << " !" << endl;
}
}
//
system("pause");
system("cls");
}
// : ( )
void funcClass::sortContacts()
{
if (this->m_fileIsEmpty)
{
cout << " !" << endl;
//
system("pause");
system("cls");
}
else
{
cout << " :" << endl;
cout << "1. " << endl;
cout << "2. " << endl;
int select = 0;
cin >> select;
for (int i = 0; i < m_num; i++)
{
int minOrMax = i; //
for (int j = i + 1; j < this->m_num; j++)
{
if (select == 1)//
{
if (this->m_cttArray[minOrMax]->name > this->m_cttArray[j]->name)
{
minOrMax = j;
}
}
else //
{
if (this->m_cttArray[minOrMax]->name < this->m_cttArray[j]->name)
{
minOrMax = j;
}
}
}
// ,
if (i != minOrMax)
{
Contacts* newContact = this->m_cttArray[i];
this->m_cttArray[i] = this->m_cttArray[minOrMax];
this->m_cttArray[minOrMax] = newContact;
}
}
cout << " ! :" << endl;
this->fileSave(); //
this->showContacts(); //
}
}
//
void funcClass::cleanFile()
{
if (this->m_fileIsEmpty)
{
cout << " !" << endl;
}
else
{
cout << " ?" << endl;
cout << "1. " << endl;
cout << "2. " << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//
ofstream fo(FILENAME, ios::trunc);//
fo.close();
if (this->m_cttArray != nullptr)
{
//
for (int i = 0; i < this->m_num; i++)
{
delete this->m_cttArray[i];
this->m_cttArray[i] = nullptr;
}
delete[] this->m_cttArray;
this->m_cttArray = nullptr;
this->m_num = 0;
this->m_fileIsEmpty = true;
}
cout << " !" << endl;
}
}
//
system("pause");
system("cls");
}
//
funcClass::~funcClass()
{
if (this->m_cttArray != nullptr)
{
for (int i = 0; i < this->m_num; i++)
{
if (this->m_cttArray[i] != nullptr)
{
delete this->m_cttArray[i];
}
}
delete[] this->m_cttArray;
this->m_cttArray = nullptr;
}
}
以前書いたC実装
前に双方向のチェーンテーブルで通信録を書いたことがありますが、単一のチェーンテーブルを考えて書いて、少し時間をかけてノックしました.今振り返ってみると、やはり評価しないほうがいいでしょう.しかし、削除するつもりはありません.これを黒い歴史にします.やはり基礎知識を補い続けなければならない.
#include
#include
#include
typedef struct Node
{
char name[100];
char addr[100];
char phone[100];
struct Node* next;
}*Pnode;
typedef struct List
{
Pnode head;//
Pnode tail;//
}*Plist;
//
Plist createList()
{
Plist plist = (Plist)malloc(sizeof(struct List));
if (plist == NULL)
{
printf(" plist
");
return NULL;
}
plist->head = (Pnode)malloc(sizeof(struct Node));
if (plist->head == NULL)
{
printf(" plist->head
");
free(plist);
return NULL;
}
plist->tail = (Pnode)malloc(sizeof(struct Node));
if (plist->head == NULL)
{
printf(" plist->tail
");
free(plist);
return NULL;
}
plist->tail = NULL;
plist->head = NULL;
return plist;
}
//1. //
void addNode(Plist list)
{
Pnode newNode = (Pnode)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("newNode
");
return;
}
//
char name[100], addr[100], phone[100];
printf(" :");
scanf_s("%s", name, 100);
strcpy_s(newNode->name, 100, name);
printf(" :");
scanf_s("%s", addr, 100);
strcpy_s(newNode->addr, 100, addr);
printf(" :");
scanf_s("%s", phone, 100);
strcpy_s(newNode->phone, 100, phone);
//1. ;2.
if (list->head == NULL)
{
list->head = newNode;
}
else
{
list->tail->next = newNode;
}
list->tail = newNode;
newNode->next = NULL;
system("cls");
printf("
");
}
//
int nodeLen(Plist list)
{
int len = 0;
Pnode newNode = list->head;
while (newNode != NULL)
{
len++;
newNode = newNode->next;
}
return len;
}
//2.
void showNode(Plist list)
{
Pnode newNode = list->head;
while (newNode != NULL)
{
printf(" :%s\t :%s\t :%s
", newNode->name, newNode->addr, newNode->phone);
newNode = newNode->next;
}
}
// ,
Pnode getNode(char name[], Plist newlist)
{
Pnode newNode = newlist->head;
while (strcmp(newNode->name, name) != 0)
{
newNode = newNode->next;
}
return newNode;
}
// ,
Pnode getNode2(Plist list, int len)
{
Pnode newNode = list->head;
int num = 1;
while(num!=len)
{
num++;
newNode = newNode->next;
}
return newNode;
}
// ,
int getNode3(Plist list, Pnode node)
{
Pnode newNode = list->head;
int len = 1;
while (strcmp(newNode->name,node->name) != 0)
{
len++;
newNode = newNode->next;
}
return len;
}
//
void delNode(Plist list)
{
printf("
");
char name[100];
scanf_s("%s", name, 100);
Pnode newNode = getNode(name, list);
int len = nodeLen(list);
if (list->head == newNode && len == 1) //
{
list->head = newNode->next;
}
else if (list->tail == newNode) //
{
list->tail = getNode2(list, len - 1);
list->tail->next = NULL;
}
else //
{
int num = getNode3(list, newNode);
Pnode node = getNode2(list, num - 1);
node->next = newNode->next;
}
free(newNode);
system("cls");
printf("
");
}
//4.
void upDate(Plist list)
{
printf(" :
");
char name[100];
scanf_s("%s", name, 100);
Pnode newNode = getNode(name, list); //
system("cls");
printf("1.
");
printf("2.
");
printf("3.
");
printf("4.
");
int key = 0;
scanf_s("%d", &key);
switch (key)
{
case 1:
system("cls");
printf(" :
");
char newname[100];
scanf_s("%s", newname, 100);
strcpy_s(newNode->name,100,newname);
printf(" !");
break;
case 2:
system("cls");
printf(" :
");
char newaddr[100];
scanf_s("%s", newaddr, 100);
strcpy_s(newNode->addr,100, newaddr);
printf(" !");
break;
case 3:
system("cls");
printf(" :
");
char newphone[100];
scanf_s("%s", newphone, 100);
strcpy_s(newNode->phone,100, newphone);
printf(" !");
case 4:
exit(1);
break;
}
}
void queryNode(Plist list)
{
printf(" :
");
char name[100];
scanf_s("%s", name, 100);
Pnode newNode = getNode(name, list); //
system("cls");
printf(" :%s\t :%s\t :%s
", newNode->name, newNode->addr, newNode->phone);
}
//
void Menu()
{
//1.
Plist newlist = createList();
if (NULL == newlist)
{
printf("
");
}
while (1)
{
system("cls");
printf("
1.
");
printf("2.
");
printf("3.
");
printf("4.
");
printf("5.
");
printf("6.
");
int key = 0;
scanf_s("%d", &key);
switch (key)
{
case 1:
system("cls"); addNode(newlist); system("pause");
break;
case 2:
system("cls"); showNode(newlist); system("pause");
break;
case 3:
system("cls"); delNode(newlist); system("pause");
break;
case 4:
system("cls"); upDate(newlist); system("pause");
break;
case 5:
system("cls"); queryNode(newlist); system("pause");
break;
case 6:
exit(1);
break;
}
}
}
// , 。 !
int main()
{
Menu();
return 0;
}