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

来源:互联网 发布:香港端口 编辑:程序博客网 时间:2024/04/30 00:40
/*++

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;
    }

    
 
原创粉丝点击