我30你40他50”结果就是120

/*++

Copyright (c) 2007 YourCompany

Module Name:

    <new>

Abstract:

    有一个字符串,里面包含一些数字,写一个函数,
    把这些数字加起来。比如“我30你40他50”结果就是120。 

Author:

    YourName (YourEmail) 2007-06-12

Revision History:

--
*/


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

#define  MAX_LEN 30 


int  main( int  argc,  char *  argv[])
{
    
char str[MAX_LEN];
    
float num[MAX_LEN];  //数组定义后会自动初始化为0,为何还要用menset???
 
    
int count=-0;  
    
char ch;  //存放取出的字符
    int i=0;
    
int j=0;
    
int n=0;   
    
int Len;  //记录输入串长度
    double sum=0.0;
    
double result=0.0;
    printf(
"[+]please input the string  ");
    printf(
"[-]example:你30岁我20岁一起是多少岁? ");
    printf(
"[-]result: 50 ");
    
    printf(
"please input the string in english or chinese: ");
    gets(str);
    Len
=strlen(str);
    
    printf(
"-----------:%f----------------------- ",pow(10.0,3.0));
    printf(
"ur string is :%s    the length is:%d ",str,Len);
    
    
while(Len!=0)
    
{
        ch
=str[i];
        printf(
"get char:  %c ",ch);
        
if(ch>='0' && ch<='9')
        
{
            j
=0;
            count
=-1
            
while(ch>='0'&&ch<='9')
            
{
                printf(
"find the number:  %c ",ch);
                num[j]
=(float)str[i++];  //强制类型转换,使字符型变为浮点
                printf("turn char to float:  %f ",num[j]);
                count
++;
                j
++;
                Len
--;
                ch
=str[i];
                printf(
"get char:  %c ",ch);
            }

            
for(n=0;n<j;n++,count--)     //取出数字 
            sum+=num[n]*pow(10.0,(float)count);  
            printf(
"fetch the number:  %f ",sum);
        }

        
else 
        
{
            i
++;
            Len
--;
        }

        result
+=sum;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值