2014-10-16 36 views
-6

我正在尝试构建一个代码来计算电容电抗一个简单的串联电容电抗器,用于我的电子类,代码的目的是无关紧要的,因为问题在于我做了一些非常错误的事情,并且需要我的C语言技能帮助,这是您需要它的公式:我如何修正编译器错误:在'{'token |“之前预计'=',',',';','asm'或'attribute'

Formula for Capacitance

这里是我的代码: (不包括头,因为他们会在后的HTML格式错误)

const float pi = 3.145962; 

float xc(float frequency, float capacitance) 





int main() 
{ 

float capreac; 
float valueofc() 
float valueoff() 
    capreac = xc(f;c;) 
} 


float xc(float frequncy, float capaitance); 
{ 
    float answer; 
    answer = (1/(2*pi*capacitance*frequency)); 
    return(answer); 


} 

我的IDE是CodeBlocks,这是我班正在使用的。在'{'token |'之前}表示“| 13 |错误:预计'=',',';','asm'或'属性',它拒绝运行。在“float xc”行和“| 6 |错误:原型函数定义中的旧式参数声明|”在xc的定义线上。

这个newb的任何希望?

+6

[了解一点点'C'(http://www.learn-c.org/),因为你已经在你的代码中有一些明显的语法错误和逻辑问题。分号混淆了 – Adam 2014-10-16 21:04:16

+4

。 – OldProgrammer 2014-10-16 21:04:19

+2

......什么是'xc(f; c;)'? – 2014-10-16 21:04:51

回答

2

1)你需要一个分号和原型的末尾:

float xc(float frequency, float capacitance);

2)变量声明没有 “()”

float f; 
float c; 

3)参数分离通过逗号:capreac = xc(f, c);

4)参数名称应该匹配变量名称:

float xc(float frequency, float capacitance); 
{ 
    float answer; 
    answer = (1.0/(2.0*pi*capacitance*frequency)); 
    ... 
+3

看到我上面的评论 - 删除你在')'和'{'之间的分号功能定义。 – 2014-10-16 21:08:36

+0

回想起来,我并不生气,我为此得到了这么多的赞誉! – 2016-01-16 04:00:44

2
#include <stdio.h> 
#include <math.h> 
typedef long double ldouble; 
ldouble xc(ldouble f, ldouble C){ return 1/(2*M_PIl*f*C);} 
int main(int argc, const char *argv[]) 
{ 
    ldouble f, C, answer; 

    puts("Enter f:"); 
    scanf("%Lf", &f); 
    puts("Enter C:"); 
    scanf("%Lf", &C); 
    answer = xc(f, C); 
    printf("Xc=%Lf\n", answer); 

    return 0; 
} 
+0

为什么ldouble而不是浮动? – 2014-10-16 21:29:21

+0

花车比较不精确。至少你最好加倍。 – PSkocik 2014-10-16 21:30:09

+2

@PSkocik:既然你要精确,你能修正PI的定义,所以它实际上是正确的吗?有一种愚蠢的错误答案。 – indiv 2014-10-16 21:47:29

相关问题