| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
Reworld,下班在家制作游戏,1500万奖金等你拿以码会友 以友辅仁
共有 474 人关注过本帖
标题:为什么我的程序执行后保存到文件后(比如我在文件中输入20个数,在键盘上输 ...
只看楼主 加入收藏
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
结帖率:57.14%
  问题点数:0  回复次数:1   
为什么我的程序执行后保存到文件后(比如我在文件中输入20个数,在键盘上输入n为15),后面的5个数就乱了?
#include <stdio.h>
#include <stdlib.h>

#define FALSE -1
#define TURE  0
#define Max_size 30
#define LIST_INIT_SIZE 10
typedef int ElemType;

typedef struct
{
     ElemType pBase[Max_size];
    int nLength;
    int nSize;         
}SqList;

int InitList(SqList *pList)    /*线性表初始化*/
{
     pList = (SqList *) malloc(sizeof (SqList));
     pList -> nLength = 0;
     pList -> nSize = Max_size;
     return TURE;      
}

void DestroyList(SqList *pList)        /*销毁线性表*/
{
      free(pList);      
}

int ListLength(SqList *pList)        /*线性表的长度*/
{
      return (pList -> nLength);     
}

int ListEmpty(SqList *pList)        /*判断线性表是否为空*/
{
      if (pList -> nLength == 0)
          return FALSE;
     else
         return TURE;      
}

int ShowList(SqList *pList)        /*输出线性表*/
{
      int pos, n = 0;
      if (ListEmpty (pList))
          return FALSE;
     for (pos = 0; pos < pList -> nLength; pos++)
     {
        printf ("%d  ", pList -> pBase[pos]);
        n++;
        if (n % 5 == 0)
            printf ("\n");
     }
     
     printf ("\n");     
}

int GetElem(SqList *pList, int pos, int e)        /*求线性表中某个元素的值*/
{
      if (pos < 1 || pos > pList -> nLength)
          return FALSE;
     else
         e = pList -> pBase [pos - 1];
         
     return TURE;
         
}

int LocateElem(SqList *pList, int e)        /*查找元素e*/
{
      int pos;
      for (pos = 0; pos < pList -> nLength && pList -> pBase[pos] != e;)
           pos++;
          if (pos >= pList -> nLength)
              return FALSE;
          else
             return (pos + 1);      
}

int ListDelete(SqList *pList, int pos, int e)            /*删除数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     e = pList -> pBase[pos];
     
     for (j = pos; j < pList -> nLength; ++j)
     {
           pList -> pBase[j - 1] = pList -> pBase[j];      
     }
      
     --pList -> nLength;
     return TURE;     
}

int ListInsert(SqList *pList, int pos, int e)            /*插入数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     
     for (j = pList -> nLength; j > pos; j--)
     {
           pList -> pBase[j] = pList -> pBase[j - 1];      
     }
     
     pList -> pBase[pos] = e;
     ++pList -> nLength;
     return TURE;
}

int ReadList(SqList *pList,int n)        /*从文件读入n(n > 10)个整数,填入顺序表*/
{
     int i;
     FILE * fp = fopen("list.txt","rw");
     
     if (fp == NULL)
     {
           printf ("错误!无法读入文件!");
         exit(0);         
     }
     
     for(i = 0; i < n; i++)
        fscanf(fp,"%d ",&pList -> pBase[i]);

     pList -> nLength = n;
     fclose(fp);
     return TURE;
}

int SaveList(SqList *pList)            /*将线性表保存到文件中*/
{
     FILE *fp;
     int i;
     fp=fopen("list.txt","r+");
     
     for(i = 0;i < pList -> nLength; i++)
       fprintf(fp,"%d ", pList -> pBase[i]);
     
    fclose(fp);
    return TURE;
}

int cShowList(SqList *pList)
{
     int pos, n, t = 0;
     if (ListEmpty (pList))
        return FALSE;
    for (pos = 0; pos < (pList -> nLength) / 2; pos++)
    {
       n = pList -> pBase[pos];
       pList -> pBase[pos] = pList -> pBase[pList -> nLength - pos - 1];
       pList -> pBase[pList -> nLength - pos - 1] = n;
    }
   
    for (pos = 0; pos < pList -> nLength; pos++)
    {
         t++;
         printf ("%d  ", pList -> pBase[pos]);
         if (t % 5 == 0)
            printf ("\n");
            
    }        
}

int main()
{      int n, e1, e2;
      SqList pList;
      printf ("(1).初始化线性表\n");
      InitList (&pList);
      
      printf ("请输入n的值:  ");
      scanf ("%d", &n);
      ReadList(&pList, n);
      printf ("\n");
      
      printf ("(2).输出%d个数至屏幕\n", n);
      ShowList(&pList);
      printf ("\n");
      
      printf ("(3).查找元素的位序\n");
      printf ("元素7的位序为%d\n", LocateElem(&pList, 7));
      printf ("\n");
      
      printf ("(4).在顺序表头、中、尾各插一个元素\n");
      ListInsert(&pList, 1, 1);
      ListInsert(&pList, 9, 8);
      ListInsert(&pList, 17, 15);
      ShowList(&pList);
      printf ("\n");
      
      printf ("(5).输入修改后的表至屏幕,并保存至文件\n");
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      printf ("(6).删除顺序表的任意两个元素\n");
      ListDelete(&pList, 5, e1);
      ListDelete(&pList, 11, e2);
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      printf ("(7).输入修改后的表至屏幕,并保存至文件\n");
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      printf ("(8).逆序排列顺序表,输出至屏幕,并保存至文件\n");
      cShowList(&pList);
      SaveList(&pList);
      
      DestroyList(&pList);
      printf ("\n");
      
      system("pause");
            
}
搜索更多相关主题的帖子: include return 线性表 键盘 
2013-09-22 19:46
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
  得分:0 
调试了好长时间了、、、、   谁能帮帮我,,  真心谢谢啊!!!
2013-09-22 19:47
快速回复:为什么我的程序执行后保存到文件后(比如我在文件中输入20个数,在键盘 ...
数据加载中...
 
   





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

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