C-使用sscanf读取算术运算符 [英] C - Reading arithmetic operators with sscanf

查看:101
本文介绍了C-使用sscanf读取算术运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个包含两个数字的字符串,它们之间有一个算术运算符,例如其中之一:

I'm reading a string that has two numbers with an arithmetic operator between them, like one of these:

A1 + B1
A1 - B1
A1 * B1
A1 / B1

我可以读取A1和B1,但不能读取操作员.我正在使用以下内容阅读:

I can read A1 and B1 but not the operator. I'm reading using this:

while (sscanf(matrix[i][c] + offset, "%c%d%*c%n",
       &col, &line, &readCharCount) == 2) {
  // do something
}

我该怎么读操作员?

推荐答案

与数字转换说明符和%s不同,%c转换说明符不会跳过空格.因此,给定您的示例输入,%*c会在操作员之前读取空白.您可以明智地使用:

Unlike the numeric conversion specifiers and %s, the %c conversion specifier does not skip blanks. Thus, given your example inputs, the %*c is reading the blank before the operator. You could sensibly use:

while (sscanf(matrix[i][c] + offset, " %c%d %c %c%d", &col1, &row1, &op, &col2, &row2) == 5)
    ...data is OK...

由于您使用的是偏移量并且正在捕获扫描结束的位置,因此可以使用:

Since you're using an offset and were capturing where the scan ended, you would use:

while (sscanf(matrix[i][c] + offset, " %c%d %c %c%d%n",
              &col1, &row1, &op, &col2, &row2, &readCharCount) == 5)
    ...data is OK...

请注意,%n转换说明符未计算在内,因此测试仍然针对5,而不是6.

Note that the %n conversion specifier is not counted, so the test remains against 5, not 6.

还要注意格式字符串中空格的谨慎放置.它们是必要且灵活的(可以处理A1+B2 OK和A1 + B2).如果要允许使用更大的电子表格,则可能需要指定:

Also note the careful placement of spaces in the format string. They're necessary and flexible (that would handle A1+B2 OK, as well as A1 + B2). If you are going to allow bigger spreadsheets, you might prefer to specify:

while (sscanf(matrix[i][c] + offset, " %4[a-zA-Z]%d %c %4[a-zA-Z]%d%n",
              col1, &row1, &op, col2, &row2, &readCharCount) == 5)
    ...data is OK...

,其中col1col2的类型从单个更改为char col1[5]; char col2[5];(这也是&被删除的原因).扫描集允许识别诸如aAa1 + BbB2之类的输入.由于%d表示法,字母或数字与数字之间允许有空格(因此代码允许aaa 999 + bbb 888.要避免这种情况很困难;我认为您需要使用两个扫描集来处理数据:

where the type of col1 and col2 changes from a single to char col1[5]; char col2[5]; (which is why the & was dropped, too). The scan sets allow inputs like aAa1 + BbB2 to be recognized. Because of the %d notation, spaces are allowed between the letter or letters and the number (so the code would allow aaa 999 + bbb 888. Avoiding that is hard; I think you'd need to process the data with two scansets:

while (sscanf(matrix[i][c] + offset, " %4[a-zA-Z]%5[0-9] %c %4[a-zA-Z]%5[0-9]%n",
              col1, row1, &op, col2, row2, &readCharCount) == 5)
    ...data is OK...

现在类型为char row1[6]; char row2[6];,并且&符再次被删除.然后,您可以放心地将row1row2转换为数字.

where the types are now char row1[6]; char row2[6]; and the ampersands have been dropped again. You can then confidently convert row1 and row2 into numbers.

另请参见:使用C语言计算单元格转换器,以获取将列号转换为相应的字母代码.

See also: Calc cell convertor in C for code to convert column numbers into the corresponding alphabetic codes.

这篇关于C-使用sscanf读取算术运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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