python【蓝桥杯vip练习题库】ALGO-142 P1103(复数运算)

试题 算法训练 P1103

资源限制
时间限制:1.0s 内存限制:256.0MB
  
  编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:

要求:(1)定义一个结构体类型来描述复数。
  (2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
  (3)必须使用结构体指针的方法把函数的计算结果返回。
  说明:用户输入:运算符号(+,-,*,/) a b c d.
  输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。

输入:
  - 2.5 3.6 1.5 4.9
输出:
  1.00±1.30i


"""
@Author:Lixiang

@Blog(个人博客地址): https://lixiang007.top/

@WeChat:18845312866

"""
import math
import string
import sys
import cmath
from itertools import permutations
fuhao,a,b,c,d=input().strip().split()
a=float(a)
b=float(b)
c=float(c)
d=float(d)
if fuhao=='-':
    temp1=complex(a,b)
    temp2=complex(c,d)
    temp=temp1-temp2
    res1="{:.2f}".format(temp.real)
    res2="{:.2f}".format(temp.imag)
    print(res1,'+',res2,'i',sep="")
if fuhao=='+':
    temp1 = complex(a, b)
    temp2 = complex(c, d)
    temp = temp1 + temp2
    res1 = "{:.2f}".format(temp.real)
    res2 = "{:.2f}".format(temp.imag)
    print(res1, '+', res2, 'i', sep="")
if fuhao=='*':
    temp1 = complex(a, b)
    temp2 = complex(c, d)
    temp = temp1 * temp2
    res1 = "{:.2f}".format(temp.real)
    res2 = "{:.2f}".format(temp.imag)
    print(res1, '+', res2, 'i', sep="")
if fuhao=='/':
    temp1 = complex(a, b)
    temp2 = complex(c, d)
    temp = temp1 / temp2
    res1 = "{:.2f}".format(temp.real)
    res2 = "{:.2f}".format(temp.imag)
    print(res1, '+', res2, 'i', sep="")

在这里插入图片描述

发布了829 篇原创文章 · 获赞 215 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_43838785/article/details/104670587