问题描述
#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
解决方法
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指针数组或一个字符串数组。我们可以使用matchs [index]或*(matches + index)指向数组中的任何一个字符串,这些字符串会退化为char指针(字符串)。然后,可以在其中使用[]或使用指针算术取消引用来找到特定的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","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",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) = '\0';
//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 != '\0');
//Now we have y
printf("\ny: %d",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
,
这是整个解决方案。您可以通过在每个结果中找到primary.main
个字符来获得y队得分的指针。 (请参阅如何获得:
指针)。
我认为最棘手的部分是在不使用stoi等功能的情况下获得每个团队的得分。我确实写了我的解决方案,但是我建议您首先自己编写(特别是如果这是家庭作业)。
str_y
,
如果我们使用指针到指针消除了所有不必要和奇怪的复杂性,我们将获得以下快速且易读的代码:
int main(void)
{
char* matchs[10] = { "2:3","17:18" };
for(int i = 0; i < 10; i++)
{
printf("%s ",matchs[i]);
}
return 0;
}
使用char**
有意义的唯一情况是,字符串的数量未知,并且它们以前哨值终止,例如NULL。与事先知道项目数量相比,这种情况不太理想,因此执行起来有些慢。示例:
#include <stdio.h>
int main(void)
{
char* matchs[10+1] = { "2:3","17:18",NULL };
for(char** ptr=matchs; *ptr != NULL; ptr++)
{
printf("%s ",*ptr);
}
return 0;
}
如果需要,您可以从那里将每个项目转换为整数,而不是printf
调用。那是另一项任务,您可能要使用strtoul
。