C 我需要帮助从这个数组中提取每个团队的分数,但我仍然没有';我不知道怎么解决它

C 我需要帮助从这个数组中提取每个团队的分数,但我仍然没有';我不知道怎么解决它,c,C,我们不能使用stoi()或令牌函数,而只能使用指针,而循环和for循环提取数据 这段代码是我尝试过的,但不起作用 输出:0:0 15:12 2:13 1:5 55:7:2 17:2 2:17:18无符号点=0; if x>y - 3 points if x<y - 0 point if x = y - 1 points 对于(int i=0;i0) 积分+=3分; } 无符号点=0; 对于(int i=0;i0) 积分+=3分; } char*matches

我们不能使用
stoi()
或令牌函数,而只能使用指针
,而
循环和
for
循环提取数据

这段代码是我尝试过的,但不起作用

输出:0:0 15:12 2:13 1:5 55:7:2 17:2 2:17:18

无符号点=0;
    if x>y - 3 points
    if x<y - 0 point
    if x = y - 1 points
对于(int i=0;i<10;i++){ INTR; char*itr; for(itr=match[i];itr!=':';itr++); r=strncmp(匹配[i],itr+1,itr-匹配[i]); if(!r) 积分+=1; 否则如果(r>0) 积分+=3分; }
无符号点=0;
对于(int i=0;i<10;i++){
INTR;
char*itr;
for(itr=match[i];itr!=':';itr++);
r=strncmp(匹配[i],itr+1,itr-匹配[i]);
if(!r)
积分+=1;
否则如果(r>0)
积分+=3分;
}

char*matches[10]是一个char指针数组或字符串数组。我们可以用matches[index]或*(matches+index)指向数组中的任何一个字符串,该字符串将衰减为一个字符指针(字符串)。然后,我们可以使用[]或指针算法来解引用,在其中找到一个特定的字符

比如说,

unsigned points = 0;

for(int i = 0; i < 10; i++){
    int r;
    char *itr;
    for (itr = match[i]; itr != ':'; itr++);
    r = strncmp(match[i], itr + 1, itr - match[i]);
    if (!r)
        points += 1;
    else if (r > 0)
        points += 3;
}

char*matches[10]是一个char指针数组或字符串数组。我们可以用matches[index]或*(matches+index)指向数组中的任何一个字符串,该字符串将衰减为一个字符指针(字符串)。然后,我们可以使用[]或指针算法来解引用,在其中找到一个特定的字符

比如说,

unsigned points = 0;

for(int i = 0; i < 10; i++){
    int r;
    char *itr;
    for (itr = match[i]; itr != ':'; itr++);
    r = strncmp(match[i], itr + 1, itr - match[i]);
    if (!r)
        points += 1;
    else if (r > 0)
        points += 3;
}

这是整个解决方案。通过在每个结果中查找
字符,可以获得y队得分的指针。(请参见如何获取
stru_y
指针)。 我认为最棘手的部分是在不使用stoi之类的函数的情况下获得每个团队的分数。我确实写了我的解决方案,但我建议你先自己写(特别是如果这是家庭作业)

#包括
内部主(空)
{
int point_x=0;
int点_y=0;
常量字符*匹配[10]={“2:3”、“0:0”、“15:12”、“2:13”、“1:5”、“55:7”、“7:2”、“17:2”、“2:17”、“17:18”};
对于(int i=0;i<10;i++)
{
//此指针指向每个结果的第一个字符
常量字符*匹配=匹配[i];
//获取此结果的“:”指针
常量字符*分隔符=匹配;
而(*separator!=':'&&&&*separator!='\0')separator++;
//当找不到“:”时,您需要处理错误,但这里假设所有结果都包含“:”。
//为了便于理解,准备两个指针,为结果中的每个分数打分。
常量字符*str_x=匹配;
const char*str_y=separator+1;//分隔符的下一个字符
//将每个字符串转换为数字。因为我们不能使用stoi,所以标记化函数
//我们必须自己做。
int得分_x=0;
while(*str_x!=':'){//
分数=10+(*str_x-'0');
strux++;
}
积分y=0;
while(*str_y!='\0'){//直到NULL
分数y=分数y*10+(*str_y-'0');
stru_y++;
}
//现在比较它们并给出分数
如果(分数x==分数y){
点_x++;
点_y++;
}否则如果(分数x>分数y){
点_x+=3;
}否则{
点y+=3;
}
}
printf(“点x=%d点y=%d\n”,点x,点y);
返回0;
}

