程序修改题(41-50)

第四十一题

题目

给定程序modi1.c的主函数中,将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun()的作用是:累加链表结点数据域中的数据作为函数值返回。

#include  <stdio.h>
typedef  struct  list
{  int  data;
   struct list  *next;
} LIST;
int fun(LIST *h)
{  LIST  *p;
/**********found**********/
   int  t;
   p=h;
/**********found**********/
   while( *p )
   {
/**********found**********/
      t=t+p.data;
      p=(*p).next;              
   }
   return  t;
}
main()
{  LIST  a, b, c, *h; 
   a.data=34;  b.data=51;  c.data=87;  c.next='\0';
   h=&a;  a.next=&b;  b.next=&c;
   printf("总和 = %d\n",fun( h ));
   getchar();
}

解析

int  t=0;

while( p )

t=t+p->data;

第四十二题

题目

#include <stdio.h>
double fun( int m )
{
  double t = 1.0;
  int i;
  for( i = 2; i <= m; i++ )
/**********found**********/
    t = 1.0-1 /i;
/**********found**********/
   _______;
}

main()
{
  int m ;
  printf( "\nPlease enter a number:\n" );
  scanf(  "%d", &m);
  printf( "\n\nThe result is %lf\n", fun( m ) );
  getchar();
}

解析

t = t-1.0 /i;

return t ;

第四十三题

题目

假定整数数列中的数不重复,并存放在数组中。给定程序modi1.c中函数fun的功能是:删除数列中值为x的元素。n中存放的是数列中元素的个数。

#include <stdio.h>
#define   N  20
int fun(int *a,int n,int x)
{   int   p=0,i;
    a[n]=x;
    while( x!=a[p] )
	p=p+1;
/**********found**********/
    if(P==n) return -1;
    else
    { for(i=p;i<n-1;i++)
/**********found**********/
	  a[i+1]=a[i];
      return n-1;
    }
}
main()
{  int  w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;
   n=10;
   printf("The original data :\n");
   for(i=0;i<n;i++) printf("%5d",w[i]);
   printf("\nInput x (to delete): "); scanf("%d",&x);
   printf("Delete  :  %d\n",x);
   n=fun(w,n,x);
   if ( n==-1 ) printf("***Not be found!***\n\n");
   else
   {  printf("The data after deleted:\n");
      for(i=0;i<n;i++) printf("%5d",w[i]);printf("\n\n");
   }
  getchar();
}

解析

if(p==n) return -1;

a[i]=a[i+1];

第四十四题

题目

给定程序modi1.c中,函数fun的功能是:从N个字符串中找出最长的那个串,并将其地址作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。

#include <stdio.h>
#include <string.h>
#define    N   5
#define    M   81
/**********found**********/
fun(char  (*sq)[M])
{  int  i;     char  *sp;
   sp=sq[0];
   for(i=0;i<N;i++)
      if(strlen( sp)<strlen(sq[i]))
         sp=sq[i] ;
/**********found**********/
   return  sq;
}
main()
{  char   str[N][M], *longest;    int   i;
   printf("Enter %d lines :\n",N);
   for(i=0; i<N; i++) gets(str[i]);
   printf("\nThe N string  :\n",N);
   for(i=0; i<N; i++) puts(str[i]);
   longest=fun(str);
   printf("\nThe longest string :\n"); puts(longest);
  getchar();
}

解析

char* fun(char  (*sq)[M])

return  sp;

第四十五题

题目

给定程序MODI1. C中函数char *fun(char *s1, char *s2,char *r) 的功能是:将两个长度相等的纯数字字符串当作两个加数,求其代表的数值之和并以字符串的形式存入r所指内存且返回。

如: s1是”723”,s2是”567", 则返回的字符串为”1290”。要考虑进位。

#include <stdio.h>
#include <string.h>
char *fun(char * s1,char *s2,char *r)
{
	int len,cy=0,md;
	char *p,*q,*t;
	len=strlen(s1);
	p=s1+len-1;
	q=s2+len-1;
	t=r+len;
/**********************found***********************/
	*t='\0';
	while(p>=s1)
	{
/**********************found***********************/
		md=*p+*q-'0'+cy;
		if(md>=10)	cy=1;
		else	cy=0;
		*t=md%10+'0';
		p--;
		q--;
		t--;
	}
/**********************found***********************/
	if(cy=1)     
		*t='1';
	else
	{
		while(*r++=*++t);
	}
	return r;
}
void main()
{
	char s1[100],s2[100],s3[101];
	strcpy(s1,"65743");
	strcpy(s2,"85339");
	fun(s1,s2,s3);
	printf("%s+%s=%s\n",s1,s2,s3);
}

解析

*t='0';

md=(*p-'0')+(*q-'0')+cy;

if(cy==1)

第四十六题

题目

给定程序MODI1. C中,函数fun的功能是:统计形参str中所出现的大写字母的范围跨度,

即按字典序最前面的大写字母与最后面的大写字母ASCII值之差,并传递回主函数输出。若没有大写字母,则函数输出0。

例如: 若str的内容为”Baybye!Doom",其中大写字母为B和D,D与B之 差为2,函数返回2,程序输出2。

若str的内容为"M68C9Xaa”,其中字典序最前面的大写字母为C,最后面的大写字母为X,X与C之差为21,函数返回21,程序输出21。

