您的位置:首页 > 其它

只用getchar函数读入一个整数(算法竞赛入门经典 完整版)

2015-06-22 22:01 441 查看
#include "stdafx.h"
#include
#include

#define MAXNUM 1000
char a[MAXNUM];

int Multi(int n)//递归方式实现10的n次方
{
if (n==0)//结束条件
{
return 1;
}
else
return 10*Multi(n-1);
}

int ReadInt()//读取整数函数封装
{
int i=0;
int n;
int sum=0;
while ((a[i++]=getchar())!='\n');//回车结束输入,存储在字符数组中
n=i-1;
while (--i)
{
sum+=(a[n-i]-'0')*Multi(i-1);
}
return sum;
}

int main(int argc, char* argv[])
{
int a;
a=ReadInt();
printf("%d",a);
while(1);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  getchar 函数库 实现