2 qq 29754969 qq_29754969 于 2016.02.12 12:43 提问

大神们!这个程序在没有错误,能运行,为什么一到蓝桥杯提交时间就会显示编译错误的?
c


评测结果 编译出错
得分 0
CPU使用 编译出错
内存使用 编译出错
试题名称 算法训练 P1103
语言 C
源代码

#include
#include
#include
struct fushu{
double real;
double i;
};
fushu* add(fushu a, fushu b){
fushu* ret = (fushu*)malloc(sizeof(fushu));
ret->real = a.real + b.real;
ret->i = a.i + b.i;
return ret;
}
fushu* minus(fushu a, fushu b){
fushu* ret = (fushu*)malloc(sizeof(fushu));
ret->real = a.real - b.real;
ret->i = a.i - b.i;
return ret;
}
fushu* multiply(fushu a, fushu b){
fushu* ret = (fushu*)malloc(sizeof(fushu));
ret->real = a.real*b.real - a.i*b.i;
ret->i = a.real*b.i + a.i*b.real;
return ret;
}
fushu* divide(fushu a, fushu b){
fushu* ret = (fushu*)malloc(sizeof(fushu));
ret->real = (a.real*b.real + a.i*b.i)/(b.real*b.real + b.i*b.i);
ret->i = (a.i*b.real - a.real*b.i)/(b.real*b.real + b.i*b.i);
return ret;
}
int main()
{

char sign;
fushu m;
fushu n;
scanf("%c%lf%lf%lf%lf",&sign,&m.real,&m.i,&n.real,&n.i);
if( sign == '+' ){
add(m,n);
printf("%.2lf+%.2lfi\n",add(m,n)->real,add(m,n)->i);
}
if( sign == '-' ){
minus(m,n);
printf("%.2lf+%.2lfi\n",minus(m,n)->real,minus(m,n)->i);
}
if( sign == '*' ){
multiply(m,n);
printf("%.2lf+%.2lfi\n",multiply(m,n)->real,multiply(m,n)->i);
}
if( sign == '/' ){
divide(m,n);
printf("%.2lf+%.2lfi\n",divide(m,n)->real,divide(m,n)->i);
}
return 0;
}
编译信息

code.c:8:2: error: unknown type name 'fushu'
fushu* add(fushu a, fushu b){
^
code.c:8:13: error: unknown type name 'fushu'
fushu* add(fushu a, fushu b){
^
code.c:8:22: error: unknown type name 'fushu'
fushu* add(fushu a, fushu b){
^
code.c:14:2: error: unknown type name 'fushu'
fushu* minus(fushu a, fushu b){
^
code.c:14:15: error: unknown type name 'fushu'
fushu* minus(fushu a, fushu b){
^
code.c:14:24: error: unknown type name 'fushu'
fushu* minus(fushu a, fushu b){
^
code.c:20:2: error: unknown type name 'fushu'
fushu* multiply(fushu a, fushu b){
^
code.c:20:18: error: unknown type name 'fushu'
fushu* multiply(fushu a, fushu b){
^
code.c:20:27: error: unknown type name 'fushu'
fushu* multiply(fushu a, fushu b){
^
code.c:26:2: error: unknown type name 'fushu'
fushu* divide(fushu a, fushu b){
^
code.c:26:16: error: unknown type name 'fushu'
fushu* divide(fushu a, fushu b){
^
code.c:26:25: error: unknown type name 'fushu'
fushu* divide(fushu a, fushu b){
^
code.c: In function 'main':
code.c:35:2: error: unknown type name 'fushu'
fushu m;
^
code.c:36:2: error: unknown type name 'fushu'
fushu n;
^
code.c:37:33: error: request for member 'real' in something not a structure or union
scanf("%c%lf%lf%
详细记录

评测点序号 评测结果 得分 CPU使用 内存使用 下载评测数据
1 编译出错 0.00 0ms 0B 输入 输出
2 编译出错 0.00 0ms 0B VIP特权
3 编译出错 0.00 0ms 0B VIP特权
4 编译出错 0.00 0ms 0B VIP特权

2个回答

qq_27183003
qq_27183003   Ds   Rxr 2016.02.12 14:17

1、如果是c语言,必须用struct fusht_t 来定义结构体变量,如果是c++,可以省略struct;
或者这样定义:

 typedef struct {
    double real;
    double i;
}fushu;

2、你的程序有一些不合理的地方,每一次调用函数都会动态申请了一个结构体变量,但都没有释放。
修改如下,供参考:

 #include <stdio.h>
#include <stdlib.h>
typedef struct {
    double real;
    double i;
}fushu;

fushu* add(fushu a, fushu b){
    fushu* ret = (fushu*)malloc(sizeof(fushu));
    ret->real = a.real + b.real;
    ret->i = a.i + b.i;
    return ret;
}
fushu* minus(fushu a, fushu b){
    fushu* ret = (fushu*)malloc(sizeof(fushu));
    ret->real = a.real - b.real;
    ret->i = a.i - b.i;
    return ret;
}
fushu* multiply(fushu a, fushu b){
    fushu* ret = (fushu*)malloc(sizeof(fushu));
    ret->real = a.real*b.real - a.i*b.i;
    ret->i = a.real*b.i + a.i*b.real;
    return ret;
}
fushu* divide(fushu a, fushu b){
    fushu* ret = (fushu*)malloc(sizeof(fushu));
    ret->real = (a.real*b.real + a.i*b.i)/(b.real*b.real + b.i*b.i);
    ret->i = (a.i*b.real - a.real*b.i)/(b.real*b.real + b.i*b.i);
    return ret;
}
int main()
{

    char sign;
    fushu m;
    fushu n;
    fushu *p=NULL;
    scanf("%c%lf%lf%lf%lf",&sign,&m.real,&m.i,&n.real,&n.i);
    if( sign == '+' ){
        p=add(m,n);
        printf("%.2lf+%.2lfi\n",p->real,p->i);
    }
    if( sign == '-' ){
        p=minus(m,n);
        printf("%.2lf+%.2lfi\n",p->real,p->i);
    }
    if( sign == '*' ){
        p=multiply(m,n);
        printf("%.2lf+%.2lfi\n",p->real,p->i);
    }
    if( sign == '/' ){
        p=divide(m,n);
        printf("%.2lf+%.2lfi\n",p->real,p->i);
    }
    if(p) delete p;

    return 0;
}
caozhy
caozhy 赞,很详细的答案,建议lz采纳
接近 2 年之前 回复
qq_29754969
qq_29754969 谢谢啊!已经解决了!
接近 2 年之前 回复
91program
91program   Ds   Rxr 2016.02.12 13:35

你最好是使用 struct fushu 来定义变量,或者返回的类型。
否则,你最好使用如下的定义:

 typedef struct fushu
 {
 ...
 }fusht_t;

然后使用 fushu_t 来定义变量等。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!