[关闭]
@chawuciren 2019-04-15T12:08:24.000000Z 字数 13733 阅读 656

第五组通讯录实验报告

C++


成员分工

林子雯:ui设计,实现按学号顺序添加、按学号顺序显示、修改功能
陈亚:彩蛋设计,实现查找、删除功能
陈广扬:程序测试, debug

开发环境

编辑器:QT
编译器:gcc
系统:Ubuntu 18.04

主要功能

按学号顺序添加
按学号顺序显示
查找
删除
修改

存储结构

单向链表
以下下为链表代码

  1. class List{
  2. public:
  3. List(QString sid="0",QString sname="0",QString sphone="0",QString saddress="0");
  4. List(const List&Stu);
  5. void insertnode(List* node);//按顺序插入节点
  6. void deleteid(QString delete_ID,QLineEdit* result);//删除
  7. void print(QTableWidget* table);//显示页表格输出
  8. void print_search_table(QTableWidget* searchTable,List* temp);//查找页表格输出
  9. void modify(List*temp,QString sid,QString sname,QString sphone,QString saddress);//修改节点数据
  10. List* search(QString ID);//查找节点
  11. void set(QString sid,QString sname,QString sphone,QString saddress);//设置节点信息
  12. ~List();
  13. private:
  14. QString ID;
  15. QString Name;
  16. QString Phone;
  17. QString Address;
  18. List* next;//后指针
  19. List* p;//前指针,但是并没有实现双向链表
  20. List* head;//存储头结点
  21. static int count;//计数
  22. };
  23. List::List(QString sid,QString sname,QString sphone,QString saddress){
  24. ID=sid;
  25. Name=sname;
  26. Phone=sphone;
  27. Address=saddress;
  28. next=nullptr;
  29. p=nullptr;
  30. head=nullptr;
  31. count++;
  32. }
  33. List::List(const List&Stu){
  34. ID=Stu.ID;
  35. Name=Stu.Name;
  36. Phone=Stu.Phone;
  37. Address=Stu.Address;
  38. next=Stu.next;
  39. head=Stu.head;
  40. p=Stu.p;
  41. count++;
  42. }
  43. void List::insertnode(List* node){
  44. if(head==nullptr){
  45. head=node;
  46. head->next=nullptr;
  47. head->p=nullptr;
  48. head->head=head;
  49. }
  50. else{
  51. bool ok;
  52. List* temp=head;
  53. List* t=head->next;
  54. int nodeID=node->ID.toInt(&ok,10);
  55. int ListID=temp->ID.toInt(&ok,10);
  56. if(nodeID<=ListID){
  57. node->next=head;
  58. head=node;
  59. }
  60. else{
  61. if(t==nullptr){
  62. head->next=node;
  63. node->next=nullptr;
  64. }else{
  65. int ListID_NEXT=t->ID.toInt(&ok,10);
  66. while((nodeID>ListID)&&(nodeID>ListID_NEXT)){
  67. temp=temp->next;
  68. t=t->next;
  69. if(t==nullptr){
  70. break;
  71. }
  72. ListID=temp->ID.toInt(&ok,10);//
  73. ListID_NEXT=t->ID.toInt(&ok,10);
  74. }
  75. node->next=temp->next;
  76. node->p=temp;
  77. temp->next=node;
  78. }
  79. }
  80. }
  81. }
  82. void List::deleteid(QString delete_ID,QLineEdit* result){
  83. List*temp=head;
  84. List*p1;
  85. List*p2=nullptr;
  86. if(temp==nullptr){
  87. result->setText("empty list QAQ");
  88. }
  89. else{
  90. p1=temp;
  91. while(delete_ID!=p1->ID&&p1->next!=nullptr){
  92. p2=p1;
  93. p1=p1->next;
  94. }
  95. if (delete_ID==p1->ID){
  96. if(delete_ID==temp->ID){
  97. temp=p1->next;
  98. head=temp;
  99. result->setText("success delete! ^-^ ");
  100. }
  101. else{
  102. p2->next=p1->next;
  103. result->setText("success delete! ^-^ ");
  104. }
  105. }
  106. else result->setText("404not found @_@");
  107. }
  108. }
  109. void List::print(QTableWidget* table){
  110. List*temp=head;
  111. int row=0;
  112. table->clearContents();//清空表
  113. while(temp!=nullptr){
  114. table->setItem(row,0,new QTableWidgetItem(temp->ID));
  115. table->setItem(row,1,new QTableWidgetItem(temp->Name));
  116. table->setItem(row,2,new QTableWidgetItem(temp->Phone));
  117. table->setItem(row,3,new QTableWidgetItem(temp->Address));
  118. temp=temp->next;
  119. row++;
  120. }
  121. }
  122. void List::print_search_table(QTableWidget *searchTable,List* temp){
  123. searchTable->setItem(0,0,new QTableWidgetItem(temp->ID));
  124. searchTable->setItem(0,1,new QTableWidgetItem(temp->Name));
  125. searchTable->setItem(0,2,new QTableWidgetItem(temp->Phone));
  126. searchTable->setItem(0,3,new QTableWidgetItem(temp->Address));
  127. }
  128. void List::modify(List*temp,QString sid,QString sname,QString sphone,QString saddress){
  129. if(sid!="") temp->ID=sid;
  130. if(sname!="") temp->Name=sname;
  131. if(sphone!="") temp->Phone=sphone;
  132. if(saddress!="") temp->Address=saddress;
  133. }
  134. void List::set(QString sid,QString sname,QString sphone,QString saddress){
  135. ID=sid;
  136. Name=sname;
  137. Phone=sphone;
  138. Address=saddress;
  139. }
  140. List* List::search(QString ID){
  141. List*temp=head;
  142. while(temp->ID!=ID){
  143. temp=temp->next;
  144. if(temp==nullptr){
  145. return nullptr;
  146. }
  147. }
  148. return temp;
  149. }
  150. List::~List(){
  151. count--;
  152. }
  153. int List::count=0;

