C语言中十六进制数据的输入与转换

故事要从一道题目说起~~~~~~~~

G - A+B Coming

Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal. _

Input

Input may contain multiple test cases.Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.

Output

Output A+B in decimal number in one line.

Sample Input

1 9
A B
a b

Sample Output

10
21
21

题目要求输入两个十六进制的数,然后以十进制输出他们的和。
在 C 语言中 ,我们可以使用 Scanf / Printf 函数来完成 输入 / 输出
十分简单,代码如下

#include<stdio.h>
int main()
{
   int a,b;
   
   while(scanf("%x %x",&a,&b)!=EOF)
   {
   	printf("%d\n",a+b);
   }
   
   return 0; 
   

} 

输入

1 9
A B
a b

输出

10
21
21

!!成功!!

但是,问题来了
C++怎么办????

。。
。。。
有请Cout / Cin 这两位神仙。

举个例子

在C++中,同样是输入一个a

  1. scanf("%d",a);
  2. cin>>a;

相较于scanf/printf , cin/cout少了好几个字母哪 可辨识度很高,但是!方便的同时,相较于C,这样的输入输出方法缺失了一些精确性。

那么问题来了
C++可以实现上文那样的输入输出吗?
答案是 可以!!

再举个栗子


版权声明:本文为qq_18125607原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。