C++ 使用while循环计算数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6601592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting digits using while loop
提问by E.O.
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.
据我所知(即使我的经验有限),它看起来很粗糙,而且相当不优雅。
Does anyone have an idea on how to improve this code (while not using an inbuilt c++ function)?
有没有人知道如何改进此代码(同时不使用内置的 C++ 函数)?
回答by Zan Lynx
In your particular example you could read the number as a string and count the number of characters.
在您的特定示例中,您可以将数字作为字符串读取并计算字符数。
But for the general case, you can do it your way or you can use a base-10 logarithm.
但是对于一般情况,您可以按照自己的方式进行操作,也可以使用以 10 为底的对数。
Here is the logarithm example:
这是对数示例:
#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;
}
回答by David Hammen
int count = (x == 0) ? 1 : (int)(std::log10(std::abs((double)(x)))))) + 1;
回答by Oliver Charlesworth
You could read the user input as a string, and then count the characters? (After sanitising and trimming, etc.)
您可以将用户输入读取为字符串,然后计算字符数吗?(经过消毒和修剪等)
Alternatively, you could get a library to do the hard work for you; convert the value back to a string, and then count the characters:
或者,您可以找一个图书馆为您完成繁重的工作;将值转换回字符串,然后计算字符数:
cin >> x;
stringstream ss;
ss << x;
int len = ss.str().length();
回答by dascandy
Given a very pipelined cpu with conditional moves, this example may be quicker:
给定一个带有条件移动的非常流水线的 cpu,这个例子可能会更快:
if (x > 100000000) { x /= 100000000; count += 8; }
if (x > 10000) { x /= 10000; count += 4; }
if (x > 100) { x /= 100; count += 2; }
if (x > 10) { x /= 10; count += 1; }
as it is fully unrolled. A good compiler may also unroll the while loop to a maximum of 10 iterations though.
因为它是完全展开的。一个好的编译器也可以将 while 循环展开到最多 10 次迭代。
回答by Dan
If x
is an integer, and by "built in function" you aren't excluding logarithms, then you could do
如果x
是一个整数,并且通过“内置函数”你不排除对数,那么你可以做
double doub_x=double(x);
double digits=log(abs(doub_x))/log(10.0);
int digits= int(num_digits);
回答by reza
#include<iostream>
using namespace std;
int main()
{
int count=0;
double x;
cout << "Enter a number: ";
cin >> x;
x /= 10;
while(x > 1)
{
count++;
x = x/10;
}
cout<<count+1;
}
回答by Lyke
Bar the suggestions of reading the number as a string, your current method of counting the number of significant decimal digits is fine. You could make it shorter, but this could arguably be less clear (extra set of parenthesis added to keep gcc from issuing warnings):
禁止将数字作为字符串读取的建议,您当前计算有效小数位数的方法很好。您可以缩短它,但这可能不太清楚(添加了额外的括号以防止 gcc 发出警告):
while((x = x/10))
count++;