现在的位置: 首页 > 14 二级C语言 > 正文

2011年9月全国二级C上机真题第7~8套

2013年02月25日 14 二级C语言 ⁄ 共 1473字 ⁄ 字号 暂无评论

第7~8套

【程序填空题_7】程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件中最后一个学生的数据,即用新的学生数据覆盖该学生原来的数据,其它学生的数据不变。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include  <stdio.h>
#define    N    5
typedef struct  student {
    long  sno;
    char  name[10];
    float  score[3];
} STU;
 
void fun(char  *filename, STU  n)
{
    FILE  *fp;
    /**********found**********/
    fp = fopen(__1__, "rb+");
    /**********found**********/
    fseek(__2__, -(long)sizeof(STU), SEEK_END);
    /**********found**********/
    fwrite(&n, sizeof(STU), 1, __3__);
    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}
    };
    STU  n= {10006,"ZhaoSi", 55, 70, 68}, ss[N];
    int  i,j;
    FILE  *fp;
    fp = fopen("student.dat", "wb");
    fwrite(t, sizeof(STU), N, fp);
    fclose(fp);
    fp = fopen("student.dat", "rb");
    fread(ss, sizeof(STU), N, fp);
    fclose(fp);
    printf("\nThe original data :\n\n");
    for (j=0; j<N; 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");
    }
}

【程序修改题_7】给定程序MODI1.C中函数Creatlink的功能是创建带头结点的单向链表,并为各结点数据域赋0到m-1的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
typedef  struct  aa {
    int  data;
    struct  aa  *next;
} NODE;
NODE *Creatlink(int  n, int  m)
{
    NODE  *h=NULL, *p, *s;
    int  i;
    /**********found***********/
    p=(NODE )malloc(sizeof(NODE));
    h=p;
    p->next=NULL;
    for(i=1; i<=n; i++) {
        s=(NODE *)malloc(sizeof(NODE));
        s->data=rand()%m;
        s->next=p->next;
        p->next=s;
        p=p->next;
    }
    /**********found***********/
    return  p;
}
outlink(NODE  *h)
{
    NODE  *p;
    p=h->next;
    printf("\n\nTHE  LIST :\n\n  HEAD ");
    while(p) {
        printf("->%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
 
main()
{
    NODE  *head;
    head=Creatlink(8,22);
    outlink(head);
}

【程序设计题_7】请编写函数fun,函数的功能是:统计一行字符串中单词的个数,作为函数值返回。一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间由若干个空格隔开,一行的开始没有空格。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
#define    N   80
int  fun( char  *s)
{    
 
}
main()
{  char  line[N];    int  num=0;
   printf("Enter a string :\n"); gets(line);
   num=fun(line);
   printf("The number of word is  :  %d\n\n",num);
}

抱歉!评论已关闭.