【JavaScript】7-36 复数四则运算 (15 分)
题目:
本题要求编写程序,计算2个复数的和、差、积、商。
输入格式:
输入在一行中按照a1 b1 a2 b2
的格式给出2个复数C1=a1+b1i
和C2=a2+b2i
的实部和虚部。题目保证C2不为0。
输出格式:
分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果
的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。
输入样例1:
|
输出样例1:
|
输入样例2:
1 1 -1 -1.01 |
输出样例2:
|
JavaScript代码:
1 const { parse } = require('path') 2 let readline = require('readline') 3 4 const rl = readline.createInterface({ 5 input: process.stdin, 6 output: process.stdout 7 }) 8 9 rl.on('line',function(line){ 10 let tokens = line.split(' ') 11 let a1 = parseFloat(tokens[0]) 12 let b1 = parseFloat(tokens[1]) 13 let a2 = parseFloat(tokens[2]) 14 let b2 = parseFloat(tokens[3]) 15 16 let ret = main(a1, b1, a2, b2) 17 console.log(ret) 18 }) 19 20 function main(a1, b1, a2,b2) { 21 let ret = new String() 22 let mid1 = new String() 23 let mid2 = new String() 24 25 if (b1 < 0) { 26 mid1 = '' 27 } else { 28 mid1 = '+' 29 } 30 31 if (b2 < 0) { 32 mid2 = '' 33 } else { 34 mid2 = '+' 35 } 36 37 38 ret = '(' + a1.toFixed(1) + mid1 + b1.toFixed(1) + 'i' + ')' + ' + ' + '(' + a2.toFixed(1) + mid2 + b2.toFixed(1) +'i' + ')' + ' = ' + dealData( sum(a1, b1, a2,b2) ) + '\n' + '(' + a1.toFixed(1) + mid1 + b1.toFixed(1) + 'i' + ')' + ' - ' + '(' + a2.toFixed(1) + mid2 + b2.toFixed(1) +'i' + ')' + ' = ' + dealData( difference(a1, b1, a2,b2) ) + '\n' + '(' + a1.toFixed(1) + mid1 + b1.toFixed(1) + 'i' + ')' + ' * ' + '(' + a2.toFixed(1) + mid2 + b2.toFixed(1) +'i' + ')' + ' = ' + dealData( product(a1, b1, a2,b2) ) + '\n' + '(' + a1.toFixed(1) + mid1 + b1.toFixed(1) + 'i' + ')' + ' / ' + '(' + a2.toFixed(1) + mid2 + b2.toFixed(1) +'i' + ')' + ' = ' + dealData( quotient(a1, b1, a2,b2) ) 39 40 return ret 41 } 42 43 function sum(a1, b1, a2,b2) { 44 let treal = a1 + a2 45 let timag = b1 + b2 46 let frac = { 47 real : treal.toFixed(1), 48 imag : timag.toFixed(1) 49 } 50 51 return frac 52 } 53 54 function difference(a1, b1, a2,b2) { 55 let treal = a1 - a2 56 let timag = b1 - b2 57 let frac = { 58 real : parseFloat(treal).toFixed(1), 59 imag : parseFloat(timag).toFixed(1) 60 } 61 62 return frac 63 } 64 65 function product(a1, b1, a2,b2) { 66 let treal = a1 * a2 - b1 * b2 67 let timag = a1 * b2 + a2 * b1 68 let frac = { 69 real : parseFloat(treal).toFixed(1), 70 imag : parseFloat(timag).toFixed(1) 71 } 72 73 return frac 74 } 75 76 function quotient(a1, b1, a2,b2) { 77 let treal = (a1 * a2 + b1 * b2) / (a2 * a2 + b2 * b2) 78 let timag = (a2 * b1 - a1 * b2) / (a2 * a2 + b2 * b2) 79 let frac = { 80 real : parseFloat(treal).toFixed(1), 81 imag : parseFloat(timag).toFixed(1) 82 } 83 84 return frac 85 } 86 87 function dealData(frac) { 88 let ret = new String() 89 let mid = new String() 90 91 if (frac.imag < 0) { 92 mid = '' 93 } else { 94 mid = '+' 95 } 96 if (frac.real == 0.0 && frac.imag == 0.0) { 97 ret = frac.real 98 } 99 else if (frac.real == 0.0 && frac.imag != 0.0) { 100 ret = frac.imag + 'i' 101 } 102 else if (frac.real != 0.0 && frac.imag == 0.0) { 103 ret = frac.real 104 } 105 else if (frac.real != 0.0 && frac.imag != 0.0) { 106 ret = frac.real + mid + frac.imag + 'i' 107 } 108 109 return ret 110 }