课程设计《题目二》

/定义类和结构体//

#define tongxunlu_H 
#include <iostream>  
#include <iomanip>  
using namespace std;  

struct Bir
{
	    int year;
	    int month;
	    int day;
};

struct Stu
{
	    char *name;
	    char *sex;
	    char *address;
 	    int code;
	    int age;
	    char *number;
	    char *QQ;
	    char *wechat;
	    struct Bir birthday;
};

template <class DT>  
struct Node  
{  
	DT data;  
    Node<DT> *next;//*prior;  
};  
  
template <class DT>  
class Student  
{  
private:  
    int length;  
    Node<DT> *first;  
public:  
    Student();  
    void Init();                                     //输入n个学生数据  
    void Insert();                                  //在表中s的位置插入x  
    void Find_Loc();                               //在表中查找序号为i的元素  
    void Find_Val();                            //在表中查找值与x相等的元素  
    void Del();                              //在表中删除序号为s的元素  
    void Destroy();                                   //删除成绩表  
    void Show();                              //把表中所有元素打印出来  
    void Choice_menu();
   
};  
  
#endif  

/定义成员函数/

#include "tongxunlu.h"
#include <iostream>
#include <iomanip>
using namespace std;




template <class DT>  
Student<DT>::Student()  
{  
    first = new Node<DT>;  
    first->next = NULL;  
    length = 0;  
}  
  
template <class DT>  
void Student<DT>::Init()  
{  
    cout << "\n欢迎来到输入记录界面\n" << endl;  
    int n = 0;  
    Node<DT> *p, *q;  
    q = first;  
    if (first->next != NULL)  
        first->next = NULL;  
    cout << "请设置你要输入的同学的数目:";  
    cin >> n;  
    for (int i = 0; i < n; i++)  
    {  

        p = new Node<DT>;  
p->data.name = new char[20];
        p->data.address = new char[20];
        p->data.sex = new char[20];
        p->data.number = new char[20];
        p->data.QQ = new char[20];
p->data.wechat = new char[20];
        cout << "\n请输入第" << i + 1 << "位同学的信息:"<<endl; 
        cout<<"名字  性别  住址   邮编  年龄      电话         QQ        微信       生日"<<endl;
        cin >> p->data.name>>p->data.sex>>p->data.address>>p->data.code>>p->data.age>>p->data.number>>p->data.QQ>>p->data.wechat>>p->data.birthday.year>>p->data.birthday.month>>p->data.birthday.day;  
        q->next = p;  
        q = p;  
        q->next = NULL;  
        length++;  
    }  
    cout << "\n同学信息输入完毕." << endl;  
    cout << "\n";  
    this->Show();  
    system("pause");                        //暂停  
    system("cls");                       //清屏  
}  
  
template <class DT>  
void Student<DT>::Insert()  
{  
    cout << "\n欢迎来到插入记录界面\n" << endl;  
    this->Show();  
    int s;  
    Node<DT> *p, *t;  
    p = first;  
    cout << "请输入你想要插入同学记录的位置:";  
    cin >>s;  
    while (s <= 0 || s > length + 1)  
    {  
        cout << "\n插入的位置不正确,请重新输入一个正确的位置:";  
        cin >> s;  
    }  
    for (int i = 0; i < s - 1; i++)  
        p = p->next;  
    t = new Node<DT>;  
t->data.name = new char[20];
        t->data.address = new char[20];
        t->data.sex = new char[20];
        t->data.number = new char[20];
        t->data.QQ = new char[20];
t->data.wechat = new char[20];
cout << "\n请输入你想插入的同学信息:"<<endl; 
        cout<<"名字  性别  住址   邮编  年龄      电话         QQ        微信       生日"<<endl;  
    cin>> t->data.name>>t->data.sex>>t->data.address>>t->data.code>>t->data.age>>t->data.number>>t->data.QQ>>t->data.wechat>>t->data.birthday.year>>t->data.birthday.month>>t->data.birthday.day;   
    t->next = p->next;  
    p->next = t;  
    length++;  
    cout << "\n同学记录插入完毕." << endl;  
    cout << "\n";  
    this->Show();  
    system("pause");  
    system("cls");  
}  
  
