通过以下 C 语言程序片段,逐条语句跟踪程序的执行流程(画出语法分 析树结构,C 语言程序片段: { int i; int j; float[10][10] a; i = 0; while ( i < 10 ) { j = 0; while ( j < 10 ) { a[i][j] = 0.0; j = j + 1; } i = i + 1; } i = 0; while ( i < 10 ) { a[i][i] = 1.0; i = i + 1; } }
时间: 2023-05-28 15:06:49 浏览: 59
程序代码如下:
```
int i;
int j;
float a[10][10];
i = 0;
while ( i < 10 ) {
j = 0;
while ( j < 10 ) {
a[i][j] = 0.0;
j = j + 1;
}
i = i + 1;
}
i = 0;
while ( i < 10 ) {
a[i][i] = 1.0;
i = i + 1;
}
```
程序的执行流程如下:
1. 定义整型变量 i。
2. 定义整型变量 j。
3. 定义浮点型二维数组 a。
4. 给 i 赋值为 0。
5. 进入 while 循环,判断 i 是否小于 10,是,执行循环体。
6. 给 j 赋值为 0。
7. 进入 while 循环,判断 j 是否小于 10,是,执行循环体。
8. 给 a[i][j] 赋值为 0.0。
9. 给 j 赋值为 j + 1。
10. 退出内层循环。
11. 给 i 赋值为 i + 1。
12. 退出外层循环。
13. 给 i 赋值为 0。
14. 进入 while 循环,判断 i 是否小于 10,是,执行循环体。
15. 给 a[i][i] 赋值为 1.0。
16. 给 i 赋值为 i + 1。
17. 退出循环。
18. 程序结束。
语法分析树结构如下:
```
Program
└── CompoundStmt
├── DeclStmt
│ ├── TypeSpec: int
│ ├── VarDecl: i
│ └── TypeSpec: int
│ └── VarDecl: j
├── DeclStmt
│ ├── TypeSpec: float
│ ├── ArrDecl: a[10][10]
│ └── EmptyExpr
├── ExprStmt
│ ├── VarExpr: i
│ └── IntegerLiteral: 0
├── WhileStmt
│ ├── BinaryOperator: i < 10
│ │ ├── VarExpr: i
│ │ └── IntegerLiteral: 10
│ └── CompoundStmt
│ ├── ExprStmt
│ │ ├── VarExpr: j
│ │ └── IntegerLiteral: 0
│ ├── WhileStmt
│ │ ├── BinaryOperator: j < 10
│ │ │ ├── VarExpr: j
│ │ │ └── IntegerLiteral: 10
│ │ └── CompoundStmt
│ │ ├── ExprStmt
│ │ │ ├── ArraySubscriptExpr: a[i][j]
│ │ │ │ ├── VarExpr: a
│ │ │ │ ├── VarExpr: i
│ │ │ │ └── VarExpr: j
│ │ │ └── FloatingLiteral: 0.0
│ │ └── ExprStmt
│ │ ├── VarExpr: j
│ │ └── BinaryOperator: j + 1
│ │ ├── VarExpr: j
│ │ └── IntegerLiteral: 1
│ └── ExprStmt
│ ├── VarExpr: i
│ └── BinaryOperator: i + 1
│ ├── VarExpr: i
│ └── IntegerLiteral: 1
└── WhileStmt
├── BinaryOperator: i < 10
│ ├── VarExpr: i
│ └── IntegerLiteral: 10
└── CompoundStmt
├── ExprStmt
│ ├── ArraySubscriptExpr: a[i][i]
│ │ ├── VarExpr: a
│ │ ├── VarExpr: i
│ │ └── VarExpr: i
│ └── FloatingLiteral: 1.0
└── ExprStmt
├── VarExpr: i
└── BinaryOperator: i + 1
├── VarExpr: i
└── IntegerLiteral: 1
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)