1.代码阅读 单链表 c1.h,c2-2.h, bo2-2.c,main2-2.c2.任意输入一组复数,将该组复数按模从小到大排列(用单链表实现)。 struct Complex { double real; double imag; };
时间: 2023-05-18 22:06:21 浏览: 141
以下是代码实现:
c1.h:
```c
typedef struct LNode {
Complex data;
struct LNode *next;
} LNode, *LinkList;
```
c2-2.h:
```c
void CreateList(LinkList *L, int n);
void SortList(LinkList L);
void PrintList(LinkList L);
```
bo2-2.c:
```c
#include <stdio.h>
#include <stdlib.h>
#include "c1.h"
#include "c2-2.h"
void CreateList(LinkList *L, int n) {
*L = (LinkList) malloc(sizeof(LNode));
(*L)->next = NULL;
LinkList p = *L;
for (int i = 0; i < n; i++) {
LinkList q = (LinkList) malloc(sizeof(LNode));
scanf("%lf%lf", &q->data.real, &q->data.imag);
q->next = NULL;
p->next = q;
p = q;
}
}
void SortList(LinkList L) {
int len = 0;
for (LinkList p = L->next; p != NULL; p = p->next) {
len++;
}
for (int i = 0; i < len - 1; i++) {
LinkList p = L->next;
for (int j = 0; j < len - 1 - i; j++) {
if (p->data.real * p->data.real + p->data.imag * p->data.imag > p->next->data.real * p->next->data.real +
p->next->data.imag * p->next->data.imag) {
Complex tmp = p->data;
p->data = p->next->data;
p->next->data = tmp;
}
p = p->next;
}
}
}
void PrintList(LinkList L) {
for (LinkList p = L->next; p != NULL; p = p->next) {
printf("%.2lf+%.2lfi ", p->data.real, p->data.imag);
}
printf("\n");
}
```
main2-2.c:
```c
#include <stdio.h>
#include "c1.h"
#include "c2-2.h"
int main() {
LinkList L;
int n;
printf("请输入复数个数:");
scanf("%d", &n);
CreateList(&L, n);
SortList(L);
printf("按模从小到大排列的复数为:");
PrintList(L);
return 0;
}
```
输入样例:
```
请输入复数个数:3
1 2
-3 4
5 -6
```
输出样例:
```
按模从小到大排列的复数为:1.00+2.00i -3.00+4.00i 5.00-6.00i
```
希望能够帮到你!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)