C语言一个小程序有几个错误,帮我看看

这个是程序
#include<stdio.h>
void main()
{
double x,y;
printf("Input x");
scanf("%lf",&x);
if(x<-1)
{
y=x^3-1;
printf("%lf",y);
}
else if(x<=1)
{
y=-3x+1;
printf("%lf",y);
}
else if(x<=10)
{
y=3e(2x-1)+5;
printf("%lf",y);
}
else
{
y=5*x+3*lg(2*x^2-1)-13;
printf("%lf",y);
}
这个是编译之后的提示
--------------------Configuration: 3 - Win32 Debug--------------------
Compiling...
3.cpp
D:\3.cpp(9) : error C2296: '^' : illegal, left operand has type 'double'
D:\3.cpp(14) : error C2059: syntax error : 'bad suffix on number'
D:\3.cpp(14) : error C2146: syntax error : missing ';' before identifier 'x'
D:\3.cpp(14) : warning C4552: '+' : operator has no effect; expected operator with side-effect
D:\3.cpp(19) : error C2021: expected exponent value, not '('
D:\3.cpp(19) : error C2059: syntax error : 'bad suffix on number'
D:\3.cpp(19) : error C2146: syntax error : missing ')' before identifier 'x'
D:\3.cpp(19) : error C2064: term does not evaluate to a function
D:\3.cpp(19) : error C2059: syntax error : ')'
D:\3.cpp(24) : error C2065: 'lg' : undeclared identifier
D:\3.cpp(24) : error C2296: '^' : illegal, left operand has type 'double'
Error executing cl.exe.

#include<stdio.h>
#include<math.h>//注意添加math头文件
void main()
{
double x,y;
printf("Input x");
scanf("%lf",&x);
if(x<-1)
{
//y=x^3-1;
y=pow(x,3)-1;//次方用pow函数,包含在math.h中
printf("%lf",y);
}
else if(x<=1)
{
//y=-3x+1;
y=-3*x+1;     //乘号不能省略                                                                                                            
printf("%lf",y);
}
else if(x<=10)
{
//y=3e(2*x-1)+5;
y=3*exp(2*x-1)+5;//指数函数用exp这个函数,包含在math.h中,不能直接写e
printf("%lf",y);
}
else
{
//y=5*x+3*lg(2*x^2-1)-13;
y=5*x+3*log(2*pow(x,2)-1)-13;//没有lg这个函数,有log这个函数
printf("%lf",y);
}
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-04-01
#include<stdio.h>
#include<math.h>
#define e 2.718
void main()
{
    double x, y;
    printf("Input x: ");
    scanf("%lf", &x);
    if (x < -1)
    {
        y = pow(x,3) - 1;
        printf("%lf", y);
    }
    else if (x <= 1)
    {
        y = -3*x + 1;
        printf("%lf", y);
    }
    else if (x <= 10)
    {
        y = 3*e*(2*x - 1) + 5;
        printf("%lf", y);
    }
    else
    {
        y = 5 * x + 3 * log(2 * pow(x, 2) - 1) - 13;
        printf("%lf", y);
    }
}

你这真心是小错误啊,自己定义数学运算符,未声明头文件,而且还缺个大括弧,我改了下,代码你对比下吧……

第2个回答  2015-04-01
在程序设计中,
^表示异或运算,要求x的3次方,可以用pow(x,3)或者x*x*x;
3x不能这样表示,应该是3*x,运算符不能少;
没有lg,要10为底,可以用换低公式,lg(x)=log(x)/log(10);
第3个回答  2015-04-01
①C 语言 中 ^ ,这个是 按位 异或 运算,不是 次方。次方只能用 乘法(*)x*x*x;
②C 语言 不支持 数学中的 乘号 省略(3x-1),要使用 *号(3*x-1);
③lg 对数,是需要 用 函数执行的。
相似回答