为什么C给我不同的答案比我的计算器? [英] Why does C give me a different answer than my calculator?

查看:133
本文介绍了为什么C给我不同的答案比我的计算器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这个code一个奇怪的问题:

I've run into an odd problem with this code:

legibIndex = 206.385 - 84.6 *(countSylb / countWord) - 1.015 *(countWord / countSent);

这是一个给定的文本文件的易读性指数的计算。
由于这是一个家庭作业,我们被告知的指数应该是什么(80或80.3完全相同)

This is the calculation for the legibility index of a given text file. Since this is a homework assignment, we were told what the Index should be (80, or exactly 80.3)

我的音节数,字数,和句子的计数是正确的(它们匹配与样品TEXTFILES给定的数字。

My syllable count, word count, and sentence count are all correct (they match up with the given numbers for the sample textfiles.

即使我很难code中,我没有得到80,即使我做的时候我把它放到我的caclulator正是因为看到的数字。我不能想象什么是错的。

Even if I hardcode the numbers in, I do not get 80, even though I do when i put it into my caclulator exactly as seen. I can't imagine what is wrong.

下面是我们给出的公式:

Here is the equation we were given:


   指数= 206.835 - 84.6 *(#音节/#字) - 1.015 *(#字/#句子)

正如你所看到的,我只是插入我的变量(持有正确的价值观。
作为参考,值:55音节,40万字,4句,由导师给出。当然是112。可读性指数值我的程序产生。

As you can see, I just plugged in my variables (which are holding the correct values. For reference, the values are : 55 Syllables, 40 Words, 4 Sentences, as given by the instructor. The values my program produces when ran is a Legibility Index of 112.

我失去了一些支架,还是什么?
我难倒!

Am I missing some brackets, or what? I'm stumped!

推荐答案

右键关闭蝙蝠,从名称(包括单词的计数的)我猜 countSylb countSent countWord 被声明为整数,因此你的部门正在做的整数算术,截断小数部分。铸造他们漂浮,这应该修复它。

Right off the bat, from the names (which include the word count) I'd guess that countSylb, countSent and countWord are declared as integers, and therefore your divisions are doing integer arithmetic, truncating the decimal portions. Cast them to floats and that should fix it.

legibIndex = 206.385 - 84.6 * ((float)countSylb / ((float)countWord) - 
               1.015 * (((float)countWord / ((float)countSent);

这篇关于为什么C给我不同的答案比我的计算器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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