以下是完整的解决方案。通过在每个结果中查找
字符,可以获得y队得分的指针。(请参见如何获取
stru_y
指针)。 我认为最棘手的部分是在不使用stoi之类的函数的情况下获得每个团队的分数。我确实写了我的解决方案,但我建议你先自己写(特别是如果这是家庭作业)

#包括
内部主(空)
{
int point_x=0;
int点_y=0;
常量字符*匹配[10]={“2:3”、“0:0”、“15:12”、“2:13”、“1:5”、“55:7”、“7:2”、“17:2”、“2:17”、“17:18”};
对于(int i=0;i<10;i++)
{
//此指针指向每个结果的第一个字符
常量字符*匹配=匹配[i];
//获取此结果的“:”指针
常量字符*分隔符=匹配;
而(*separator!=':'&&&&*separator!='\0')separator++;
//当找不到“:”时,您需要处理错误,但这里假设所有结果都包含“:”。
//为了便于理解,准备两个指针,为结果中的每个分数打分。
常量字符*str_x=匹配;
const char*str_y=separator+1;//分隔符的下一个字符
//将每个字符串转换为数字。因为我们不能使用stoi,所以标记化函数
//我们必须自己做。
int得分_x=0;
while(*str_x!=':'){//
分数=10+(*str_x-'0');
strux++;
}
积分y=0;
while(*str_y!='\0'){//直到NULL
分数y=分数y*10+(*str_y-'0');
stru_y++;
}
//现在比较它们并给出分数
如果(分数x==分数y){
点_x++;
点_y++;
}否则如果(分数x>分数y){
点_x+=3;
}否则{
点y+=3;
}
}
printf(“点x=%d点y=%d\n”,点x,点y);
返回0;
}

如果我们消除了指针对指针的所有不必要和奇怪的复杂性,我们将得到以下快速可读的代码:

#include <stdio.h>

int main(void)
{
    int point_x = 0;
    int point_y = 0;
    const char *matches[10] = { "2:3", "0:0", "15:12", "2:13", "1:5", "55:7", "7:2", "17:2", "2:17", "17:18" };

    for (int i = 0; i < 10; i++)
    {
        // This pointer points the first character of each result
        const char* match = matches[i];

        // Get the pointer of ":" of this result
        const char* separator = match;
        while( *separator != ':' && *separator != '\0' ) separator++;
        // You would need to handle error when ':' is not found but here just assumes all the results contain ":".

        // To make it easy to understand prepare two pointers to point each score in a result.
        const char* str_x = match;
        const char* str_y = separator+1; // next character of the separator

        // Convert each string to number. Because we cannot use stoi, tokenize functions
        // we have to do it by ourselves.
        int score_x = 0;
        while( *str_x != ':'){ // until separator
            score_x = score_x*10 + (*str_x - '0');
            str_x++;
        }
        int score_y = 0;
        while( *str_y != '\0'){ // until NULL
            score_y = score_y*10 + (*str_y - '0');
            str_y++;
        }

        // Now compare them and give points
        if( score_x == score_y ){
            point_x++;
            point_y++;
        }else if( score_x > score_y ){
            point_x+=3;
        }else{
            point_y+=3;
        }
    }

    printf("point_x=%d   point_y=%d\n", point_x, point_y);

    return 0;
}

从那以后,如果您愿意的话,您可以将每个项目转换为整数,而不是
printf
调用。这是另一项任务,您可能需要使用
strtoul

如果我们消除指针指向指针的所有不必要和奇怪的复杂性,我们将得到以下快速可读的代码:

#include <stdio.h>

