ScalarVectors.c 4.99 KB
Newer Older
iker_martin's avatar
iker_martin committed
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ScalarVectors.h"

int CreateInts (int **vint, int num)
	{
		if ((*vint = (int *) malloc (sizeof(int)*num)) == NULL)
			{ printf ("Memory Error (CreateInts(%d))\n", num); exit (1); }
	}

int InitInts (int *vint, int n, int frst, int incr) 
	{
		int i, *p1 = vint, num = frst;

		for (i=0; i<n; i++) 
			{ *(p1++) = num; num += incr; }
	}
		
int CopyInts (int *src, int *dst, int n)
	{ memmove (dst, src, sizeof(int) * n); }

int CopyShiftInts (int *src, int *dst, int n, int shft)
	{
		int i, *p1 = src, *p2 = dst;

		for (i=0; i<n; i++)
			*(p2++) = *(p1++) + shft;
	}
	
int TransformLengthtoHeader (int *vec, int n)
	{
		int i, *pi = vec; 
	        for (i=0; i<n; i++) { *(pi+1) += *pi; pi++; }
	}

int TransformHeadertoLength (int *vec, int n)
	{
		int i, *pi = &(vec[n]); 
	        for (i=n; i>=1; i--) { *pi -= *(pi-1); pi--; }
	}

void GetIntFromString (char *string, int *pnum, int numC, int shft)
	{
		int j = 0, num = 0, neg = 0;
		char *pchar = string;

		while ((j < numC) && ((*pchar < '0') || (*pchar > '9')) &&
           (*pchar != '+') && (*pchar != '-')) { j++; pchar++; }
		if (j < numC)
			{
				if ((*pchar == '+') || (*pchar == '-'))
					{ neg = (*pchar == '-'); j++; pchar++; }
				while ((j < numC) && (*pchar >= '0') && (*pchar <= '9'))
					{ num = num * 10 + (*pchar - 48); j++; pchar++; }
			}
		if (neg) num = -num;
		*pnum = num + shft; 
	}

void GetIntsFromString (char *string, int *vec, int numN, int numC, int shft)
	{
		int i, *pint = vec;
		char *pchar = string;

		for (i=0; i<numN; i++)
			{ GetIntFromString (pchar, (pint++), numC, shft); pchar += numC; }
	}

void GetFormatsFromString (char *string, int *vec, int numN, int numC)
	{
		int i, k = 0;
		int *pint = vec;
		char *pchar = string, *pch = NULL, c = ' ', c2;

		for (i=0; i<numN; i++)
			{ 
				pch = pchar; 
				while (*pch == ' ') pch++;
				sscanf (pch, "(%i%c", pint, &c);
				if ((c == 'P') || (c == 'p')) 
					{ 
						sscanf (pch, "(%i%c%i%c%i.%i)", &k, &c2, pint, &c, pint+1, pint+2); 
						pint += 3; 
					}
				else if ((c == 'E') || (c == 'e') || (c == 'D') || (c == 'd')	||
								 (c == 'F') || (c == 'f') || (c == 'G') || (c == 'g'))
					{ 
						sscanf (pch, "(%i%c%i.%i)", pint, &c, pint+1, pint+2); 
						pint += 3; 
					}
				else 
					{ sscanf (pch, "(%i%c%i)", pint, &c, pint+1); pint += 2; }
				pchar += numC;
			}
	}

int PrintInts (int *vint, int num)
	{
		int i, *pi = vint;

		for (i=0; i<num; i++) printf ("%d ", *(pi++));
		printf ("\n");
	}

int RemoveInts (int **vint)
	{ free (*vint); *vint = NULL; }

int CreateDoubles (double **vdouble, int num)
	{
		if ((*vdouble = (double *) malloc (sizeof(double)*num)) == NULL)
			{ printf ("Memory Error (CreateDoubles(%d))\n", num); exit (1); }
	}

int InitDoubles (double *vdouble, int n, double frst, double incr) 
	{
		int i; 
		double *p1 = vdouble, num = frst;

		for (i=0; i<n; i++) 
			{ *(p1++) = num; num += incr; }
	}
		
void GetDoubleFromString (char *string, double *pdbl, int numC)
	{
		int j, k, exp, neg;
		double num, frac;
		char *pchar = string;

		j = 0; exp = 0; neg = 0; num = 0.0; frac = 1.0;
		while ((j < numC) && ((*pchar < '0') || (*pchar > '9')) && 
					 (*pchar != '+') && (*pchar != '-') && (*pchar != '.')) { j++; pchar++; }
		if (j < numC)
			{
				if ((*pchar == '+') || (*pchar == '-'))
					{ neg = (*pchar == '-'); j++; pchar++; }
				if (j < numC)
					{
						if (*pchar != '.')
							while ((j < numC) && (*pchar >= '0') && (*pchar <= '9'))
								{ num = num * 10 + (*pchar - 48); j++; pchar++; }
						if (j < numC)
							{
								if (*pchar == '.')
									{
										j++; pchar++; 
										while ((j < numC) && (*pchar >= '0') && (*pchar <= '9'))
											{ frac /= 10; num += (*pchar-48) * frac; j++; pchar++; }
									}
								if (neg) num = -num;
								if (j < numC)
									{
										if ((*pchar == 'e') || (*pchar == 'E') || (*pchar == 'd') || (*pchar == 'D'))
											{
												neg = 0; j++; pchar++; 
												if (j < numC)
													{
														if ((*pchar == '+') || (*pchar == '-'))
															{ neg = (*pchar == '-'); j++; pchar++; }
														if (j < numC)
															{
																while ((j < numC) && (*pchar >= '0') && 
																		 (*pchar <= '9'))
																	{ exp = exp*10 + (*pchar-48); j++; pchar++; }
																if (neg) exp = -exp;
																for (k=0; k<exp; k++) num *= 10;
																for (k=0; k>exp; k--) num /= 10;
															}
													}
											}
									}
							}
						else
 							if (neg) num = -num;
					}
			}
		*pdbl = num; 
	}

void GetDoublesFromString (char *string, double *vec, int numN, int numC)
	{
		int i;
		double *paux = vec;
		char *pchar = string;

		for (i=0; i<numN; i++)
			{ GetDoubleFromString (pchar, (paux++), numC); pchar += numC; }
	}

int PrintDoubles (double *vdouble, int num)
	{
		int i;
		double *pd = vdouble;

		for (i=0; i<num; i++) printf ("%e ", *(pd++));
		printf ("\n");
	}

int RemoveDoubles (double **vdouble)
	{ free (*vdouble); *vdouble = NULL; }