| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1858 人关注过本帖
标题:二进制文件和链表
只看楼主 加入收藏
云lsq
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-3
结帖率:50%
收藏
已结贴  问题点数:5 回复次数:5 
二进制文件和链表
有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学号、姓名、yuwen、shuxue、yingyu成绩)(按学号升序),将这些写入二进制文件stud.dat中。插入另外一个学生的信息。要求将该生插入到按学号排序的合适位置,然后将所有内容写到另外一个二进制文件stud1.dat中,并输出stud1.dat文件内容。注意:不允许使用数组!
例如:
输入:
101        wang     89    98    67
103        li        60    80    90
106        fang        75    91    99
110        zhang    100    50    77
113        qu        50    60    78
请输入要添加的学生信息:105 zhou 80 90 100
输出:
101        wang     89    98    67
103        li        60    80    90
105        zhou     80     90     100
106        fang        75    91    99
110        zhang    100    50    77
113        qu        50    60    78
搜索更多相关主题的帖子: 二进制 文件 输入 学号 dat 
2017-12-31 11:30
云lsq
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-3
收藏
得分:0 
求代码
2017-12-31 11:32
云lsq
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-3
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
    int id;
    char name[20];
    int score1,score2,score3;
    struct student *next;
};
typedef struct student SNODE;
SNODE *creat(SNODE *head)
{
    SNODE *r,*s;
    int id;
    char name[20];
    int score1,score2,score3;
    head=(SNODE *)malloc(sizeof(SNODE));
    r=head;
    printf("输入学号:\n");
    scanf("%d",&id);
  while(id!=0)
  {
      printf("输入姓名:\n");
      scanf("%s",name);
      printf("输入三门课程的成绩:\n");
      scanf("%d%d%d",&score1,&score2,&score3);
      s=(SNODE *)malloc(sizeof(SNODE));
      s->id=id;strcpy(s->name,name);s->score1=score1;
      s->score2=score2;s->score3=score3;
      r->next=s;
      r=s;
      printf("输入学号:\n");
      scanf("%d",&id);
  }
  r->next='\0';
  return head;
}
void print(SNODE *head)
{
    SNODE *p;
    p=head->next;
    if(p=='\0')  printf("LinkList is NULL!\n");
    else
    {
        printf("StudentInformation:\n");
        do{
            printf("%d %s %d %d %d\n",p->id,p->name,p->score1,p->score2,p->score3);
            p=p->next;
        }while(p!='\0');
    }
    printf("\n");
}
SNODE *insert(SNODE *head,int x,int y,char str[],int z1,int z2,int z3)
{
    SNODE *p1,*p2,*s;
    s=(SNODE *)malloc(sizeof(SNODE));
    s->id=y;strcpy(s->name,str);s->score1=z1;
    s->score2=z2;s->score3=z3;
    p1=head;
    p2=head->next;
    while((p2!='\0')&&(p2->id!=x))
    {
        p1=p2;
        p2=p2->next;
    }
    s->next=p2;
    p1->next=s;
    return head;
}
int main()
{
    SNODE *head;
    int new_id,new_score1,new_score2,new_score3,x;
    char str[20];
    head=NULL;
    head=creat(head);
    print(head);
    printf("输入插入位置:\n");
    scanf("%d",&x);
    printf("输入插入的数据:\n");
    scanf("%d%s%d%d%d",&new_id,str,&new_score1,&new_score2,&new_score3);
    head=insert(head,x,new_id,str,new_score1,new_score2,new_score3);
    print(head);
    return 0;
}
请修改
2017-12-31 11:35
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:5 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct student
{
    int id;
    char name[20];
    int score1,score2,score3;
} STUDEN;

typedef struct snode
{
    STUDEN st;
    struct snode *next;
} SNODE;

