C ++程序输入转换为hex,oct和dec [英] C++ program input converts to hex, oct and dec

查看:132
本文介绍了C ++程序输入转换为hex,oct和dec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++程序必须:

1.不要使用cout超过两次;

2.声明一个类型的int变量;

3.使用cout提示用户输入任何十进制整数数值;

4.使用cin获取并将用户输入值存储在int类型变量中;

5.使用cout以十进制,八进制和十六进制显示输入值,使用以下10格式,其中D表示十进制值,O表示八进制值,H表示十六进制值11 :D decimal = O octal = H hexadecimal例如,如果用户输入22,程序将显示以下内容,全部在一行:22十进制= 26八进制= 16十六进制



我尝试过:



我尝试过使用函数。请不要使用功能。让程序尽可能简单,请注释。

C++ program must:
1. not use cout more than twice;
2. declare one type int variable;
3. use cout to prompt the user to enter any decimal integer numeric value;
4. use cin obtain and store the user input value in the type int variable;
5. use cout to display the input value in decimal, octal, and hexadecimal using the following 10 format, where D represents the decimal value, O represents the octal value, and H represents the 11 hexadecimal value: D decimal = O octal = H hexadecimal For example, if the user were to enter 22 your program would display the following, all on one line: 22 decimal = 26 octal = 16 hexadecimal

What I have tried:

I have tried using functions. Please do not use functions. Make program as simple as possible and please comment.

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


引用:

我试过用过功能。请不要使用功能。使程序尽可能简单,请发表评论。

I have tried using functions. Please do not use functions. Make program as simple as possible and please comment.

为什么不呢?功能很好。

你的任务非常简单,只需从用户那里选择一个数字,然后用不同的基础打印出它的表示。如果您被允许,那么您可以使用strtol(请参阅 strtol - C ++参考 [ ^ ])(或者,在更多C ++ apporach中sstreamstream [ ^ ]对象)。另一方面,写自己这样简单的转换功能并不是一项艰巨的任务。

Why not? Functions are good.
You task is pretty trivial, just pick a number from the user and then print out its representation using different basis. If you're allowed then you could use the strtol (see strtol - C++ Reference[^]) (or, in a more C++ apporach a stringstream[^] object). On the other hand writing yourself such simple conversion function is not a daunting task.


如前所述,你不会为你完成它。



假设您知道如何使用 cout 打印字符串并使用 cin 来读取整数输入,我只能猜测你不知道如何使用一个 cout 语句以三种不同的格式打印整数值。



这可以使用流操作符 - C ++参考 [ ^ ]。对于整数,有三个指定 base - C ++ Reference [ ^ ]。
As already noted you will not get it done for you.

Assuming that you know how to use cout to print a string and cin to read an integer input, I can only guess that you don't know how to print the integer value in three different formats using a single cout statement.

This can be done using stream manipulators - C++ Reference[^]. For integers there are three to specify the base - C++ Reference[^].


这篇关于C ++程序输入转换为hex,oct和dec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