C语言为什么会将我的乘法操作与指针赋值混淆?

huangapple go评论123阅读模式
英文:

Why is C confusing my multiplicative operations with pointer assignment?

问题

int hashedValue = n * ((int)((k * A%1)));
被标记为指针错误expression must have an integral type。但我不理解这个错误,因为*与后缀而不是前缀操作一起使用。请帮忙解释。谢谢!

英文:
  1. int multiplicationMethod(int k, int n)
  2. {
  3. if(n > SIZE || n < SIZE)
  4. {
  5. printf("IndexOutOfBounds Error. Please Try different slot number.");
  6. }
  7. else
  8. {
  9. int N = rand();
  10. float A = N/RAND_MAX;
  11. int hashedValue = n * ((int)((k * A%1)));
  12. HashTable[hashedValue] = k;
  13. return hashedValue;
  14. }
  15. }

int hashedValue = n * ((int)((k * A%1)));
is being tagged with a pointer error expression must have an integral type
I don't understand this though because the * is used with the post NOT prefix operation. Please help. Thanks!

答案1

得分: 3

k * A%1

你不能这样做。A 是一个浮点数;而 % 只接受整数参数。

这对于 C 语言来说是个尴尬之处。对于这个限制,现在已经没有好的理由了。只需在遇到这个运算符时自动调用 fmod

应该写成 k * fmod(A, 1)

正如评论中指出的,你需要将 float A = N/RAND_MAX; 改为 float A = N/(float)RAND_MAX; 才能使你的代码正常工作;否则 A 将永远为 0。

英文:

k * A%1

You can't do that. A is a float; and % only takes integer arguments.

This is an embarrassment to C. There's no good for this restriction anymore. Just auto-call fmod when you see that operator.

Should read k * fmod(A, 1)

As pointed out in comments, you need to change float A = N/RAND_MAX; to float A = N/(float)RAND_MAX; for your code to work; otherwise A will always be 0.

答案2

得分: 0

  1. srand((unsigned int)time(NULL));
  2. float A = (float)(rand()) / (float)(RAND_MAX);
  3. int B = 1;
  4. int hashedValue = (int)((n * (k * (modf(A, &B)))));
  5. HashTable[hashedValue] = k;

"srand() 生成一个随机种子,以在每次调用方法时生成一个随机数。rand() 方法中的随机整数生成原本产生一个整数,但被强制转换为浮点数,然后除以一个极大的浮点数。这样的结果是一个介于 0 和 1 之间的随机浮点数。

接下来的过程相对容易理解。但需要注意的是,modf 解析了浮点数 A 和整数 B 的内存地址。"

  1. <details>
  2. <summary>英文:</summary>
  3. srand((unsigned int)time(NULL));
  4. float A = (float)(rand()) / (float)(RAND_MAX);
  5. int B = 1;
  6. int hashedValue = (int)((n* (k* (modf(A,&amp;B)))));
  7. HashTable[hashedValue] = k;
  8. sreand() generates a random seed to generate a random number upon each subsequent call to method. Rand int generation in rand() method originally produces an int but is typecasted to a float and then divided by an incredibly large float. The result of this is a random float between 0 and 1.
  9. The next process is relatively self-explanatory. However, it is important to note that modf parses the float a and the memory address to B.
  10. </details>
  11. # 答案3
  12. **得分**: -3
  13. 根据DaveS的说法,当尝试计算浮点数的模数时,需要使用fmod()而不是%取模运算符。
  14. 以下是您的示例代码,经过一些调整。需要指出的重要事项是,您需要非常小心您的命名约定,使用单个字母字符很容易,但在循环之外使用会影响代码的可读性。此外,方法名称应该具有描述性,"multiplicationMethod" 并没有提供关于方法实际功能的真正信息。
  15. ```cpp
  16. #include <iostream>
  17. #include <unordered_map>
  18. #include <math.h>
  19. const int SIZE = 20;
  20. class TestClass
  21. {
  22. std::unordered_map<int, std::string> hashTable;
  23. public:
  24. int multiplicationMethod(int value, int slotNum)
  25. {
  26. if (slotNum > SIZE)
  27. {
  28. printf("IndexOutOfBounds 错误。请尝试不同的槽号。");
  29. }
  30. else
  31. {
  32. int N = rand();
  33. float A = N / RAND_MAX;
  34. int hashedValue = slotNum * ((int)(value * fmod(A, 1)));
  35. hashTable[hashedValue] = value;
  36. printf(std::to_string(hashedValue).c_str());
  37. return hashedValue;
  38. }
  39. }
  40. };
  41. int main()
  42. {
  43. TestClass t;
  44. t.multiplicationMethod(4, 4);
  45. }

希望这有助于您理解代码的内容。

英文:

As DaveS stated you need to use fmod() rather than the % modulo operator when trying to work out the modulus of a float.

Here is your example with some tweaks. Big thing I have to point out is that you need to be very careful with your naming conventions it is easy to use one letter characters but outside of a loop it will affect the readability of your code. Also method names should be descriptive multiplicationMethod gives no real information as to what the method actually does.

  1. #include &lt;iostream&gt;
  2. #include &lt;unordered_map&gt;
  3. #include &lt;math.h&gt;
  4. const int SIZE = 20;
  5. class TestClass
  6. {
  7. std::unordered_map&lt;int, std::string&gt; hashTable;
  8. public:
  9. int multiplicationMethod(int value,int slotNum)
  10. {
  11. if(slotNum &gt; SIZE)
  12. {
  13. printf(&quot;IndexOutOfBounds Error. Please Try different slot number.&quot;);
  14. }
  15. else
  16. {
  17. int N = rand();
  18. float A = N/RAND_MAX;
  19. int hashedValue = slotNum * ((int)(value * fmod(A,1)));
  20. hashTable[hashedValue] = value;
  21. printf(std::to_string(hashedValue).c_str());
  22. return hashedValue;
  23. }
  24. }
  25. };
  26. int main()
  27. {
  28. TestClass t;
  29. t.multiplicationMethod(4,4);
  30. }

huangapple
  • 本文由 发表于 2023年8月5日 05:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定