質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1224閲覧

エラーの解決をしたいです!

tosakagreen

総合スコア1

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/02/06 08:57

編集2021/02/06 09:39

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; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tosakagreen

2021/02/06 08:57

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.
1T2R3M4

2021/02/06 09:10

教授もMac使っているのですか。
Zuishin

2021/02/06 09:14

競合していますね。リンカオプションを設定するのが難しいなら Windows か Linux を使えばいいんじゃないでしょうか。
tosakagreen

2021/02/06 09:16

教授はおそらくWindowsだとおもいます。
1T2R3M4

2021/02/06 09:19

大学の推奨マシンがMacだったってこと? 推奨環境がWindowsだったのなら自力で解決しないと。
tosakagreen

2021/02/06 09:24

パソコンに疎くてすみません。 自分は大学1年で今年プログラミングを始めました。本来なら大学のパソコンでプログラミングをする様ですが、授業がオンラインとなっているため各自のパソコンで環境構築をして下さいとなりました。 大学からのパソコン指定はなく自由で、大学からmac、Windowsそれぞれ環境構築の資料をもらって構築した以外はいじってないです。
Zuishin

2021/02/06 09:33 編集

資料に書いてある通りにしても動かないなら学校側の不手際なので問い合わせてください。他にもできない学生がいるはずなので、一人だけ良ければいいというものではありません。 資料に書いてないことをしたなら最初からやり直してください。 「動かない時は OS を変えろ」というのが Mac 使いの流儀なので、その流儀に沿うなら Windows を使うのが良いでしょう。「Windows を買っても Mac は使えないが Mac を買えば Windows は使える」と 100 回くらい聞いています。
WhiteTempest

2021/02/06 09:47

>回答者の皆さま 定義重複ですかねぇ。 とりあえずですが、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
1T2R3M4

2021/02/06 09:48

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h 上記は正しい(あなたの読みたいcomplex.hの)パスですか。
dodox86

2021/02/06 10:23 編集

既に指摘をいただいていますが、標準(?)のcomplex.h をincludeしてしまっているようです。 独自のcomplex.h はCのファイルと同じディレクトリに配置して#include "./complex.h" などとするか、mycomplex.hなどとリネームして使うのが良いのでは?
tosakagreen

2021/02/06 13:54

皆様ご指摘ありがとうございました。 解決出来ました。
guest

回答2

0

そのエラーが出る不完全なコードに手を加えて、実行できるようにしていく、という課題です
この問題がわからないなら、アカの他人に聞くのではなく、その教授に聞くようにしましょう

投稿2021/02/06 09:00

y_waiwai

総合スコア87784

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tosakagreen

2021/02/06 09:10

問題の文頭に「このプログラムは複素数の和を求めるものである。また実行時の入出力結果も示している。」と書いてあり、不完全の物に手を加えて作成せよとは書いてないです。 課題を赤の他人に解かせようと言う意図は全くありません。
y_waiwai

2021/02/06 09:17

それならなおのこと、それを出した教授に問い合わせるべきものじゃないでしょうか。 出題の意図なんかそれこそアカの他人に聞くほうが間違ってます
guest

0

ベストアンサー

そもそもですが、
配られたものをすべて取り込みきれていないのではないでしょうか?
※ヘッダーに宣言のある関数の実体がありませんよ。

本当に現在提示されているものしか配られていないのであれば、
やはり意図を教授に聞くべきです。
あくまで考え方のサンプルという位置付けで配られたものである可能性が高いです。
※使用する関数の実体は自分達で作成しなさい(≒拡張)ともとれますし。

投稿2021/02/06 09:23

WhiteTempest

総合スコア404

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tosakagreen

2021/02/06 09:35

すみません、パソコンとスマホを経由していて、いろいろ手違いがあった様です。正しくソースコード直しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問