| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 613 人关注过本帖
标题:求助,写入文件的数据不匹配
取消只看楼主 加入收藏
klpk521
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-28
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:1 
求助,写入文件的数据不匹配
设计了一个学生成绩管理系统,退出程序之前写入txt文件之后,打开文件发现里面的数值非常大,求点拨
另外,求平均分该怎么写代码呢?用链表来
程序代码:
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define INPUT %d %d %s %d %d %d %d %d %d %d %d %d %d
#define SCANF &pC->num,&pC->Class,pC->name,&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]
#define PRINT pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]
struct student
{
    int num;
    int Class;
    char name[10];
    int finish[10];
    struct student * next;
};
   
struct student *insert(struct student *head,
                       struct student *pNode)
{
    // pPrev为前驱结点位置,pNext为后继结点位置
    struct student *pPrev,*pNext;
    pNext = head;
    if(head == NULL) {     //空链表
        head = pNode;      //插入到第一个位置
        pNode->next = NULL;
    }else{                 //非空链表                      
        /*
        查找插入位置。
        将pNode->num与pNext->num相比较,
        如果pNode->num > pNext-> num ,
        则待插入的结点应插在pNext所指的结点之后的某个位置,
        此时将pNext后移,并使pPrev指向刚才pNext所指的结点.
        */
        while((pNext != NULL) &&
              (pNode->num > pNext->num))  {
            pPrev = pNext;   
            pNext = pNext->next;
        }
            
        if (pNext == NULL)  {   //未找到插入位置
            pPrev->next = pNode;//插入到链表最后位置
            pNode->next = NULL;
        }else{
            if(head == pNext)   //第一个位置
                head = pNode;
            else                //其他位置
                pPrev->next = pNode;    
          
            pNode->next = pNext;
        }
    }
    return head ;
}
struct student * add_score()
{
    struct student *head = NULL;
    struct student *pC,*pEnd;
    pC =pEnd =(struct student*) malloc(LEN);
    puts("请输入学号(输入0退出):");
    //scanf("INPUT",SCANF);
    scanf("%d",&pC->num);printf("%d",pC->num);
    if(pC->num==0)
    {return head;}
    else
    puts("\n请输入班级(如:1):");
    scanf("%d",&pC->Class);
    puts("\n请输入姓名:");
    scanf("%s",pC->name);
    puts("\n请输入十次成绩:");
    scanf("%d %d %d %d %d %d %d %d %d %d",&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]);
    while(pC->num>0)
    {
        if(head==NULL)
        {
            head=pC;
        }
        else
        {
            pEnd->next = pC;
        }
        pEnd=pC;
   
        head = insert(head , pC);
   
        pC=(struct student*)malloc(LEN);
      
        puts("请输入学号(输入0退出):");
        scanf("%d",&pC->num);
        if(pC->num==0)
        {return head;}
        else
        puts("\n请输入班级(如:一班):");
          scanf("%d",&pC->Class);
        puts("\n请输入姓名:");
         scanf("%s",pC->name);
        puts("\n请输入十次成绩:");
         scanf("%d %d %d %d %d %d %d %d %d %d",&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]);
    }
    pEnd->next=NULL;
    return head;
}
struct student *display_one(struct student *head, int num)
{
    struct student *pC = head;
    if(num==0)
    {return head;}
    else
    {
    if(pC==NULL)
    puts("数据不存在.");
    else if(pC != NULL && num != pC->num)
    {
        printf("%d %d %s %d %d %d %d %d %d %d %d %d %d \n",pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]);
    }
    }
    return head;
}



struct student *display_class(struct student *head,int Class)
{
    struct student *pC = head;
    while(pC!=NULL && Class==pC->Class)
    {
        printf("%d %d %s %d %d %d %d %d %d %d %d %d %d\n",pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]);
        pC=pC->next;
    }
    return head;
}
struct student *alter(struct student *head,int num)
{
    int i,score;
    struct student *pC=head;
  
    if(head==NULL)
    {
        printf("学号不存在.");
        return NULL;
    }
    while(pC!=NULL && num!=pC->num)
    {
        pC=pC->next;
    }
   printf("pC == %p\n", pC);
    while(i!=0)
    {
        printf("您想要更改第几次作业情况?(输入0退出)\n");
           scanf("%d",&i);
           if(i!=0)
           {
               printf("\n请输入分数:");
               scanf("%d",&pC->finish[i-1]);
           }
    }
    return head;
}
struct student *delete_one(struct student *head, int num)
{
    struct student *pC,*pP;
    pC = head;
    if (pC == NULL)
    { 
        printf("没有数据.\n");
        return NULL;
    }
      
    while (pC != NULL && num != pC->num)
    {
         pP = pC;
         pC = pC->next;
    }
  
    if(pC!=NULL)
    {
        printf("删除学号:%d\n", num);
      
        if  (pC == head)
        {
            head = pC->next;                
        }
        else
        {               
            pP->next = pC->next;
        }
       // free(pC); 
    }
    else{
        printf("学号 %d 不存在!\n",num);
    }
  
    return head;      
}
//struct student *
void fileread(struct student *head)
{
    FILE *fp;
    struct student *pC=head;
    if((fp=fopen("record.txt","r+"))==NULL)
    {
        printf("文件打开错误!");
    }
    while(pC!=NULL)
    {
        fscanf(fp,"%d %d %s %d %d %d %d %d %d %d %d %d %d\n",&pC->num,&pC->Class,pC->name,&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]);
        pC=pC->next;
    }
    fclose(fp);
}

void filesave(struct student *head)
{
    FILE *fp;
    struct student *pC=head;

    if((fp=fopen("record.txt","r+"))==NULL)
    {
        printf("文件打开错误!");
    }

    while(pC!=NULL)
    {
        fprintf(fp,"%d %d %s %d %d %d %d %d %d %d %d %d %d\n",pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]);
        pC=pC->next;
    }
    fclose(fp);
}

int main()
{
    struct student *mHead,*pC;
    int num;
    int choice,Class;
    pC=mHead;

    fileread(mHead);

    while (choice!=7)
    {
        printf("\t\t\t欢迎进入学生作业完成情况管理系统\n");
        printf("\t\t\t--------------------------------\n\n\n");
        printf("1.录入某位学生某次作业的分数\n");
        printf("2.给定学号,显示某位学生的作业完成情况\n");
        printf("3.给定某个班级的班号,显示该班所有学生的作业完成情况\n");
        printf("4.给定某位学生的学号,修改该学生的作业完成信息\n");
        printf("5.给定某位学生的学号,删除该学生的信息\n");
        printf("6.提供一些统计各类信息的功能\n");
        printf("7.退出\n\n");
        printf("请选择功能:");

    scanf("%d",&choice);

    switch (choice)
    {
    case 1:
    add_score();break;

    case 2:
    printf("请输入学号:");
    scanf("%d",&num);
    display_one(mHead,num);break;

    case 3:
    printf("请输入班级:");
    scanf("%d",&Class);
    display_class(mHead,Class);break;

    case 4:
    printf("请输入学号:");
    scanf("%d",&num);
    alter(mHead,num);break;

    case 5:
    printf("请输入学号:");
    scanf("%d",&num);
    delete_one(mHead,num);break;

    case 6:

    case 7:
    ;}
    }
    filesave(mHead);

    return 0;
}

搜索更多相关主题的帖子: 设计 管理系统 
2011-12-15 17:49
klpk521
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-28
收藏
得分:0 
回复 4楼 CrystalFan
第一行添加数量是什么意思 求解释
2011-12-16 00:02
快速回复:求助,写入文件的数据不匹配
数据加载中...
 
   



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

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