回 帖 发 新 帖 刷新版面

主题:[讨论]vc6.0编译程序时出现 该内存不能为“written”


#include <iostream.h>     // cin 及 cout
#include <string.h>          // 字符串处理
#include <stdio.h>          // 文件操作(读文件)
#include <stdlib.h>          // system("cls")
#include <iomanip.h>
#include <malloc.h>       // 用到申请内存函数 malloc() 和释放内存函数 free()




struct address                   /*家庭地址*/

        char city[10];           /*城市*/
        char town[10];              /*县城*/
        char village[10];       /*乡镇*/
};

struct telephone                   /*联系方式*/
{
        char SJ[50];               /*手机*/
        char JD[30];               /*家庭电话*/
        char XD[30];               /*学校电话*/
};

struct person                    /*个人信息*/
{  
        char name[20];          /*名字*/ 
        char sex[10] ;          /*性别*/ 
        char MZ[16];            /*民族*/
        char GJ[17];            /*国籍*/  
        char XL[19];            /*学历*/
};    

struct score                        //成绩
{
        char num[20];                //学号
        char english[20];
        char chinese[20];
        char math[20];
        char physics[20];
};

class student            //定义节点的类型
{
public:
        char address[100];        //地址
        char birthday[100];        //出生日期
        struct score sc;        //成绩
        struct person pe;        //个人信息
        struct telephone te;    //联系方式
        bool flag;
        student *next;
};

#include "student.h"

class LinkedList
{
private:
    student* head;

public:
    LinkedList(void);
    LinkedList::~LinkedList(void);

    student* creatlist(int);                //创建链表
    int listlen();                            //返回链表长度
    student* findnode(int);                //通过查找序号返回节点的指针
    student* find(char c[]);                //通过查找姓名返回节点的指针
    int find2(char c[]);                    //通过查找姓名返回节点的序号
    student* insnode(int);                    //插入节点
    void delnode(int);                        //删除节点

    student* load();                        //初始化:从外部读入数据

    void readstr(FILE *f,char *string);        //读行函数
    bool check(char *a, char *b);            //对比两个字符串是否相等
    void help();                            //显示帮助菜单

    void editperson(student*);                //编辑个人说明
    void editscore(student*);                //编辑学科成绩
    void edittelephone(student*);            //编辑联系方式

    void dispname();                        //显示所有学生姓名
    void dispnode(student* p);                //显示一个学生的所有信息
    void dispperson(student*);                //显示一个学生的个人说明
    void dispscore(student*);                //显示一个学生的学科成绩
    void disptelephone(student*);            //显示一个学生的联系方式

};


 
#include <iostream.h>  // cin 及 cout
#include <string.h>       // strcpy():    字符串复制
#include <stdlib.h>       // system("cls")
#include <stdio.h>       // 文件操作(写文件)
#include "LinkedList.h"      // 我们要创建类 LinkedList 的对象和节点指针,所以要把类的头文件包含进来

class window
{
    LinkedList L1;
public:                
    void Loop();                            //主循环            
    void display();                            //显示菜单    
};


#include "linkedList.h"

LinkedList::LinkedList()
{
    head = NULL;
}

bool LinkedList::check(char *a, char *b)        //对比两个字符串是否相等
{
    int i;
    int j=strlen(b);
    for(i=0; i<j; i++)
    {
        if(*a==*b)
        {
            a++;
            b++;
        }
        else
            return 0;
    }
    return 1;
}


student* LinkedList::creatlist (int n)       //创建链表
{
    student *h=NULL;
    student *s=NULL;
    student *t=NULL;
    int i=1;

    for(int j=0; j<n; j++)
    {
        if(i==1)            //创建第一个节点
        {
            student *h=new student;
            h->next=NULL;
            t=h;
        }
        else                 //创建其余节点
        {
            student *s=new student;
            s->next=NULL;
            t->next=s;
            t=s;            //t 始终指向生成的单链表的最后一个节点
        }

        i++;
    }

    head=h;
    return h;
}


void LinkedList::readstr(FILE *f,char *string)
{    
    do
    {
                           //①: 先读入一行文本
    fgets(string, 255, f);  //fgets(): 从文件 f 读入长度为 255-1 的字符串
                           //          并存入到 string 中
    } while ((string[0] == '/') || (string[0] == '\n'));

    return;
}

