C++ C++;本福德';美国的法律课程。
所以我必须写一个程序来 =>分析三个不同的数据文件,并尝试确认本福德定律。您将创建一个控制台应用程序,打开每个文件,计算以“1”、“2”、“3”等开头的值的数量,然后输出每个数字的百分比 <>我想我已经把它放下来了,但是我在DEV C++中一直出错。C++ C++;本福德';美国的法律课程。,c++,benfords-law,C++,Benfords Law,所以我必须写一个程序来 =>分析三个不同的数据文件,并尝试确认本福德定律。您将创建一个控制台应用程序,打开每个文件,计算以“1”、“2”、“3”等开头的值的数量,然后输出每个数字的百分比 我想我已经把它放下来了,但是我在DEV C++中一直出错。 int analyzeData(string fname) { ifstream infile(string fname); int tmp,count = 0; float percents[9]; int nums[
int analyzeData(string fname) {
ifstream infile(string fname);
int tmp,count = 0;
float percents[9];
int nums[9] = { 0 };
if(!infile.good())
return 1;
while(!infile.eof())
{
infile >> tmp;
tmp = first(tmp);
if(tmp > 0)
{
nums[tmp - 1] ++;
count++;
}
}
这是说‘good’、‘eof’和‘infle’是非类类型?
我不知道那是什么意思!
非常感谢您的帮助!谢谢 首先
ifstream infile(string fname);
应该是
ifstream infile(fname);
您的版本是函数原型,而不是变量声明
其次,这是循环到文件末尾的错误方法
while (!infile.eof())
{
infile >> tmp;
...
}
这是正确的方法
while (infile >> tmp)
{
...
}
这一定是我们在这里看到的最常见的错误eof
不会做您认为它会做的事情,任何人在(!infle.eof())时告诉您编写,都是错误的
最后,first(tmp)
不是从整数中获取第一个数字的正确方法。你必须比这更努力一点。不要把输入读成整数,而是把行读成字符串,从字符串中抓取第一个数字。或者您可以读取一个整数,然后将tmp除以10,直到结果小于10
让您的生活更轻松,并使用数字作为数组的索引。您需要能够索引值1-9,因此需要将数组声明得更大一些。百分比也是如此
int nums[9] = { 0 }; // works, but do less work
float percents[9];
int nums[10] = { 0 }; // do this, then you can us the digit to index nums[]
float percents[10];
tmp>0不需要守卫,因为你有空间容纳所有10个数字
//if( tmp > 0 )
//{
...
//}
你不需要从tmp中减去一
int analyzeData(string fname)
{
ifstream infile(fname);
int tmp,count = 0;
float percents[10];
int nums[10] = { 0 };
if(!infile.good())
return 1;
while(infile >> tmp)
{
tmp = first(tmp);
{
nums[tmp] ++;
count++;
}
}
if(count<1) count=1; //avoid division by zero
for( tmp=1; tmp<10; ++tmp )
cout<<tmp<<":"<<nums[tmp]<<",pct:"<<(nums[tmp]*1.0)/count<<eol;
}
int-analyzeData(字符串fname)
{
ifstream infle(fname);
int tmp,计数=0;
浮动百分比[10];
int nums[10]={0};
如果(!infle.good())
返回1;
而(内嵌>>tmp)
{
tmp=第一(tmp);
{
nums[tmp]++;
计数++;
}
}
如果(countmy nemesiseof()
我们又见面了!