HydroCODE_1D 0.1
This is a implementation of fully explict forward Euler scheme for 1-D Euler equations of motion on Lagrangian/Eulerian coordinate
str_num_common.c
浏览该文件的文档.
1
6#include <math.h>
7#include <string.h>
8#include <stdio.h>
9
10
28int format_string(char * str)
29{
30 int i = 0, length = 0, j = 0;
31 int sign = 1;
32 int flag_dot = 0; // The number of dots in the string should be at most one.
33 int pos_dot = 0;
34 int flag_e = 0;
35 int pos_e = 0;
36
37 length = strlen(str);
38
39 for(j = 0; j < length; ++j)
40 {
41 if((str[j] == 69) || (str[j] == 101))
42 {
43 str[j] = 101;
44 flag_e += 1;
45 pos_e = j;
46 }
47 }
48
49 // There could not be more than one 'e' in one number.
50 if(flag_e > 1)
51 return 0;
52 if((flag_e) && (pos_e == 0))
53 return 0;
54 if((flag_e) && (pos_e == length-1))
55 return 0;
56 // A dot only could not be a number.
57 if((str[0] == 46) && (length == 1))
58 return 0;
59 // A '-' only could not be a number.
60 if(str[0] == 45)
61 {
62 if(length == 1)
63 return 0;
64 sign = -1;
65 }
66
67 // Eliminate '-' from the string and return -1.
68 if(sign < 0)
69 {
70 for(i = 0; i < length; ++i) // Eliminate '-'
71 str[i] = str[i+1];
72 length -= 1;
73 pos_e -= 1;
74 if(pos_e == 0)
75 return 0;
76 }
77
78 if(flag_e)
79 {
80 for(i = 0; i < length; ++i)
81 {
82 if(str[i] == 45)
83 {
84 // After eliminate '-', the only possible position for '-' is behind 'e'
85 if((i-pos_e) != 1)
86 return 0;
87 else if(i == length-1)
88 return 0;
89 }
90 // There could not be two dots in one number.
91 else if((str[i] == 46) && (flag_dot > 0))
92 return 0;
93 else if(str[i] == 46)
94 {
95 flag_dot += 1;
96 pos_dot = i;
97 }
98 }
99 if((flag_dot) && (pos_dot >= (pos_e-1)))
100 return 0;
101 }
102 else
103 {
104 for(i = 0; i < length; ++i)
105 {
106 if(str[i] == 45)
107 return 0;
108 else if((str[i] == 46) && (flag_dot > 0))
109 return 0;
110 else if(str[i] == 46)
111 flag_dot += 1;
112 }
113 }
114
115 return sign;
116}
117
126double str2num(char * number)
127{
128 double result = 0.0, super_script = 0.0;
129 int idx = 0, dot = -2;
130 int i = 0, j = 0;
131 int length = 0;
132 int pos_e = 0;
133 char * after_e = number;
134 int sign = 1;
135
136 length = strlen(number);
137
138 for(j = 0; j < length; ++j)
139 if(number[j] == 101)
140 pos_e = j;
141
142 if(pos_e)
143 {
144 after_e = number + pos_e + 1;
145 number[pos_e] = 0;
146 result = str2num(number);
147 if(after_e[0] == 45)
148 {
149 sign = -1;
150 after_e += 1;
151 }
152 super_script = str2num(after_e);
153 result = result * pow(10.0, sign * super_script);
154 }
155 else
156 {
157 while(number[idx] != 0)
158 {
159 if(number[idx] == 46)
160 {
161 dot = idx - 1;
162 idx = 0;
163 break;
164 }
165 ++idx;
166 }
167
168 if(dot == -2)
169 dot = idx - 1;
170
171 for (i = 0; i <= dot; ++i)
172 result += (double)(number[i] - 48)*pow(10, dot - i);
173
174 dot += 1;
175 for (i = 1; i < length - dot; ++i)
176 result += (double)(number[dot+i] - 48)*pow(0.1, i);
177 }
178
179 return result;
180}
int format_string(char *str)
This function examine whether a string represents a real number.
double str2num(char *number)
This function transform a string consisting '1', '2', ..., and '.' into the real number that it repre...