#include <stdio.h>
#include <string.h> 

int fun(char* str) {  
  int k, c0, c1;
  c0 = c1 = -1;
  for (k=0; k<strlen(str); k++) 
    if ( (str[k] >= 'A') && (str[k] <= 'Z') ) 
	{  c0 = c1 = k;
	   break;
	}
  if (c0 == -1)
	  return 0;
/**********************found***********************/
  for (k=0; k<strlen(str)-1; k++) 
  {
/**********************found***********************/
    if ( (str[k] >= 'A') || (str[k] <= 'Z') ) 
    {
      if (str[k] < str[c0])
        c0 = k;
      if (str[k] > str[c1])
        c1 = k;
    }
  }
/**********************found***********************/
  return c1 - c0;
}

int main()
{ 
  char str[100];
  int l;
  printf("input string:");
  scanf("%s", str);
  l = fun(str);
  printf("span:%d\n", l);
  return 0;
}

解析

for (; k<strlen(str); k++) 

if ( (str[k] >= 'A') && (str[k] <= 'Z') ) 

return str[c1] - str[c0];

第四十七题

题目

#include <stdio.h>
/************found************/
fun ( int   m )
{
    double y = 0, d ;
    int    i ;
    /************found************/
    for( i = 100, i <= m, i += 100 )
    {
        d = (double)i * (double)i ;
        y += 1.0 / d ;
    }
    return( y ) ;
}

main( )
{
    int  n = 2000 ;
    printf( "\nThe result is %lf\n", fun ( n ) ) ;
  getchar();
}

解析

double fun ( int   m )

for( i = 100; i <= m; i += 100 )

第四十八题

题目

给定程序modi1.c中,函数fun的功能是:统计子字符串substr在字符串str中出现的次数。

例如,若字符串为aaas 1kaaas,子字符串为as,则应输出2。

#include <stdio.h>

int fun (char *str,char *substr)
{  int i,j,k,num=0;
/************found************/
   for(i = 0, str[i], i++)
     for(j=i,k=0;substr[k]==str[j];k++,j++)
/************found************/
       If(substr[k+1]=='\0')
       {  num++;
          break;
       }
   return num;
}

main()
{
  char str[80],substr[80];
  printf("Input a string:") ;
  gets(str);
  printf("Input a substring:") ;
  gets(substr);
  printf("%d\n",fun(str,substr));
  getchar();
}

解析

for(i = 0; str[i]; i++)

if(substr[k+1]=='\0')

第四十九题

题目

给定程序MODI1. C中,函数fun的功能是:从一个3行4列的矩阵中找出最大数及最大数所

在位置的下标,函数返回值为最大数,形参pRow和pCo1返回最大数所在位置的下标。

#include <stdio.h>

int func(int (*p)[4], int m, int n, int *pRow, int *pCol)
{
	int i, j, max;
/**********found**********/
	max = *p;
	*pRow = 0;
	*pCol = 0;
	for (i=0;i<m;i++)
	{
		for (j=0;j<n;j++)
		{
			if ( *(*(p+i)+j) > max)
			{
				max = *(*(p+i) + j);
				*pRow = i;
/**********found**********/
				*pCol = i + j;
			}
		}
	}
/**********found**********/
	return *max;
}

int main()
{
	int aa[3][4] = {{1,3,5,7},{21,19,27,22},{11,17,18,20}};
	int maxVal, row, col;
	maxVal = func(aa, 3, 4, &row, &col);
	printf("%d %d %d\n", maxVal, row, col);
	return 0;
}

解析

max = **p;

*pCol = j;

return max;

第五十题

题目

给定程序MODI1. C中,函数void list (MYDATA *h)的功能是:列出带头结点单链表中所有

没有删除标记的数据。调用这个函数时,传给形参h的是指向单链表头结点的指针。

例如:当10个结点的数据为1,2,3,4,5,6, 7,8,9, 10时,输出将是:3 4 6 7 8 9 10。

其中,各个数据所对应的删除标记是由随机数产生的。

#include<stdio.h>
#include<stdlib.h>
typedef struct dat
{
	char deleted;	//是否删除:0-未删除,1-删除
	int data;
	struct dat* next;
} MYDATA;
void list(MYDATA *h)
{
/******found******/
	MYDATA  p;
	p=h->next;
	while(p!=NULL)
	{
	/******found******/
		if(p->data==0)
		{
			printf("%d  ",p->data);
		}
/******found******/
		p=next;
	}
}

void creat(MYDATA *h,int *d,int n)
{
	MYDATA *p, *q;
	int i=0,del;
	q=h;
	while(n>0)
	{
		p=( MYDATA *)malloc(sizeof(MYDATA));
		del=rand()%2;
		p->data=d[i];
		p->deleted=del;
		p->next=q->next;
		q->next=p;
		q=p;
		n--;i++;
	}
}
void main()
{
	MYDATA *head;
	int n=10,dd[]={1,2,3,4,5,6,7,8,9,10};
	head=(MYDATA *)malloc(sizeof(MYDATA));
	head->next=NULL;
	creat(head,dd,n);
	list(head);
}

解析

MYDATA  *p;

if(p->deleted==0)

p=p->next;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值