-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
108 lines (91 loc) · 2.55 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include <stdlib.h>
#include <stdio.h>
typedef struct complex_number{
int real;
int im;
}complex_number;
typedef struct vector{
int number_of_elements;
int capacity;
complex_number* number;
}vector;
typedef struct vector_int{
int number_of_elements;
int capacity;
int* elem;
}vector_int;
void vector_create(vector *v,int size);
int is_vector_empty(vector *v);
int size(vector *v);
complex_number load(vector *v,int number);
void resize(vector *v);
void vector_push_back(vector *v, complex_number c);
void vector_print(vector *v);
void vector_create_int(vector_int *v,int size);
void vector_push_back_int(vector_int *v, int c);
void read_from_file(vector_int *string, vector_int *colomn, vector *value, int *num_strings, int *num_colomns);
void resize_int(vector_int *v);
void vector_print_int(vector_int *v);
//vector.c
void vector_create(vector *v,int size){
v->capacity = size;
v->number_of_elements = 0;
v->number = malloc(sizeof(complex_number) * v->capacity);
}
int is_vector_empty(vector *v){
return (v->number_of_elements == 0) ? 1 : 0;
}
int size(vector *v){
return v->capacity;
}
complex_number load(vector *v,int number){
if((number >= 0) && (number <= v->capacity - 1)){
return v->number[number];
}
}
void resize(vector *v){
v->capacity++;
v->number = realloc(v->number, sizeof(complex_number) * v->capacity);
}
void vector_push_back(vector *v, complex_number c) {
if(v->number_of_elements == v->capacity) {
resize(v);
}
v->number[v->number_of_elements] = c;
v->number_of_elements++;
}
void vector_print(vector *v){
for(int i = 0; i < v->number_of_elements; i++){
if(i != v->number_of_elements - 1){
printf("[%d + i * %d] -> ", v->number[i].real, v->number[i].im);
} else {
printf("[%d + i * %d]\n", v->number[i].real, v->number[i].im);
}
}
}
void vector_create_int(vector_int *v,int size){
v->capacity = size;
v->number_of_elements = 0;
v->elem = malloc(sizeof(int) * v->capacity);
}
void vector_push_back_int(vector_int *v, int c) {
if(v->number_of_elements == v->capacity) {
resize_int(v);
}
v->elem[v->number_of_elements] = c;
v->number_of_elements++;
}
void resize_int(vector_int *v){
v->capacity++;
v->elem = realloc(v->elem, sizeof(int) * v->capacity);
}
void vector_print_int(vector_int *v){
for(int i = 0; i < v->number_of_elements ; i++){
printf("%d ", v->elem[i]);
}
printf("\n");
}
#endif