第一篇:西北農(nóng)林科技大學(xué) c語言上機 實習(xí)8答案
1.文本文件中字符個數(shù)統(tǒng)計 #include
char ch, filename[200];
int count = 0;
FILE *fp;
scanf(“%s”, filename);
if((fp = fopen(filename, “r”))== NULL)
{
printf(“File open error!n”);
exit(1);
}
while((ch = fgetc(fp))!= EOF)
{
count++;
}
printf(“%dn”,count);
if(fclose(fp))
{
printf(“File close error!n”);
exit(1);
}
return 0;}
2.文件中數(shù)據(jù)的排序 #include
int a[10], temp = 0, i = 0, j = 0, k = 0;
char filename1[80], filename2[80];
FILE *fp1,*fp2;
scanf(“%s”, filename1);
scanf(“%s”, filename2);
if((fp1 = fopen(filename1, “rb”))== NULL)
{
printf(“Input file open error!n”);
exit(1);
}
if((fp2 = fopen(filename2,“wb”))== NULL)
{
printf(“Output file create error!n”);
exit(1);
}
for(i = 0;i < 10;i++)
{
fread(&a[i], sizeof(int), 1, fp1);
/* 讀出數(shù)據(jù) */
}
for(i = 0;i < 9;i++)
{
k = i;
for(j = i + 1;j < 10;j++)
if(a[k] > a[j])k = j;
if(k!= i)
{
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
} for(i = 0;i < 10;i++)
/*
{
fwrite(&a[i], sizeof(int), 1, fp2);
}
if(fclose(fp1))
{
printf(“Input file close error!n”);
exit(1);
}
if(fclose(fp2))
{
printf(“Output file close error!n”);
exit(1);
}
/*驗證結(jié)果的代碼,請不要改動*/
if((fp1 = fopen(filename2, “rb”))== NULL)
{
printf(“Result file open error!n”);
exit(1);
}
/* 排序 */ 寫入數(shù)據(jù) */
for(i = 0;i < 10;i++)
{
fread(&a[i], sizeof(int), 1, fp1);
/* 讀出數(shù)據(jù) */
}
if(fclose(fp1))
{
printf(“Result file close error!n”);
exit(1);
}
for(i = 0;i < 9;i++)
printf(“%d ”, a[i]);
printf(“%dn”, a[i]);
return 0;} 3 二進制數(shù)據(jù)文件/ #include
int a[10], temp = 0, i = 0, j = 0, k = 0;
char filename1[80], filename2[80];
FILE *fp1,*fp2;
scanf(“%s”, filename1);
scanf(“%s”, filename2);
if((fp1 = fopen(filename1, “r”))== NULL)
{
printf(“Input file open error!n”);
exit(1);
}
if((fp2 = fopen(filename2, “wb”))== NULL)
{
printf(“Output file open error!n”);
exit(1);
}
for(i = 0;i < 10;i++)
{
fscanf(fp1, “%d”, &a[i]);
fwrite(&a[i], sizeof(int), 1, fp2);
}
if(fclose(fp1))
{
printf(“Input file close error!n”);
exit(1);
}
if(fclose(fp2))
{
printf(“Output file close error!n”);
exit(1);
}
if((fp1 = fopen(filename2, “rb”))== NULL)
{
printf(“Result file open error!n”);
exit(1);
}
for(i = 0;i < 10;i++)
{
fread(&a[i], sizeof(int), 1, fp1);
}
if(fclose(fp1))
{
printf(“Result file close error!n”);
exit(1);
}
for(i = 0;i < 9;i++)
printf(“%d ”, a[i]);
printf(“%dn”, a[i]);
return 0;}
4.比較2個文本文件的內(nèi)容 #include
int i = 1, flag = 0;
char filename1[80], filename2[80];
FILE *fp1, *fp2;
/* 讀出數(shù)據(jù) */
scanf(“%s”, filename1);
scanf(“%s”, filename2);
if((fp1 = fopen(filename1, “r”))== NULL)
{
printf(“Input file1 open error!n”);
exit(1);
}
if((fp2 = fopen(filename2,“r”))== NULL)
{
printf(“Input file2 open error!n”);
exit(1);
}
while(!feof(fp1))
{
if(fgetc(fp1)== fgetc(fp2))
i++;
else
{
flag = 1;
break;
}
}
if(flag)
printf(“%dn”, i);
else
printf(“is equaln”);if(fclose(fp1))
{
printf(“Input file1 close error!n”);
exit(1);
}
if(fclose(fp2))
{
printf(“Input file2 close error!n”);
exit(1);
}
return 0;}
第二篇:西北農(nóng)林科技大學(xué) c語言上機 實習(xí)5答案
實習(xí)五答案
1、用指針實現(xiàn)排序
/* exer 5-1 由鍵盤輸入 10個整數(shù),將它們按由小到大順序排列 */ #include
int a[10],*p = NULL;
/* 說明數(shù)組和指針 */
int i, j, temp;
for(p = a;p < a + 10;p++)
/* 指針從數(shù)組首到尾 */
{
scanf(“%d”, p);
/* 利用指針依次輸入 */
}
p = a;
for(i = 0;i < 9;i++)
/* 利用指針依次比較 */
{
for(j = i + 1;j < 10;j++)
if(*(p + i)> *(p + j))
{
temp = *(p + i);
*(p + i)= *(p + j);
*(p + j)= temp;
}
}
for(p=a;p < a + 9;p++)
printf(“%d ”, *p);
printf(“%dn”, *p);}
2、用指針實現(xiàn)字符串排序
/*exer 5-2 將 10個長度小于20的字符串排序*/ #include
char szStr[10][20],*psz[10],szTemp[20];
int i,j;
for(i = 0;i < 10;i++)
{
psz[i] = szStr[i];
/*指針數(shù)組的每個元素指向各行串*/
}
for(i = 0;i < 10;i++)
{
gets(psz[i]);
/*輸入10個字符串*/
}
for(i = 0;i < 9;i++)
/*字符串排序*/
{
for(j = i + 1;j < 10;j++)
{
if(strcmp(psz[i], psz[j])> 0)
{
strcpy(szTemp, psz[i]);
strcpy(psz[i], psz[j]);
strcpy(psz[j], szTemp);
}
}
}
for(i = 0;i < 10;i++)
{
puts(psz[i]);
/*輸出字符串*/
} }
3、數(shù)據(jù)倒置
/*exer 5-3 將具有10個元素的一維數(shù)組中的數(shù)據(jù)倒置 */ #include
int a[10], iTemp, *p, *q;
int i;
for(i = 0;i < 10;i++)
{
scanf(“%d”, a + i);
/*輸入數(shù)組*/
}
p = a;
q = a + 9;
for(p = a, q = a + 9;p < q;p++, q--)
/*首尾交換*/
{
iTemp = *p;
*p = *q;
*q = iTemp;
}
for(i = 0;i < 10;i++)
{
printf(“%d ”, *(a + i));
/*輸出*/
} }
4、用指針實現(xiàn)數(shù)據(jù)位置調(diào)整 /*exer 5-4 輸入 10個整數(shù),將最大的調(diào)到最后,最小的調(diào)到最前*/ #include
void swap(int *px, int *py);
int main(void){
int a[10], iTemp, *pMax, *pMin;
int i;
for(i = 0;i < 10;i++)
{
scanf(“%d”, a + i);/*輸入數(shù)組*/
}
pMax = a;
/*最大值指針指向數(shù)組首地址*/
pMin = a;
/*最小值指針指向數(shù)組首地址*/
for(i = 1;i < 10;i++)
{
if(*(a+i)> *pMax)
pMax = a + i;
if(*(a+i)< *pMin)
pMin = a + i;
}
/*最大值是第1個元素,最小值是最后一個元素*/
if(pMax == a && pMin == a + 9)
{
swap(pMax, pMin);
}
else
{
/*最小值與第一個元素交換*/
swap(a, pMin);
/*最大值與最后一個元素交換*/
swap(a + 9, pMax);
}
for(i = 0;i < 10;i++)
{
printf(“%d ”, *(a + i));
/*輸出*/
}
return 0;}
void swap(int *px, int *py){
int temp;
temp = *px;
*px = *py;
*py = temp;}
5、用指針實現(xiàn)查找二維數(shù)組中最大數(shù)及其位置
/*exer 5-5 找出二維數(shù)組(設(shè)3行4列)中的最大數(shù)及其位置 */ #include
int a[3][4],(*p)[4], iCol, iRow;
int i, j;
p = a;
for(i = 0;i < 3;i++)
{
for(j = 0;j < 4;j++)
scanf(“%d”, *(p + i)+ j);
/*輸入數(shù)組*/
}
iCol = 0;
/*設(shè)最大數(shù)及其位置 */
iRow = 0;
for(i = 0;i < 3;i++)
{
for(j = 0;j < 4;j++)
{
if(*(*(p + i)+ j)> *(*(p + iRow)+ iCol))
{
iRow = i;
iCol = j;
/*修改位置*/
}
}
}
printf(“%d %d %dn”, iRow, iCol, a[iRow][iCol]);}
6、用指針實現(xiàn)子字符串提取
/*exer 5-6 由鍵盤輸入一串字符,從字符串下標為m開始
取出n個字符(m和n由鍵盤輸入),形成一個新的字符串
*/ #include
char szStrSour[80], szStrDest[80], *pszSour, *pszDest;
int i, m, n;
gets(szStrSour);
/* 輸入字符串 */
scanf(“%d%d”, &m, &n);
/* 輸入位置和長度 */
pszSour = szStrSour + m;
/* 確定起始位置 */
pszDest = szStrDest;
for(i = 0;i < n;i++)
/* 依次取n個字符 */
{
*pszDest = *pszSour;
pszDest++;
pszSour++;
}
*pszDest = '