student* LinkedList::load()
{
    FILE *fp;
    student *p=new student;
    char c[255];
    int num;
    if((fp=fopen("student.txt", "r"))==NULL)
    {
        cout<<"打开文件失败"<<endl;
        return 0;
    }
    readstr(fp, c);
    sscanf(c, "The Length Of Link: %d", &num);        //获取链表长度

    p=creatlist(num);                                //创建链表

    for(int i=0; i<num; i++)
    {
        readstr(fp, c);
        strcpy(p->address, c);
        readstr(fp, c);
        strcpy(p->birthday, c);

        readstr(fp, c);
        strcpy(p->sc.num, c);
        readstr(fp, c);
        strcpy(p->sc.chinese, c);
        readstr(fp, c);
        strcpy(p->sc.english, c);
        readstr(fp, c);
        strcpy(p->sc.math, c);
        readstr(fp, c);
        strcpy(p->sc.physics, c);

        readstr(fp, c);
        strcpy(p->pe.name, c);
        readstr(fp, c);
        strcpy(p->pe.sex, c);
        readstr(fp, c);
        strcpy(p->pe.GJ, c);
        readstr(fp, c);
        strcpy(p->pe.MZ, c);
        readstr(fp, c);
        strcpy(p->pe.XL, c);

        readstr(fp, c);
        strcpy(p->te.SJ, c);
        readstr(fp, c);
        strcpy(p->te.JD, c);
        readstr(fp, c);
        strcpy(p->te.XD, c);

        p=p->next;
    }

    fclose(fp);

    return p;
}

void LinkedList::dispnode(student* p)        //显示一个学生的所有信息
{
    if(p!=NULL)
    {
        dispperson(p);
        dispscore(p);
        disptelephone(p);
    }
}

void LinkedList::dispname()        //显示所有学生姓名
{
    student* p=head;
    cout<<"现有的学生:  "<<endl;
    if(p==NULL)
        cout<<"没有任何学生数据"<<endl;
    while(p!=NULL)
    {
        cout<<"姓名:    "<<p->pe.name;

        p=p->next;
    }
}

int LinkedList::listlen()       //返回链表长度
{
    int i=0;
    student* p=head;
    while(p!=NULL)
    {
        p=p->next;
        i++;
    }
    return i;
}

student* LinkedList::findnode (int i)      //通过查找序号返回节点的指针
{
    student* p=head;
    int j=1;
    if( i>listlen()||i<=0 )          // i 上溢或下溢
        return NULL;
    else
    {
        while( p!=NULL &&  j<i )    //查找第 i 个节点并由 p 指向该节点
        {
            j++;
            p=p->next;
        }
        return p;
    }
}

student* LinkedList::find(char c[])      //通过查找姓名返回节点的指针
{
    student* p=head;
    int j=1;
    strcat(c, "\n");                //从外部读入的字符串末尾都带了一个换行符
    while( p!=NULL &&  !(check(c, p->pe.name)))    //查找第 i 个节点并由 p 指向该节点    
    {    
        j++;    
        p=p->next;
    }
    return p;
}

int LinkedList::find2(char c[])      //通过查找姓名返回节点的序号
{
    student* p=head;
    int j=1;
    strcat(c, "\n");                //从外部读入的字符串末尾都带了一个换行符
    while( p!=NULL &&  !(check(c, p->pe.name)))    //查找第 i 个节点并由 p 指向该节点    
    {    
        j++;
        p=p->next;
    }
    return j;
}

student* LinkedList::insnode(int i)
{
    student *h=head;
    student *p=new student;
    student *t=new student;    //创建节点 s
    t->next=NULL;
    if(i==0)           //i=0 时 s 作为该单链表的第一个节点
    {
        t->next = h;
        h=t;           //重新定义头节点
    }
    else
    {
        p=findnode(i);       //查找第 i 个节点,并由 p 指向该节点
        if(p!=NULL)
        {
            t->next=p->next;
            p->next=t;
        }
        else cout<<"输入的 i 值不正确"<<endl;
    }
    head=h;

    return t;
}

