ページ 1 / 1
C言語
Posted: 2015年9月30日(水) 20:52
by ゆうき316
このプログラムを作ってください よろしくお願いします!
以下の要件を満たすプログラムを作成せよ
課題名:引数・返り値に構造体を使った複素数計算関数
•要件
–複素数の足し算を行うcomplex_sum関数と掛け算を行うcomplex_product関数を作成せよ
•引数:複素数(その1),複素数(その2)の2つ
•返り値:複素数(計算結果)
•動作:複素数その1と複素数その2を足してその結果を返り値にする
–このとき,計算は作成した関数で,出力はmain関数で行うこと
•出力:
1つ目の複素数の実部,虚部を入力してください:【実数値(1),実数値(2)を入力】
2つ目の複素数の実部,虚部を入力してください:【実数値(3),実数値(4)を入力】
(【実数値(1)】+【実数値(2)】i)+(【実数値(3)】+【実数値(4)】i)=【加算結果】
(【実数値(1)】+【実数値(2)】i)×(【実数値(3)】+【実数値(4)】i)=【乗算結果】
Re: C言語
Posted: 2015年9月30日(水) 21:08
by Dixq (管理人)
まずフォーラムルールをお読みください。
http://dixq.net/board/board.html
当サイトでは宿題の代行はしません。課題の丸投げは禁止行為です。
自分が作ったプログラムを提示してどこが分からないのか明確にして再度質問してください。
Re: C言語
Posted: 2015年9月30日(水) 21:10
by ゆうき316
宿題ではありません
Re: C言語
Posted: 2015年9月30日(水) 21:12
by ゆうき316
この先がわかりません
#include <stdio.h>
#include<string.h>
struct complex_number{
float real ;
float imag ;
};
void complex_sum(struct complex_number,struct complex_number);
void complex_product(struct complex_number,struct complex_number)
int main(void)
{
struct complex_number comp1;
struct complex_number comp2;
printf("1つ目の複素数の実部、虚部を入力してください:");
scanf("%f,%f",&comp1.real,&comp1.imag);
printf("2つ目の複素数の実部、虚部を入力してください:");
scanf("%f,%f",&comp2.real,&comp2.imag);
return 0 ;
}
void
Re: C言語
Posted: 2015年9月30日(水) 21:14
by ゆうき316
資格試験のためにやっています
わかる方よろしくお願いします
Re: C言語
Posted: 2015年9月30日(水) 21:54
by softya(ソフト屋)
ここまで書けて、ここで詰まる原因が分かりません。
何処が悩みのポイントでしょうか?
Re: C言語
Posted: 2015年9月30日(水) 22:43
by Hiragi(GKUTH)
まず現時点で、返り値に計算結果が必要であるので関数の型はcomplex_numberである必要があります。
複素数同士の計算と言うのは調べれば出てくる(高校の数IIで習った)のでその通りに実装すればよいかと思います。
Re: C言語
Posted: 2015年9月30日(水) 23:50
by box
コード:
#include <stdio.h>
#include <math.h>
typedef struct {
double real;
double imag;
} Complex;
Complex complex_sum(Complex m, Complex n)
{
Complex ans;
ans.real = m.real + n.real;
ans.imag = m.imag + n.imag;
return ans;
}
Complex complex_product(Complex m, Complex n)
{
Complex ans;
ans.real = m.real * n.real - m.imag * n.imag;
ans.imag = m.real * n.imag + m.imag * n.real;
return ans;
}
void printAnswer(Complex m, Complex n, Complex a)
{
printf("(%f%c%fi)+(%f%c%fi)=%f%c%fi\n",
m.real, (m.imag >= 0) ? '+' : '-', fabs(m.imag),
n.real, (n.imag >= 0) ? '+' : '-', fabs(n.imag),
a.real, (a.imag >= 0) ? '+' : '-', fabs(a.imag));
}
int main(void)
{
Complex n1, n2, ans;
printf("1つ目の複素数の実部,虚部を入力してください:");
scanf("%lf,%lf", &(n1.real), &(n1.imag));
printf("2つ目の複素数の実部,虚部を入力してください:");
scanf("%lf,%lf", &(n2.real), &(n2.imag));
ans = complex_sum(n1, n2);
printAnswer(n1, n2, ans);
ans = complex_product(n1, n2);
printAnswer(n1, n2, ans);
return 0;
}