(1)掌握結構體類型以及結構體變量的定義與使用。
(2)綜合運用結構體、數組、指針等知識,解決相關問題。
(3)會正確定義FILE*指針,掌握文件操作從打開、讀寫到關閉的完整過程。
(4)理解文本文件和二進制文件的區別和不同的讀寫方式。
硬件: 微型計算機
軟件: Windows 操作系統、Microsoft Visual Studio 2010
編寫程序exp8_3.c,驗證用戶輸入的日期格式是否正確,如果不正確,則提示重新輸入,直到重新輸入正確為止。(提示:需要定義一個表示日期的結構體類型struct Date,包括年、月、日信息,并用typedef重新定義新類型名Date;檢查日期是否有效,定義為函數int checkDate(Date date))。
實驗解答:
① 源程序代碼如下:#includestruct Date
{int year;
int month;
int day;
};
typedef struct Date Date;
int checkData(Date date)
{if (date.year< 1900 || date.year>2018)
{return 0;
}
if (date.month< 1 || date.month>12)
{return 0;
}
if (date.month == 1 ||
date.month == 3 ||
date.month == 5 ||
date.month == 7 ||
date.month == 8 ||
date.month == 10 ||
date.month == 12 )
{if (date.day< 1 || date.day >31)
{ return 0;
}
}
else if (date.month == 4 ||
date.month == 6 ||
date.month == 9 ||
date.month == 11)
{if (date.day< 1 || date.day >30)
{ return 0;
}
}
else
{if (date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0)
{ if (date.day< 1 || date.day >29)
{ return 0;
}
}
else
{ if (date.day< 1 || date.day >28)
{ return 0;
}
}
}
return 1;
}
int main()
{Date date; int flag;
do
{printf("please input date!\n");
scanf_s("%d%d%d", &date.year, &date.month, &date.day);
flag = checkData(date);
if (flag)
{ printf("The date is valid!\n");
}
else
{ printf("Invalid date!\n");
}
} while (!flag);
return 0;
}
② 運行程序時,依次輸入下面的幾組年月日數據作為測試用例,觀察程序的運行情況測試用例 輸入的原始數據 需要重新輸入或你的輸出結果
用例1 2002 4 31 please input date!
2002 4 31
Invalid date!
please input date!
用例2 1809 12 3 please input date!
1809 12 3
Invalid date!
please input date!
用例3 2020 5 4 please input date!
2020 5 4
Invalid date!
please input date!
用例4 2000 2 29 please input date!
2000 2 29
The date is valid!
用例5 1908 14 23 please input date!
1908 14 23
Invalid date!
please input date!
用例6 2003 11 -8 please input date!
2003 11 -8
Invalid date!
please input date!
用例7 1996 2 31 please input date!
1996 2 31
Invalid date!
please input date!
用例8 1996 5 19 please input date!
1996 5 19
The date is valid!
實驗題目(2)【見實驗教材實驗九的題目1】:編寫程序exp9_1.c ,從鍵盤讀入一系列字符并以“#”結束,將讀入的字符(不包括#號)存入文本文件D:\f1.txt中,再從該文件讀取內容,并在顯示器上原樣顯示。
實驗解答: 寫出完整的源程序代碼并做適當注釋:
#include#includevoid writeFile(int ch, FILE* fp);
void readFile(int ch, FILE* fp);
int main()
{FILE* fp;
char ch = 0;
fp = fopen("D:\\f1.txt", "w+");
if (fp == NULL)
{printf("file error\n");
exit(1);
}
writeFile(ch, fp);
rewind(fp);
readFile(ch, fp);
fclose(fp);
return 0;
}
void writeFile(int ch, FILE* fp)
{printf("Please enter a string of characters ending with #:\n");
ch = getchar();
while (ch != '#')
{fputc(ch, fp);
ch = getchar();
}
}
void readFile(int ch, FILE* fp)
{while ((ch = fgetc(fp)) != EOF)
{putchar(ch);
}
printf("\n");
}
實驗題目(3)【見實驗教材實驗九的題目2】:某班有學生若干名(不超過40名),其信息的組織采用如下的結構體定義。編寫程序exp9_2.c,完成要求的功能。
struct Student
{char ID[20];
char name[30];
int age;
double score;
};
實驗解答:
①請寫出完整的源程序代碼并做適當注釋:#include#includestruct Student
{char ID[20];
char name[30];
int age;
double score;
};
typedef struct Student Stu;
#define N 50
void CreateFile(Stu [],int n,FILE *fp);
void ReadOut(Stu [],int n,FILE *fp);
void Sort(Stu [],int len);
int main()
{int n,i,x;
Stu stu[N];
FILE *fp=NULL;
do
{printf("Please input the number of students:\n");
scanf("%d",&n);
}while(n<1||n>40);
for(i=0;ix=i+1;
printf("%d(ID name age score):\n",x);
scanf("%s%s%d%lf",stu[i].ID,stu[i].name,&stu[i].age,&stu[i].score);
}
CreateFile(stu,n,fp);
printf("before being sorted:\n");
ReadOut(stu,n,fp);
printf("after being sorted:\n");
Sort(stu,n);
return 0;
}
void CreateFile(Stu stu[],int n,FILE *fp)
{fp=fopen("D:\\Info.dat","wb+");
if(fp==0) //文?件t打?¨°開a后¨?需¨¨判D斷?是o?否¤?正y確¨?¤
{printf("file error\n");
exit(1);
}
fwrite(stu,sizeof(Stu),n,fp);
fclose(fp);
}
void ReadOut(Stu stu[],int n,FILE *fp)
{int i=0;
fp=fopen("D:\\Info.dat","rb");
if(fp==0) //文?件t打?¨°開a后¨?需¨¨判D斷?是o?否¤?正y確¨?¤
{printf("file error\n");
exit(1);
}
fread(&stu[i],sizeof(Stu),n,fp);
for(i=0;iint i,k,index;
Stu temp;
for(k=0;kindex=k;
for(i=k+1;istu[index].score)
index=i;
if(index!=k)
{ temp=stu[index];
stu[index]=stu[k];
stu[k]=temp;
}
}
for(i=0;i
② 運行程序,你從鍵盤輸入的內容以及屏幕顯示的結果如下:Please input the number of students:
3
1(ID name age score):
101 a 19 90
2(ID name age score):
102 b 18 99
3(ID name age score):
103 y 20 78
before being sorted:
101 a 19 90.00
102 b 18 99.00
103 y 20 78.00
after being sorted:
102 b 18 99.00
101 a 19 90.00
103 y 20 78.00
請按任意鍵繼續. . .
四、實驗小結(包括問題和解決方法、心得體會、意見與建議、實驗出錯信息及解決方案等)
(一)實驗中遇到的主要問題及解決方法驗證日起合法性的判斷條件,太繁瑣??磿系睦}后找到可以將月份的天數存入數組。
(二)實驗心得對于文件的操作還應該繼續熟悉與拓展。
多上機調試。
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
名稱欄目:南京郵電大學高級語言程序設計實驗六(結構體與文件實驗)-創新互聯
本文鏈接:http://www.2m8n56k.cn/article42/dhiehc.html
成都網站建設公司_創新互聯,為您提供軟件開發、Google、App開發、手機網站建設、定制開發、定制網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