gpt4 book ai didi

c - 如果我连续看到同一个字符,则退出 while 循环

转载 作者:行者123 更新时间:2023-11-30 15:17:04 25 4
gpt4 key购买 nike

我应该读取与此类似的输入行

1234-56789   11:22:33:44: .... :88 

其中右侧最多可以是 8 个两位数整数。如果整数不足,则为以下格式

1234-56789   11:22:33:: 

最后一个数字后面总是跟着两个冒号。我已经尝试过以下方法

while((Line + tot) != ':' && (Line + tot + 1) != ':')
while(Line + tot && (Line + tot + 1) != ':')
while(sscanf(Line + tot, "%c%c", &firstColon, &secondColon) != 2)

在一起扫描两个冒号之后,上面的所有三行是否会继续进入 while 循环,我不希望这样做,因为我知道我没有更多的整数可以跟随。在第三个 while 循环中,我 100% 确定我将冒号分配到了各自的位置,因为我打印了它们的值,但我仍然不确定为什么它继续进入...我使用 tot 作为我的 arrayIndex,这样我不要每次都从头读。预先感谢您的宝贵时间。

最佳答案

while((Line + tot) != ':' && (Line + tot + 1) != ':')
while(Line + tot && (Line + tot + 1) != ':')

这些是错误的,因为 Line + tot 是一个指针,而不是 ':'
此外,a != ':' && b != ':'a = '1', b = ':' 中变为 false。这意味着这不是一个检查是否有两个分号的好表达式。

while(sscanf(Line + tot, "%c%c", &firstSemi, &secondSemi) != 2)

这是错误的,因为这只检查是否只有零个或一个字符。

试试这个:

while(*(Line + tot) != ':' || *(Line + tot + 1) != ':')
while(sscanf(Line + tot, "%c%c", &firstSemi, &secondSemi) != 2 || firstSemi != ':' || secondSemi != ':')

请注意,firstSemisecondSemi 必须是 char 类型才能以这种方式使用。

关于c - 如果我连续看到同一个字符,则退出 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706207/

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