template <class T>  
void Student<T>::Find_Loc()  
{  
    cout << "\n欢迎来到按系统默认编号查找界面\n" << endl;  
    this->Show();  
    int s;  
    Node<T> *p;  
    p = first;  
    cout << "请输入你想要查找的同学记录的位置:";  
    cin >> s;  
    while (s <= 0 || s > length)  
    {  
        cout << "\n你输入的编号有误,请重新输入一个正确的编号:";  
        cin >> s;  
    }  
    for (int i = 0; i < s; i++)  
        p = p->next;  
    cout << "\n你想要查找的同学信息为:"<<endl;  
    cout <<p->data.name<<setw(4)<<p->data.sex<<setw(10)<<p->data.address<<setw(8)<<p->data.code<<setw(4)<<p->data.age<<setw(13)<<p->data.number<<setw(11)<<p->data.QQ<<setw(8)<<p->data.wechat<<setw(10)<<p->data.birthday.year<<setw(3)<<p->data.birthday.month<<setw(3)<<p->data.birthday.day<<endl; 
    cout << "查找完毕." << endl;  
    system("pause");  
    system("cls");  
  
}  
  
template <class DT>  
void Student<DT>::Find_Val()  
{  
    cout << "\n欢迎来到按同学信息查找界面\n" << endl;  
    bool t = false;  
    int i = 1;  
    Node<DT> *p;  
    p = first->next;  
    char x[20];  
    cout << "请输入想要查找的号码:";  
    cin >> x;  
    while (p != NULL)  
    {  
        if (x == p->data.number)  
        {  
            cout << "\n你查找的号码的同学名为:" << p->data.name << endl;  
            t = true;  
        }  
        p = p->next;  
        i++;  
    }  
    if (t == false)  
        cout << "\n没有符合条件的同学记录." << endl;  
    system("pause");  
    system("cls");  
}  
  
template <class DT>  
void Student<DT>::Del()  
{  
    cout << "\n欢迎来到删除记录界面\n" << endl;  
    this->Show();  
    int s;  
    Node<DT> *p, *q;  
    p = first;  
    cout << "请输入你想要删除的同学的位置为:";  
    cin >> s;  
    while (s <= 0 || s > length)  
    {  
        cout << "\n你输入的同学位置有误,请重新输入一个正确的同学位置:";  
        cin >> s;  
    }  
    for (int i = 0; i < s - 1; i++)  
        p = p->next;  
    q = p->next;  
    p->next = q->next;  
    cout << "\n你删除的同学为:";  
    cout << q->data.name<<setw(8)<< endl;  
    delete q;  
    length--;  
    cout << "\n删除完毕." << endl;  
    cout << "\n";  
    this->Show();  
    system("pause");  
    system("cls");  
}  
  
template <class DT>  
void Student<DT>::Destroy()  
{  
    Node<DT> *p, *q;  
    p = first;  
    q = p;  
    for (int i = 0; i < length; i++)  
    {  
        q = q->next;  
        delete p;  
        p = q;  
    }  
    length = 0;  
cout << "\n记录表清空完毕.\n" << endl;  
  
}  
  


template <class DT>  
void Student<DT>::Show()  
{  
    if (first->next == NULL)  
    {  
        cout << "\n记录表里数据为空,显示操作失败!!";  
        return;  
    }  
      
    
Node<DT> *p; 


    p = first->next;  
    cout << "\n记录表中同学信息为:" << endl;  
    while (p != NULL)  
    {  
cout <<p->data.name<<setw(4)<<p->data.sex<<setw(10)<<p->data.address<<setw(8)<<p->data.code<<setw(4)<<p->data.age<<setw(13)<<p->data.number<<setw(11)<<p->data.QQ<<setw(8)<<p->data.wechat<<setw(10)<<p->data.birthday.year<<setw(3)<<p->data.birthday.month<<setw(3)<<p->data.birthday.day<<endl; 
        p = p->next;  
    }  
    cout << endl;  
    cout << "\n\n";  
}  




