鱼C论坛

 找回密码
 立即注册
查看: 2972|回复: 12

朋友的C++作业,后天要交,很多bug,帮改一下,拜托大家了

[复制链接]
发表于 2013-4-23 15:49:38 | 显示全部楼层 |阅读模式
100鱼币
题 目 二:带表头结点的单链表实现
题目描述:线性表是n( 3 0)个元素a0,a1,…,an-1的有序集合,记为(a0,a1,…,an-1)。其中,n是线性表中元素的个数,称为线性表的长度,n = 0时称为空表。实现线性表有两种存储表示法:顺序存储结构和链式存储结构。设计并实现一个链式存储结构的线性表,即单链表。
基本要求:
1、设计并实现一个带表头结点单链表结构。
2、单表的初始化函数以及增删查改等函数,以及释放每个结点的空间。
3、有简单的交互界面供使用者进行功能测试。
提高要求:
1、如果单链表中存放的不是简单的数据类型(如int型),而是一个结构类型(如一个学生结构)。单链表该如何设计?学生结构如下:
学号        姓名        性别        出生        语文        数学        外语        总分
02070101        张丽        女        1980        90         100        98       
02070102        林东妹        女        1982        98        97        87       
02070103        赵启南        男        1979        89        94        86       
……                                                       
2、系统启动时可以从文本文件中读取学生信息构建单链表;系统关闭后,单链表中的学生信息可以保存在文本文件中。

#include <iostream.h>
#include <string.h>
#include <fstream.h>
void menu();

class Student
{
        char Name[10];
        char sex[10];
        int Chinese;
        int Maths;
        int English;
        int total;
        int No;
public:
        Student();
    Student(char *na,char *se,int Ch,int Ma,int En,int to,int no);
        ~Student();
        void Display();
};

Student::Student(char *na,char *se,int Ch,int Ma,int En,int to,int no)
{
        strcpy(Name,na);
        strcpy(sex,se);
        Chinese=Ch;
        Maths=Ma;
        English=En;
        total=Chinese+Maths+English;
        No=no;
}


template <class T>               
class Node
{
public:
        T data;
        Node *link;
};

template <class T>
class SingleList
{
private:
        int currentSize;
        Node<T> *first;
public:
        SingleList();

        ~SingleList();

        int Insert();
        int Search();
        int Delete();
        int Modify();
        void Display()
{
        SingleList *p=first->link;
{
          cout<<p->data<<endl;
      p=p->link;
}
}
        friend istream&operator>>(istream&in,Student&k)
        {
                in>>k.na;
                in>>k.no;
                in>>k.se;
                in>>k.Ch;
                in>>k.Ma;
                in>>k.En;
                in>>k.to;
                return in;
        }
        friend ostream&operator<<(ostream&out,const Student&k)
        {
                out>>k.na;
                out>>k.no;
                out>>k.se;
                out>>k.Ch;
                out>>k.Ma;
                out>>k.En;
                out>>k.to;
                return out;
        }
};

template <class T>
SingleList<T>::SingleList()
{
        currentSize = 0;
        first = new Node<T>;
       
        first->link = NULL;
}

template <class T>
SingleList<T>::~SingleList()
{
}

template <class T>
int SingleList<T>::Insert(int i, T val)
{
        if (i <0 || i > currentSize)
                return 0;

        Node<T> *p = first;
        for (int j = 1; j <=i; ++j)
        {
                p = p->link;
        }

        Node<T> *q = new Node<T>;
        q->data = val;
        q->link = p->link;
        p->link = q;
        currentSize++;
        return 1;
}

template <class T>
int SingleList<T>::Delete(int i)
{
                if (i <0 || i > currentSize)
            cout<<"找不到您要的信息"<<endl;
                return 0;
        Node<T> *p = first;
        for (int j = 1; j <=i; ++j)
        {
                p = p->link;
        }
    q->link = p;
        q->link = p->link;
        delete p;
        currentSize--;
        return 1;

}

