我需要帮助从这个数组中提取每个团队的分数,但我仍然不知道如何解决它


#include <stdio.h>

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

char *(*pt) = matchs; // Pointing to array matchs

for(int i = 0; i < 10; i++){

printf("%s ", *++(pt));

}

return 0;        
}

我们的足球队以十场比赛获得冠军。每个匹配的结果看起来像";x: y";。所有匹配的结果都记录在阵列中

例如:["3:1", "22:2", "0:19", ...]

写一个函数,用这样的列表来计算每支球队在锦标赛中的积分。每场比赛的点数规则:

if x>y - 3 points
if x<y - 0 point
if x = y - 1 points

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

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

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

以下是整个解决方案。你可以通过在每个结果中找到:字符来获得y队得分的指针。(请参阅如何获取str_y指针(。我认为最棘手的部分是在不使用stoi这样的函数的情况下获得每支球队的分数。我确实写了我的解决方案,但我建议你先自己写(尤其是如果这是家庭作业(。

#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 != '' ) 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 != ''){ // 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=%dn", point_x, point_y);
return 0;
}

char*matches[10]是一个char指针数组或字符串数组。我们可以指向数组中具有matches[index]或*(matches+index(的任何一个字符串,这些字符串会衰减为char指针(字符串(。然后,我们可以通过[]或指针算术的解引用在其中找到一个特定的字符。

例如,

#include <stdio.h>
int main()
{
char *matches[10] = { "2:3", "0:0", "15:12", "2:13", "1:5", "55:7", "7:2", "17:2", 
"2:17", "17:18"};
int i;
for(i = 0; i < 10; i++)
{
/*Dereference once will give a char pointer (string), dereference twice
will give us a single char*/
printf("n%s", *(matches + i)); //This will print the whole string, i.e. "2:3" or {2,:,3,0}
printf("nx: %c", *(*(matches + i) + 0)); //This will print '2'
printf("ny: %c", *(*(matches + i) + 2)); //This will print '3'
/*Note we can compare chars directly, as they are essentially 1 byte ints, so
('2' < '3') will evaluate to 1 (true)*/
/*Note we will also run into problems with scores that are multiple chars, such as 17:20
We can work around this with an if statement checking if a char is ':', which 
divides the numbers. We can then print them out or write them to a file or
whatever. Note we can find a literal accurate integer representation of a char
without stoi or atoi by doing: int number = '9' - '0'; This will give the number 9.
*/
}
return 0;
}

编辑:为了详细说明关于计算多个字符的分数,

#include <stdio.h>
#include <math.h>
int str_to_int(const char * string, int length)
{
int i;
int num = 0;
for(i = 0; i < length; i++)
{
int place_value = pow(10, (length - i - 1));
num += place_value * (*(string + i) - '0');
}
return num;
}
int main()
{
char *matches[10] = { "2:3", "0:0", "15:12", "2:13", "1:5", "55:7", "7:2", "17:2", 
"2:17", "17:18"};
int i;
for(i = 0; i < 10; i++)
{
/*Dereference once will give a char pointer (string), dereference twice
will give us a single char*/
printf("n%s", *(matches + i)); //This will print the whole string, i.e. "2:3" or {2,:,3,0}
char current; //To hold the current char we are reading.
int counter = 0;
char number[5]; //To hold a string representation of a number

do
{
current = *(*(matches + i) + counter);
if(current != ':')
*(number + counter) = current;

counter++;
}
while (current != ':');
*(number + counter - 1) = '';
//We now have the x value.
printf("nx: %d", str_to_int(number, counter - 1));
int counter2 = 0;
do
{
current = *(*(matches + i) + counter);
*(number + counter2) = current;

counter2++;
counter++;
}
while (current != '');
//Now we have y
printf("ny: %d", str_to_int(number, counter2 - 1));

}
return 0;
}

这将能够找到具有多个字符的分数。

编辑2:输出:

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

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

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

使用char**唯一有意义的情况是,字符串的数量未知,并且它们以sentinel值(如NULL(终止。这是一种比提前知道项目数量更不理想的情况,因此执行起来有些慢。示例:

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

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

相关内容

最新更新