待修改的【C程序】中存在3个错误,请指出各个错误所在的行号,并给出相应的修改意见。
第1题
阅读以下说明,Java代码将应填入(n)处的字句写在对应栏内。
【说明】
链表和栈对象的共同特征是:在数据上执行的操作与在每个对象中实体存储的基本类型无关。例如,一个栈存储实体后,只要保证最后存储的项最先用,最先存储的项最后用,则栈的操作可以从链表的操作中派生得到。程序6-1实现了链表的操作,程序6-2实现了栈操作。
import java.io.*;
class Node //定义结点
{ private String m_content;
private Node m_next;
Node(String str)
{ m_content=str;
m_next=null; }
Node(String str,Node next)
{ m_content=str;
m_next=next; }
String getData() //获取结点数据域
{ return m_content;}
void setNext(Node next] //设置下一个结点值
{ m_next=next; }
Node getNext() //返回下一个结点
{ return m_next; )
}
【程序6-1】
class List
{ Node Head;
List()
{ Head=null; }
void insert(String str) //将数据str的结点插入在整个链表前面
{ if(Head==null)
Head=new Node(str);
else
(1)
}
void append(String str) //将数据str的结点插入在整个链表尾部
{ Node tempnode=Head;
it(tempnode==null)
Heed=new Node(str);
else
{ white(tempnode.getNext()!=null)
(2)
(3) }
}
String get() //移出链表第一个结点,并返回该结点的数据域
{ Srting temp=new String();
if(Head==null)
{ System.out.println("Errow! from empty list!")
System.exit(0); }
else
{ temp=Head.getData();
(4) }
return temp;
}
}
【程序6-2】
class Stack extends List
{ void push(String str) //进栈
{ (5) }
String pop() //出栈
{ return get();}
}
第2题
阅读下列程序说明和C程序,把应填入其中(n)处的字句,写在对应栏内。
【程序说明】
对角线下元素全为0的矩阵称为上三角矩阵,设对于一个n×n的上三角矩阵a,为节约存贮,只将它的上三角元素按行主序连续存放在数组b中。下面的函数trans在不引入工作数组的情况下,实现将a改为按列主序连续存放在数组b中。
设n=5,
b=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
经调用trans函数后,b变为
b=(1,2,6,3,7,10,4,8,11,13,5,9,12,14,15)
函数tans对数组元素的存贮位置作调整。调整过程中存在若干个循环传送链:
b(i1)→b(i2)→b(ij)→b(i1)1≤j<n
例如,考察调整后的数组元素b(2)(值为6),与该元素相关的位置调整将形成下面的循环传送链:
b(2)→b(3)→b(6)→……→b(12)→b(9)→b(5)→b(2)
关键是确定循环传送链的下标i1,i2,…,ij,以及在考察调整后的元素b(k)(k;3,4,…)时能判定b(k)是已被传送过的某传送链上的元素。
函数ctr(k,n)计算调整后的数组b的第k个元素b(k)在原数组b中的位置,该位置作为函数ctr(k,n)的返回值。函数ctr根据k确定它在矩阵中的行号i和列号j(注意行号和列号均从 0算起),然后按矩阵存放原则计算出它在b中的位置。
【程序】
trans(b,n)
int n,b[]
{
int m,k,r,cc,rr;
int w;
m=(n+1)*n/2-4;
k=2;
while(m>0)
{
r=ctr(k,n);
if(r==k)
m--;
else
{
cc=k;rr=r;
while (1)
{
cc=rr,rr=ctr(cc,n);
}
if (2)
{
cc=k;rr=r;w=b[k];
while (3)
{
b[cc]=b[rr];m--;
cc=rr,rr=ctf(cc,n);
}
b[cc]-w; (4);
}
}
k++;
}
}
ctr( k,n )
int k,n
{
int i,j;
i=k;j=0;
while (5)
i - =++j ;
return(i*n+j-i*(i+1)/2);
}
第3题
阅读以下说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【说明】
将A,B,C,D,E,F这6个变量排成如图(a)所示的三角形,这6个变量分别取[1,6]中的整数,且均不相同。求使三角形三条边上的变量之和相等的全部解。图(b)就是一个解。
程序引入变量a,b,c,d,e,f,并让它们分别顺序取1~6的整数,在它们互不相同的条件下,测试由它们排列成的如图(a)所示的三角形三条边上的变量之和是否相等,如果相等即为一种符合要求的排列,就输出它们。
【程序】
void main()
{ int a, b, c, d, e, f;
for(a=1;a<=6;a++)
for(b=1;b<=6;b++) {
if((1)) continue;
for(c==1;c<=6;c++) {
if((2))continue;
for(d=1;d<=6;d++) {
if((3)) continue;
for(e=1;e<=6;e++) {
if((4)) continue;
f=21-(a+b+c+d+e);
if((5)){
printf("%6d",a);
printf("%4d%4d",b,f);
printf("%2d%4d%4d",c,d,e);
scanf("%*c"); /*按回车键,继续找解*/
}
}
}
}
}
}
第4题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【函数2.1】
void sort(char *s,int num)
{int i,j--num;
char t;
while(j-->1)
for(i=0;i<j;i++)
if(s[i]>s[i+1])
{t=s[i];
s[i]=s[i+1];
s[i+1]=t;
}
void main()
{char *s="CEAedea";
sort(s,5);
printf("%s",s);
}
上述程序的结果是(1)
【函数2.2】
void main()
{ union {int ig[6];
Char s[12];} try;
try. ig[0]=0x4542; try.ig[1]=0x2049;
try. ig[2]=0x494a; try.ig[3]=0x474e;
try. ig[4]=0x0a21; try.ig[5]=0x0000;
pintf("%s",try, s);
}
上述程序的结果是(2)
【函数2.3】
void main()
{ char *letter[5]= { "ab","efgh","ijk","nmop","st"};
char **p;
int i;
p=letter;
for(i=0;i<4;i++) .
printf("%s",p[i]);
}
上述程序的结果是(3)
【函数2.4】
main()
{int i=4,j=6,k=8,*p=&I,*q=&j,*r=&k;
int x,y,z;
x=p==&i;
y=3*-*p/(*q)+7;
z=*(r=&k)=*p**q;
printf("x=%d,y=%d,z=%d",x,y,z);
}
上述程序的结果是(4)
【函数2.5】
int a[]={5,4,3,2,1 };
void main()
{int i;
int f=a[0];
int x=2;
for(i=0;i<5;i++)
f+=f*x+a[i];
printf("%d",f);
}
上述程序的结果是(5)
第5题
阅读下列函数说明和C函数,将应填入(n)处的字句写在对应栏内。
[说明]
链式存储的队列称为链队。根据队列的FIFO原则,为了操作上的方便,可以使用带头指针front和尾指针rear的单链表来实现链队。若链队元素的数据类型为datatype,则链队结构描述如下:
typedef struct node
{ datatypedata;
structnode *next;
} QNode; /*链队结点的类型*/
typedef struct
{ QNnode *front,*rear;
} LQueue; /*将头尾指针封装在一起的链队*/
以下这种链队的几个例子:
设q是一个指向链队的指针,即LQueue *q。下面各函数的功能说明如下:
(1) LQueue *Init_LQueue():创建并返回一个带头尾结点的空链队;
(2) intEmpty_LQueue( LQueue *q):判断链队q是否空;
(3) void In_LQueue(LQueue *q, datatypex):将数据x压入链队q;
(4) int Out_LQueue(LQuere *q, datatype *x):弹出链队q的第一个元素x,若成功则返回返回1否则返回0。
[函数]
LQueae *Init_LQueue()
{ LQueue *q, *p;
q=malloc(sizeof(LQueue)); /*申请链队指针*/
P=malloc(sized(QNode));/*申请头尾指针结点*/
p->next=NULL;
(1)=p;
return q;
}
int Empty_LQueue(LQueue *q)
{ if(q->front (2) q>rear) return 0;
else return 1;
}
void In_LQueue(LQueue *q, datatype x)
{ QNoda *p;
p=malloc(sizeof(QNnode));/*申请新接点*/
p->data=x;
p->next=NULL;
(3)=p;
q->rear=p;
}
int Out_LQueue(LQueue *q, datatype *x)
{ QNnode *p;
if(Empty_LQueue(q)) return 0; /*队空,操作失败*/
else{
p=q->front->next;
*x=(4);
(5)=p->next;
free(p);
if (q->front->next= =NULL)q->rear=q->front;
return 1;
}
}
第6题
阅读下列函数说明和C函数,将应填入(n)处的字句写在对应栏内。
[函数2.1说明]
Fibonacci数列中头两个数均为1,从第三个数开始,每个数等于前两个数之和。下述程序计算Fibonacci数列中前15个数,并以每行5个数的格式输出。
[函数2.1]
include <stdio.h>
main()
{ int i,f,f1=1,f2=1;
printf("%5d%5d",f1,f2);
for(i=3;i<=15;i++){
f=(1);
printf("%5d",f);
if((2)= =0) printf("\n");
f1=12;
(3);
}
}
[函数2.2说明]
函数fun(char *str1,char *str2)的功能是将字符串str2拼接到str1之后。
[函数2.2]
fun(char *str1,char *str2)
{ int i,j;
for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';j++) (4);
(5);
}
第7题
下而程序实现十进制向其他进制的转换。
[C++程序]
include"ioStream.h"
include"math.h"
include <conio.h>
typedef struct node{
int data;
node *next;
}Node;
class Transform
{
public:
void Trans(int d,int i); //d为数字;i为进制
void print();
private:
Node *top;
};
void Transform.:Trans(int d,int i)
{
int m,n=0;
Node *P;
while(d>0)
{
(1) ;
d=d/i;
p=new Node;
if(!n){
P->data=m;
(2) j
(3) ;
n++;
}
else{
p->data=m;
(4) ;
(5) ;
}
}
}
void Transform.:print()
{
Node *P;
while(top!=NULL)
{
p=top;
if(P->data>9)
cout<<data+55:
else
cout<<data;
top=p->next;
delete P;
}
}
第8题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
这是一个模拟渡口管理的算法。某汽车轮渡口,过江渡船每次能载10辆车过江。过江车辆分为客车类和火车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上4辆客车,才允许上一辆货车;若等待客车不足4辆,则以货车代替,若无货车等待则允许客车都上船。
程序中用到的函数有enqueue(queue*sq,elemtype*x)在队列sq中入队一个元素x;outqueue(queue*sq,elemtype*x)在队列sq中出队一个元素,并将其值赋给x;empty(queue*sq)判断队列sq是否为空队,若为空,返回1;否则返回0。
[C程序]
include<stdio.h>
void pass(){
queue bus,truct; /*bus表示客车队列,truck表示货车队列*/
char ch;
int n,tag; /* ]n为车号,tag为标志,tag=0表示客车,tag=1表示货车*/
intcount=0,countbus=0,counttruck=0; /*分别表示上渡船汽车数、客车数、货车数*/
while(1){
printf("输入命令: \n");
Scanf("%c",&ch);
switch(ch){
case'e':
case'E': printf("车号: \n");
Scanf("%d",&n);
printf("客车\货车(0\1): \n");
scanf("%d",&tag);
if( (1) )
enqueue(&bus,n);
else
enqueue(&truck,n);
break;
case'i':
case'I': while(count<10){
if( (2) && empty(&bus)==0){ /*客车出队*/
outqueue(&bus,&n);
printf("上船的车号为: \n");
count++;
(3) ;
}
eise if( (4) ){ /*货车出队*/
countbus=0;
outqueue(&truck,&n);
printf("上船的车号为: \n");
count++;
counttruck++;
}
else if(empty(&bus)==0){
(5);
outqueue(&truck,&n);
printf("没有10辆车排队轮渡\n");
count++;
countbus++;
}
else{
printf("没有10辆车排队轮渡\n");
retUrn;
}
break;
}
case'q':
case'Q':break;
}
if(ch=='q' || ch=='Q')
break;
}
}
第9题
阅读以下说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【说明】
输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
【函数】
main ( )
{
int number[10];
input (number);
max min (number);
output (number);
}
input (number)
int number[10];
{int i;
for ( i=0;i<9;i++ )
scanf ( "%d,",&number[i] );
scanf ( "%d",&number[9] );
}
max_min ( array )
int array[10];
{int *max,*min,k,1;
int *p,*arr_end;
arr end=(1);
max=min=array;
for ( p=(2);p<arr_end;p++ )
if((3)) max=p;
else if ( *p<*min ) min=p;
(4);
l=*min;
(5);array[0]=1;1=*p;
*p=array[9];array[9]=k;k=*p;
return;
}
output ( array )
int array[10];
{ int *p;
for ( p=array;p<array+9;p++ )
printf ( "%d,",*p );
printf ( "%d\n",array[9] );
}
第10题
阅读以下说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【说明】
下面的程序构造一棵以二叉链表为存储结构的二叉树算法。
【函数】
BTCHINALR *createbt ( BTCHINALR *bt )
{
BTCHINALR *q;
struct node1 *s [30];
int j,i;
char x;
printf ( "i,x =" ); scanf ( "%d,%c",&i,&x );
while (i!=0 && x!='$')
{ q = ( BTCHINALR* malloc ( sizeof ( BTCHINALR )); //生成一个结点
(1);
q->1child = NULL;
q->rchild = NULL;
(2);
if((3);)
{j=i/2 //j为i的双亲结点
if(i%2==0
(4) //i为j的左孩子
else
(5) //i为j的右孩子
}
printf ( "i,x =" ); scanf ( "%d,%c",&i,&x ); }
return s[1]
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!