尝试将字符串数组检查,如果字符串描述功能 [英] Try to insert string to array to check if the string describe function

查看:82
本文介绍了尝试将字符串数组检查,如果字符串描述功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我描述函数的字符串,我需要检查,如果这是功能
这种想法来找我把字符串数组然后检查

 的字符串: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,编曲[0] [1] = 2,编曲[1] [1] = 3.2 ...
我需要帮助怎么办呢还是获得建议到另一个想法如何检查如果字符串函数?我希望我解释自己better..thanks

在示例字符串:

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

没有描述的功能,因为我们有(1,11.1)和(1,3.2)
问题是如何插入串入阵

编辑净度:

他是问以下问题:鉴于(显然)均匀地许多彩车的字符串,让我们重新present它的形式为:

 X1 Y1 X2 Y2 X3 Y3 ...... XN Y​​N

然后字符串定义一个函数如果每个 XJ Ĵ,如果喜的值不同XJ 是在相同,则值毅 YJ 是相同的。

他想知道如何检查一个字符串是否定义一个函数。<​​/ P>

解决方案

 的#include&LT;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;诠释主(){
    {//通过的strtok分割字符串,摧毁strtok的输入
        字符输入[] =1 2 11.1 3.2 44.1 3 5.4 4 7.4 5 6 1111.0
        的char *数组[10] [2];
        字符*项目;
        焦炭**为=&安培;阵列[0] [0];
        INT I,计数= 0;
        对于(项目= strtok的(输入,);项目= NULL;!=项目的strtok(NULL,)){
            *为++ =项目;
            ++计数;
        }
        对于(i = 0; I&LT;计数/ 2; ++ I){
            的printf(%S%S \\ N阵列[我] [0],数组[我] [1]);
        }
    }
    的printf(\\ n);
    {//通过转换双重的strtod
        字符*输入=1 1 11.1 3.2 44.1 3 5.4 4 7.4 5 6 1111.0
        双阵列[10] [2];
        双项;
        双*为=&安培;阵列[0] [0];
        字符* P;
        INT I,计数= 0,pairSize;
        为(P =输入;!* P ='\\ 0'){
            项=的strtod(对,&放大器; P);
            如果(* P ==''|| * P =='\\ 0'){
                *为++ =项目;
                ++计数;
            }其他{
                的printf(无效号码\\ n);
                打破;
            }
        }
        pairSize =计数/ 2;
        对于(i = 0; I&LT; pairSize ++我){
            的printf(%G,%G \\ N阵列[我] [0],数组[我] [1]);
        }
        //检查单调增加
        对于(I = 0; i + 1的&下; pairSize ++ⅰ){
            如果(阵列[我] [0]&GT; =阵列[I + 1] [0]){
                的printf(无效的序列在阵列内容[%d] [0] =%G和数组内容[%d] [0] =%G \\ N,
                    我,阵列[我] [0],I + 1,数组[I + 1] [0]);
            }
        }
    }    返回0;
}

I have a string describing the function and I need to check if this is function This idea came to me to put the string array then check

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

the string describe the function (1,11.1),(2,3.2) (3,44.1) etc...

and I try to insert it to array like arr[0][0]=1 and arr[1][0]=11.1,arr[0][1]=2,arr[1][1]=3.2... And i need help how to do it or get advice to another idea how to check If the string is function?I hope I explained myself better..thanks

in to example the string:

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

isn't describe function because we have (1,11.1) and (1,3.2) the question is how to insert the string into array

EDIT for clarity:

He is asking the following question: Given a string of (apparently) evenly many floats, let's represent it in the form:

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

Then the string "defines a function" if for every xi and xj with i different from j, if the values at xi and xj are the same, then the values at yi and yj are the same.

He wants to know how to check whether a string "defines a function."

解决方案

#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;
}

这篇关于尝试将字符串数组检查,如果字符串描述功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