C言語

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ゆうき316

C言語

#1

投稿記事 by ゆうき316 » 9年前

このプログラムを作ってください よろしくお願いします!
以下の要件を満たすプログラムを作成せよ
課題名:引数・返り値に構造体を使った複素数計算関数

•要件
–複素数の足し算を行う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)=【乗算結果】

アバター
Dixq (管理人)
管理人
記事: 1662
登録日時: 14年前
住所: 北海道札幌市
連絡を取る:

Re: C言語

#2

投稿記事 by Dixq (管理人) » 9年前

まずフォーラムルールをお読みください。
http://dixq.net/board/board.html
当サイトでは宿題の代行はしません。課題の丸投げは禁止行為です。

自分が作ったプログラムを提示してどこが分からないのか明確にして再度質問してください。

ゆうき316

Re: C言語

#3

投稿記事 by ゆうき316 » 9年前

宿題ではありません

ゆうき316

Re: C言語

#4

投稿記事 by ゆうき316 » 9年前

この先がわかりません
#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

ゆうき316

Re: C言語

#5

投稿記事 by ゆうき316 » 9年前

資格試験のためにやっています
わかる方よろしくお願いします

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 14年前
住所: 東海地方
連絡を取る:

Re: C言語

#6

投稿記事 by softya(ソフト屋) » 9年前

ここまで書けて、ここで詰まる原因が分かりません。
何処が悩みのポイントでしょうか?
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

アバター
Hiragi(GKUTH)
記事: 167
登録日時: 14年前
住所: 大阪府
連絡を取る:

Re: C言語

#7

投稿記事 by Hiragi(GKUTH) » 9年前

まず現時点で、返り値に計算結果が必要であるので関数の型はcomplex_numberである必要があります。
複素数同士の計算と言うのは調べれば出てくる(高校の数IIで習った)のでその通りに実装すればよいかと思います。
だいがくせい!

box
記事: 2002
登録日時: 14年前

Re: C言語

#8

投稿記事 by box » 9年前

コード:

#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;
}
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

閉鎖

“C言語何でも質問掲示板” へ戻る