status SaveList(SqList L,char FileName[])
// 如果線性表L存在,將線性表L的的元素寫到FileName檔案中,回傳OK,否則回傳INFEASIBLE。
{
// 請在這里補充代碼,完成本關任務
/********** Begin *********/
if(!L.elem) return INFEASIBLE;
else
{
FILE *fp;
if((fp = fopen(FileName,"wb+")) == NULL)
return ERROR;
for(int i = 0;i < L.length;i++)
fprintf(fp,"%d ",L.elem[i]);
fclose(fp);
return OK;
}
/********** End **********/
}
status LoadList(SqList &L,char FileName[])
// 如果線性表L不存在,將FileName檔案中的資料讀入到線性表L中,回傳OK,否則回傳INFEASIBLE。
{
// 請在這里補充代碼,完成本關任務
/********** Begin *********/
if(!L.elem)
{
L.length = 0;
FILE *fp1;
if((fp1 = fopen(FileName,"r")) == NULL)
return ERROR;
while(fread(&L.elem[L.length],sizeof(ElemType),1,fp1))
L.length++;
fclose(fp1);
return OK;
}
else return INFEASIBLE;
/********** End **********/
}
平臺會自動讀取輸入資料,對你撰寫的代碼進行測驗,并輸出結果。
測驗輸入:0;此時表示對不存在的線性表進行寫檔案操作,或表示讀入檔案資料覆寫已存在的線性表導致資料丟失。
預期輸出:
INFEASIBLE
測驗輸入:1 11 12 13 14 15 0,第一個數1表示對一個已存在的線性表進行操作,后續是一個以0結束的資料元素序列。
預期輸出:
5
11 12 13 14 15
就當輸入第二個測驗集時什么都沒輸出
這是main函式
#include "def.h"
#include "string.h"
#include "stu.h"
int main()
{
SqList L;
FILE *fp;
char FileName[30];
int f,i=0,j,e;
strcpy(FileName,"src/step13/list.dat");
scanf("%d",&f);
if (!f)
{
L.elem=NULL;
j=SaveList(L,FileName);
if (j!=INFEASIBLE) printf("不能對不存在的線性表進行寫檔案操作!");
else
{
L.elem=(ElemType *) malloc(sizeof(ElemType));
j=LoadList(L,FileName);
if (j!=INFEASIBLE) printf("不能對已存在的線性表進行讀檔案操作!");
else printf("INFEASIBLE");
free(L.elem);
}
}
else
{
L.elem=(ElemType *) malloc(sizeof(ElemType)*LIST_INIT_SIZE);
L.length=0;
L.listsize= LIST_INIT_SIZE;
scanf("%d",&e);
while (e)
{
L.elem[i++]=e;
scanf("%d",&e);
}
L.length=i;
j=SaveList(L,FileName);
free(L.elem);
L.elem=NULL;
j=LoadList(L,FileName);
printf("%d\n",L.length);
for(i=0;i<L.length;i++)
printf("%d ",L.elem[i]);
}
return 1;
}
uj5u.com熱心網友回復:
題目呢

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/159456.html
標籤:C語言
上一篇:Mac安裝Tomcat
