C二级第六套试题

1.

题目

代码

#include  <stdio.h>
#include  <string.h>
#include  <ctype.h>
#define  N  9
long  ctod( char  *s )
{ long  d=0;
  while(*s)
    if(isdigit( *s))  {//是否是数字
/**********found**********/
      d=d*10+*s-'0';//转化为对应的数值
/**********found**********/
      s++;  
	 }
  return  d;
}
long  fun( char  *a, char  *b )
{
/**********found**********/
  return  ctod(a)+ctod(b);
}
void main()
{ char  s1[N],s2[N];
  do
  { printf("Input  string  s1 : "); gets(s1); }
  while( strlen(s1)>N );
  do
  { printf("Input  string  s2 : "); gets(s2); }
  while( strlen(s2)>N );
  printf("The result is:  %ld\n", fun(s1,s2) );
}


结果

ch-'0'是计算每个数字字符在数字中所对应的位置,较难理解的是10*d ,因为输入的是多个数,每次计算完一个数字字符,依然处在循环中带入下一次的计算,因此10*d的作用就是把上一次算得的结果往高位送,每乘以一个十就高一位。

2.

题目

代码

#include <stdio.h>
/**********found**********/
void fun ( char *s, int *a, int *b )//指针类型
{
  while ( *s )
  {  if ( *s >= 'A' && *s <= 'Z' )
/**********found**********/
       *a=*a+1 ;
     if ( *s >= 'a' && *s <= 'z' )
/**********found**********/
        *b=*b+1;
     s++;
  }
}

void main( )
{  char   s[100];  int   upper = 0, lower = 0 ;
   printf( "\nPlease a string :  " );  gets ( s );
   fun ( s,  &upper, &lower );//传的大小写参数是地址
   printf( "\n upper = %d  lower = %d\n", upper, lower );
}

结果

3.

题目

代码

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
float fun (float h )
{
    int temp=(int)(h*1000+5)/10;//取整操作
	return (float)temp/100;
}
void main()
{
  FILE *wf;
  float a;
  system("CLS");
  printf("Enter a: ");
  scanf ("%f",&a);
  printf("The original data is :  ");
  printf("%f\n\n", a);
  printf("The  result : %f\n", fun(a));
/******************************/
  wf=fopen("out.dat","w");
  fprintf(wf,"%f",fun(8.32533));
  fclose(wf);
/*****************************/
}

结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值