gpt4 book ai didi

c - 更新特殊情况的矩阵

转载 作者:行者123 更新时间:2023-11-30 14:48:02 25 4
gpt4 key购买 nike

我正在尝试编写一个函数,它获取矩阵 9x9 并按照以下规则根据用户的输入进行更新:

  1. 有效数字介于 1 到 9 之间(零无效)。
  2. 我必须使用 scanf 直到得到 EOF
  3. 输入包含数字和符号。有效输入是一对两位数字,后跟符号或 EOF 或空格。超过两位数的字符串无效。例如(123% 无效,但 12% 有效)。

示例:

输入:10 33%55^21 $123%

输出:

0     0     0     0     0     0     0     0     0
1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

说明:10123 无效。 335521 有效,因此我们将 1 放入 22 中, 4410

我尝试做的事情:

void updateMarix(int matrix[][9]) {
int digits = 0, one_previous, two_previous;
char input;
while (scanf("%c", &input) != EOF) {
if(isValidDigit(input)) {
digits++;
if(digits == 1) {
two_previous = input - '0' - 1;
continue;
} else if(digits == 2){
one_previous = input - '0' -1;
continue;
}
} else if(digits == 2) {
matrix[two_previous][one_previous]++;
}
digits = 0; // reset
}
}

大多数测试都以成功告终,但也有一些测试失败。我认为这是因为我没有处理最后一个输入(例如,如果它以 22 结尾,它不会更新它,因为在我的实现中,更新是在下一次迭代中,当其他符号作为输入)。

有更好的实现吗?我的代码变得困惑且不干净。

*编辑:它应该忽略无效输入,并且 a3b 不算数,a03b 也不算数,但 a13b 算作13 意味着我们应该增加 matrix[0][2] 中的数字。

编辑 2:@JonathanLeffler 提到了 FSM,所以我尝试创建一个:

enter image description here

尽管它不处理 1234 (无效数字)或 123 (同样无效)的情况。最相似的事情是创建一个从第二个数字符号的箭头(但这并不完全正确,因为在1234%12中只有12有效。

最佳答案

我认为你的 FSM 需要 4 个状态加上最终状态:

  1. 读取到零位数字 (D0)。
  2. 读取一位数字 (D1)。
  3. 读取两位数字 (D2)。
  4. 数字无效,但不再需要错误报告 (DI)。

也有 4 种不同的输入:

  1. 数字 1-9。
  2. 数字 0。
  3. 其他。
  4. EOF。

我在每个状态中使用了状态开关和 if/else 代码,但这会导致代码有些冗长。 OTOH,我相信它可以正确处理输入。

/*
** FSM
** States: 0 digits (D0), 1 digit (D1), 2 digits (D2), digits invalid (DI)
** Inputs: digit 1-9 (D), digit 0 (0), other (O), EOF.
** Action: S - save, E - error, I - ignore, P - print
** Body of FSM encodes "action;state"
**
** State D0 D1 D2 DI
** Input
** D S;D1 S;D2 E;D2 I;DI
** O I;D0 E;D0 P;D0 I;D0
** 0 E;D2 E;D2 E;D2 I;DI
** EOF I;end E;end P;end I;end
*/

#include <assert.h>
#include <ctype.h>
#include <stdio.h>

enum State { D0, D1, D2, DI };
enum Input { Digit, Zero, Other, End };

static int debug = 0;

static enum Input input(int *rv)
{
int c = getchar();
if (debug)
printf("Input: %c\n", (c == EOF) ? 'X' : c);
*rv = c;
if (c == EOF)
return End;
if (isdigit(c))
{
*rv = c - '0';
return (c == '0') ? Zero : Digit;
}
return Other;
}

static void updateMatrix(int matrix[9][9])
{
char pair[2] = { 0, 0 };
enum State state = D0;

int c;
enum Input value;
while ((value = input(&c)) != End)
{
switch (state)
{
case D0:
if (value == Digit)
{
pair[0] = c;
state = D1;
}
else if (value == Zero)
{
fprintf(stderr, "Received zero digit - invalid\n");
state = DI;
}
else
{
assert(value == Other);
}
break;

case D1:
if (value == Digit)
{
pair[1] = c;
state = D2;
}
else if (value == Zero)
{
fprintf(stderr, "Received zero digit - invalid\n");
state = DI;
}
else
{
assert(value == Other);
fprintf(stderr, "Received one digit where two expected\n");
state = D0;
}
break;

case D2:
if (value == Digit)
{
fprintf(stderr, "Received more than two digits where two were expected\n");
state = DI;
}
else if (value == Zero)
{
fprintf(stderr, "Received zero digit - invalid\n");
state = DI;
}
else
{
assert(value == Other);
printf("Valid number %d%d\n", pair[0], pair[1]);
matrix[pair[0]-1][pair[1]-1] = 1;
state = D0;
}
break;

case DI:
if (value == Other)
state = D0;
break;
}
}

if (state == D2)
{
printf("Valid number %d%d\n", pair[0], pair[1]);
matrix[pair[0]-1][pair[1]-1] = 1;
}
else if (state == D1)
fprintf(stderr, "Received one digit where two expected\n");
}

static void dump_matrix(const char *tag, int matrix[9][9])
{
printf("%s:\n", tag);
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
printf("%4d", matrix[i][j]);
putchar('\n');
}
}

int main(void)
{
int matrix[9][9] = { 0 };

updateMatrix(matrix);
dump_matrix("After input", matrix);

return 0;
}

根据您的测试输入,它会生成输出:

Received zero digit - invalid
Valid number 33
Valid number 55
Valid number 21
Received more than two digits where two were expected
After input:
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

在大部分无效的输入文件上:

123345132
bbbb12cccc1dddd011dd

它产生输出:

Received more than two digits where two were expected
Valid number 12
Received one digit where two expected
Received zero digit - invalid
After input:
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

您可以(轻松地)争辩说,错误消息可能提供更多信息(识别错误字符,可能还包括先前的有效数字),但它只会为每个无效序列生成一条错误消息,这是有益的。

关于c - 更新特殊情况的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787385/

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