定义一种复数类型称为Complex,包含real和imag两个整型域。使用Complex类型定义两个复数变量a和b,从键盘输入这两个变量的值,计算这两个复数的和与乘积并一行以一个空格分隔显示结果。(此题为计算机自动评分题,请严格按照以下规定编程,否则无法得分:不要有任何输出提示文字直接显示结果,结果后面不要跟回车。) 例如,分两行输入a和b的值: 3+3j 1-2j 则在一行显示:4+1j 9-3j
时间: 2024-02-13 07:06:09 浏览: 24
以下是C++的实现代码:
```C++
#include <iostream>
using namespace std;
struct Complex{
int real;
int imag;
};
int main() {
Complex a, b, sum, product;
char plus, multiply, j;
cin >> a.real >> plus >> a.imag >> j >> b.real >> plus >> b.imag >> j;
sum.real = a.real + b.real;
sum.imag = a.imag + b.imag;
product.real = a.real * b.real - a.imag * b.imag;
product.imag = a.imag * b.real + a.real * b.imag;
cout << sum.real << "+" << sum.imag << "j " << product.real << "+" << product.imag << "j";
return 0;
}
```
以下是C的实现代码:
```C
#include <stdio.h>
struct Complex{
int real;
int imag;
};
int main() {
Complex a, b, sum, product;
char plus, multiply, j;
scanf("%d%c%d%c%c%d%c%d%c", &a.real, &plus, &a.imag, &j, &multiply, &b.real, &plus, &b.imag, &j);
sum.real = a.real + b.real;
sum.imag = a.imag + b.imag;
product.real = a.real * b.real - a.imag * b.imag;
product.imag = a.imag * b.real + a.real * b.imag;
printf("%d+%dj %d+%dj", sum.real, sum.imag, product.real, product.imag);
return 0;
}
```
注意:输入格式必须按照题目要求,不要有任何提示文字输出,结果后面不要跟回车。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)