目录
一、写链表,求复数和
#include<stdio.h>
struct Node //先定义一个结构体
{
int head;
int a;
int b;
int tail;
};
struct Node N[1000];
int x, y;
int main()
{
int num;
scanf_s("%d", &num);
for (int i = 0; i < num; i++)
{
N[i].head = i;
scanf_s("%d %d", &N[i].a, &N[i].b);
N[i].tail = i+1;
}
for (int j = 0; j < num; j++)
{
x += N[j].a;
y += N[j].b;
}
printf("%d", x);
if (y > 0)
{
printf("+%di", y);
}
else
printf("%di", y);
return 0;
}
二、求两个复数的加减乘除
1、要求:
编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:
要求:(1)定义一个结构体类型来描述复数。
(2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
(3)必须使用结构体指针的方法把函数的计算结果返回。
2、代码
#include<iostream>
using namespace std;
char str1;
float *x;
struct Node {//定义一个结构体
int head;
float a;
float b;
int tail;
};
struct Node N[100];
float *add( ) {
static float p[2];
p[0] = N[0].a + N[1].a;
p[1]= N[0].b + N[1].b;
return p;
}
float* sub() {
static float p[2];
p[0] = N[0].a - N[1].a;
p[1] = N[0].b - N[1].b;
return p;
}
float* mul() {
static float p[2];
p[0] = N[0].a * N[1].a- N[0].b * N[1].b;
p[1] = N[0].a *N[1].b+ N[0].b * N[1].a;
return p;
}
float* div() {
static float p[2];
p[0] = (N[0].a * N[1].a + N[0].b * N[1].b)/(N[1].a* N[1].a+ N[1].b * N[1].b);
p[1] = (N[0].b * N[1].a- N[0].a * N[1].b )/ (N[1].a * N[1].a + N[1].b * N[1].b);
return p;
}
int main()
{
cin >> str1>>N[0].a >> N[0].b >> N[1].a >> N[1].b;
switch (str1) {
case '+': x = add(); break;
case '-': x = sub(); break;
case '*': x = mul(); break;
case '/': x = div(); break;
default:break;
}
printf("%.2f", *x);
printf("+");
printf("%.2f", *(x+1));
printf("i");
return 0;
}