这道题中怎么用scanf代替getchar输入字符
输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入一个以回车结束的字符串(少于80个字符),滤去所有的非十六进制字符后,组成一个新字符串(十六进制形式),然后将其转换为十进制数后输出。
输入输出示例:括号内为说明
输入:
2 (repeat=2)
10
+A
输出:
Dec = 16
Dec = 10
#include "stdio.h"
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
i = 0;
while((ch = getchar()) != '\n'){
str[i]=ch;
i++;
}
str[i] = 0;
number=0;
for(i=0;str[i]!='\0';i++){
if(str[i]>='0'&&str[i]<='9')
number=number*16+str[i]-'0';
else if(str[i]>='A'&&str[i]<='F')
number=number*16+str[i]-'A'+10;
else if(str[i]>='a'&&str[i]<='f')
number=number*16+str[i]-'a'+10;
}
printf("Dec = %ld\n",number);
}
}