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);
/*****************************/
}
结果