我正在尝试将伪代码转换为C代码.伪代码和Python一样缩进。我想把它变成花括号。有办法吗?示例伪代码如下所示:
int my_function(int a, int b)
int c;
int d;
if(a==5)
c = 6;
d = 7;
else
c = 8;
d = 9;
return (c+d);我想把它转换成以下内容。
int my_function(int a, int b)
{
int c;
int d;
if(a==5)
{
c = 6;
d = 7;
}
else
{
c = 8;
d = 9;
}
return (c+d);
}发布于 2019-12-14 15:05:59
假设您打算编写真正的代码来执行转换,而不是寻找工具或编辑器工具,那么它只是文本处理。
在(真)伪代码中,您将执行以下操作:
For each line:
if the indentation is greater than the current level then:
insert a line before with an opening brace at the current level,
increase the current level
otherwise if the indentation is less than the current level then:
While current level > new level
insert a line after with an closing brace at the current level,
decrease the current level发布于 2019-12-14 15:02:37
要打开类似Python的伪代码,您可以在任何缩进比当前行缩进更多的行之前插入一个{,并且在块后插入一个},就像需要展开缩进级别一样。
关于大括号的位置,有多种样式。您在问题中发布的示例可能会增加行数,而牺牲可读性,并且可能会使一些愚蠢的错误更难检测,例如:
while ((c = getchar()) = EOF);
{
putchar(c);
}流行的另一种样式是在命令块( if、for、while、do或switch语句)的行尾插入开头的大括号,在单独行的开头插入大括号,然后是if语句的else部分或do语句的while部分。
在这里,您的代码为这种样式进行了修改:
int my_function(int a, int b) {
int c, d;
if (a == 5) {
c = 6;
d = 7;
} else {
c = 8;
d = 9;
}
return c + d;
}https://stackoverflow.com/questions/59336049
复制相似问题