上海交大C++程序设计试题集(含答案)(4)

2019-03-28 20:51

}NODE2;

NODE2 *f(NODE1 *h)

{ NODE2 *u, *v, *p, *list;

for(list = NULL; h ; h = h->next) { /* 顺序考察已知链表 */

for(u = NULL, v = list; ___(1)___ ; u = v, v = v->next);/* 寻找插入位置 */ if ( ___(2)___ ) v->count++;

else { p = (NODE2 *)malloc(sizeof(NODE2));

p->val = h->val; ___(3)___ ;

if(___(4)___) list = p; else u->next = p; ___(5)___ ; } } return list; }

4.11.某服务公司征询顾客意见,以考核公司的 n(<40) 位服务员的工作业绩。设服务员已按 1、2、3、?顺序连续编号,顾客意见是顺序列出第一名(最佳)至第十名的服务员编号。设所收到的信息已存于正文文件SOURCE.DAT中,每位顾客给出的10服务员编号用一个正文行表示,服务员编号之间用空白符分隔,某些名次位置上的服务员编号可以为 0,表示该顾客认为相应名次空缺,没有合适的服务员,编号0称为空缺编号。

若一行意见信息中有不是服务员编号的整数,也不是空缺编号,或所给的服务员编号有重复出现,或一行有十个以上编号,或不足十个编号(包括空缺编号),则这条意见信息作废。 程序综合顾客对各服务员的评定情况,给每位服务员累计计分,各名次得分标准如下: 一 二 三 四 五 六 七 八 九 十 15 12 9 7 6 5 4 3 2 1

程序最后顺序输出各服务员各名次所得票数和他的合计得分。

程序中函数fgets(buf, 80, fp)实现从指定文件读入一行信息,若文件结束返回NULL,否则返回buf。

#include #define N 40

#define FNAME \

int mark[] = {15, 12, 9, 7, 6, 5, 4, 3, 2, 1};

int score[N][10]; /* score[i][j] 是 i 号服务员得j+1名的票数 */ char buf[81], *p;

FILE *fp;

int i, j, k, c, d, error, b[10];

void main()