int main(void)
{
    int point_x = 0;
    int point_y = 0;
    const char *matches[10] = { "2:3", "0:0", "15:12", "2:13", "1:5", "55:7", "7:2", "17:2", "2:17", "17:18" };

    for (int i = 0; i < 10; i++)
    {
        // This pointer points the first character of each result
        const char* match = matches[i];

        // Get the pointer of ":" of this result
        const char* separator = match;
        while( *separator != ':' && *separator != '\0' ) separator++;
        // You would need to handle error when ':' is not found but here just assumes all the results contain ":".

        // To make it easy to understand prepare two pointers to point each score in a result.
        const char* str_x = match;
        const char* str_y = separator+1; // next character of the separator

        // Convert each string to number. Because we cannot use stoi, tokenize functions
        // we have to do it by ourselves.
        int score_x = 0;
        while( *str_x != ':'){ // until separator
            score_x = score_x*10 + (*str_x - '0');
            str_x++;
        }
        int score_y = 0;
        while( *str_y != '\0'){ // until NULL
            score_y = score_y*10 + (*str_y - '0');
            str_y++;
        }

        // Now compare them and give points
        if( score_x == score_y ){
            point_x++;
            point_y++;
        }else if( score_x > score_y ){
            point_x+=3;
        }else{
            point_y+=3;
        }
    }

    printf("point_x=%d   point_y=%d\n", point_x, point_y);

    return 0;
}
从那个以后,若你们愿意的话,你们可以将每个项目转换成整数,而不是<代码
2:3                                                                                                                                                                                  
x: 2                                                                                                                                                                                 
y: 3                                                                                                                                                                                 
0:0                                                                                                                                                                                  
x: 0                                                                                                                                                                                 
y: 0                                                                                                                                                                                 
15:12                                                                                                                                                                                
x: 15                                                                                                                                                                                
y: 12                                                                                                                                                                                
2:13                                                                                                                                                                                 
x: 2                                                                                                                                                                                 
y: 13                                                                                                                                                                                
1:5                                                                                                                                                                                  
x: 1                                                                                                                                                                                 
y: 5                                                                                                                                                                                 
55:7                                                                                                                                                                                 
x: 55                                                                                                                                                                                
y: 7                                                                                                                                                                                 
7:2                                                                                                                                                                                  
x: 7                                                                                                                                                                                 
y: 2                                                                                                                                                                                 
17:2                                                                                                                                                                                 
x: 17                                                                                                                                                                                
y: 2                                                                                                                                                                                 
2:17                                                                                                                                                                                 
x: 2                                                                                                                                                                                 
y: 17                                                                                                                                                                                
17:18                                                                                                                                                                                
x: 17                                                                                                                                                                                
y: 18     
#include <stdio.h>

int main(void)
{
    int point_x = 0;
    int point_y = 0;
    const char *matches[10] = { "2:3", "0:0", "15:12", "2:13", "1:5", "55:7", "7:2", "17:2", "2:17", "17:18" };

    for (int i = 0; i < 10; i++)
    {
        // This pointer points the first character of each result
        const char* match = matches[i];

        // Get the pointer of ":" of this result
        const char* separator = match;
        while( *separator != ':' && *separator != '\0' ) separator++;
        // You would need to handle error when ':' is not found but here just assumes all the results contain ":".

        // To make it easy to understand prepare two pointers to point each score in a result.
        const char* str_x = match;
        const char* str_y = separator+1; // next character of the separator

        // Convert each string to number. Because we cannot use stoi, tokenize functions
        // we have to do it by ourselves.
        int score_x = 0;
        while( *str_x != ':'){ // until separator
            score_x = score_x*10 + (*str_x - '0');
            str_x++;
        }
        int score_y = 0;
        while( *str_y != '\0'){ // until NULL
            score_y = score_y*10 + (*str_y - '0');
            str_y++;
        }

        // Now compare them and give points
        if( score_x == score_y ){
            point_x++;
            point_y++;
        }else if( score_x > score_y ){
            point_x+=3;
        }else{
            point_y+=3;
        }
    }

    printf("point_x=%d   point_y=%d\n", point_x, point_y);

    return 0;
}
int main(void) 
{
  char* matchs[10] = { "2:3",  "0:0", "15:12", "2:13", "1:5", 
                       "55:7", "7:2", "17:2",  "2:17", "17:18" };

  for(int i = 0; i < 10; i++)
  {
    printf("%s ", matchs[i]);
  }

  return 0;
}
#include <stdio.h>
    
int main(void) 
{
  char* matchs[10+1] = { "2:3",  "0:0", "15:12", "2:13", "1:5", 
                         "55:7", "7:2", "17:2",  "2:17", "17:18", NULL };

  for(char** ptr=matchs; *ptr != NULL; ptr++)
  {
    printf("%s ", *ptr);
  }
  return 0;
}