Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有人能给我解释一下这个算法吗?c++;_C++_Algorithm - Fatal编程技术网

C++ 有人能给我解释一下这个算法吗?c++;

C++ 有人能给我解释一下这个算法吗?c++;,c++,algorithm,C++,Algorithm,我想知道这个代码是怎么工作的 我不能理解这里的算法 #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); f

我想知道这个代码是怎么工作的

我不能理解这里的算法

#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;
}
此代码应打印此形状

或者您可以在这里测试代码


对于由此带来的不便,我深表歉意。

因为您将所有变量定义为int,并且正在对布尔值进行计算,它会自动将该布尔值转换为int。(i==L/2)和(i>L/2)这两个值输出为1或0。将真值设为1,假为0,则可以理解函数

l = cons + i * 2 - 2 * (i == L / 2) - 4 *(i - L / 2) * (i > L / 2);
试试看

#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;
}

离题。考虑使用所有警告和调试信息编译代码(<代码> g++-Walth-Wop-g <代码>…)并在调试器中一步一步地运行它(例如<代码> GDB < /代码>)。顺便说一句,这与其说是C++
(i==L/2)
(i>L/2)
这两个值输出为1或0,不如说是一个初等数学问题()。考虑<代码>真< /代码>为1,<代码> false <代码>为0;它考虑编译!离题!这个程序根本不应该起作用。(C\C++)中的函数被称为
Draw()
,而
main()
正在调用一个名为
Draw()
的函数;将其中一个重命名为小写或大写将使其可编译。抱歉,我没有注意到:)这不是一个函数,而是一个赋值表达式。也许用重写它会有帮助。@basilestrynkevitch如果
(i=L/2)
赋值表达式,它不是吗?但是这里
(i==L/2)
。您可以看到,
i
的值在整个
l=cons+i*2-2*(i==l/2)-4*(i-l/2)*(i>l/2)
是一个赋值表达式,用作语句时不会改变。C语言中没有赋值语句(每个赋值都是一个表达式,大多数都用作语句)好吧,现在我明白了,非常感谢:]但是为什么java不能编译这段代码呢?比如说坏操作符第一个是int,第二个是boolean,我们能用java做同样的事情吗?(自动将布尔值转换为整数)再次感谢:]这不是Java,而是C。
#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;
}
(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