{ for(i = 0; i < N; i++) for(j = 0; j < 10; j++) score[i][j] = 0; if((fp = fopen(FNAME, \

{ printf(\ }

while ((p = fgets(buf, 80, fp)) != NULL) {

error = 0; k = 0; /* error出错标志, k 编号个数*/

while (!error) {

16--34 注:解答写在答卷纸上,试卷中的解答不评分

while (*p == ' ' || *p == '\\t') p++; /* 跳过空白类字符 */ if (*p == '\\0' || *p == '\\n') { /* 一行结束 */

error = ___(1)___ ; break; /* 检查编号个数 */

}

if (*p < '0' || *p > '9') { /* 有非法字符 */

error = 1; break; /* 设定有错标志后,结束当前行 */

}

if (k == 10) { /* 已有了十个编号,不可再有 */

error = 1; break; /* 设定有错标志后,结束当前行 */ }

c = 0; /* 译出一个编号 */

while (*p >= '0' && *p <= '9') {

c = ___(2)___ ; p++;

}

if (c >= N) { error = 1; break; } /* 非法编号 */ b[k++] = c; /* 编号存入 */

if (c != 0) { /* 一个非空缺编号 */

for(i = 0; ___(3)___ ; i++); /* 检查服务员编号是否有重复*/ error = ___(4)___ ; /* 如有重复编号, 则置出错标志 */ }

}

if (!error)

for(i = 0; i < k; i++) if (b[i]) ___(5)___ ; }

fclose(fp);

for(i=1; i < N; i++) /* 输出结果 */

{ printf(\输出服务员编号 */

for(d = 0, j = 0; j < 10; j++) {

printf(\

d += score[i][j] * mark[j]; /* 求合计得分 */ }

printf(\输出合计得分 */ }

printf(\ }

4.12.以下函数的功能是已知链表首指针, 将链表颠倒,返回颠倒后的链表的首指针。设链表表元类型NODE的定义如下所示。 typedef struct node {

int val; struct node *next; } NODE;

NODE * reverse (NODE *h) { NODE *p, *v1, *v2;

v2 = h;/* v2 指向链表的首表元*/ v1 = NULL; /* 已颠倒部分为空*/

17--34 注:解答写在答卷纸上,试卷中的解答不评分

while (v2 != NULL) { /* 还未颠倒完,循环 */

p = v2->next; ____(6)____ ; v1 = v2; v2 = p; }

____(7)____ ;

}

4.13.已知数组的n个元素,生成一个从小到大的有序链表,函数返回链表首指针。 设链表表元类型NODE的定义如下所示。 typedef struct node {

int val; struct node *next; } NODE;

NODE *cSortList(int *a, int n)

{ NODE *u, *w, *p, *h = NULL; int k;

for(k = 0; k < n; k++) { p = (NODE *)malloc(sizeof(NODE)); p->val = a[k]; u = h;

while ( ____(8)____ ) { w = u; u = u->next; }

if ( ____(9)____ ) h = p; else _____(10)____ ; p->next = u; }

return h;

}

4.14本题给出的函数orderStd(stdType std[], int n)根据存于数组中的学生成绩信息,求各学生从高分到低分的名次。要求成绩相同的学生,他们的名次相同,所有名次连续编号,不考虑同一名次的人数多少。设数组的每个元素对应一个学生,是一个结构,有学生的学号、姓名、成绩和名次。函数先按成绩从高分到低分顺序排序,然后求得学生的名次。排序采用改进的冒泡法,即下一次比较范围的上界是上一轮扫视比较时最后一次交换的位置。另为了避免排序时交换结构,引入指针数组,变交换结构为交换指针。 typedef struct node {

char no[8]; /* 存储学号 */

char *name; /* 存储指向名字字符串的指针 */ int score; /* 存储成绩 */ int order; /* 存储名次 */ }stdType;

stdType **ptar, *t;

void orderStd(stdType std[ ], int n) { int m, i, j;

ptar = (stdType **)malloc( ___(1)___ );

for(i = 0; i < n; i++) /* 为数组 ptar 设定初值 */

18--34 注:解答写在答卷纸上,试卷中的解答不评分

ptar[i] = std+i;

/* 以下采用冒泡法排序, 按成绩由高到低排序 */ m = n-1;

while ( ___(2)___ ) {

for( j = 0, i = 0; i < m; i++) if ( ___(3)___ ) { t = ptar[i];

ptar[i] = ptar[i+1]; ptar[i+1] = t; j = i ; }

m = j; }

for(ptar[0]->order = i = 1; i < n; i++) /* 按排名次 */ ptar[i]->order = ptar[i]->score == ___(4)___ ? ___(5)___ ; free(ptar); }

4.15函数

intNode * searchDOLink(intNode *h, int key, intNode ** pp)

实现在已知有序(从小到大)链表h中查找值为key的新结点的插入位置(插入结点的前驱结点指针通过指针参数pp带回,插入结点的后继结点指针通过函数值返回)。

函数intNode *sortCopy(intNode *t)利用函数searchDOLink ()实现由已知链表t复制出一个有序链表返回。设链表表元类型intNode的定义如下: typedef struct node { int val;

struct node *next; } intNode;

intNode * searchDOLink(intNode *h, int key, intNode ** pp) { intNode *v = h, *u = NULL;

while ( (1) && (2) ) { u = v; v = v->next; }

*pp = u; return v; }

4.16函数intNode *sortCopy(intNode *t)利用函数searchDOLink ()实现由已知链表t复制出一个有序链表返回。设链表表元类型intNode的定义如下: typedef struct node { int val;

struct node *next; } intNode;

19--34 注:解答写在答卷纸上,试卷中的解答不评分

intNode *sortCopy(intNode *t)

{ intNode *h = NULL,/* 新链表的首指针 */

*q, /*新结点插入处的前驱结点指针 */ *p, /*新结点插入处的后驱结点指针 */ *W /*新结点指针 */ ;

for(; t != NULL; t = t->next) { /*逐一考察原链表的结点*/ p = searchDOLink( ___(1)___ );

w = (intNode *)malloc(sizeof(intNode)); w->val = t->val; w->next = p;

if(q == NULL) (2) = w; else (3) = w; }

return h; }

4.17以下程序找出所有各位数字的立方和等于1099的3位数。 设变量i是一个3位数,循环体将3位数变量i分拆出它的百位数字、十位数字和个位数字,然后判这三个数字的立方和是否是1099,若是就输出该变量的值。

#include void main()

{ int i , a , b , c ; /* 变量定义 */ for( i = 100 ; i <= 999 ; i++ ){

a = i/100; b = ___(1)___; c = i; if ( ___(2)___ == 1099) printf(\ } }

4.18函数struct intNode *searchLink(struct intNode *h, int key)在首指针为h的链

表中查找值为key的表元,返回该表元的指针。

设链表的表元类型为: struct intNode { int value; struct node *next;}; struct intNode *searchLink(struct intNode *h, int key) { while(___(3)___ && ___(4)___ != key) ___(5)___; return h; }

4.19以下程序将正文文件plain.txt中所有小写英文字母用其后继字母替换(如字母b用

字母c替换),字母z用字母a替换,其余字符保持不变,形成的密文显示在屏幕上。 #include #define N 1000 void main()

{ FILE *fp;

if((fp = fopen(___(6)___) == NULL) {

printf(”Can not open file\\n”) ; return ;

20--34 注:解答写在答卷纸上,试卷中的解答不评分


上海交大C++程序设计试题集(含答案)(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:环境监测报告样板

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: