【发布时间】:2016-05-21 09:24:07
【问题描述】:
我想创建一个从特定字符串返回双精度数组的函数。
我尝试了多种选择,但都没有成功
我有一个给定的函数 createWeightsArray,我需要填充它。 numofgrades 也会给出,这很有帮助
字符串将类似于:“30% 40% 50%”,我需要一个双数组 {0.3,0.4,0.5}
这是我最近的尝试:
double* createWeightsArray(char* str, int numOfGrades) {
double *gradesweight;
gradesweight = (double*)malloc(numOfGrades * sizeof(double));
int i = 0, n = 0;
while (*str != '\0') {
while (strchr("%", *str)) ++str;
if (*str == '\0') break;
*(gradesweight + n) = (atof(str) / 100);
n++;
str = strstr(str, "% ");
if (str == NULL) break;
*str = '\0';
++str;
}
return gradesweight;
我们将不胜感激
【问题讨论】:
-
给我们一个你想使用的字符串格式。它应该是您将传递给此函数的一种标准格式。我们是否应该考虑你总是传递“30% 40% 50%”这种字符串?
-
是的!字符串之王总是这样,其中的元素数量也将作为 numOfgrades 给出。 @Mazhar