C言語です。初めて使います。
文字数制限かかってしまうので返信にエラーコード貼ります!
状況説明です。大学の課題で、複素数を2つ入力しの四則演算を求めよと言う課題が出ました。
まず、複素数の和のソースコードを配られ、そこから拡張して作りなさいと言う物なんですけど、そもそもそのソースコードがエラーをはいて動かないです。写し間違えないように3.4回見直して、調べたらもしたのですがどうしても解決しません。
どこを直したら良いですか?
/* 複素数の和を求めるプログラム */ #include <stdio.h> #include "complex.h" int main (void) { complex u, v; puts("2つの複素数(u, v)を入力してください."); puts("u = "); u = comp_scan (); puts("v = "); v = comp_scan (); puts("\ n 入 力 結 果 を 示 し ま す . \n"); printf("u = "); comp_print(u, Orth); printf(" = "); comp_print(u, Polar); printf("v = "); comp_print(v, Orth); printf(" = "); comp_print(v, Polar); puts("\ n 演 算 結 果 を 示 し ま す . \n"); printf("u + v = "); comp_print(comp_add(u,v), Orth); printf(" = "); comp_print(comp_add(u,v), Polar); return (0); } /* *complex.h 複素数の演算を記述するプログラムヘッダ */ #include <stdio.h> /* 複素数を表す構造体 */ typedef struct { double real; double imag; } complex; /* 直行形式,極形式を表す列挙体 */ typedef enum {Orth, Polar} form; /* 複素数uとvの加算結果を返す関数 */ complex comp_add (complex u, complex v); /* 複素数uの絶対値を返す関数 */ double comp_abs (complex u); /* 複素数uの偏角を返す関数 */ double comp_arg (complex u); /* 読み込んだ複素数を返す関数 */ complex comp_scan (void); /* 読み込んだ複素数を返す関数 */ void comp_print (complex z, form f); /* * complex.c * * 複素数の演算を記述するプログラム */ #include <stdio.h> #include <math.h> /* コンパイルオプション -lm を付ける */ #include "complex.h" #define sqr(x) ((x) * (x)) /* xの2乗を求める関数形式マクロ */ /* 複素数uとvの加算結果を返す関数 */ complex comp_add (complex u, complex v) { complex w; w.real = u.real + v.real; w.imag = u.imag + v.imag; return (w); } /* 複素数uの絶対値を返す関数 */ double comp_abs (complex u) { return (sqrt(sqr(u.real) + sqr(u.imag))); } /* 複素数uの偏角を返す関数 */ double comp_arg (complex u) { return (atan2(u.imag, u.real)); } /* 読み込んだ複素数を返す関数 */ complex comp_scan (void) { complex z; printf("Re: "); scanf("%lf", &z.real); printf("Im: "); scanf("%lf", &z.imag); return (z); } /* 読み込んだ複素数を返す関数 */ void comp_print (complex z, form f) { switch(f) { case Orth : printf("%f+%fi\n", z.real, z.imag); break; case Polar : printf("%fexp(%fi)\n", comp_abs(z), comp_arg(z)); break; } }
tamesi.c:13:13: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex u, v;
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:17:17: error: implicit declaration of function 'comp_scan' is invalid
in C99 [-Werror,-Wimplicit-function-declaration]
u = comp_scan ();
^
tamesi.c:23:13: error: implicit declaration of function 'comp_print' is invalid
in C99 [-Werror,-Wimplicit-function-declaration]
comp_print(u, Orth);
^
tamesi.c:23:27: error: use of undeclared identifier 'Orth'
comp_print(u, Orth);
^
tamesi.c:25:27: error: use of undeclared identifier 'Polar'
comp_print(u, Polar);
^
tamesi.c:28:27: error: use of undeclared identifier 'Orth'
comp_print(v, Orth);
^
tamesi.c:30:27: error: use of undeclared identifier 'Polar'
comp_print(v, Polar);
^
tamesi.c:35:24: error: implicit declaration of function 'comp_add' is invalid in
C99 [-Werror,-Wimplicit-function-declaration]
comp_print(comp_add(u,v), Orth);
^
tamesi.c:35:39: error: use of undeclared identifier 'Orth'
comp_print(comp_add(u,v), Orth);
^
tamesi.c:37:39: error: use of undeclared identifier 'Polar'
comp_print(comp_add(u,v), Polar);
^
tamesi.c:50:9: error: expected ';' after struct
} complex;
^
;
tamesi.c:47:8: warning: typedef requires a name [-Wmissing-declarations]
typedef struct {
^~~~~~~
tamesi.c:50:10: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
} complex;
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:50:10: warning: declaration does not declare anything
[-Wmissing-declarations]
} complex;
^~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^~~~~~~~
tamesi.c:56:9: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_add (complex u, complex v);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:56:27: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_add (complex u, complex v);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:56:38: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_add (complex u, complex v);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:56:17: error: conflicting types for 'comp_add'
complex comp_add (complex u, complex v);
^
tamesi.c:35:24: note: previous implicit declaration is here
comp_print(comp_add(u,v), Orth);
^
tamesi.c:59:26: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
double comp_abs (complex u);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:62:26: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
double comp_arg (complex u);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:65:9: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_scan (void);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:65:17: error: conflicting types for 'comp_scan'
complex comp_scan (void);
^
tamesi.c:17:17: note: previous implicit declaration is here
u = comp_scan ();
^
tamesi.c:68:26: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
void comp_print (complex z, form f);
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:68:14: error: conflicting types for 'comp_print'
void comp_print (complex z, form f);
^
tamesi.c:23:13: note: previous implicit declaration is here
comp_print(u, Orth);
^
tamesi.c:82:2: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_add (complex u, complex v)
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:82:20: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_add (complex u, complex v)
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:82:31: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex comp_add (complex u, complex v)
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:85:11: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
complex w;
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
tamesi.c:87:12: error: member reference base type '_Complex double' is not a
structure or union
w.real = u.real + v.real;
~^~~~~
tamesi.c:87:21: error: member reference base type '_Complex double' is not a
structure or union
w.real = u.real + v.real;
~^~~~~
tamesi.c:87:30: error: member reference base type '_Complex double' is not a
structure or union
w.real = u.real + v.real;
~^~~~~
tamesi.c:88:12: error: member reference base type '_Complex double' is not a
structure or union
w.imag = u.imag + v.imag;
~^~~~~
tamesi.c:88:21: error: member reference base type '_Complex double' is not a
structure or union
w.imag = u.imag + v.imag;
~^~~~~
tamesi.c:88:30: error: member reference base type '_Complex double' is not a
structure or union
w.imag = u.imag + v.imag;
~^~~~~
tamesi.c:94:19: warning: plain '_Complex' requires a type specifier; assuming
'_Complex double'
double comp_abs (complex u)
^
double
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
16 warnings and 20 errors generated.
教授もMac使っているのですか。
競合していますね。リンカオプションを設定するのが難しいなら Windows か Linux を使えばいいんじゃないでしょうか。
教授はおそらくWindowsだとおもいます。
大学の推奨マシンがMacだったってこと?
推奨環境がWindowsだったのなら自力で解決しないと。
パソコンに疎くてすみません。
自分は大学1年で今年プログラミングを始めました。本来なら大学のパソコンでプログラミングをする様ですが、授業がオンラインとなっているため各自のパソコンで環境構築をして下さいとなりました。
大学からのパソコン指定はなく自由で、大学からmac、Windowsそれぞれ環境構築の資料をもらって構築した以外はいじってないです。
資料に書いてある通りにしても動かないなら学校側の不手際なので問い合わせてください。他にもできない学生がいるはずなので、一人だけ良ければいいというものではありません。
資料に書いてないことをしたなら最初からやり直してください。
「動かない時は OS を変えろ」というのが Mac 使いの流儀なので、その流儀に沿うなら Windows を使うのが良いでしょう。「Windows を買っても Mac は使えないが Mac を買えば Windows は使える」と 100 回くらい聞いています。
>回答者の皆さま
定義重複ですかねぇ。
とりあえずですが、complexが一般用語すぎたってことで、
Fukusosu
とかに変えるのが、楽だと思いますが。。。
※さすがに、初めてプログラムやる子にやらせるのには、酷な気がしました。。。
---
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:37:17: note:
expanded from macro 'complex'
#define complex _Complex
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h
上記は正しい(あなたの読みたいcomplex.hの)パスですか。
既に指摘をいただいていますが、標準(?)のcomplex.h をincludeしてしまっているようです。
独自のcomplex.h はCのファイルと同じディレクトリに配置して#include "./complex.h" などとするか、mycomplex.hなどとリネームして使うのが良いのでは?
皆様ご指摘ありがとうございました。
解決出来ました。
回答2件
あなたの回答
tips
プレビュー