我不知道为什么这个程序不会编译。它告诉我有一个问题,但我只是不知道这个代码有什么问题。
我正在哈佛大学在线学习CS50入门课程,而且我在编写这个功能频率时遇到了麻烦。我知道这可以编码得更简单,但我只是没有经验使编程更加顺畅和优雅。问题似乎在功能的最后。我猜我忘了返回一个值,但我不知道发生了什么。
//Calculates frequency (in Hz) of a note
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int frequency(string note);
int main(void)
{
string note = get_string();
printf("Piano Note: ");
frequency(note);
return 0;
}
int frequency(string note)
{
char key = note[0];
char accidental;
int octave;
int counter = 0;
while (note != '\0')
{
counter++;
}
if(counter == 3)
accidental = note[1];
octave = note[2];
if(counter == 2)
octave = note[1];
if (counter == 3)
{
if (key == 'A')
{
if (accidental == '#')
{
if(octave == 4)
{
return 466;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 415;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
}
else if (key == 'B')
{
if (accidental == '#')
{
if(octave == 4)
{
return 523;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 466;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if (key == 'C')
{
if (accidental == '#')
{
if(octave == 4)
{
return 277;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 247;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)10/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)10/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'D')
{
if (accidental == '#')
{
if(octave == 4)
{
return 311;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 277;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'E')
{
if (accidental == '#')
{
if(octave == 4)
{
return 349;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)4/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)4/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 311;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'F')
{
if (accidental == '#')
{
if(octave == 4)
{
return 370;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}}
else if (accidental == 'b')
{
if(octave == 4)
{
return 330;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'G')
{
if (accidental == '#')
{
if(octave == 4)
{
return 415;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 370;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
}
if (counter == 2)
{
if(key == 'A')
{
if(octave == 4)
{
return 440;
}
else if(octave < 4)
{
int count = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int divisor = 0, hertz = 440;
while(divisor != count)
{
hertz /= 2;
divisor++;
}
return hertz;
}
else if(octave > 4)
{
int count = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int divisor = 0, hertz = 440;
while(divisor != count)
{
hertz *= 2;
divisor++;
}
return hertz;
}
}
else if(key == 'B')
{
if(octave == 4)
{
return 494;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)2/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)2/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if(key == 'C')
{
if(octave == 4)
{
return 262;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)9/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)9/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'D')
{
if(octave == 4)
{
return 294;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)7/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)7/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'E')
{
if(octave == 4)
{
return 330;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'F')
{
if(octave == 4)
{
return 349;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'G')
{
if(octave == 4)
{
return 392;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)2/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)2/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
}
}}}}}}}}}}}}
答案 0 :(得分:0)
虽然该代码看起来很糟糕,但是编译错误表明你的函数有一个分支,它将在没有return语句的情况下命中函数的末尾。如果看起来它正常工作,在函数中添加一个默认的返回值,比如int retVal = 0;然后就在你的函数结束之前把行返回retVal;
我没有读完整个示例源代码。你可能有if / elseif / elseif / etc没有else case,它也应该返回默认值,这将通过编译类型错误。
如果您不想返回默认值,请返回错误代码并让调用者检查它以确保它按照您的想法执行操作。
答案 1 :(得分:0)
在fflush(stdout);
之后,您需要printf("Piano Note: ");
主功能。 stdout可以是行缓冲的,没有尾随
换行,输出可能不会实际发送到stdout的任何地方
可能就是你的屏幕。
更重要的是,以下循环永远不会终止(假设 那个注意!='\ 0',
while (note != '\0')
{
counter++;
}
如果注意!= 0,将进入循环,但循环内没有任何内容 更新注释,因此条件仍然为真,循环将 重新执行。
我没有在任何地方看到'string'的定义。如果这是一个typedef 对于'char *',这是一个可怕的想法,隐藏指针 会导致混乱。如果它是别的东西,那么很难 说出行为应该是什么。你将它与一个字母进行比较,这意味着 'string'是char的typedef,但这不是很有用, 如果你想要一个字符串。
所以你应该丢失typedef,并且get_string()应该返回 char *,而不是'string'。我怀疑while循环正在尝试 计算字符串的长度。使用strlen(3)。
我没有算他们,但看起来你可能有太多的近距离支撑 在末尾。当然不会与任何可能的事情对齐 使视觉检查变得容易。