如何将此C ++函数转换为C?

时间:2015-05-02 04:52:20

标签: c

下面的代码是用C ++编写的。如何将其翻译为C?

 void drawBoard()

    {
    system( "cls" );
    cout << "SCORE: " << score << endl << endl;
    for( int y = 0; y < 4; y++ )
    {
        cout << "+------+------+------+------+" << endl << "| ";
        for( int x = 0; x < 4; x++ )
        {
        if( !board[x][y].val ) cout << setw( 4 ) << " ";
        else cout << setw( 4 ) << board[x][y].val;
        cout << " | ";
        }
        cout << endl;
    }
    cout << "+------+------+------+------+" << endl << endl;
    }

2 个答案:

答案 0 :(得分:1)

代码已经非常兼容C语言。但是,cout是C ++构造。

要使其完全兼容C,您可以将printf替换为cout << "SCORE: " << score << endl << endl; --> printf("SCORE: %d \n\n", score); 。例如,在您的代码中,

lineTwo[1].replaceAll(" ", "") == entered.replaceAll(" ", "")

您必须使用不同的参数来获取格式和输出,但这是一般的想法。这个网站是一个很好的参考:Printf

答案 1 :(得分:0)

假设valscoreint,请注意以下几行:

cout << "SCORE: " << score << endl << endl;
cout << "+------+------+------+------+" << endl << "| ";
cout << setw( 4 ) << " ";
cout << setw( 4 ) << board[x][y].val;

会转换为:

printf("SCORE: %d\n\n", score);
printf("+------+------+------+------+\n| ";
printf("    ");
printf("%4d", board[x][y].val);

你可以弄清楚其余部分。