• 计算机二级-C语言-字符数字转化为整型数字。形参与实参类型相一致。double类型的使用。


    //函数fun功能:将a和b所指的两个字符串分别转化成面值相同的整数,并进行相加作为函数值返回,规定只含有9个以下数字字符。

    //重难点:字符数字转化为整型数字。

     1 #include  <stdio.h>
     2 #include  <string.h>
     3 #include  <ctype.h>
     4 #define  N  9
     5 long  ctod( char  *s )
     6 { long  d=0;
     7   while(*s)//指针指向字符串首地址。
     8     if(isdigit( *s))  {//此函数检查参数是否为字符整数类型。
     9 /**********found**********/
    10       d=d*10+*s-'0';//转化为整型类型。
    11 /**********found**********/
    12       s++;  
    13      }
    14   return  d;
    15 }
    16 long  fun( char  *a, char  *b )
    17 {
    18 /**********found**********/
    19   return  ctod(a)+ ctod(b);
    20 }
    21 void main()
    22 { char  s1[N],s2[N];
    23   do
    24   { printf("Input  string  s1 : "); gets(s1); }
    25   while( strlen(s1)>N );
    26   do
    27   { printf("Input  string  s2 : "); gets(s2); }
    28   while( strlen(s2)>N );
    29   printf("The result is:  %ld
    ", fun(s1,s2) );
    30 }

    //函数fun功能:分别统计字符串大写字母和小写字母的个数。

    //重难点:形参与实参类型相一致。

     1 #include <stdio.h>
     2 /**********found**********/
     3 void fun ( char *s, int *a, int *b )
     4 {
     5   while ( *s )
     6   {  if ( *s >= 'A' && *s <= 'Z' )
     7 /**********found**********/
     8        *a=*a+1 ;
     9      if ( *s >= 'a' && *s <= 'z' )
    10 /**********found**********/
    11         *b=*b+1;
    12      s++;
    13   }
    14 }
    15 
    16 void main( )
    17 {  char   s[100];  int   upper = 0, lower = 0 ;
    18    printf( "
    Please a string :  " );  gets ( s );
    19    fun ( s,  & upper, &lower );
    20    printf( "
     upper = %d  lower = %d
    ", upper, lower );
    21 }

    //函数fun功能是:使变量h中的值保留两位小数,并对第三位进行四舍五入

    //重难点:切记这里使用double类型,使用float会导致不准确。

     1 #include <stdio.h>
     2 #include <conio.h>
     3 #include <stdlib.h>
     4 double fun (double h )//切记这里使用double类型,使用float会导致不准确。
     5 {//第一种方法:
     6 /*    double p;
     7     int s,a;
     8     p = h * 1000;
     9     s = p;
    10     a = s % 10;
    11     if (a > 4) s = s + 1;
    12     p = s;
    13     return p/1000;*/
    14     int s,n,n1,n2,n3;//第二种方法:
    15     double p,q;
    16     s = h;
    17     p = h - (double)s;
    18     p = p * 1000;
    19     n = p;
    20     n1 = n / 100;
    21     n2 = n % 100 / 10;
    22     n3 = n % 10;
    23     if (n3 > 4) n2 = n2 + 1;
    24     q = (double)s + ((double)n1 / 10) + ((double)n2 / 100);
    25     return q;
    26 }
    27 void main()
    28 {
    29   FILE *wf;
    30   float a;
    31   system("CLS");//清屏。
    32   printf("Enter a: ");
    33   scanf ("%f",&a);
    34   printf("The original data is :  ");
    35   printf("%f
    
    ", a);
    36   printf("The  result : %f
    ", fun(a));
    37 /******************************/
    38   wf=fopen("out.dat","w");
    39   fprintf(wf,"%f",fun(8.32533));往文件中写入。
    40   fclose(wf);
    41 /*****************************/
    42 }
  • 相关阅读:
    JAVA 设计模式 组合模式
    JAVA 设计模式 模板方法模式
    SpringBoot 数据篇之使用JDBC
    [Spring]01_环境配置
    [spring]03_装配Bean
    [Spring]04_最小化Spring XML配置
    [Quartz笔记]玩转定时调度
    [Spring]支持注解的Spring调度器
    asp.net core 系列 13 日志
    asp.net core 系列 12 选项 TOptions
  • 原文地址:https://www.cnblogs.com/ming-4/p/10308470.html
Copyright © 2020-2023  润新知