小编Ism*_*ael的帖子

C 语言中的有限状态机

我正在尝试按照此图在 C 中创建 FSM: FSM

并具有如下所示的输出: 在此处输入图片说明

我首先定义了一个enum包含所有可能状态的:

typedef enum {
Start = 0,
Build_Id = 1,
Identifier = 2,
Build_Num = 3,
Number = 4,
Error = 5
} State_type;
Run Code Online (Sandbox Code Playgroud)

这是我目前拥有的代码:

State_type analyzeData(State_type* currState, char c);

int main(int argc, char** argv) {
  State_type currState = 0;
  for (int i = 1; i < strlen(*argv); ++i) {
    analyzeData(&currState, *argv[i]);
  }
}



State_type analyzeData(State_type* currState, char c) {
  if (currState == 0) {
    if (isblank(c)) {
      *currState = (State_type) 0;
      return …
Run Code Online (Sandbox Code Playgroud)

c enums fsm

5
推荐指数
1
解决办法
5244
查看次数

使用嵌套列表/嵌套循环

我想弄清楚如何在 Python 中使用嵌套列表/嵌套循环。我应该编写函数来创建一个新表,该表由以列表形式添加 2 个输入表的索引组成。因此,例如,如果函数是 addTables,我将运行:

addTables([[1,1],[1,1]],[[2,2],[2,2]])
Run Code Online (Sandbox Code Playgroud)

我会得到:

[[3,3],[3,3]]
Run Code Online (Sandbox Code Playgroud)

我很难弄清楚。首先,当我运行代码时,我编写的函数返回 [3,3,3,3]:

def addElements(element1,element2):
    newlist = []
    for i in range(0,len(element1)):
        for j in range(0,len(element1[i])):
            new_element = element1[i][j] + element2[i][j]
            newlist.append(new_element)
    return newlist
Run Code Online (Sandbox Code Playgroud)

其次,我们应该使用多个函数。我不知道如何拆分程序,以便不同的部分由不同的功能完成。有人能指出我正确的方向吗?任何提示将非常感谢。

python nested list

3
推荐指数
1
解决办法
9129
查看次数

标签 统计

c ×1

enums ×1

fsm ×1

list ×1

nested ×1

python ×1