题设:给入一个字符串,对其中的数字求和。
例:输入:acb1.2afr76fdrewfdoc32 输出:109.2(1.2+76+32)
输入:5.5.5 输出:10.5(5.5+5)
答案(指定用C实现):
评论:感觉这个题目和计算机等级考试的三级题目差不多。应该说还要稍微难一些。我大概用了十多分钟。如果用C#或者Java(ACM允许用Java),加上正则表达式,应该相当容易,呵呵。
例:输入:acb1.2afr76fdrewfdoc32 输出:109.2(1.2+76+32)
输入:5.5.5 输出:10.5(5.5+5)
答案(指定用C实现):
char inputString[100];
int isNumber(char c)
{
return c>'0'&&c<'9';
}
int isPoint(char c)
{
return c=='.';
}
int getRangeEnd(int cursor)
{
int hasPoint=0;
int isStillNumber=1;
while(isStillNumber)
{
cursor++;
if(hasPoint)
{
isStillNumber=isNumber(inputString[cursor]);
}
else
{
if(isNumber(inputString[cursor]))
{
isStillNumber=1;
continue;
}
if(isPoint(inputString[cursor]))
{
hasPoint=1;
isStillNumber=1;
continue;
}
isStillNumber=0;
}
}
return cursor;
}
int getPow(int i)
{
int total=1;
while(i>1)
{
total*=10;
i--;
}
return total;
}
float getNumber(int beginCursor,int endCursor)
{
int pointPosition=1;
float number=0;
int length,cursor;
length=endCursor-beginCursor;
for(cursor=beginCursor;cursor<endCursor;cursor++)
{
if(isPoint(inputString[cursor]))
{
pointPosition=cursor-beginCursor;
}
else
{
number=number*10+inputString[cursor]-48;
}
}
return number/getPow(length-pointPosition);
}
void main()
{
int cursor=0,inputLength=0;
int rangeBegin=0,rangeEnd=0;
float sum=0;
gets(inputString);
/* Get the length of the inputString */
while(inputString[cursor]!='\0') cursor++;
inputLength=cursor;
cursor=0;
while(cursor<inputLength)
{
if(isNumber(inputString[cursor]))
{
rangeBegin=cursor;
rangeEnd=getRangeEnd(cursor);
sum+=getNumber(rangeBegin,rangeEnd);
cursor=rangeEnd+1;
}
else
{
cursor++;
}
}
printf("%f\n",sum);
}
int isNumber(char c)
{
return c>'0'&&c<'9';
}
int isPoint(char c)
{
return c=='.';
}
int getRangeEnd(int cursor)
{
int hasPoint=0;
int isStillNumber=1;
while(isStillNumber)
{
cursor++;
if(hasPoint)
{
isStillNumber=isNumber(inputString[cursor]);
}
else
{
if(isNumber(inputString[cursor]))
{
isStillNumber=1;
continue;
}
if(isPoint(inputString[cursor]))
{
hasPoint=1;
isStillNumber=1;
continue;
}
isStillNumber=0;
}
}
return cursor;
}
int getPow(int i)
{
int total=1;
while(i>1)
{
total*=10;
i--;
}
return total;
}
float getNumber(int beginCursor,int endCursor)
{
int pointPosition=1;
float number=0;
int length,cursor;
length=endCursor-beginCursor;
for(cursor=beginCursor;cursor<endCursor;cursor++)
{
if(isPoint(inputString[cursor]))
{
pointPosition=cursor-beginCursor;
}
else
{
number=number*10+inputString[cursor]-48;
}
}
return number/getPow(length-pointPosition);
}
void main()
{
int cursor=0,inputLength=0;
int rangeBegin=0,rangeEnd=0;
float sum=0;
gets(inputString);
/* Get the length of the inputString */
while(inputString[cursor]!='\0') cursor++;
inputLength=cursor;
cursor=0;
while(cursor<inputLength)
{
if(isNumber(inputString[cursor]))
{
rangeBegin=cursor;
rangeEnd=getRangeEnd(cursor);
sum+=getNumber(rangeBegin,rangeEnd);
cursor=rangeEnd+1;
}
else
{
cursor++;
}
}
printf("%f\n",sum);
}
评论:感觉这个题目和计算机等级考试的三级题目差不多。应该说还要稍微难一些。我大概用了十多分钟。如果用C#或者Java(ACM允许用Java),加上正则表达式,应该相当容易,呵呵。
本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/06/12/423747.html,如需转载请自行联系原作者