template <class DT>  
void Student<DT>::Choice_menu()
{  
    int ch;  
    cout << "┏----------------------------------------------------------------------------┓";  
    cout << "┃                                                                            ┃";  
    cout << "┃ ┏---------------------------同学信息管理-------------------------------┓ ┃";  
    cout << "┃ ┃                                                                      ┃ ┃";  
    cout << "┃ ┃                                                                      ┃ ┃";  
    cout << "┃ ┃                         1. 输 入 记 录                               ┃ ┃";  
    cout << "┃ ┃                         2. 插 入 记 录                               ┃ ┃";  
    cout << "┃ ┃                         3. 按系统默认编号查找                        ┃ ┃";  
    cout << "┃ ┃                         4. 按同学号码查找                            ┃ ┃";  
    cout << "┃ ┃                         5. 删 除 记 录                               ┃ ┃";  
    cout << "┃ ┃                         6. 清 空 记 录                               ┃ ┃";    
    cout << "┃ ┃                         0. 退       出                               ┃ ┃";  
    cout << "┃ ┃                                                                      ┃ ┃";  
    cout << "┃ ┗----------------------------------------------------------------------┛ ┃";  
    cout << "┗----------------------------------------------------------------------------┛";  
    cout << "                                                                                ";  
    cout << "                                                                                ";  
    cout << "请选择你要执行的选项:"; 
    cin >> ch;  
    while (ch != 1 && ch != 2 && ch != 3 && ch != 4 &&ch != 5 &&ch != 6 &&ch != 0)
{  
        cout << "\n没有该选项,请重新选择." << endl;  
        cout << "\n请选择你要执行的选项:";  
        cin >> ch;  
    }  
    switch (ch)
{  
    case 1:  
        system("cls");  
        this->Init();  
        break;  
    case 2:  
        system("cls");  
        this->Insert();  
        break;  
    case 3:  
        system("cls");  
        this->Find_Loc();  
        break; 
case 4:  
        system("cls");  
        this->Find_Val();  
        break;  
case 5:  
        system("cls");  
        this->Del();  
        break;  
case 6:  
        system("cls");  
        this->Destroy();  
        break;  
    case 0:  
        cout << "\n 感谢您的使用!\n" << endl; 
        exit(EXIT_FAILURE);                          //用于退出操作
    }  
}  


主函数

#include "tongxunlu.cpp"  
#include <iostream>  
using namespace std;  
  
int main()  
{  
    Student<Stu> ss;
    while(1)
     ss.Choice_menu();  
     return 0;  
}  




说实话,这个代码框架是抄题目四的小伙伴的,在这个基础上我修改了数据类型和某些具体操作,虽然不懂this指针的作用和system函数的用法,但我在修改过程中也学到了好多东西。我曾经花了一个小时查找所谓的error linker2001,随后发现是文件包含错误;也发现0错误但无法实现的状况,最后知道是结构体内的指针必须初始化;明白了所谓的appcrash没有网上说的那么复杂,只不过是缺少某代码罢了。虽然这一切不是我原创的,我也承认我能力有限,知识贫乏,无法完全按照老师的要求完成任务,甚至连双链表也无法做到。但我想说,我已经尽力了,至少目前是尽力了,我也收获了不少,对结构体和类的理解更加深入了,也对大一学到的一些算法语句更加熟悉。

最后我还是要问一下,双链表到底要怎样实现?在博客里我会另外发表一篇双链表的,主要问题是插入信息后显示程序崩溃,应该是代码问题,真心不懂如何解决。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值