i want to know how this code is working,
我想知道这段代码是如何工作的,
i can't understand the algorithm here
我无法理解这里的算法
#include <stdio.h>
void Draw(int length) {
int cons = 3,L,l;
length -= length % 2;
L = length + cons;
for (int i = 0; i < L; i++){
l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
for (int j = 0; j < (L-l) / 2; j++) {
printf(" ");
}
for (int j = 0; j < l; j++) {
if (j == l/2)
printf("|");
else if (i == L/2)
printf("=");
else if (j == 0 && i < L/2 || (j == l - 1 && i > L / 2))
printf("/");
else if (j == l - 1 && i < L / 2 || (j == 0 && i > L / 2))
printf("\\");
else
printf("*");
}
printf("\n");
}
}
int main(int argc, char **argv) {
Draw(5);
printf("\n");
Draw(6);
return 0;
}
I want clear clarification to this particular line, please :
我想清楚澄清这一特定的一行,请:
l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
this code should print this shape
此代码应打印此形状
点击查看图片
or you can test the code here https://ideone.com/49mtT4
或者你可以在这里测试代码https://ideone.com/49mtT4
and i apologize for any inconvenience
对于给您带来的任何不便,我深表歉意
1 个解决方案
#1
0
since you you defined all your variables as int and you are making a calculation on boolean, it automatically convert that boolean into int. (i == L / 2) and (i > L / 2) these two values output either 1 or 0. consider true as 1 and false as 0.then you can understand the function
因为你将所有变量定义为int并且你在布尔值上进行计算,所以它会自动将该boolean转换为int。 (i == L / 2)和(i> L / 2)这两个值输出1或0.将true视为1,将false视为0.然后您可以理解函数
l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
try follwing
#include <stdio.h>
#include <iostream>
using namespace std;
void Draw(int length) {
int cons = 3,L,l,test;
length -= length % 2;
L = length + cons;
for (int i = 0; i < L; i++){
l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
test = (i == L / 2);
cout << "(i == L / 2):" << test<< endl;
test = (i > L / 2);
cout << "(i > L / 2):" << test<< endl;
}
}
int main(int argc, char **argv) {
Draw(5);
return 0;
}
Which will output
哪个会输出
(i == L / 2):0
(i > L / 2):0
(i == L / 2):0
(i > L / 2):0
(i == L / 2):0
(i > L / 2):0
(i == L / 2):1
(i > L / 2):0
(i == L / 2):0
(i > L / 2):1
(i == L / 2):0
(i > L / 2):1
(i == L / 2):0
(i > L / 2):1
#1
0
since you you defined all your variables as int and you are making a calculation on boolean, it automatically convert that boolean into int. (i == L / 2) and (i > L / 2) these two values output either 1 or 0. consider true as 1 and false as 0.then you can understand the function
因为你将所有变量定义为int并且你在布尔值上进行计算,所以它会自动将该boolean转换为int。 (i == L / 2)和(i> L / 2)这两个值输出1或0.将true视为1,将false视为0.然后您可以理解函数
l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
try follwing
#include <stdio.h>
#include <iostream>
using namespace std;
void Draw(int length) {
int cons = 3,L,l,test;
length -= length % 2;
L = length + cons;
for (int i = 0; i < L; i++){
l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
test = (i == L / 2);
cout << "(i == L / 2):" << test<< endl;
test = (i > L / 2);
cout << "(i > L / 2):" << test<< endl;
}
}
int main(int argc, char **argv) {
Draw(5);
return 0;
}
Which will output
哪个会输出
(i == L / 2):0
(i > L / 2):0
(i == L / 2):0
(i > L / 2):0
(i == L / 2):0
(i > L / 2):0
(i == L / 2):1
(i > L / 2):0
(i == L / 2):0
(i > L / 2):1
(i == L / 2):0
(i > L / 2):1
(i == L / 2):0
(i > L / 2):1