# Data structure title details C

## The first chapter is introduction

### 7-1 operation and maximum value of complex number (10 points)

This topic requires reading in four integers a, B, C and D, constructing two complex numbers according to these four integers: a+bi and c+di, and then outputting them, and outputting their sum, difference, product and quotient, as well as the largest of the above results (real and imaginary parts are operated within the range of integers). The comparison convention of the size of two complex numbers is: first compare the real part (the real part is larger or smaller, that is, the whole complex number is considered to be larger or smaller). When the real part is the same, compare the size of the imaginary part. If the imaginary part is also the same, it is considered that the two complex numbers are equal. It is also necessary to pay attention to the output habits of complex numbers: for example, 0+0i – > {0}, 0-8i – > {- 8i}, - 3 + 0I – > {- 3}, 3 ± 4I – > {3-4i}
Input format:

Enter four integers separated by spaces. Ensure that the second complex number entered is non-zero.

Output format:

Output seven complex numbers in seven lines, namely: the two complex numbers, their sum, difference, product, quotient and the maximum value of the four operations.

Input example:

3 -4 -3 4
No blank lines at the end
Output example:

The corresponding output is given here. For example:

{3-4i}
{-3+4i}
{0}
{6-8i}
{7+24i}
{-1}
{7+24i}
No blank lines at the end

```#include <stdio.h>

struct Complex{
int shibu;
int xubu;
}number1,number2,number3,number4,number5,number6,MaxNumber;

void show(struct Complex number){
if((number.xubu>0)&&(number.shibu!=0)){
printf("{%d+%di}\n",number.shibu,number.xubu);
}
if((number.xubu<0)&&(number.shibu!=0)){
printf("{%d%di}\n",number.shibu,number.xubu);
}
if((number.xubu==0)&&(number.shibu!=0)){
printf("{%d}\n",number.shibu);
}

if((number.xubu!=0)&&(number.shibu==0)){
printf("{%di}\n",number.xubu);
}
if((number.xubu==0)&&(number.shibu==0)){
printf("{0}\n");
}
}
void jia(struct Complex number1,struct Complex number2){
number3.shibu=number1.shibu+number2.shibu;
number3.xubu=number1.xubu+number2.xubu;
show(number3);
}

void jian(struct Complex number1,struct Complex number2){
number4.shibu=number1.shibu-number2.shibu;
number4.xubu=number1.xubu-number2.xubu;
show(number4);
}
void cheng(struct Complex number1,struct Complex number2){
number5.shibu=number1.shibu*number2.shibu-number1.xubu*number2.xubu;
number5.xubu=number1.xubu*number2.shibu+number2.xubu*number1.shibu;
show(number5);
}
void chu(struct Complex number1,struct Complex number2){
number6.shibu=(number1.shibu*number2.shibu+number1.xubu*number2.xubu)/(number2.shibu*number2.shibu+number2.xubu*number2.xubu);
number6.xubu=(number1.xubu*number2.shibu-number1.shibu*number2.xubu)/(number2.shibu* number2.shibu+number2.xubu*number2.xubu);
show(number6);
}

void bijiao(struct Complex number){
if(number.shibu>MaxNumber.shibu)
{
MaxNumber.shibu=number.shibu;
MaxNumber.xubu=number.xubu;
}
if((number.shibu==MaxNumber.shibu)&&(number.xubu>MaxNumber.xubu))
{
MaxNumber.shibu=number.shibu;
MaxNumber.xubu=number.xubu;
}

}

int main() {
scanf("%d %d %d %d",&number1.shibu,&number1.xubu,&number2.shibu,&number2.xubu);

MaxNumber.shibu=0;
MaxNumber.xubu=0;
show(number1);
show(number2);

jia(number1,number2);
jian(number1,number2);
cheng(number1,number2);
chu(number1,number2);

bijiao(number3);
bijiao(number4);
bijiao(number5);
bijiao(number6);
show(MaxNumber);

return 0;
}

```

For the output of real and imaginary parts, it would be better to consider the imaginary part first and then the real part. I'm too lazy to change it.

### 7-2 input legitimacy judgment + prime judgment (10 points)

Implement isPrime() function with integer parameters and exception handling. Returns True if the integer is a prime, False otherwise.
Input format:

Enter a number

Output format:

If the integer is prime, the output is "prime", otherwise the output is "not prime"; If the input does not meet the requirements, output "input error".

Input example:

A set of inputs is given here. For example:

11
No blank lines at the end
Output example:

The corresponding output is given here. For example:

It's a prime
No blank lines at the end

```#include <stdio.h>
#include <math.h>
int IsPrime(int m){
int i, k;
if (m <= 1)return 0;
for (i=2;i<(int)sqrt(m); i++){
k = m%i;
if (k==0)
return 0;

}
return 1;
}
int shuru(float m){
if(m - (int)m == 0)return 1;
else return 0;
}

int main(){
int flag;
float n;
scanf("%f", &n);
flag = IsPrime(n);
int am=shuru(n);
if(am){
if (flag)printf("It's a prime\n");
else printf("Not prime\n");
}else printf("Input error\n");
}

```

## Chapter II linear table

### 7-1 establishment and traversal of sequence table (30 points)

Read in n values and N integers, establish a sequence table and traverse the output.
Input format:

Read in n and n integers

Output format:

Output n integers separated by spaces (there is no space after the last number).

Input example:

A set of inputs is given here. For example:

4
-3 10 20 78
No blank lines at the end
Output example:

The corresponding output is given here. For example:

-3 10 20 78
No blank lines at the end

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

int main(){
int n;
scanf("%d",&n);

int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}

for(int i=0;i<n-1;i++){
printf("%d ",a[i]);
}

if(n!=0){
printf("%d",a[n-1]);
}
}

```

### 7-2 finding the intersection of sets (20 points)

Find the intersection of integer set A and integer set B.
Input format:

The input has three lines:

The first line is the number of elements m and N of A and B (m, n < = 100);

The second row is the m elements of set A;

The third line is the n elements of set A.

Output format:

Output all elements of the intersection (in the order in which they appear in the A set, with no spaces after the last output). If the intersection is empty, "NULL" is output.

Input example:

A set of inputs is given here. For example:

3 4
10 9 2
9 10 8 0
No blank lines at the end
Output example:

The corresponding output is given here. For example:

10 9
No blank lines at the end

```#include <stdio.h>

int main(){
int n;
scanf("%d",&n);

int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}

for(int i=0;i<n-1;i++){
printf("%d ",a[i]);
}

if(n!=0){
printf("%d",a[n-1]);
}
}

```

Tags: C data structure

Posted on Tue, 05 Oct 2021 17:04:25 -0400 by yhchan