為大家收集整理了《2013計(jì)算機(jī)二級C語言上機(jī)練習(xí)題及答案(15)》供大家參考,希望對大家有所幫助?。?!
程序通過定義學(xué)生結(jié)構(gòu)體變量,存儲(chǔ)了學(xué)生的學(xué)號、姓名和3門課的成績。所 有學(xué)生數(shù)據(jù)均以二進(jìn)制方式輸出到student.dat文件中。函數(shù)fun的功能是從指定 文件中找出指定學(xué)號的學(xué)生數(shù)據(jù),讀入此學(xué)生數(shù)據(jù),對該生的分?jǐn)?shù)進(jìn)行修改,使 每門課的分?jǐn)?shù)加3分,修改后重寫文件中該學(xué)生的數(shù)據(jù),即用該學(xué)生的新數(shù)據(jù)覆蓋 原數(shù)據(jù),其它學(xué)生數(shù)據(jù)不變;若找不到,則什么都不做。 請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除, 使程序得出正確的結(jié)果。
注意:源程序存放在考生文件夾下的BLANK1.C中。
不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
給定源程序:
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, long sno)
{ FILE *fp;
STU n; int i;
fp = fopen(filename,"rb+");
while (!feof(__1__))
{ fread(&n, sizeof(STU), 1, fp);
if (n.sno__2__sno) break;
}
if (!feof(fp))
{ for (i=0; i<3; i++) n.score[i] += 3;
fseek(__3__, -1L*sizeof(STU), SEEK_CUR);
fwrite(&n, sizeof(STU), 1, fp);
}
fclose(fp);
}
main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
fun("student.dat", 10003);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe data after modifing :\n");
for (j=0; j
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
}
解題思路:
本題是考察如何從指定文件中找出指定學(xué)號的學(xué)生數(shù)據(jù),并進(jìn)行適當(dāng)?shù)男薷模薷暮笾匦聦懟氐轿募性搶W(xué)生的數(shù)據(jù)上,即用該學(xué)生的新數(shù)據(jù)覆蓋原數(shù)據(jù)。
第一處:判斷讀文件是否結(jié)束,所以應(yīng)填:fp。
第二處:從讀出的數(shù)據(jù)中判斷是否是指定的學(xué)號,其中學(xué)號是由形參sno來傳遞的,所以應(yīng)填:==。
第三處:從已打開文件fp中重新定位當(dāng)前讀出的結(jié)構(gòu)位置,所以應(yīng)填:fp。
給定程序MODI1.C中函數(shù)fun的功能是:利用插入排序法對字符串中的字符按從小到大的順序進(jìn)行排序。插入法的基本算法是:先對字符串中的頭兩個(gè)元素進(jìn)行排序。然后把第三個(gè)字符插入到前兩個(gè)字符中,插入后前三個(gè)字符依然有序; 再把第四個(gè)字符插入到前三個(gè)字符中,……。待排序的字符串已在主函數(shù)中賦予。
請改正程序中的錯(cuò)誤,使它能得出正確結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
給定源程序:
#include
#include
#define N 80
void insert(char *aa)
{ int i,j,n; char ch;
n=strlen[ aa ];
for( i=1; i
c=aa[i];
j=i-1;
while ((j>=0) && ( ch
{ aa[j+1]=aa[j];
j--;
}
aa[j+1]=ch;
}
}
main( )
{ char a[N]="QWERTYUIOPASDFGHJKLMNBVCXZ";
int i ;
printf ("The original string : %s\n", a);
insert(a) ;
printf("The string after sorting : %s\n\n",a );
}
解題思路:
第一處: 函數(shù)應(yīng)該使用圓括號,所以應(yīng)改為:n=strlen(aa) ;。
第二處: 變量c沒有定義,但后面使用的是ch變量,所以應(yīng)改為:ch=aa[i];。
N名學(xué)生的成績已在主函數(shù)中放入一個(gè)帶頭節(jié)點(diǎn)的鏈表結(jié)構(gòu)中,h指向鏈表的頭節(jié)點(diǎn)。請編寫函數(shù)fun,它的功能是:找出學(xué)生的高分,由函數(shù)值返回。
注意: 部分源程序在文件PROG1.C文件中。
請勿改動(dòng)主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入你編寫的若干語句。
給定源程序:
#include
#include
#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun( STREC *h )
{
}
STREC * creat( double *s)
{ STREC *h,*p,*q; int i=0;
h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
while(i
{ q=(STREC*)malloc(sizeof(STREC));
q->s=s[i]; i++; p->next=q; p=q;
}
p->next=0;
return h;
}
outlist( STREC *h)
{ STREC *p;
p=h->next; printf("head");
do
{ printf("->%2.0f",p->s);p=p->next;}
while(p!=0);
printf("\n\n");
}
main()
{ double s[N]={85,76,69,85,91,72,64,87}, max;
STREC *h;
h=creat( s ); outlist(h);
max=fun( h );
printf("max=%6.1f\n",max);
NONO();
}
解題思路:
本題是考察如何從鏈表中求出學(xué)生的高分。
我們給出的程序是利用while循環(huán)語句以及臨時(shí)結(jié)構(gòu)指針p變量來求出高分。
1. 將鏈表中的第1個(gè)值賦給變量max。
2. 將鏈表指針p的初始位置指向h的next指針(h->next)。
3. 判斷p指針是否結(jié)束,如果結(jié)束,則返回max,否則做下一步。
4. 判斷max是否小于p->s,如果小于,則max取p->s,否則不替換。
5. 取p->next賦值給p(取下一結(jié)點(diǎn)位置給p),轉(zhuǎn)3繼續(xù)。
參考答案:
double fun( STREC *h )
{
double max=h->s;
STREC *p;
p=h->next;
while(p)
{ if(p->s>max )
max=p->s;
p=p->next;
}
return max;
}
STREC * creat( double *s)
{ STREC *h,*p,*q; int i=0;
h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
while(i
{ q=(STREC*)malloc(sizeof(STREC));
q->s=s[i]; i++; p->next=q; p=q;
}
p->next=0;
return h;
}
outlist( STREC *h)
{ STREC *p;
p=h->next; printf("head");
do
{ printf("->%2.0f",p->s);p=p->next;}
while(p!=0);
printf("\n\n");
}
程序通過定義學(xué)生結(jié)構(gòu)體變量,存儲(chǔ)了學(xué)生的學(xué)號、姓名和3門課的成績。所 有學(xué)生數(shù)據(jù)均以二進(jìn)制方式輸出到student.dat文件中。函數(shù)fun的功能是從指定 文件中找出指定學(xué)號的學(xué)生數(shù)據(jù),讀入此學(xué)生數(shù)據(jù),對該生的分?jǐn)?shù)進(jìn)行修改,使 每門課的分?jǐn)?shù)加3分,修改后重寫文件中該學(xué)生的數(shù)據(jù),即用該學(xué)生的新數(shù)據(jù)覆蓋 原數(shù)據(jù),其它學(xué)生數(shù)據(jù)不變;若找不到,則什么都不做。 請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除, 使程序得出正確的結(jié)果。
注意:源程序存放在考生文件夾下的BLANK1.C中。
不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
給定源程序:
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, long sno)
{ FILE *fp;
STU n; int i;
fp = fopen(filename,"rb+");
while (!feof(__1__))
{ fread(&n, sizeof(STU), 1, fp);
if (n.sno__2__sno) break;
}
if (!feof(fp))
{ for (i=0; i<3; i++) n.score[i] += 3;
fseek(__3__, -1L*sizeof(STU), SEEK_CUR);
fwrite(&n, sizeof(STU), 1, fp);
}
fclose(fp);
}
main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
fun("student.dat", 10003);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe data after modifing :\n");
for (j=0; j
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
}
解題思路:
本題是考察如何從指定文件中找出指定學(xué)號的學(xué)生數(shù)據(jù),并進(jìn)行適當(dāng)?shù)男薷模薷暮笾匦聦懟氐轿募性搶W(xué)生的數(shù)據(jù)上,即用該學(xué)生的新數(shù)據(jù)覆蓋原數(shù)據(jù)。
第一處:判斷讀文件是否結(jié)束,所以應(yīng)填:fp。
第二處:從讀出的數(shù)據(jù)中判斷是否是指定的學(xué)號,其中學(xué)號是由形參sno來傳遞的,所以應(yīng)填:==。
第三處:從已打開文件fp中重新定位當(dāng)前讀出的結(jié)構(gòu)位置,所以應(yīng)填:fp。
給定程序MODI1.C中函數(shù)fun的功能是:利用插入排序法對字符串中的字符按從小到大的順序進(jìn)行排序。插入法的基本算法是:先對字符串中的頭兩個(gè)元素進(jìn)行排序。然后把第三個(gè)字符插入到前兩個(gè)字符中,插入后前三個(gè)字符依然有序; 再把第四個(gè)字符插入到前三個(gè)字符中,……。待排序的字符串已在主函數(shù)中賦予。
請改正程序中的錯(cuò)誤,使它能得出正確結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
給定源程序:
#include
#include
#define N 80
void insert(char *aa)
{ int i,j,n; char ch;
n=strlen[ aa ];
for( i=1; i
c=aa[i];
j=i-1;
while ((j>=0) && ( ch
{ aa[j+1]=aa[j];
j--;
}
aa[j+1]=ch;
}
}
main( )
{ char a[N]="QWERTYUIOPASDFGHJKLMNBVCXZ";
int i ;
printf ("The original string : %s\n", a);
insert(a) ;
printf("The string after sorting : %s\n\n",a );
}
解題思路:
第一處: 函數(shù)應(yīng)該使用圓括號,所以應(yīng)改為:n=strlen(aa) ;。
第二處: 變量c沒有定義,但后面使用的是ch變量,所以應(yīng)改為:ch=aa[i];。
N名學(xué)生的成績已在主函數(shù)中放入一個(gè)帶頭節(jié)點(diǎn)的鏈表結(jié)構(gòu)中,h指向鏈表的頭節(jié)點(diǎn)。請編寫函數(shù)fun,它的功能是:找出學(xué)生的高分,由函數(shù)值返回。
注意: 部分源程序在文件PROG1.C文件中。
請勿改動(dòng)主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入你編寫的若干語句。
給定源程序:
#include
#include
#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun( STREC *h )
{
}
STREC * creat( double *s)
{ STREC *h,*p,*q; int i=0;
h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
while(i
{ q=(STREC*)malloc(sizeof(STREC));
q->s=s[i]; i++; p->next=q; p=q;
}
p->next=0;
return h;
}
outlist( STREC *h)
{ STREC *p;
p=h->next; printf("head");
do
{ printf("->%2.0f",p->s);p=p->next;}
while(p!=0);
printf("\n\n");
}
main()
{ double s[N]={85,76,69,85,91,72,64,87}, max;
STREC *h;
h=creat( s ); outlist(h);
max=fun( h );
printf("max=%6.1f\n",max);
NONO();
}
解題思路:
本題是考察如何從鏈表中求出學(xué)生的高分。
我們給出的程序是利用while循環(huán)語句以及臨時(shí)結(jié)構(gòu)指針p變量來求出高分。
1. 將鏈表中的第1個(gè)值賦給變量max。
2. 將鏈表指針p的初始位置指向h的next指針(h->next)。
3. 判斷p指針是否結(jié)束,如果結(jié)束,則返回max,否則做下一步。
4. 判斷max是否小于p->s,如果小于,則max取p->s,否則不替換。
5. 取p->next賦值給p(取下一結(jié)點(diǎn)位置給p),轉(zhuǎn)3繼續(xù)。
參考答案:
double fun( STREC *h )
{
double max=h->s;
STREC *p;
p=h->next;
while(p)
{ if(p->s>max )
max=p->s;
p=p->next;
}
return max;
}
STREC * creat( double *s)
{ STREC *h,*p,*q; int i=0;
h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
while(i
{ q=(STREC*)malloc(sizeof(STREC));
q->s=s[i]; i++; p->next=q; p=q;
}
p->next=0;
return h;
}
outlist( STREC *h)
{ STREC *p;
p=h->next; printf("head");
do
{ printf("->%2.0f",p->s);p=p->next;}
while(p!=0);
printf("\n\n");
}