# C语言，请问怎么对一个数进行四舍五入并保留一位小数

【描述】

【输入】

【输出】

【输入示例】

2 3.08 -2.04 5.06
【输出示例】
(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i

• 写回答

#### 2条回答默认 最新

• CodeBytes 2023-02-12 17:25
关注

该回答引用ChatGPT

``````
#include<stdio.h>
#include<math.h>

typedef struct {
double real, imag;
}Complex;

Complex add(Complex c1, Complex c2) {
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}

Complex subtract(Complex c1, Complex c2) {
Complex result;
result.real = c1.real - c2.real;
result.imag = c1.imag - c2.imag;
return result;
}

Complex multiply(Complex c1, Complex c2) {
Complex result;
result.real = c1.real * c2.real - c1.imag * c2.imag;
result.imag = c1.real * c2.imag + c1.imag * c2.real;
return result;
}

Complex divide(Complex c1, Complex c2) {
Complex result;
result.real = (c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag);
result.imag = (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag);
return result;
}

void printComplex(Complex c, char operator) {
if (c.real == 0) {
if (c.imag == 0) {
printf("0.0\n");
}
else if (c.imag > 0) {
printf("%.1fi\n", c.imag);
}
else {
printf("-%.1fi\n", fabs(c.imag));
}
}
else if (c.imag == 0) {
printf("%.1f\n", c.real);
}
else if (c.imag > 0) {
printf("(%.1f+%.1fi)\n", c.real, c.imag);
}
else {
printf("(%.1f-%.1fi)\n", c.real, fabs(c.imag));
}
}

int main() {
Complex c1, c2, result;
scanf("%lf%lf%lf%lf", &c1.real, &c1.imag, &c2.real, &c2.imag);
printComplex(c1, '+');
printComplex(c2, '=');
printComplex(result, '\0');
result = subtract(c1, c2);
printComplex(c1, '-');
printComplex(c2, '=');
printComplex(result, '\0');
result = multiply(c1, c2);
printComplex(c1, '*');
printComplex(c2, '=');
printComplex(result

``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 问题事件

• 已结题 （查看结题原因） 2月12日
• 已采纳回答 2月12日
• 创建了问题 2月12日

#### 悬赏问题

• ¥15 nrf52810-c三个a 程序
• ¥15 lego-loam跑出来的roll误差很大
• ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
• ¥15 STM32cubeMX里的FreeRTOS无法释放内存
• ¥15 CATIA有些零件打开直接单机确定终止
• ¥15 请问有会的吗，用MATLAB做
• ¥15 phython如何实现以下功能？查找同一用户名的消费金额合并—
• ¥15 孟德尔随机化怎样画共定位分析图
• ¥18 模拟电路问题解答有偿速度
• ¥15 CST仿真别人的模型结果仿真结果S参数完全不对