关键技术和技术难点

1.实现页面切换

在QT的ui界面使用stack widget
在实现时调用函数

  1. ui->stackedWidget->setCurrentIndex(0);

2.读取存储用户输入并输出至表格中

ui界面设置LineEdit,实现时在确认按钮的槽函数中调用函数ui->lineEdit_6->text();读取输入,存入链表。
ui设置tablewidget,输出时调用ui->tablewidget->setItem(row,col,new QTableWidgetItem(""));设置表格信息。

3.链表类的实现

实现见上一条

4.错误信息设置

包括查找、删除不存在的节点,对空链表删除的信息提示

5.子窗口实现彩蛋设计

源代码

address_01.pro

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2019-04-14T14:45:20
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = address_01
  9. TEMPLATE = app
  10. # The following define makes your compiler emit warnings if you use
  11. # any feature of Qt which has been marked as deprecated (the exact warnings
  12. # depend on your compiler). Please consult the documentation of the
  13. # deprecated API in order to know how to port your code away from it.
  14. DEFINES += QT_DEPRECATED_WARNINGS
  15. # You can also make your code fail to compile if you use deprecated APIs.
  16. # In order to do so, uncomment the following line.
  17. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  18. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  19. CONFIG += c++11
  20. SOURCES += \
  21. main.cpp \
  22. mainwindow.cpp \
  23. image.cpp \
  24. coloregg.cpp
  25. HEADERS += \
  26. mainwindow.h \
  27. coloregg.h
  28. FORMS += \
  29. mainwindow.ui \
  30. coloregg.ui
  31. # Default rules for deployment.
  32. qnx: target.path = /tmp/$${TARGET}/bin
  33. else: unix:!android: target.path = /opt/$${TARGET}/bin
  34. !isEmpty(target.path): INSTALLS += target
  35. RESOURCES += \
  36. image.qrc

coloregg.h(彩蛋页头文件)

  1. #ifndef COLOREGG_H
  2. #define COLOREGG_H
  3. #include <QWidget>
  4. #include <QMovie>
  5. namespace Ui {
  6. class coloregg;
  7. }
  8. class coloregg : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit coloregg(QWidget *parent = nullptr);
  13. ~coloregg();
  14. private slots:
  15. void on_okbut_clicked();
  16. signals:
  17. void signalshowmenu();
  18. private:
  19. Ui::coloregg *ui;
  20. void Init();
  21. };
  22. #endif // COLOREGG_H

