<tfoot id='1iXyO'></tfoot>
    1. <i id='1iXyO'><tr id='1iXyO'><dt id='1iXyO'><q id='1iXyO'><span id='1iXyO'><b id='1iXyO'><form id='1iXyO'><ins id='1iXyO'></ins><ul id='1iXyO'></ul><sub id='1iXyO'></sub></form><legend id='1iXyO'></legend><bdo id='1iXyO'><pre id='1iXyO'><center id='1iXyO'></center></pre></bdo></b><th id='1iXyO'></th></span></q></dt></tr></i><div id='1iXyO'><tfoot id='1iXyO'></tfoot><dl id='1iXyO'><fieldset id='1iXyO'></fieldset></dl></div>
        <bdo id='1iXyO'></bdo><ul id='1iXyO'></ul>

      <small id='1iXyO'></small><noframes id='1iXyO'>

      1. <legend id='1iXyO'><style id='1iXyO'><dir id='1iXyO'><q id='1iXyO'></q></dir></style></legend>

        使用while循环计算数字

        Counting digits using while loop(使用while循环计算数字)
      2. <small id='J9IBN'></small><noframes id='J9IBN'>

        <i id='J9IBN'><tr id='J9IBN'><dt id='J9IBN'><q id='J9IBN'><span id='J9IBN'><b id='J9IBN'><form id='J9IBN'><ins id='J9IBN'></ins><ul id='J9IBN'></ul><sub id='J9IBN'></sub></form><legend id='J9IBN'></legend><bdo id='J9IBN'><pre id='J9IBN'><center id='J9IBN'></center></pre></bdo></b><th id='J9IBN'></th></span></q></dt></tr></i><div id='J9IBN'><tfoot id='J9IBN'></tfoot><dl id='J9IBN'><fieldset id='J9IBN'></fieldset></dl></div>
        <tfoot id='J9IBN'></tfoot>

            <tbody id='J9IBN'></tbody>
            • <bdo id='J9IBN'></bdo><ul id='J9IBN'></ul>
            • <legend id='J9IBN'><style id='J9IBN'><dir id='J9IBN'><q id='J9IBN'></q></dir></style></legend>

                  本文介绍了使用while循环计算数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我最近正在制作一个程序,需要检查用户输入的数字中的位数.结果我做了以下代码:

                  I was recently making a program which needed to check the number of digits in a number inputted by the user. As a result I made the following code:

                  int x;    
                  cout << "Enter a number: ";
                  cin >> x;
                  x /= 10;
                  while(x > 0)
                  {
                    count++;
                    x = x/10;
                  }
                  

                  据我所知(即使我的经验有限),它看起来很粗糙,而且相当不雅.

                  From what I can tell (even with my limited experience) is that it seems crude and rather unelegant.

                  有没有人知道如何改进此代码(同时不使用内置的 C++ 函数)?

                  Does anyone have an idea on how to improve this code (while not using an inbuilt c++ function)?

                  推荐答案

                  在您的特定示例中,您可以将数字作为字符串读取并计算字符数.

                  In your particular example you could read the number as a string and count the number of characters.

                  但对于一般情况,您可以按照自己的方式进行操作,也可以使用以 10 为底的对数.

                  But for the general case, you can do it your way or you can use a base-10 logarithm.

                  这里是对数示例:

                  #include <iostream>
                  #include <cmath>
                  
                  using namespace std;
                  
                  int main()
                  {
                      double n;
                      cout << "Enter a number: ";
                      cin >> n;
                  
                      cout << "Log 10 is " << log10(n) << endl;
                      cout << "Digits are " << ceil(log10(fabs(n)+1)) << endl;
                      return 0;
                  }
                  

                  这篇关于使用while循环计算数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  • <bdo id='pAkCn'></bdo><ul id='pAkCn'></ul>

                    1. <tfoot id='pAkCn'></tfoot>
                          <tbody id='pAkCn'></tbody>
                      • <legend id='pAkCn'><style id='pAkCn'><dir id='pAkCn'><q id='pAkCn'></q></dir></style></legend>

                        <small id='pAkCn'></small><noframes id='pAkCn'>

                      • <i id='pAkCn'><tr id='pAkCn'><dt id='pAkCn'><q id='pAkCn'><span id='pAkCn'><b id='pAkCn'><form id='pAkCn'><ins id='pAkCn'></ins><ul id='pAkCn'></ul><sub id='pAkCn'></sub></form><legend id='pAkCn'></legend><bdo id='pAkCn'><pre id='pAkCn'><center id='pAkCn'></center></pre></bdo></b><th id='pAkCn'></th></span></q></dt></tr></i><div id='pAkCn'><tfoot id='pAkCn'></tfoot><dl id='pAkCn'><fieldset id='pAkCn'></fieldset></dl></div>