template <class T>
int SingleList<T>::Modify(int i,T val)
{
                if (i <0 || i > currentSize)
                cout<<"找不到您要的信息"<<endl;
                return 0;
                     Node<T> *p = first;
        for (int j = 1; j <=i; ++j)
        {
                p = p->link;
        }
    q->data = val;
        return 1;
}

template <class T>
int SingleList<T>::Search(int i)
{
        if(i<0||i>currentSize)
                cout<<"找不到您需要的信息"<<endl;
                return 0;

                     Node<T> *p = first;
        for (int j = 1; j <=i; ++j)
        {
                p = p->link;
        }
     return 1;
}


int main()
{

        SingleList<Student> sl;
    Student a;
    ifstream data("data.txt");
        data >> a.no;
        data >> a.na;
    data >> a.se;
        data >> a.Ch;
        data >> a.Ma;
        data >> a.En;
        data >> a.to;
        sl.Insert();
        sl.Delete();
    sl.Modify();
        sl.Search();
        do
        {
                menu();
                int choice;
                cin>>choice;
                switch(choice)
                {
                case 1:sl.Insert(); break;

                case 2:cout<<"请输入您要删除的学号"<<endl;
                        int de;
                        cin>>de;
                        sl.Delete(de);
            break;
                case 3:cout<<"请输入您要修改的学号"<<endl;
                        int mo;
                        cin>>mo;
            sl.Modify(mo);
                        break;
                case 4:cout<<"请输入您要查询的学号"<<endl;
                        int s;
                        cin>>s;
            sl.Search(s);
                        a.Display();
                        break;
                case 5:break;
                }
        }
        /*data >> a.Name;
        data >> a.No;
        sl.Insert(0, a);

        data >> a.Name;
        data >> a.No;
        sl.Insert(0, a);
       
        strcpy(a.Name, "zhangsan");
        a.No = 20;
        sl.Insert(0, a);


        strcpy(a.Name, "lisi");
        a.No = 30;
        sl.Insert(1, a);

        strcpy(a.Name, "wang");
        a.No = 40;
        sl.Insert(2, a);
        */
    return 0;

}

void menu()
{   
        cout<<"*****请输入您的选择*****"<<endl;
        cout<<"*****1.插入学生信息*****"<<endl;
        cout<<"*****2.删除学生信息*****"<<endl;
        cout<<"*****3.修改学生信息*****"<<endl;
        cout<<"*****4.查找学生信息*****"<<endl;
        cout<<"*****5.退出学生系统*****"<<endl;
}







题目和代码.zip

2.01 KB, 下载次数: 17

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-4-23 15:56:20 | 显示全部楼层
我只是路过打酱油的。C++还没学会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-23 16:58:37 | 显示全部楼层
不会:dizzy:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-23 17:02:26 | 显示全部楼层
我感觉跟我大一时候做的东西一样  不过我没好好学 抄的  帮不上忙了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-23 23:22:28 | 显示全部楼层
自己搞把,不是很难,一天时间认真做,肯定能做会的,只要把逻辑捋顺了,就没有什么问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-24 00:20:19 | 显示全部楼层
艾玛!写到疼啊!注释都没有!我不看没有注释的代码的!这很头疼!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-4-24 12:50:34 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-24 23:35:56 | 显示全部楼层
楼主你可以尝试把char 改成string,这个问题的输入量不打,用cin>>输入string不会太浪费时间的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-25 10:49:42 | 显示全部楼层
好高级的样子
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-25 12:58:29 | 显示全部楼层
楼主加油,鱼C加油!我们都看好你哦!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-25 22:56:49 | 显示全部楼层
我只是路过打酱油的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-4-27 01:23:06 | 显示全部楼层
强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-4-27 21:27:27 | 显示全部楼层
x87648510 发表于 2013-4-27 01:23
强烈支持楼主ing……

灌水严重啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 04:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表