mainwindow.h(功能页头文件)

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include<QMessageBox>
  5. #include<QTableWidget>
  6. #include<QDebug>
  7. #include<QMenu>
  8. #include<QMenuBar>
  9. #include<QAction>
  10. #include<QDialog>
  11. #include<QLabel>
  12. #include<QPalette>
  13. #include"coloregg.h"
  14. namespace Ui {
  15. class MainWindow;
  16. }
  17. class List{
  18. public:
  19. List(QString sid="0",QString sname="0",QString sphone="0",QString saddress="0");
  20. List(const List&Stu);
  21. void insertnode(List* node);
  22. void deleteid(QString delete_ID,QLineEdit* result);
  23. void print(QTableWidget* table);
  24. void print_search_table(QTableWidget* searchTable,List* temp);
  25. void modify(List*temp,QString sid,QString sname,QString sphone,QString saddress);
  26. List* search(QString ID);
  27. void set(QString sid,QString sname,QString sphone,QString saddress);
  28. ~List();
  29. private:
  30. QString ID;
  31. QString Name;
  32. QString Phone;
  33. QString Address;
  34. List* next;
  35. List* p;
  36. List* head;
  37. static int count;
  38. };
  39. class MainWindow : public QMainWindow
  40. {
  41. Q_OBJECT
  42. public:
  43. explicit MainWindow(QWidget *parent = nullptr);
  44. ~MainWindow();
  45. private slots:
  46. void on_pushButton_clicked();
  47. void on_pushButton_2_clicked();
  48. void on_pushButton_4_clicked();
  49. void on_pushButton_3_clicked();
  50. void on_pushButton_5_clicked();
  51. void on_pushButton_8_clicked();
  52. void on_pushButton_10_clicked();
  53. void on_pushButton_6_clicked();
  54. void on_pushButton_11_clicked();
  55. void on_pushButton_7_clicked();
  56. void on_pushButton_9_clicked();
  57. void on_pushButton_12_clicked();
  58. void on_pushButton_13_clicked();
  59. void dealdocoloregg();
  60. private:
  61. Ui::MainWindow *ui;
  62. List* Head;
  63. coloregg a;
  64. };
  65. #endif // MAINWINDOW_H

coloregg.cpp(彩蛋页实现)

  1. #include "coloregg.h"
  2. #include "ui_coloregg.h"
  3. coloregg::coloregg(QWidget *parent) :
  4. QWidget(parent),
  5. ui(new Ui::coloregg)
  6. {
  7. ui->setupUi(this);
  8. Init();
  9. }
  10. coloregg::~coloregg()
  11. {
  12. delete ui;
  13. }
  14. void coloregg::Init(){
  15. QMovie *movie=new QMovie("/home/chawuciren/chawuciren/Coding2019/address_CY/source.gif");
  16. movie->start();
  17. ui->label->setMovie(movie);
  18. ui->label->setScaledContents(true);
  19. connect(ui->okbut,&QPushButton::click,this,&coloregg::on_okbut_clicked);
  20. }
  21. void coloregg::on_okbut_clicked()
  22. {
  23. emit signalshowmenu();
  24. }

main.cpp(主函数)

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MainWindow w;
  7. w.show();
  8. return a.exec();
  9. }

