数组-01. 字符转换(15)

时间:2015-02-01 00:34:01   收藏:0   阅读:378

本题要求提取一个字符串中的所有数字字符(‘0‘……‘9‘),将其转换为一个整数输出。

输入格式:

输入在一行中给出一个不超过80个字符且以回车结束的字符串。

输出格式:

在一行中输出转换后的整数。题目保证输出不超过长整型范围。

输入样例:

free82jeep5

输出样例:

825

跟0相关的要点,只有一个0,或者以0开头。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
#include <stdlib.h>

using namespace::std; 

int main(){
    
    char temp;
    scanf("%c",&temp);
    int flag=0;
    int count=0;
    while(temp!=\n)
    {
        if(temp<=9&&temp>=0)
        {
            if(flag==1)
            {
                printf("%c",temp);    
            }else if(flag==0&&temp!=0)
            {
                flag=1;
                printf("%c",temp);
            }
            else{
                count++;
            }
            
        }
        
        scanf("%c",&temp);
    } 
    if(count!=0&&flag==0)
    {
            printf("0");
    }
    return 0;
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!