addressbook.cpp
#include "addressbook.h"
#include "ui_addressbook.h"
AddressBook::AddressBook(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AddressBook)
{
ui->setupUi(this);
}
AddressBook::~AddressBook()
{
delete ui;
}
void AddressBook::ReadFromFile()
{
QFile file("chenhc.txt");
if(!file.open(QIODevice::ReadWrite |QIODevice::Text))
qDebug() << file.errorString();
QTextStream in(&file);
//record类的成员变量数
int rcd_num = 5;
while(1)
{
QString str[rcd_num];
for(int i = 0; i < rcd_num; ++i)
{
str[i] = in.readLine();
}
//读取到的字符串长度为零,读取完毕
if(str[rcd_num-1].size() == 0) break;
//将新构造的record对象放入vector中
records.push_back(Record(str[0], str[1], str[2], str[3], str[4]));
}
file.close();
}
void AddressBook::SaveToFile()
{
QFile file("chenhc.txt");
if(!file.open(QIODevice::WriteOnly |QIODevice::Text))
qDebug() << file.errorString();
QTextStream in(&file);
//将记录信息按照格式以文件流的形式输出到文件中
for(int i = 0; i < records.size(); ++i)
{
in << records[i].name << "\n"
<< records[i].phone_num << "\n"
<< records[i].relation << "\n"
<< records[i].province << "\n"
<< records[i].city << "\n";
}
file.close();
}
//获取目标记录函数,以引用的形式返回,将所有目标record的索引放入vector中,idx中存储用户所选择的索引
bool AddressBook::GetTargetRcd(vector &id, int &idx)
{
bool ok;
//从输入窗口中读取关键字
QString keywords = QInputDialog::getText(this, tr("Processing..."), tr("Key Words: "),
QLineEdit::Normal, tr("ChenHC"), &ok);
//读取失败直接退出
if(!ok) return false;
//转化为string
string SrchStr = keywords.toStdString();
QString DisplyStr;
int cnt = 0;
//find those records, and store their id
for(int i = 0; i < records.size(); ++i)
{
//进行按关键字的查找比较
if((signed)records[i].name.toStdString().find(SrchStr) != -1
|| (signed)records[i].phone_num.toStdString().find(SrchStr) != -1
|| (signed)records[i].relation.toStdString().find(SrchStr) != -1
|| (signed)records[i].province.toStdString().find(SrchStr) != -1
|| (signed)records[i].city.toStdString().find(SrchStr) != -1)
{
id.push_back(i);
//transform int to char *
char num[105];
sprintf(num, "%d.\n", ++cnt);
//将记录信息链接成一个字符串输出
DisplyStr += QString(num) + "Name: " + records[i].name + '\n';
DisplyStr += "PhoneNum: " + records[i].phone_num + '\n';
DisplyStr += "Relation: " + records[i].relation + "\n";
DisplyStr += "Province: " + records[i].province + "\n";
DisplyStr += "City: " + records[i].city + "\n\n";
}
}
DisplyStr += "Please input the index:";
//没有匹配的record,弹出warning窗口
if(id.size() < 1)
{
QMessageBox::warning(this, tr("Warning"), tr("Your input is error!"),QMessageBox::Abort);
return false;
}
//读取用户输入索引
QString Idx = QInputDialog::getText(this, tr("Processing..."), tr(DisplyStr.toStdString().c_str()),
QLineEdit::Normal, tr("1"), &ok);
if(!ok) return false;
idx = Idx.toInt();
//do for error input
if(!(1 <= idx && idx <= (int)id.size()))
{
QMessageBox::warning(this, tr("Warning!"), tr("Your input is error!"),QMessageBox::Abort);
return false;
}
return true;
}
bool AddressBook::GetNewRcd(Record &newrcd)
{
bool ok;
//add_name
QString name = QInputDialog::getText(this, tr("Processing..."), tr("Name: "),
QLineEdit::Normal, tr("ChenHC"), &ok);
if(!ok) return false;
//add_phone_number
there:
QString PhoneNum = QInputDialog::getText(this, tr("Processing..."), tr("Phone Number: "),
QLineEdit::Normal, tr("110"), &ok);
bool check = true;
for(int i = 0; i < PhoneNum.size(); ++i)
if(!isdigit(PhoneNum.toStdString()[i])) check = false;
if(!check)
{
QMessageBox::warning(this, tr("Warning"), tr("Your input is not number!"),QMessageBox::Abort);
goto there;
//return false;
}
if(!ok) return false;
//add_relation
QStringList RelaList;
RelaList << tr("Relative") << tr("Classmate") << tr("Acquaintance") << tr("Others");
QString rela = QInputDialog::getItem(this, tr ("Processing..."), tr("Relation:"), RelaList, 0, false, &ok);
if(!ok) return false;
//add_province
QString prov = QInputDialog::getText(this, tr("Processing..."), tr("Province: "),
QLineEdit::Normal, tr("Zhejiang"), &ok);
if(!ok) return false;
//add_city
QString cit = QInputDialog::getText(this, tr("Processing..."), tr("City: "),
QLineEdit::Normal, tr("Jinhua"), &ok);
if(!ok) return false;
newrcd = Record(name, PhoneNum, rela, prov, cit);
return true;
}
//Add按钮槽函数
void AddressBook::on_AddBtn_clicked()
{
Record newrcd;
//读取新纪录
bool ok = GetNewRcd(newrcd);
if(!ok) return;
records.push_back(newrcd);
//弹出提示成功读取的窗口
QMessageBox::information(this, tr("Result"), tr("Success!"),QMessageBox::Ok);
//把改变后的记录存文件
SaveToFile();
}
//Delete按钮槽函数
void AddressBook::on_DelBtn_clicked()
{
vector id;
int idx;
//获取目标记录函数,以引用的形式返回,将所有目标record的索引放入vector中,idx中存储用户所选择的索引
bool ok = GetTargetRcd(id, idx);
if(!ok) return;
//删除用户所选择的记录
records.erase(records.begin() + id[idx-1]);
//弹出提示成功删除的窗口
QMessageBox::information(this, tr("Result"), tr("Success!"),QMessageBox::Ok);
SaveToFile();
}
//Display按钮槽函数
void AddressBook::on_DisplayBtn_clicked()
{
QString DisplyStr;
//记录按姓名字典序排序
sort(records.begin(), records.end());
//存文件
SaveToFile();
//链接所有的record信息
int cnt = 0;
for(int i = 0; i < records.size(); ++i, ++cnt)
{
DisplyStr += "Name: " + records[i].name + '\n';
DisplyStr += "PhoneNum: " + records[i].phone_num + '\n';
DisplyStr += "Relation: " + records[i].relation + "\n";
DisplyStr += "Province: " + records[i].province + "\n";
DisplyStr += "City: " + records[i].city + "\n\n";
}
//deal with plural
QString s;
if(cnt > 1) s = "s";
char num[105];
sprintf(num, "%d ", cnt);
//输出结果到界面上
DisplyStr = "There are " + QString(num) + "Record" + s + '\n' + DisplyStr;
ui->DisplayEdit->setPlainText(DisplyStr);
}
//Modify按钮槽函数
void AddressBook::on_ModifyBtn_clicked()
{
vector id;
int idx;
//以引用的形式返回,将所有目标record的索引放入vector中,idx中存储用户所选择的索引
bool ok = GetTargetRcd(id, idx);
if(!ok) return;
Record newrcd;
//读取新纪录
ok = GetNewRcd(newrcd);
if(!ok) return;
records[id[idx-1]] = newrcd;
//弹出提示成功修改的窗口
QMessageBox::information(this, tr("Result"), tr("Success!"),QMessageBox::Ok);
SaveToFile();
}
//当LineEdit内容被改变时做出响应的槽函数
void AddressBook::on_SrchEdit_textChanged(const QString &arg1)
{
string SrchStr = ui->SrchEdit->text().toStdString();
QString DisplyStr;
if(SrchStr.size() == 0)
{
ui->DisplayEdit->setPlainText(DisplyStr);
return;
}
//记录记录条数
int cnt = 0;
//按关键字进行查找
for(int i = 0; i < records.size(); ++i)
{
if((signed)records[i].name.toStdString().find(SrchStr) != -1
|| (signed)records[i].phone_num.toStdString().find(SrchStr) != -1
|| (signed)records[i].relation.toStdString().find(SrchStr) != -1
|| (signed)records[i].province.toStdString().find(SrchStr) != -1
|| (signed)records[i].city.toStdString().find(SrchStr) != -1)
{
++cnt;
DisplyStr += "Name: " + records[i].name + '\n';
DisplyStr += "PhoneNum: " + records[i].phone_num + '\n';
DisplyStr += "Relation: " + records[i].relation + "\n";
DisplyStr += "Province: " + records[i].province + "\n";
DisplyStr += "City: " + records[i].city + "\n\n";
}
}
//处理单复数
QString s;
if(cnt > 1) s = "s";
char num[105];
sprintf(num, "%d ", cnt);
//将查找到的符合关键字的record都显示到界面上
DisplyStr = "There are " + QString(num) + "Record" + s + '\n' + DisplyStr;
ui->DisplayEdit->setPlainText(DisplyStr);
}
//Help按钮槽函数
void AddressBook::on_HelpBtn_clicked()
{
QFile file("help.txt");
//打开help。txt文件
if(!file.open(QIODevice::ReadWrite |QIODevice::Text))
qDebug() << file.errorString();
QTextStream in(&file);
QString cur, s;
while(1)
{
cur = in.readLine();
if(cur.size() == 0) break;
s += cur + '\n';
}
//显示help文件内容到MessageBox中去
QMessageBox::information(this, tr("Help"), tr(s.toStdString().c_str()),QMessageBox::Ok);
file.close();
}
addressbook.h
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "record.h"
#include
namespace Ui {
class AddressBook;
}
class AddressBook : public QMainWindow
{
Q_OBJECT
public:
explicit AddressBook(QWidget *parent = 0);
~AddressBook();
void SaveToFile();
void ReadFromFile();
bool GetTargetRcd(vector &, int &);
bool GetNewRcd(Record &);
private slots:
void on_AddBtn_clicked();
void on_DelBtn_clicked();
void on_DisplayBtn_clicked();
void on_ModifyBtn_clicked();
void on_SrchEdit_textChanged(const QString &arg1);
void on_HelpBtn_clicked();
private:
Ui::AddressBook *ui;
QVector records;
};
#endif // ADDRESSBOOK_H
main.cpp
#include "addressbook.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AddressBook w;
//w.setObjectName("addressbook");
w.ReadFromFile();
//w.setStyleSheet("#addressbook{border-image:url(C://Users//hello//Desktop//bg.png);}");
w.show();
return a.exec();
}
record.cpp
#include "record.h"
Record::Record(QString s1, QString s2, QString s3, QString s4, QString s5)
: name(s1), phone_num(s2), relation(s3), province(s4), city(s5)
{
}
bool Record::operator<(const Record &rhs)
{
//transform name to standard
string a(name.toStdString()), b(rhs.name.toStdString());
for(char &ch : a)
{
ch = tolower(ch);
}
for(char &ch : b)
{
ch = tolower(ch);
}
return a < b;
}
record.h
#ifndef RECORD_H
#define RECORD_H
#include
using namespace std;
class Record
{
public:
Record() = default;
Record(QString, QString, QString, QString, QString);
QString name;
QString phone_num;
QString relation;
QString province;
QString city;
bool operator<(const Record &rhs);
};
#endif // RECORD_H