| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 427 人关注过本帖
标题:麻烦高手帮我看下 该动哪些地方 才能让内存能读
只看楼主 加入收藏
langman945
Rank: 1
等 级:新手上路
帖 子:9
专家分:4
注 册:2010-10-31
结帖率:100%
收藏
 问题点数:0 回复次数:0 
麻烦高手帮我看下 该动哪些地方 才能让内存能读
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
 struct student
{
    int xh;
    char xm[10];
    int nl;
    char xb[10];
    char bj[8];
    struct student *next;
        
}stu[4];
 int n;
///////////////////////////////////////
void input()
{
    FILE *fp;
    int i;
    fp=fopen("e:\\in.txt","w");
printf("请输入学号,姓名,年龄,性别,班级\n");
    for(i=0;i<2;i++)
    {
        scanf("%s%s%d%s%s",&stu[i].xh,stu[i].xm,&stu[i].nl,stu[i].xb,stu[i].bj);
        fprintf(fp,"%s%s%d%s%s\n",stu[i].xh,stu[i].xm,stu[i].nl,stu[i].xb,stu[i].bj);
    }
    fclose(fp);
}
////////////////////////////////
void ReadDat()
{
    FILE *fp;
    int i;
    if((fp=fopen("e:\\in.txt","r"))==NULL)
    {
        printf("cannot open file");
            exit(0);
    }
    for(i=0;i<2;i++)
    {
        fscanf(fp,"%s%s%d%s%s",&stu[i].xh,stu[i].xm,&stu[i].nl,stu[i].xb,stu[i].bj);
        printf("%s%s%d%s%s\n",stu[i].xh,stu[i].xm,stu[i].nl,stu[i].xb,stu[i].bj);
    }
    fclose(fp);
}
/////////////////////////////////
struct student * CreatStu(struct student *head)
{
    int i,j,t;
    for(i=0;i<2;i++)
   
        for(j=0;j<2-i;j++)
   
   
        if(stu[j].xh>stu[j+1].xh )
        {
            t=stu[j].xh;
            stu[j].xh=stu[j+1].xh;
            stu[j+1].xh=t;
        }


        struct student *p1,*p2;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    printf("请输入数据\n");
    p1->xh=stu[i].xh;
    strcpy(p1->xm,stu[i].xm);
    p1->nl=stu[i].nl;
    strcpy(p1->xb,stu[i].xb);
    strcpy(p1->bj,stu[i].bj);
    p1->next=NULL;
    head=p1;
    p2=p1;
    while(head!=NULL)
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student*)malloc(sizeof(struct student));
        
        p1->xh=stu[i].xh;
    strcpy(p1->xm,stu[i].xm);
    p1->nl=stu[i].nl;
    strcpy(p1->xb,stu[i].xb);
    strcpy(p1->bj,stu[i].bj);
        p1->next=NULL;
        p2->next=p1;
        p2=p1;
    }
    return head;



}
/////////////////////////////////////////
void DisplayStu(struct student *head)
{
    struct student *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("\n%s%s%d%s%s\n",temp->xh,temp->xm,temp->nl,temp->xb,temp->bj);
        temp=temp->next;

    }
   
}
//////////////////////////////////////
struct student *InsertStu(    struct student *head)
{
   

   
   
   
        struct student *p1,*p2,*p3;
        p1=(struct student*)malloc(sizeof(struct student));
        p1->xh=n;
        p2=head;
        if(head=NULL)
        {
            head=p1;
            p1->next=NULL;

        }
        else
        {
            while(n>p2->xh&&p2->next!=NULL)
            {
                p3=p2;
                p2=p2->next;
            }
            if(n<=p2->xh)
                if(head==p2)
                {
                    head=p1;
                    p1->next=p2;
                }
                else
                {
                    p3->next=p1;
                    p1->next=p2;
                }
                else
                {
                    p2->next=p1;
                    p1->next=NULL;
                }
        }
        return head;
}
/////////////////////////////////////////////////////////////////////
struct student * DeleteStu(struct student *head)
{
   

    struct student *temp,*p;
        temp=head;
        if(head==NULL)
            printf("只是空表\n");
        else
        {
            temp=head;
            while(strcmp(temp->xb,"nan")!=0&&temp->next!=NULL)
            {
                p=temp;
                temp=temp->next;
            }
            if(strcmp(temp->xb,"nan")==0)
            {
                if(temp==head)
                {
                    head=head->next;
                    free(temp);
                }
                else
                {
                    p->next=temp->next;
                    free(temp);
                }
            }
        }
   
    return head;
   

}
//////////////////////////////////
void WriteDat (struct student *head)
{
    printf("已写入out文件\n");
    FILE *fp;
    struct student *p;
    p=head;
    fp=fopen("e:\\out.txt","w");
    while(p!=NULL)
    {
        
        fprintf(fp,"%d %s %d %d %s \n",p->xh,p->xm,p->nl,p->xb,p->bj);
        p=p->next;
    }
}
///////////////////////////////////////
void main()
{
    struct student *head;
    head=NULL;
     struct student *insert();
     struct student *delet();
     struct student *creat();
   input();
   head=CreatStu(head);
   DeleteStu(head);
   InsertStu(head);


}
   
搜索更多相关主题的帖子: 内存 麻烦 
2010-11-04 22:18
快速回复:麻烦高手帮我看下 该动哪些地方 才能让内存能读
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026832 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved