二级C语言操作例题(三十八)

一、程序填空题

在此程序中,函数fun的功能是:将不带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数据域从头至尾的数据为10,4,2,8,6,排序后链表结点数据域从头至尾的数据为2,4,6,8,10。

#include  <stdio.h>

#include  <stdlib.h>

#define    N    6

typedef struct node {

  int  data;

  struct node  *next;

} NODE;

void fun(NODE  *h)

{ NODE  *p, *q;    int  t;

  p = h;

  while (p) {

/**********found**********/

     q = __1__ ;

/**********found**********/

     while (__2__)

     {  if (p->data > q->data)

       {  t = p->data;  p->data = q->data;  q->data = t;  }

       q = q->next;

    }

/**********found**********/

    p = __3__ ;

  }

}

NODE *creatlist(int  a[])

{  NODE  *h,*p,*q;        int  i;

   h=NULL;

   for(i=0; i<N; i++)

   {  q=(NODE *)malloc(sizeof(NODE));

      q->data=a[i];

      q->next = NULL;

      if (h == NULL)  h = p = q;

      else    {  p->next = q;  p = q;   }

   }

   return  h;

}

void outlist(NODE  *h)

{  NODE  *p;

   p=h;

   if (p==NULL)  printf("The list is NULL!\n");

   else

   {  printf("\nHead  ");

      do

      {  printf("->%d", p->data); p=p->next;  }

      while(p!=NULL);

      printf("->End\n");

  }

}

void main()

{  NODE  *head;

   int  a[N]= {0, 10, 4, 2, 8, 6 };

   head=creatlist(a);

   printf("\nThe original list:\n");

   outlist(head);

   fun(head);

   printf("\nThe list after inverting :\n");

   outlist(head);

}

答案:(1) p->next (2) q (3) p->next

二、程序修改题

在此程序中,函数fun的功能是:将s所指字符串中的字母转换为按字母序列的后续字母(如’Z’转化为’A’,’z’转化为’a’),其他字符不变。

#include  <stdlib.h>

#include  <stdio.h>

#include  <ctype.h>

#include  <conio.h>

void fun(char *s)

{

/*************found**************/

 while(*s!= '@')

   { if(*s>='A'&&*s<='Z'||*s>='a'&&*s<='z')

       {if(*s=='Z')  *s='A';

        else if(*s=='z')  *s='a';

        else   *s+=1;

       }

/*************found**************/

    (*s)++;

   }

}

void main()

{ char s[80];

  system("CLS");

  printf("\n Enter a string with length<80:\n\n");

  gets (s);

  printf("\n The string:\n\n");

  puts(s);

  fun(s);

  printf("\n\n The Cords :\n\n");

  puts(s);

}

答案:(1) while(*s) (2) s++;

三、程序设计题

在此程序中,编写函数fun,函数的功能是:移动一维数组中的内容,若数组中有n个整数,要求把下标从0~p(含p,p小于等于n-1)的数组元素平移到数组最后。

例如,一维数组中的原始内容:1,2,3,4,5,6,7,8,9,10;p的值为3。移动后,一维数组中的内容应为:5,6,7,8,9,10,1,2,3,4。

#include <stdio.h>

#define    N    80

void  fun(int  *w, int  p, int  n)

{

}

void main()

{  int  a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

   int  i,p,n=15;void NONO ();

   printf("The original data:\n");

   for(i=0; i<n; i++)printf("%3d",a[i]);

   printf("\n\nEnter  p:  ");scanf("%d",&p);

   fun(a,p,n);

   printf("\nThe data after moving:\n");

   for(i=0; i<n; i++)printf("%3d",a[i]);

   printf("\n\n");

   NONO();

}

void NONO ()

{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */

  FILE *rf,*wf ; int a[N], i, j, p, n ;

  rf = fopen("in.dat","r") ;

  wf = fopen("out.dat","w") ;

  for(i = 0 ; i < 5 ; i++) {

    fscanf(rf, "%d %d", &n, &p) ;

    for(j = 0 ; j < n ; j++) fscanf(rf, "%d", &a[j]) ;

    fun(a, p, n) ;

    for(j = 0 ; j < n ; j++) fprintf(wf, "%3d", a[j]) ; fprintf(wf, "\n") ;

  }

  fclose(rf) ; fclose(wf) ;

}

答案:

int i,j,t;

for(i=p+1;i<n;i++)

for(j=i;j>i-p-1;j--)

{t=w[j];

w[j]=w[j-1];

w[j-1]=t;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值