Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

C言語の関数に構造体を渡して2つの複素数を追加するにはどうすればよいですか?


Cプログラミング言語で2つの複素数を加算するには、ユーザーは2つの複素数を構造体のメンバーとして取り、ユーザー定義関数を作成して、これら2つの数値に対して加算演算を実行する必要があります。

アルゴリズム

2つの複素数の加算については、以下のアルゴリズムを参照してください。

Step 1: Declare struct complex with data members.
Step 2: Declare name for structure and variables.
Step 3: Enter real and imaginary part for first complex number at run time.
Step 4: Enter real and imaginary part for second complex number at runtime
Step 5: Compute addition of number1 and number2 by calling function. Go to step 7.
Step 6: Print the result.
Step 7: Compute addition
  • Declare temp variable
  • temp.real = num1.real + num2.real;
  • temp.imag = num1.imag + num2.imag;
  • return (temp);

以下は、関数に構造体を渡すことによって2つの複素数を加算するためのCプログラムです-

#include <stdio.h>
typedef struct complex{
   float real;
   float imag;
} complex;
complex addition(complex num1, complex num2);
int main(){
   complex num1, num2, value;
   printf("entering real and imag parts of first complex no:\n ");
   scanf("%f %f", &num1.real, &num1.imag);
   printf("entering real and imag parts of second complex no:\n ");
   scanf("%f %f", &num2.real, &num2.imag);
   value= addition(num1, num2);
   printf("result = %.1f + %.1fi", value.real, value.imag);
   return 0;
}
complex addition(complex num1, complex num2){
   complex temp;
   temp.real = num1.real + num2.real;
   temp.imag = num1.imag + num2.imag;
   return (temp);
}

出力

上記のプログラムを実行すると、次の結果が得られます-

entering real and imag parts of first complex no:
entering real and imag parts of second complex no:
result = 0.0 + 0.0i

  1. OpenOfficeCalcで数値の列または行を合計する方法

    知っておくべきこと 結果を表示するセルを選択してから、SUM(Σ ) 入力 近くの列または行を自動的に追加します。必要に応じて範囲を変更します。 またはFXカテゴリ数学 。 機能の下 、 SUM次へ追加するセルを選択します。 別のオプションは、計算するデータの範囲のSUM関数を手動で入力することです。例: =SUM(A1:A6) 。 この記事では、SUM関数を使用してOpenOfficeCalcv。4.1.6で数値の行または列を追加するさまざまな方法について説明します。 OpenOfficeCalcSUM関数 この関数に入る2つの方法は次のとおりです。 SUMの使用 機能ショー

  2. Windows 11 で言語を追加する方法

    Windows 11 で言語を追加 Microsoft による発表の時点で、Windows 11 は既に Windows 10 を持っている人のための無料アップグレードです。このようにして、Windows 11 と互換性のあるすべての PC は Windows 11 にアップグレードできます。 Windows 11 が必要とするハードウェア要件。その見返りに、新しいバージョンの Windows には、ゲームのプレイやアプリの実行など、コンピューターでの操作を簡単にする多くの新機能があります。 たとえば、Windows のユーザー インターフェイスを改善したい場合は、まったく新しいレイアウ