用函数和复数数据类型 设计2个复数的加、乘和取模运算程序 stdio.h
时间: 2024-02-29 09:52:11 浏览: 185
设计程序以实现任意两个高次多项式的加法和乘法运算.docx
0; // 已占用床位数
Student *p = student_list;
while (p != NULL) {
total_student++;
if (p->dorm[0] != '\0') {
occupied_bed++;
}
p = p->next以下是一个使用结构体和函数实现复数的加、乘和取模运算的程序,其中用到了;
}
Dorm *q = dorm_list;
while (q != NULL) {
total_dorm++;
total_bed += q `stdio.h` 头文件:
```c
#include <stdio.h>
#include <math.h>
// 定义复数结构体
->total;
occupied_bed += q->total - q->empty;
q = q->next;
}
printf("总学typedef struct {
double real;
double imag;
} complex;
// 复数加法
complex add(complex c1, complex c生数:%d\n", total_student);
printf("总宿舍数:%d\n", total_dorm);
printf("总床2){
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位数:%d\n", total_bed);
printf("已占用床位数:%d\n", occupied_bed);
printf("入.real = c1.real * c2.real - c1.imag * c2.imag;
result.imag = c1.real * c2.imag住率:%.2f%%\n", (float)occupied_bed / total_bed * 100);
printf("退宿率: + c1.imag * c2.real;
return result;
}
// 复数模长
double mod(complex c){
return sqrt%.2f%%\n", (float)(total_student - occupied_bed) / total_student * 100);
}
int main() {
(c.real * c.real + c.imag * c.imag);
}
int main() {
complex c1 = {2.0, 3 int choice;
do {
show_menu();
scanf("%d", &choice);
switch (choice) {
case 1.0};
complex c2 = {4.0, 5.0};
complex c3, c4;
double mod:
add_student();
break;
case 2:
add_dorm();
break;
case 3:
check_c1, mod_c2;
// 复数加法
c3 = add(c1, c2);
printf("c_in();
break;
case 4:
check_out();
break;
case 5:
report();
break;
1 + c2 = %.1f + %.1fi\n", c3.real, c3.imag);
// 复数乘法
c4 = multiply(c1, c2);
printf("c1 * c2 = %.1f + %.1fi\n", case 0:
printf("退出系统!\n");
break;
default:
printf("无效操作!\ c4.real, c4.imag);
// 复数模长
mod_c1 = mod(c1);
mod_c2 =n");
break;
}
} while (choice != 0);
return 0;
}
```
需要注意的是, mod(c2);
printf("|c1| = %.1f\n", mod_c1);
printf("|c2| = %.1这只是一个简单的实现,仅供参考,实际使用中需要根据需求进行相应的修改和完善。
阅读全文