mainwindow.cpp(功能页实现)

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. /////////////////////////////////链表类实现////////////////////////////////////////
  4. List::List(QString sid,QString sname,QString sphone,QString saddress){
  5. ID=sid;
  6. Name=sname;
  7. Phone=sphone;
  8. Address=saddress;
  9. next=nullptr;
  10. p=nullptr;
  11. head=nullptr;
  12. count++;
  13. }
  14. List::List(const List&Stu){
  15. ID=Stu.ID;
  16. Name=Stu.Name;
  17. Phone=Stu.Phone;
  18. Address=Stu.Address;
  19. next=Stu.next;
  20. head=Stu.head;
  21. p=Stu.p;
  22. count++;
  23. }
  24. void List::insertnode(List* node){
  25. if(head==nullptr){
  26. head=node;
  27. head->next=nullptr;
  28. head->p=nullptr;
  29. head->head=head;
  30. }
  31. else{
  32. bool ok;//
  33. List* temp=head;
  34. List* t=head->next;
  35. int nodeID=node->ID.toInt(&ok,10);//
  36. int ListID=temp->ID.toInt(&ok,10);//
  37. if(nodeID<=ListID){
  38. node->next=head;
  39. head=node;
  40. }
  41. else{
  42. if(t==nullptr){
  43. head->next=node;
  44. node->next=nullptr;
  45. }else{
  46. int ListID_NEXT=t->ID.toInt(&ok,10);
  47. while((nodeID>ListID)&&(nodeID>ListID_NEXT)){
  48. temp=temp->next;
  49. t=t->next;
  50. if(t==nullptr){
  51. break;
  52. }
  53. ListID=temp->ID.toInt(&ok,10);//
  54. ListID_NEXT=t->ID.toInt(&ok,10);
  55. }
  56. node->next=temp->next;
  57. node->p=temp;
  58. temp->next=node;
  59. }
  60. }
  61. }
  62. }
  63. void List::deleteid(QString delete_ID,QLineEdit* result){
  64. List*temp=head;
  65. List*p1;
  66. List*p2=nullptr;
  67. //int row=0;
  68. if(temp==nullptr){
  69. //table_3->setItem(row,0,new QTableWidgetItem("empty list"));
  70. result->setText("empty list QAQ");
  71. }
  72. else{
  73. p1=temp;
  74. while(delete_ID!=p1->ID&&p1->next!=nullptr){
  75. p2=p1;
  76. p1=p1->next;
  77. }
  78. if (delete_ID==p1->ID){
  79. if(delete_ID==temp->ID){
  80. temp=p1->next;
  81. head=temp;
  82. result->setText("success delete! ^-^ ");
  83. }
  84. else{
  85. p2->next=p1->next;
  86. result->setText("success delete! ^-^ ");
  87. }
  88. }
  89. else result->setText("404not found @_@");
  90. }
  91. }
  92. void List::print(QTableWidget* table){
  93. List*temp=head;
  94. int row=0;
  95. table->clearContents();
  96. //int iLen = table->rowCount();
  97. //for(int i=0;i<iLen;i++)
  98. //{
  99. //table->removeRow(i);
  100. //}
  101. //int row=table->rowCount();
  102. while(temp!=nullptr){
  103. //table->insertRow(row);
  104. table->setItem(row,0,new QTableWidgetItem(temp->ID));
  105. table->setItem(row,1,new QTableWidgetItem(temp->Name));
  106. table->setItem(row,2,new QTableWidgetItem(temp->Phone));
  107. table->setItem(row,3,new QTableWidgetItem(temp->Address));
  108. temp=temp->next;
  109. row++;
  110. }
  111. }
  112. void List::print_search_table(QTableWidget *searchTable,List* temp){
  113. searchTable->setItem(0,0,new QTableWidgetItem(temp->ID));
  114. searchTable->setItem(0,1,new QTableWidgetItem(temp->Name));
  115. searchTable->setItem(0,2,new QTableWidgetItem(temp->Phone));
  116. searchTable->setItem(0,3,new QTableWidgetItem(temp->Address));
  117. }
  118. void List::modify(List*temp,QString sid,QString sname,QString sphone,QString saddress){
  119. if(sid!="") temp->ID=sid;
  120. if(sname!="") temp->Name=sname;
  121. if(sphone!="") temp->Phone=sphone;
  122. if(saddress!="") temp->Address=saddress;
  123. }
  124. void List::set(QString sid,QString sname,QString sphone,QString saddress){
  125. ID=sid;
  126. Name=sname;
  127. Phone=sphone;
  128. Address=saddress;
  129. }
  130. List* List::search(QString ID){
  131. List*temp=head;
  132. while(temp->ID!=ID){
  133. temp=temp->next;
  134. if(temp==nullptr){
  135. return nullptr;
  136. }
  137. }
  138. return temp;
  139. }
  140. List::~List(){
  141. count--;
  142. }
  143. int List::count=0;
  1. ////////////////////////////////主窗口构造函数///////////////////////////////////////
  2. ////////////////////////////////包括对控件的状态设置,背景设置,彩蛋处理设置///////////////////////////////////////
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow)
  6. {
  7. //ui控件设置
  8. ui->setupUi(this);
  9. QStringList header;
  10. header<<"Name"<<"ID"<<"Phone"<<"Address";
  11. ui->tableWidget->setHorizontalHeaderLabels(header);
  12. ui->tableWidget->setRowCount(10);
  13. ui->tableWidget->setColumnCount(4);
  14. ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
  15. ui->tableWidget_2->setHorizontalHeaderLabels(header);
  16. ui->tableWidget_2->setRowCount(1);
  17. ui->tableWidget_2->setColumnCount(4);
  18. ui->tableWidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);
  19. //background
  20. QPalette pal = this->palette();
  21. pal.setBrush(backgroundRole(), QPixmap("://image/bg3.jpg"));
  22. setPalette(pal);
  23. //初始化链表
  24. Head=new List;
  25. //彩蛋信号处理
  26. connect(&a,&coloregg::signalshowmenu,this,&MainWindow::dealdocoloregg);
  27. }
  28. MainWindow::~MainWindow()//主窗口析构函数
  29. {
  30. delete ui;
  31. }
  32. ////////////////////////////////彩蛋处理信号的槽函数///////////////////////////////////////
  33. void MainWindow::dealdocoloregg(){
  34. a.close();
  35. this->show();
  36. }
  37. //////////////////////////////主窗口的槽函数,主要实现页面切换,信息读取、显示等主页面功能/////////////////////////////////////////////////////////////////////////////////
  38. /////////////////////////////////////页面切换///////////////////////////////////////
  39. void MainWindow::on_pushButton_clicked()
  40. {
  41. ui->stackedWidget->setCurrentIndex(1);
  42. }
  43. void MainWindow::on_pushButton_3_clicked()
  44. {
  45. ui->stackedWidget->setCurrentIndex(0);
  46. }
  47. void MainWindow::on_pushButton_5_clicked()
  48. {
  49. ui->stackedWidget->setCurrentIndex(5);
  50. }
  51. void MainWindow::on_pushButton_8_clicked()
  52. {
  53. ui->stackedWidget->setCurrentIndex(0);
  54. }
  55. void MainWindow::on_pushButton_7_clicked()
  56. {
  57. ui->stackedWidget->setCurrentIndex(4);
  58. }
  59. void MainWindow::on_pushButton_12_clicked()
  60. {
  61. ui->stackedWidget->setCurrentIndex(0);
  62. }
  63. void MainWindow::on_pushButton_13_clicked()
  64. {
  65. ui->stackedWidget->setCurrentIndex(0);
  66. }
  67. void MainWindow::on_pushButton_6_clicked()
  68. {
  69. ui->stackedWidget->setCurrentIndex(3);
  70. }

