@chawuciren
2019-04-15T12:08:24.000000Z
字数 13733
阅读 946
C++
林子雯:ui设计,实现按学号顺序添加、按学号顺序显示、修改功能
陈亚:彩蛋设计,实现查找、删除功能
陈广扬:程序测试, debug
编辑器:QT
编译器:gcc
系统:Ubuntu 18.04
按学号顺序添加
按学号顺序显示
查找
删除
修改
单向链表
以下下为链表代码
class List{public:List(QString sid="0",QString sname="0",QString sphone="0",QString saddress="0");List(const List&Stu);void insertnode(List* node);//按顺序插入节点void deleteid(QString delete_ID,QLineEdit* result);//删除void print(QTableWidget* table);//显示页表格输出void print_search_table(QTableWidget* searchTable,List* temp);//查找页表格输出void modify(List*temp,QString sid,QString sname,QString sphone,QString saddress);//修改节点数据List* search(QString ID);//查找节点void set(QString sid,QString sname,QString sphone,QString saddress);//设置节点信息~List();private:QString ID;QString Name;QString Phone;QString Address;List* next;//后指针List* p;//前指针,但是并没有实现双向链表List* head;//存储头结点static int count;//计数};List::List(QString sid,QString sname,QString sphone,QString saddress){ID=sid;Name=sname;Phone=sphone;Address=saddress;next=nullptr;p=nullptr;head=nullptr;count++;}List::List(const List&Stu){ID=Stu.ID;Name=Stu.Name;Phone=Stu.Phone;Address=Stu.Address;next=Stu.next;head=Stu.head;p=Stu.p;count++;}void List::insertnode(List* node){if(head==nullptr){head=node;head->next=nullptr;head->p=nullptr;head->head=head;}else{bool ok;List* temp=head;List* t=head->next;int nodeID=node->ID.toInt(&ok,10);int ListID=temp->ID.toInt(&ok,10);if(nodeID<=ListID){node->next=head;head=node;}else{if(t==nullptr){head->next=node;node->next=nullptr;}else{int ListID_NEXT=t->ID.toInt(&ok,10);while((nodeID>ListID)&&(nodeID>ListID_NEXT)){temp=temp->next;t=t->next;if(t==nullptr){break;}ListID=temp->ID.toInt(&ok,10);//ListID_NEXT=t->ID.toInt(&ok,10);}node->next=temp->next;node->p=temp;temp->next=node;}}}}void List::deleteid(QString delete_ID,QLineEdit* result){List*temp=head;List*p1;List*p2=nullptr;if(temp==nullptr){result->setText("empty list QAQ");}else{p1=temp;while(delete_ID!=p1->ID&&p1->next!=nullptr){p2=p1;p1=p1->next;}if (delete_ID==p1->ID){if(delete_ID==temp->ID){temp=p1->next;head=temp;result->setText("success delete! ^-^ ");}else{p2->next=p1->next;result->setText("success delete! ^-^ ");}}else result->setText("404not found @_@");}}void List::print(QTableWidget* table){List*temp=head;int row=0;table->clearContents();//清空表while(temp!=nullptr){table->setItem(row,0,new QTableWidgetItem(temp->ID));table->setItem(row,1,new QTableWidgetItem(temp->Name));table->setItem(row,2,new QTableWidgetItem(temp->Phone));table->setItem(row,3,new QTableWidgetItem(temp->Address));temp=temp->next;row++;}}void List::print_search_table(QTableWidget *searchTable,List* temp){searchTable->setItem(0,0,new QTableWidgetItem(temp->ID));searchTable->setItem(0,1,new QTableWidgetItem(temp->Name));searchTable->setItem(0,2,new QTableWidgetItem(temp->Phone));searchTable->setItem(0,3,new QTableWidgetItem(temp->Address));}void List::modify(List*temp,QString sid,QString sname,QString sphone,QString saddress){if(sid!="") temp->ID=sid;if(sname!="") temp->Name=sname;if(sphone!="") temp->Phone=sphone;if(saddress!="") temp->Address=saddress;}void List::set(QString sid,QString sname,QString sphone,QString saddress){ID=sid;Name=sname;Phone=sphone;Address=saddress;}List* List::search(QString ID){List*temp=head;while(temp->ID!=ID){temp=temp->next;if(temp==nullptr){return nullptr;}}return temp;}List::~List(){count--;}int List::count=0;
在QT的ui界面使用stack widget
在实现时调用函数
ui->stackedWidget->setCurrentIndex(0);
ui界面设置LineEdit,实现时在确认按钮的槽函数中调用函数ui->lineEdit_6->text();读取输入,存入链表。
ui设置tablewidget,输出时调用ui->tablewidget->setItem(row,col,new QTableWidgetItem(""));设置表格信息。
实现见上一条
包括查找、删除不存在的节点,对空链表删除的信息提示
#-------------------------------------------------## Project created by QtCreator 2019-04-14T14:45:20##-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = address_01TEMPLATE = app# The following define makes your compiler emit warnings if you use# any feature of Qt which has been marked as deprecated (the exact warnings# depend on your compiler). Please consult the documentation of the# deprecated API in order to know how to port your code away from it.DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.# In order to do so, uncomment the following line.# You can also select to disable deprecated APIs only up to a certain version of Qt.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0CONFIG += c++11SOURCES += \main.cpp \mainwindow.cpp \image.cpp \coloregg.cppHEADERS += \mainwindow.h \coloregg.hFORMS += \mainwindow.ui \coloregg.ui# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += targetRESOURCES += \image.qrc
#ifndef COLOREGG_H#define COLOREGG_H#include <QWidget>#include <QMovie>namespace Ui {class coloregg;}class coloregg : public QWidget{Q_OBJECTpublic:explicit coloregg(QWidget *parent = nullptr);~coloregg();private slots:void on_okbut_clicked();signals:void signalshowmenu();private:Ui::coloregg *ui;void Init();};#endif // COLOREGG_H
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include<QMessageBox>#include<QTableWidget>#include<QDebug>#include<QMenu>#include<QMenuBar>#include<QAction>#include<QDialog>#include<QLabel>#include<QPalette>#include"coloregg.h"namespace Ui {class MainWindow;}class List{public:List(QString sid="0",QString sname="0",QString sphone="0",QString saddress="0");List(const List&Stu);void insertnode(List* node);void deleteid(QString delete_ID,QLineEdit* result);void print(QTableWidget* table);void print_search_table(QTableWidget* searchTable,List* temp);void modify(List*temp,QString sid,QString sname,QString sphone,QString saddress);List* search(QString ID);void set(QString sid,QString sname,QString sphone,QString saddress);~List();private:QString ID;QString Name;QString Phone;QString Address;List* next;List* p;List* head;static int count;};class MainWindow : public QMainWindow{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_4_clicked();void on_pushButton_3_clicked();void on_pushButton_5_clicked();void on_pushButton_8_clicked();void on_pushButton_10_clicked();void on_pushButton_6_clicked();void on_pushButton_11_clicked();void on_pushButton_7_clicked();void on_pushButton_9_clicked();void on_pushButton_12_clicked();void on_pushButton_13_clicked();void dealdocoloregg();private:Ui::MainWindow *ui;List* Head;coloregg a;};#endif // MAINWINDOW_H
#include "coloregg.h"#include "ui_coloregg.h"coloregg::coloregg(QWidget *parent) :QWidget(parent),ui(new Ui::coloregg){ui->setupUi(this);Init();}coloregg::~coloregg(){delete ui;}void coloregg::Init(){QMovie *movie=new QMovie("/home/chawuciren/chawuciren/Coding2019/address_CY/source.gif");movie->start();ui->label->setMovie(movie);ui->label->setScaledContents(true);connect(ui->okbut,&QPushButton::click,this,&coloregg::on_okbut_clicked);}void coloregg::on_okbut_clicked(){emit signalshowmenu();}
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);MainWindow w;w.show();return a.exec();}
#include "mainwindow.h"#include "ui_mainwindow.h"/////////////////////////////////链表类实现////////////////////////////////////////List::List(QString sid,QString sname,QString sphone,QString saddress){ID=sid;Name=sname;Phone=sphone;Address=saddress;next=nullptr;p=nullptr;head=nullptr;count++;}List::List(const List&Stu){ID=Stu.ID;Name=Stu.Name;Phone=Stu.Phone;Address=Stu.Address;next=Stu.next;head=Stu.head;p=Stu.p;count++;}void List::insertnode(List* node){if(head==nullptr){head=node;head->next=nullptr;head->p=nullptr;head->head=head;}else{bool ok;//List* temp=head;List* t=head->next;int nodeID=node->ID.toInt(&ok,10);//int ListID=temp->ID.toInt(&ok,10);//if(nodeID<=ListID){node->next=head;head=node;}else{if(t==nullptr){head->next=node;node->next=nullptr;}else{int ListID_NEXT=t->ID.toInt(&ok,10);while((nodeID>ListID)&&(nodeID>ListID_NEXT)){temp=temp->next;t=t->next;if(t==nullptr){break;}ListID=temp->ID.toInt(&ok,10);//ListID_NEXT=t->ID.toInt(&ok,10);}node->next=temp->next;node->p=temp;temp->next=node;}}}}void List::deleteid(QString delete_ID,QLineEdit* result){List*temp=head;List*p1;List*p2=nullptr;//int row=0;if(temp==nullptr){//table_3->setItem(row,0,new QTableWidgetItem("empty list"));result->setText("empty list QAQ");}else{p1=temp;while(delete_ID!=p1->ID&&p1->next!=nullptr){p2=p1;p1=p1->next;}if (delete_ID==p1->ID){if(delete_ID==temp->ID){temp=p1->next;head=temp;result->setText("success delete! ^-^ ");}else{p2->next=p1->next;result->setText("success delete! ^-^ ");}}else result->setText("404not found @_@");}}void List::print(QTableWidget* table){List*temp=head;int row=0;table->clearContents();//int iLen = table->rowCount();//for(int i=0;i<iLen;i++)//{//table->removeRow(i);//}//int row=table->rowCount();while(temp!=nullptr){//table->insertRow(row);table->setItem(row,0,new QTableWidgetItem(temp->ID));table->setItem(row,1,new QTableWidgetItem(temp->Name));table->setItem(row,2,new QTableWidgetItem(temp->Phone));table->setItem(row,3,new QTableWidgetItem(temp->Address));temp=temp->next;row++;}}void List::print_search_table(QTableWidget *searchTable,List* temp){searchTable->setItem(0,0,new QTableWidgetItem(temp->ID));searchTable->setItem(0,1,new QTableWidgetItem(temp->Name));searchTable->setItem(0,2,new QTableWidgetItem(temp->Phone));searchTable->setItem(0,3,new QTableWidgetItem(temp->Address));}void List::modify(List*temp,QString sid,QString sname,QString sphone,QString saddress){if(sid!="") temp->ID=sid;if(sname!="") temp->Name=sname;if(sphone!="") temp->Phone=sphone;if(saddress!="") temp->Address=saddress;}void List::set(QString sid,QString sname,QString sphone,QString saddress){ID=sid;Name=sname;Phone=sphone;Address=saddress;}List* List::search(QString ID){List*temp=head;while(temp->ID!=ID){temp=temp->next;if(temp==nullptr){return nullptr;}}return temp;}List::~List(){count--;}int List::count=0;
////////////////////////////////主窗口构造函数///////////////////////////////////////////////////////////////////////包括对控件的状态设置,背景设置,彩蛋处理设置///////////////////////////////////////MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){//ui控件设置ui->setupUi(this);QStringList header;header<<"Name"<<"ID"<<"Phone"<<"Address";ui->tableWidget->setHorizontalHeaderLabels(header);ui->tableWidget->setRowCount(10);ui->tableWidget->setColumnCount(4);ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);ui->tableWidget_2->setHorizontalHeaderLabels(header);ui->tableWidget_2->setRowCount(1);ui->tableWidget_2->setColumnCount(4);ui->tableWidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);//backgroundQPalette pal = this->palette();pal.setBrush(backgroundRole(), QPixmap("://image/bg3.jpg"));setPalette(pal);//初始化链表Head=new List;//彩蛋信号处理connect(&a,&coloregg::signalshowmenu,this,&MainWindow::dealdocoloregg);}MainWindow::~MainWindow()//主窗口析构函数{delete ui;}////////////////////////////////彩蛋处理信号的槽函数///////////////////////////////////////void MainWindow::dealdocoloregg(){a.close();this->show();}//////////////////////////////主窗口的槽函数,主要实现页面切换,信息读取、显示等主页面功能//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////页面切换///////////////////////////////////////void MainWindow::on_pushButton_clicked(){ui->stackedWidget->setCurrentIndex(1);}void MainWindow::on_pushButton_3_clicked(){ui->stackedWidget->setCurrentIndex(0);}void MainWindow::on_pushButton_5_clicked(){ui->stackedWidget->setCurrentIndex(5);}void MainWindow::on_pushButton_8_clicked(){ui->stackedWidget->setCurrentIndex(0);}void MainWindow::on_pushButton_7_clicked(){ui->stackedWidget->setCurrentIndex(4);}void MainWindow::on_pushButton_12_clicked(){ui->stackedWidget->setCurrentIndex(0);}void MainWindow::on_pushButton_13_clicked(){ui->stackedWidget->setCurrentIndex(0);}void MainWindow::on_pushButton_6_clicked(){ui->stackedWidget->setCurrentIndex(3);}
图片:
关联函数
/////////////////////////////显示页输出信息///////////////////////////////////////void MainWindow::on_pushButton_2_clicked(){ui->stackedWidget->setCurrentIndex(2);Head->print(ui->tableWidget);}
图片:
关联函数:
//////////////////////////添加页读取信息////////////////////////////////////////////void MainWindow::on_pushButton_4_clicked(){QString ID=ui->lineEdit->text();QString Name=ui->lineEdit_2->text();QString Phone=ui->lineEdit_3->text();QString Address=ui->lineEdit_4->text();List *Stu=new List(ID,Name,Phone,Address);Head->insertnode(Stu);}
图片:
关联函数:
////////////////////////////////查找页读取、显示信息//////////////////////////////////void MainWindow::on_pushButton_10_clicked(){QString ID=ui->lineEdit_10->text();List* temp=Head->search(ID);if(temp!=nullptr){Head->print_search_table(ui->tableWidget_2,temp);}else{ui->tableWidget_2->setItem(0,0,new QTableWidgetItem("404"));ui->tableWidget_2->setItem(0,1,new QTableWidgetItem("not"));ui->tableWidget_2->setItem(0,2,new QTableWidgetItem("found"));ui->tableWidget_2->setItem(0,3,new QTableWidgetItem("TAT"));}a.show();}
图片:
关联函数
//////////////////////////////删除页读取、删除信息/////////////////////////////void MainWindow::on_pushButton_11_clicked(){QString delete_id=ui->lineEdit_11->text();Head->deleteid( delete_id,ui->lineEdit_12);}
图片:
关联函数:
//////////////////////////////修改页读取信息/////////////////////////////////void MainWindow::on_pushButton_9_clicked(){QString ID_point=ui->lineEdit_5->text();List*temp=Head->search(ID_point);if(temp!=nullptr){QString ID=ui->lineEdit_6->text();QString Name=ui->lineEdit_7->text();QString Phone=ui->lineEdit_8->text();QString Address=ui->lineEdit_9->text();Head->modify(temp,ID,Name,Phone,Address);}else{qDebug()<<"404";}ui->stackedWidget->setCurrentIndex(0);}