void LinkedList::delnode(int i)            //删除第 i 个节点
{
    student *h=head, *p=head;
    student *s=new student;
        
    int j=1;
    if(i==1)       //删除第一个节点
    {
        h=h->next;
        delete p;
    }
    else
    {
        p=findnode(i-1);     //查找第 i-1 个节点,并由 p 指向这个节点
        if(p!=NULL && p->next!=NULL)
        {
            s=p->next;      // s 指向要删除的节点
            p->next=s->next;
            delete s ;
        }
        else
            cout<<"输入的 i 值不正确"<<endl;
    }
    head=h;
}

void LinkedList::editperson(student* p)
{
    char c[100];
    cout<<"请输入姓名: "<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->pe.name, c);

    cout<<"请输入性别:"<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->pe.sex, c);

    cout<<"请输入生日(格式举例:1982-1-1): "<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->birthday, c);

    cout<<"请输入民族:"<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->pe.MZ, c);

    cout<<"请输入国籍:"<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->pe.GJ, c);

    cout<<"请输入学历:"<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->pe.XL, c);

    cout<<"请输入家庭住址(例如:广西玉林市解放路11号"<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->address, c);

    cout<<"编辑个人信息完成!"<<endl;

    dispperson(p);
}

void LinkedList::editscore(student* p)
{
    char a[50];
    cout<<"请输入学号: "<<endl;
    cin>>a;
    strcat(a, "\n");
    strcpy(p->sc.num, a);

    cout<<"请输入大学语文成绩: "<<endl;
    cin>>a;
    strcat(a, "\n");
    strcpy(p->sc.chinese, a);

    cout<<"请输入英语成绩: "<<endl;
    cin>>a;
    strcat(a, "\n");
    strcpy(p->sc.english, a);

    cout<<"请输入数学成绩: "<<endl;
    cin>>a;
    strcat(a, "\n");
    strcpy(p->sc.math, a);

    cout<<"请输入物理成绩: "<<endl;
    cin>>a;
    strcat(a, "\n");
    strcpy(p->sc.physics, a);

    cout<<"编辑学科成绩完成!"<<endl;

    dispscore(p);
}

void LinkedList::edittelephone(student* p)
{
    char c[50];
    cout<<"请输入手机号码: "<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->te.SJ, c);

    cout<<"请输入家庭电话号码: "<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->te.JD, c);

    cout<<"请输入学校电话号码: "<<endl;
    cin>>c;
    strcat(c, "\n");
    strcpy(p->te.XD, c);

    cout<<"编辑联系方式完成!"<<endl;

    disptelephone(p);
}

void LinkedList::dispperson(student* p)
{
    cout<<"姓名:        "<<p->pe.name;
    cout<<"性别:        "<<p->pe.sex;
    cout<<"民族:        "<<p->pe.MZ;
    cout<<"国籍:        "<<p->pe.GJ;
    cout<<"学历:        "<<p->pe.XL;
    cout<<"出生日期:    "<<p->birthday;
    cout<<"家庭住址:    "<<p->address;
}

void LinkedList::dispscore(student* p)
{
    cout<<"学号:        "<<p->sc.num;
    cout<<"大学语文成绩:    "<<p->sc.chinese;
    cout<<"英语成绩:    "<<p->sc.english;
    cout<<"数学成绩:    "<<p->sc.math;
    cout<<"物理成绩:    "<<p->sc.physics;
}

void LinkedList::disptelephone(student* p)
{
    cout<<"手机号码是:    "<<p->te.SJ;
    cout<<"家庭电话是:    "<<p->te.JD;
    cout<<"学校电话是:    "<<p->te.XD;
}

void LinkedList::help()
{
    cout<<endl<<endl;
    cout<<"*********************************************************"<<endl;
    cout<<"1:            编辑个人信息"<<endl;
    cout<<"2:            编辑学科成绩"<<endl;
    cout<<"3:            编辑联系方式"<<endl;
    cout<<"4:            显示个人信息"<<endl;
    cout<<"5:            显示学科成绩"<<endl;
    cout<<"6:            显示联系方式"<<endl;
    cout<<"7:            显示该学生所有信息"<<endl;
    cout<<"8:            帮助菜单"<<endl;
    cout<<"9:            返回上一级菜单"<<endl;
    cout<<"*********************************************************"<<endl;
}

