試題一
【說明】
該程序的功能是從文件IN.DAT中讀取一篇英文文章存入到字符串?dāng)?shù)組xx中,以行為單位對行中以空格或標(biāo)點符號為分隔的所有單詞進(jìn)行倒排。最后把已處理的字符串(應(yīng)不含標(biāo)點符號)仍按行重新存入字符串?dāng)?shù)組xx中,最后把結(jié)果xx輸出到文件OUT6.DAT中。
例如:原文:You He Me
I am a student.
結(jié)果:Me He You
student a am I
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標(biāo)點符號和空格。
【函數(shù)】
#include
#include
#include
#include
char xx[50][80];
int maxline=0;/*文章的總行數(shù)*/
int ReaaDat(void);
void WriteDat(void);
void StrOL(void)
{
char*pl,*p2,t[80];
int i;
for(i=0;i {p1=xx[i];t[0]=0;
while(*p1)p1++;
while(p1>=xx[i])
{while(!isalpha(*p1)&&p1!=xx[i])p1--;
p2=p1;
while( (1) )p1--;
if(p1==xx[i])
if(isalpha(*p1))p1--;
else if(!isalpha(*(p1+1)))break;
p2++;
(2) ;
strcat(t,p1+1);
strcat(t," ");
}
strcpy(xx[i],t);
}
}
void main()
{
if( (3) ){
printf("數(shù)據(jù)文件in.dat不能打開!\n\007");
return;
}
StrOL();
writeDat();
getch();
}
int ReadDat(void)
{
FILE*fp;
int i=0;
char*p;
if((fp=fopen("e:\\a\\in.dat","r"))==NULL)return 1;
while(fgets(xx[i],80,fp)!=NULL){
p=strchr(xx[i],′\n′);
if(p)*p=0;
i++;
}
maxline= (4)
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE*fp;
int i;
fp=fopen("e:\\a\\out6.dat","w");
> for(i=0;i< (5) ;i++){
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
【答案】
(1)isalpha(*p1)&&p1!=xx[i]
(2)*p2=0
(3)ReadDat()
(4)i
(5)maxline
試題二
閱讀下列說明和流程圖,將應(yīng)填入(n)的語句寫在答題紙的對應(yīng)欄內(nèi)。
【流程圖說明】
下面的流程(如圖1所示)用N-S盒圖形式描述了在一棵二叉樹排序中查找元素的過程,節(jié)點有3個成員:data,left和right。其查找的方法是:首先與樹的根節(jié)點的元素值進(jìn)行比較:若相等則找到,返回此結(jié)點的地址;若要查找的元素小于根節(jié)點的元素值,則指針指向此結(jié)點的左子樹,繼續(xù)查找;若要查找的元素大于根節(jié)點的元素值,則指針指向此結(jié)點的右子樹,繼續(xù)查找。直到指針為空,表示此樹中不存在所要查找的元素。
【算法說明】
【流程圖】
將上題的排序二叉樹中查找元素的過程用遞歸的方法實現(xiàn)。其中NODE是自定義類型:
typedef struct node{
int data;
struct node*left;
struct node*right;
}NODE;
【算法】
NODE*SearchSortTree(NODE*tree,int e)
{
if(tree!=NULL)
{
if(tree->data (4) ;∥小于查找左子樹
else if(tree->data (5) ;∥大于查找左子樹
else return tree;
}
return tree;
}
【答案】
(1)p=p->left
(2)p=p->right
(3)return P
(4)return SearchSortTree(tree->left)
(5)return SearchSortTree(tree->right)
試題三
假設(shè)以帶頭結(jié)點的單循環(huán)鏈表作非遞減有序線性表的存儲結(jié)構(gòu)。函數(shù)deleteklist(LinkList head)的功能是刪除表中所有數(shù)值相同的多余元素,并釋放結(jié)點空間。
例如:鏈表初始元素為:
(7,10,10,21,30,42,42,42,51,70)
經(jīng)算法操作后變?yōu)椋?BR> (7,10,21,30,42,51,70)
【函數(shù)1】
void deleteklist(LinkList head)
{
LinkNode*p,*q;
p=head->next;
while(p!=head)
{
q=p->next;
while( (1) )
{
(2) ;
free(q);
q=p->next;
}
p=p->next;
}
}
【說明2】
已知一棵完全二叉樹存放于一個一維數(shù)組T[n]中,T[n]中存放的是各結(jié)點的值。下面的程
序的功能是:從T[0]開始順序讀出各結(jié)點的值,建立該二叉樹的二叉鏈表表示。
【函數(shù)2】
#include
typedef struct node {
int data;
stuct node leftChild,rightchild;
}BintreeNode;
typedef BintreeNode*BinaryTree;
void ConstrncTree(int T[],int n,int i,BintreeNode*&ptr)
{
if(i>=n) (3) ;∥置根指針為空
else
{
ptr=-(BTNode*)malloc(sizeof(BTNode))
ptr->data=T[i];
ConstrucTree(T,n,2*i+1, (4) );
ConstrucTree(T,n, (5) ,ptr->rightchild);
}
}
main(void)
{/*根據(jù)順序存儲結(jié)構(gòu)建立二叉鏈表*/
Binarytree bitree;int n;
printf("please enter the number of node:\n%s";n);
int*A=(int*)malloc(n*sizeof(int));
for(int i=0;i
for(int i=0;i
ConstructTree(A,n,0,bitree);
}
答案:
(1)q!=head &&q->data==p->data
(2)p->next=q->next
(3)ptr=NULL
(4)ptr->leftchild
(5)2*i+2
試題四
閱讀下列函數(shù)說明和C函數(shù),將應(yīng)填入 n 處的字句寫在答題紙的對應(yīng)欄內(nèi)。
[函數(shù)2.1說明]
函數(shù)strcat(char s[], char t[])的功能是:將字符串t復(fù)制連接字符串s的尾部,并返回新
字符串的首地址作為函數(shù)值。例如:若s=“abcd”,t=“efg”,則新字符串應(yīng)該是“abcdefg”。
[函數(shù)2.1]
char *strcat(char s[], char t[])
{ char *p;
p = s + strlen(s)-1
while( (1) ) {
(2) ;
}
*p = ‘\0’;
return s;
}
[函數(shù)2.2說明]
函數(shù)f(char *str, char del)的功能是:將非空字符串str中的指定字符del刪除,形成一個
新字符串仍存放在str所指內(nèi)存單元中。
例如若str的值為“33123333435
”,del的值為‘3’,調(diào)用此函數(shù)后,新字符串為:“1245”。
[函數(shù)2.2]
void f(char *str, char del)
{
int i, j, len;
len=strlen(str);
i=j=0;
while(i if ( (3) )
(4) = str[i];
i++;
}
(5) ;
}
試題五
閱讀以下說明和C代碼,將應(yīng)填入 n 處的字句寫在答題紙的對應(yīng)欄內(nèi)。
[說明]
下面程序中函數(shù)fun的功能是:在含有10 個元素的s數(shù)組中查找數(shù),及數(shù)所在位置 (即,下標(biāo)值),數(shù)可能不止一個。數(shù)作為函數(shù)值返回,數(shù)的個數(shù)通過指針變量n傳回,所在位置由數(shù)組pos傳回。
例如:
若輸入 2 8 5 7 8 4 5 3 2 8
則應(yīng)輸出:
The max: 8
Total: 3 //數(shù)出現(xiàn)次數(shù)
The positions: 1 4 9
#include
#define M 10
int fun(int *a, int *n, int pos[])
{ int i, k, max=-32767;
(1)
for(i=0; i if( (2) ) max=a[i];
for(i=0; i if( (3) ) pos[k++]=i;
*n=k;
return max;
}
main()
{ int a[M], pos[M], i=0, j, n;
printf("Enter 10 number :");
for(i=0; i j=fun( (5) );
printf("The max: %d\n", j);
printf("Total: %d",n);
printf("The position:");
for(i=0; i printf("\n");
}