int input(SNODE *s)
{
    printf("输入学号:\n");
    scanf("%d", &s->st.id);
    if (s->st.id == 0)
        return 0;
    printf("输入姓名和三门课程的成绩:\n");
    scanf("%s%d%d%d", s->st.name, &s->st.score1, &s->st.score2, &s->st.score3);
    while (getchar() != '\n') NULL;
    s->next = NULL;
    printf("\n");
    return 1;
}

SNODE *create()
{
    SNODE *head, *r, *s, sn;
    if (!input(&sn))
        return NULL;
    head = (SNODE *)malloc(sizeof(SNODE));
    r = head;
    *r = sn;
    while (input(&sn))
    {
        s = (SNODE *)malloc(sizeof(SNODE));
        *s = sn;
        r->next = s;
        r = s;
    }
    return head;
}

void print(SNODE *p)
{
    if (p==NULL)
    {
        printf("LinkList is NULL!\n");
        return;
    }
    printf("StudentInformation:\n");
    for (; p; p=p->next)
        printf("%d %s %d %d %d\n", p->st.id, p->st.name, p->st.score1, p->st.score2, p->st.score3);
}

SNODE *insert(SNODE *head, SNODE *sn)
{
    SNODE *p, *q, *s;
    p = head;
    q = NULL;
    s = (SNODE *)malloc(sizeof(SNODE));
    *s = *sn;
    while (p)
    {
        if (p->st.id >= s->st.id)
        {
            s->next = p;
            break;
        }
        q = p;
        p = p->next;
    }
    if (q)
        q->next = s;
    else
        head = s;
    return head;
}

void _free(SNODE *p)
{
    SNODE *q;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }
}

SNODE *_read(char *path)
{
    FILE *fp = fopen(path, "rb");
    SNODE sn, *head=NULL, *p, *q;
    if (fread(&sn, sizeof(STUDEN), 1, fp) == 1)
    {
        head = (SNODE *)malloc(sizeof(SNODE));
        q = head;
        *q = sn;
        q->next = NULL;
    }
    while (fread(&sn, sizeof(STUDEN), 1, fp) == 1)
    {
        p = (SNODE *)malloc(sizeof(SNODE));
        *p = sn;
        p->next = NULL;
        q->next = p;
        q = p;
    }
    fclose(fp);
    return head;
}

void _save(SNODE *h, char *path)
{
    FILE *fp = fopen(path, "wb");
    while (h)
    {
        fwrite(h, sizeof(STUDEN), 1, fp);
        h = h->next;
    }
    fclose(fp);
}

int main()
{
    SNODE sn, *head = create();
    _save(head, "stud.dat");
    _free(head);
    printf("\n输入插入的数据:\n");
    if (input(&sn))
    {
        head = _read("stud.dat");
        head = insert(head, &sn);
        _save(head, "stud1.dat");
        _free(head);
        head = _read("stud1.dat");
        print(head);
        _free(head);
    }
    return 0;
}
2017-12-31 19:23
云lsq
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-3
收藏
得分:0 
回复 4楼 吹水佬
很正确,水平太高了就是有点不太懂,插入不懂,可以简化一些么,
2018-01-01 10:15
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:0 
以下是引用云lsq在2018-1-1 10:15:50的发言:

很正确,水平太高了就是有点不太懂,插入不懂,可以简化一些么,

输入数据的input()还要改改:
int input(SNODE *s)
{
    printf("输入学号:\n");
    scanf("%d", &s->st.id);
    if (s->st.id == 0)
        return 0;
    int ret;
    s->next = NULL;
    printf("输入姓名和三门课程的成绩:\n");
    ret = (scanf("%s%d%d%d", s->st.name, &s->st.score1, &s->st.score2, &s->st.score3) == 4);
    while (getchar() != '\n') NULL;
    if (!ret)
        printf("无效的数据输入");
    printf("\n");
    return ret;
}

insert()是简单的插入排序,就是在符合条件的当前节点和上一个节点之间作插入处理,最后确定头节点并返回。
2018-01-01 15:40
快速回复:二进制文件和链表
数据加载中...
 
   



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

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