実習 複素数の逆数を求める
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication11 { class Program { static void Main() { Console.WriteLine("-複素数を求めるプログラム-"); Console.Write("複素数の実数部を入力してください: "); double x = GetDouble(); Console.Write("複素数の虚数部を入力してください: "); double y = GetDouble(); double abs = Math.Sqrt(x * x + y * y); double[] z = new double[] { x / abs, -y / abs }; string str_y = y > 0 ? string.Format("+ " + y) : string.Format("- " + (-y)); string str_zy = z[1] > 0 ? string.Format("+ " + z[1]) : string.Format("- " + (-z[1])); Console.WriteLine("{0} {1}iの逆数は{2} {3}iです", x, str_y, z[0], str_zy); } private static double GetDouble() { double x; while (!double.TryParse(Console.ReadLine(), out x)) { Console.Write("実数を入力してください: "); } return x; } } }
↓
-複素数を求めるプログラム- 複素数の実数部を入力してください: a 実数を入力してください: 3 複素数の虚数部を入力してください: -4 3 - 4iの逆数は0.6 + 0.8iです