显示页

图片:
p3.png-5.8kB
关联函数

  1. /////////////////////////////显示页输出信息///////////////////////////////////////
  2. void MainWindow::on_pushButton_2_clicked()
  3. {
  4. ui->stackedWidget->setCurrentIndex(2);
  5. Head->print(ui->tableWidget);
  6. }

添加页

图片:
p2.png-13.7kB
关联函数:

  1. //////////////////////////添加页读取信息////////////////////////////////////////////
  2. void MainWindow::on_pushButton_4_clicked()
  3. {
  4. QString ID=ui->lineEdit->text();
  5. QString Name=ui->lineEdit_2->text();
  6. QString Phone=ui->lineEdit_3->text();
  7. QString Address=ui->lineEdit_4->text();
  8. List *Stu=new List(ID,Name,Phone,Address);
  9. Head->insertnode(Stu);
  10. }

查找页

图片:
p4.png-13.8kB
关联函数:

  1. ////////////////////////////////查找页读取、显示信息//////////////////////////////////
  2. void MainWindow::on_pushButton_10_clicked()
  3. {
  4. QString ID=ui->lineEdit_10->text();
  5. List* temp=Head->search(ID);
  6. if(temp!=nullptr){
  7. Head->print_search_table(ui->tableWidget_2,temp);
  8. }
  9. else{
  10. ui->tableWidget_2->setItem(0,0,new QTableWidgetItem("404"));
  11. ui->tableWidget_2->setItem(0,1,new QTableWidgetItem("not"));
  12. ui->tableWidget_2->setItem(0,2,new QTableWidgetItem("found"));
  13. ui->tableWidget_2->setItem(0,3,new QTableWidgetItem("TAT"));
  14. }
  15. a.show();
  16. }

删除页

图片:
p5.png-12.9kB
关联函数

  1. //////////////////////////////删除页读取、删除信息/////////////////////////////
  2. void MainWindow::on_pushButton_11_clicked()
  3. {
  4. QString delete_id=ui->lineEdit_11->text();
  5. Head->deleteid( delete_id,ui->lineEdit_12);
  6. }

修改页

图片:
p6.png-20.3kB
关联函数:

  1. //////////////////////////////修改页读取信息/////////////////////////////////
  2. void MainWindow::on_pushButton_9_clicked()
  3. {
  4. QString ID_point=ui->lineEdit_5->text();
  5. List*temp=Head->search(ID_point);
  6. if(temp!=nullptr){
  7. QString ID=ui->lineEdit_6->text();
  8. QString Name=ui->lineEdit_7->text();
  9. QString Phone=ui->lineEdit_8->text();
  10. QString Address=ui->lineEdit_9->text();
  11. Head->modify(temp,ID,Name,Phone,Address);
  12. }
  13. else{
  14. qDebug()<<"404";
  15. }
  16. ui->stackedWidget->setCurrentIndex(0);
  17. }

ui设计

彩蛋页

color.png-6.5kB

功能页

p1.png-14kB
p2.png-13.7kB
p3.png-5.8kB
p4.png-13.8kB
p5.png-12.9kB
p6.png-20.3kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注