gpt4 book ai didi

c - 尝试将字符串插入数组以检查字符串是否描述函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:32 24 4
gpt4 key购买 nike

我有一个描述函数的字符串,我需要检查这是否是函数我想到了放字符串数组然后检查

 the string: "1 11.1 2 3.2 3 44.1 4 5.4 5 7.4 6 1111.0"

字符串描述函数 (1,11.1),(2,3.2) (3,44.1) 等...

然后我尝试将它插入到像 arr[0][0]=1 和 arr[1][0]=11.1,arr[0][1]=2,arr[1][1]= 这样的数组中3.2...我需要帮助如何去做或获得关于如何检查字符串是否有效的另一个想法的建议?我希望我能更好地解释自己..谢谢

以字符串为例:

 "1 11.1 1 3.2 3 44.1 4 5.4 5 7.4 6 1111.0"

不是描述函数,因为我们有 (1,11.1) 和 (1,3.2)问题是如何将字符串插入数组

为清楚起见编辑:

他在问以下问题:给定一串(显然)均匀的 float ,让我们用以下形式表示它:

"x1 y1 x2 y2 x3 y3 ... xn yn"

如果对于每个 xixji 不同于 j 则字符串“定义一个函数” ,如果 xixj 的值相同,则 yiyj 的值相同.

他想知道如何检查一个字符串是否“定义了一个函数”。

最佳答案

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
{//split string by strtok, destroy input by strtok
char input[] = "1 11.1 2 3.2 3 44.1 4 5.4 5 7.4 6 1111.0";
char *array[10][2];
char *item;
char **to = &array[0][0];
int i, count=0;
for(item = strtok(input, " "); item != NULL; item = strtok(NULL, " ")){
*to++ = item;
++count;
}
for(i = 0;i<count / 2;++i){
printf("%s, %s\n", array[i][0], array[i][1]);
}
}
printf("\n");
{//convert double by strtod
char *input = "1 11.1 1 3.2 3 44.1 4 5.4 5 7.4 6 1111.0";
double array[10][2];
double item;
double *to = &array[0][0];
char *p;
int i, count=0, pairSize;
for(p = input; *p != '\0';){
item = strtod(p, &p);
if(*p == ' ' || *p == '\0'){
*to++ = item;
++count;
} else {
printf("invalid number\n");
break;
}
}
pairSize = count / 2;
for(i = 0;i<pairSize;++i){
printf("%g, %g\n", array[i][0], array[i][1]);
}
//Check the monotonic increase
for(i = 0;i+1<pairSize;++i){
if(array[i][0] >= array[i+1][0]){
printf("invalid sequence at array[%d][0] = %g and array[%d][0] = %g\n",
i, array[i][0], i+1, array[i+1][0]);
}
}
}

return 0;
}

关于c - 尝试将字符串插入数组以检查字符串是否描述函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20621859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com