
/**//*++
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;
}
本文介绍了一个简单的C语言程序,该程序能够从一段包含数字的字符串中提取所有数字并计算其总和。例如,对于输入我30你40他50,程序将输出120作为结果。
2526





