Learn Algorithms with
ARDEN
First level ADT (First-Class Data Type)
The ADT we implemented before we had this feature that it is only used to represent an object. We can use Stack like Int, we declare an integer int m; similar to a stack stack star; unfortunately, use C language, it does not support overload, we have no way to give us ADT give *, - , Such an operation, but you can use your function.
The first-class ADT is something: it can have multiple instances, we can use it to declare the variable, which is assigned to these variables.
Let's make a plural ADT: Think about the complex number of real and imaginary parts, we give two floating point numbers to indicate data, and do the following: Initialization, access real part, unauthorized, multiplication.
// file name: complex.h
Typedef struct complex
{
Float Re;
Float IM;
}
Complex INIT (FLOAT, FLOAT);
FLOAT RE (Complex);
Float Im (Complex);
Complex Mult (Complex, Complex);
Realism is not difficult:
// File Name: complex.c
#include
#include
Complex init (Float M_re, Float M_im)
{
Complex C;
C.RE = M_RE;
C.IM = m_im;
Return C;
}
FLOAT RE (Complex M_C)
{
Return M_C.RE;
}
FLOAT IM (Complex M_C)
{
Return m_c.im;
}
Complex (Complex FC, Complex SC)
{
Complex C;
C.RE = fc.re * sc.re-fc.im * sc.IM;
C.IM = fc.re * sc.re fc.im * sc.IM;
Return C;
}
Try to use it, Wri, EVERYTHING RUNS Good. Just, and better. Let us look back, think about what we have worked hard, and what is the purpose of a plurality of documents? Yes, abstract. How to abstract? Data package. Is it good? not yet.
Typedef struct complex
{
Float Re;
Float IM;
}
In Complex.h, the above sentence is completely exposed to the customer's eyes that reference Complex.h's customers. This way users can actually don't need anything INIT (FLOAT, FLOAT). Direct Complex C1; C1.RE = 2; C1.IM = 3; to achieve the goal, we must face reality and do it in front of it. I can tell you very responsibly, if there is a change in the process, these direct operations will make you a headache, and the consequences are very serious.
It's not too late, let us improve it, the most convenient way:
I don't see the structure definition in Complex.h! But we must also declare it, it seems to have some contradictions. Find an alternative:
// file name: complex.h
TypedEf struct complex * complex
Complex INIT (FLOAT, FLOAT);
FLOAT RE (Complex);
Float Im (Complex);
Complex Mult (Complex, Complex);
In implementation, let's define the structure:
// File Name: complex.c
#include
#include
Struct complex {float re; float im;
Comp init (Float M_re, Float M_im)
{
Comp C = Malloc (SIZEOF * C);
C-> RE = M_RE;
C-> IM = m_im;
Return C;
}
FLOAT RE (CoMP M_C)
{
RETURN M_C-> RE;
}
Float IM (comp m_c)
{
Return M_C-> IM;
}
Comp MULT (Comp FC, Comp SC)
{
RETURN INIT (RE (FC) * IM (SC), RE (Fc) * RE (SC) IM (Fc) * IM (SC));
}
Better one. Experienced friends have already despised this dementia version of Memory Leak plural. The truth is that I don't know how to give it free. Today has gone quite rich. The concept is also hoping to remind us how to design and post your own ADT.