【发布时间】:2018-09-25 15:33:07
【问题描述】:
我试图了解为什么我对此 Pset 的频率部分的最终计算没有正确计算。我正在通过调试器,它显示“步骤”计数正确,但计算不起作用。
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
string note = get_string("note:");
{
if (strlen(note) == 2)
{
int key = note[0];
int octave = atoi(¬e[1]);
int steps = 0;
if(key == 'A')
{
steps = (key - 65) + ((octave - 4) * 12);
}
float frequencyf = (440 * (pow(2, (steps / 12))));
int frequency = (round(frequencyf));
printf("%i", frequency);
}
else if (strlen(note) == 3)
{
int key = note[0];
int accidental = note[1];
int octave = atoi(¬e[2]);
int steps = 0;
if(accidental == '#')
{
if(key == 'A')
{
steps = ((key - 65) + ((octave - 4) * 12) + 1);
}
}
else if(accidental == 'b')
{
if(key == 'A')
{
steps = ((key - 65) + ((octave - 4) * 12) - 1);
}
}
float frequencyf = (440 * (pow(2, (steps / 12))));
int frequency = (round(frequencyf));
printf("%i", frequency);
}
}
}
【问题讨论】:
-
有一个cs50 stack exchange site如果你有兴趣。
-
啊没看到,抱歉!
-
请记住,在 C 语言中,整数除法会产生整数结果:
1/2 == 0、5/2 == 2等。如果您希望除法的结果是浮点值,则至少以下之一操作数必须是浮点类型。尝试将steps除以12.0看看是否没有帮助。