LinkedList::~LinkedList()
{
    student *pa=head;
    student *pb=new student;
    if(pa!=NULL)
    {
        pb=pa->next;
        if(pb==NULL)
            free(pa);
        else
        {
            while(pb!=NULL)
            {
                free(pa);
                pa=pb;
                pb=pb->next;
            }
            free(pa);
        }
    }
}

回复列表 (共7个回复)

沙发

#include "window.h"

void window::display()
{
    system("color f2"); //颜色 同样是调用doc
    cout<<endl<<endl;
    cout<<"**************************** 学生管理系统 **************************"<<endl;
    cout<<setw(42)<<"1:添加学生信息"<<endl;
    cout<<setw(42)<<"2:删除学生信息"<<endl;
    cout<<setw(46)<<"3:显示所有学生姓名"<<endl;
    cout<<setw(42)<<"4:显示学生信息"<<endl;
    cout<<setw(42)<<"5:修改学生信息"<<endl;
    cout<<setw(38)<<"6:帮助菜单"<<endl;
    cout<<setw(38)<<"7:保存数据"<<endl;
    cout<<setw(38)<<"0:退出系统"<<endl;
    cout<<"********************************************************************"<<endl;
}

void window::Loop()
{
    LinkedList L1;        //List 对象
    char ch[20];
    student *p=new student;
    student *head=new student;   
    int i;            //存放节点序号

    p=L1.load();    //初始化:从外部读入数据创建链表

    head=p;
    display();

    while(1)
    {
        cout<<endl<<endl;
        cout<<"********************************************************************"<<endl;
        cout<<setw(52)<<"请输入选择(帮助选项--> 6 ): "<<endl;
        cout<<"********************************************************************"<<endl;
        cin>>ch;

        system("cls");
        system("color f2"); //颜色 同样是调用doc
        if(L1.check(ch, "1"))
        {
            p=L1.insnode(0);
            head=p;

            system("cls");
            cout<<endl;
            cout<<"************** 添加一个学生信息 ******************"<<endl;
            cout<<"下面输入个人信息: "<<endl;
            L1.editperson(p);
            cout<<"下面输入学科成绩: "<<endl;
            L1.editscore(p);
            cout<<"下面输入联系方式: "<<endl;
            L1.edittelephone(p);
        }

        if(L1.check(ch, "2"))
        {
            system("cls");
            system("color f2"); //颜色 同样是调用doc
            cout<<endl;
            cout<<"************** 删除一个学生信息 ******************"<<endl;
            L1.dispname();
            cout<<"请输入学生姓名: "<<endl;
            cin>>ch;
            i=L1.find2(ch);
            L1.delnode(i);
        }

        if(L1.check(ch, "3"))
        {
            system("cls");
            system("color f2"); //颜色 同样是调用doc
            cout<<endl;
            cout<<"************** 显示所有学生姓名 ******************"<<endl;
            L1.dispname();
        }

        if(L1.check(ch, "4"))
        {
            system("cls");
            system("color f2"); //颜色 同样是调用doc
            cout<<endl;
            cout<<"************** 根据姓名显示单个学生所有信息 ******************"<<endl;
            L1.dispname();
            cout<<"请输入学生姓名: "<<endl;
            cin>>ch;
            p=L1.find(ch);
            L1.dispnode(p);
        }

       
        

板凳

        if(L1.check(ch, "6"))
        {
            display();
        }

        if(L1.check(ch, "7"))        //保存数据
        {
            FILE *fp;
            system("color f2"); //颜色 同样是调用doc
            cout<<"保存数据成功!"<<endl;
            if((fp=fopen("student.txt", "w"))==NULL)
            {
                cout<<"打开文件失败"<<endl;
                return;
            }

            int i;
            char t[255];

            //将 L1.listlen() 赋予字符串中的数字
            sprintf(t, "The Length Of Link: %d\n", L1.listlen());    

            fputs(t, fp);
            strcpy(t, "\n");
            fputs(t, fp);

            p=L1.findnode(1);                //将链表头指针赋予 p

            for(i=0; i<L1.listlen(); i++)
            {
                fputs(p->address, fp);        //输出地址
                fputs(p->birthday, fp);        //输出生日

                fputs(p->sc.num, fp);        //输出学号
                fputs(p->sc.chinese, fp);    //输出语文成绩
                fputs(p->sc.english, fp);    //输出英语成绩
                fputs(p->sc.math, fp);        //输出数学成绩
                fputs(p->sc.physics, fp);    //输出物理成绩

                fputs(p->pe.name, fp);        //输出姓名
                fputs(p->pe.sex, fp);        //输出性别
                fputs(p->pe.GJ, fp);        //输出国籍
                fputs(p->pe.MZ, fp);        //输出民族
                fputs(p->pe.XL, fp);        //输出学历

                fputs(p->te.SJ, fp);        //输出手机
                fputs(p->te.JD, fp);        //输出家庭电话
                fputs(p->te.XD, fp);        //输出学校电话
                fputs(t, fp);

                p=p->next;
            }

3 楼

p=head;
            fclose(fp);
        }

        if(L1.check(ch, "5"))
        {
            char c[20];
            system("cls");
            system("color f2"); //颜色 同样是调用doc
            cout<<endl;
            cout<<"************** 根据姓名对单个学生进行编辑 ******************"<<endl;
            L1.dispname();
            cout<<"请输入学生姓名: "<<endl;
            cin>>c;
            p=L1.find(c);

            system("cls");
            cout<<endl<<endl;
            cout<<"*********************************************************"<<endl;
            cout<<"1:            编辑个人信息"<<endl;
            cout<<"2:            编辑学科成绩"<<endl;
            cout<<"3:            编辑联系方式"<<endl;
            cout<<"4:            显示个人信息"<<endl;
            cout<<"5:            显示学科成绩"<<endl;
            cout<<"6:            显示联系方式"<<endl;
            cout<<"7:            显示该学生所有信息"<<endl;
            cout<<"8:            帮助菜单"<<endl;
            cout<<"9:            返回上一级菜单"<<endl;
            cout<<"*********************************************************"<<endl;

            while(1)
            {
                cout<<endl<<endl;
                cout<<"请输入选择(帮助选项--> 8 ): "<<endl;
                cin>>c;
                system("cls");
                system("color f2"); //颜色 同样是调用doc

                if(L1.check(c, "1"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    cout<<endl;
                    cout<<"************** 编辑个人信息 ******************"<<endl;
                    L1.editperson(p);
                }
            
                else if(L1.check(c, "2"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    cout<<endl;
                    cout<<"************** 编辑学科成绩 ******************"<<endl;
                    L1.editscore(p);
                }

4 楼

else if(L1.check(c, "3"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    cout<<endl;
                    cout<<"************** 编辑联系方式 ******************"<<endl;
                    L1.edittelephone(p);
                }
            
                else if(L1.check(c, "4"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    cout<<endl;
                    cout<<"************** 显示个人信息 ******************"<<endl;
                    L1.dispperson(p);
                }

                else if(L1.check(c, "5"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    cout<<endl;
                    cout<<"************** 显示学科成绩 ******************"<<endl;
                    L1.dispscore(p);
                }

                else if(L1.check(c, "6"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    cout<<endl;
                    cout<<"************** 显示联系方式 ******************"<<endl;
                    L1.disptelephone(p);
                }

                else if(L1.check(c, "7"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    L1.dispnode(p);
                }

                else if(L1.check(c, "8"))
                {
                    system("cls");
                    system("color f2"); //颜色 同样是调用doc
                    L1.help();
                }

                else if(L1.check(c, "9"))
                {
                    display();
                    break;    //用 break 跳出本循环,不要用 return ,return 是退出程序
                }
            }
        }

        else if(L1.check(ch, "0"))
            return;
    }
    return;
}

5 楼

#include "window.h"

void main()
{
    window O1;
    O1.Loop();
}

6 楼

救命啊,这是我的作业,现在好纠结啊,哪位高手赶快来帮帮忙吧···············

7 楼

那把错误提示发出来呀!这样估计没人帮你!别人的时间既有限,又宝贵!

我来回复

您尚未登录,请登录后再回复。点此登录或注册