是否有更干净或更简单的方法来重写我编写的代码?

问题描述 投票:0回答:1

我是编程新手,目前正在参加C ++入门课程。我的第二个实验告诉我复制this。我试图使我的代码尽可能简单,而又不会在课堂上没有涉及的事情上超越自己。我只是想做一些不同的事情,以便我自己学习更多。在我向男朋友寻求帮助之前,我不得不查了几个小时。他精通C ++,但我确保不要做任何太高级的事情。

这是我做的:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()

{
    cout << setw(39) << setfill('*') << "*" << endl;

    // center = (number of asterisks / 2) + (text size / 2)
    // round up on odd text sizes
    cout << setfill(' ') << setw(28) << "Nathania Tasico\n" << setw(23) << "CSE 100\n" << setw(32) << "Welcome to C++ Programming" << endl;
    cout << setw(39) << setfill('*') << "*" << endl << endl;

    // Right justified = number of asterisks - text size 
    cout << "1. The sum of 2 + 3" << setfill(' ') << setw(19) << "= " << 2 + 3 << endl;
    cout << "2. The multiplication of 5*6" << setw(10) << "= " << 5 * 6 << endl;
    cout << "3. When I divide 15/7, the quotient" << setw(3) << "= " << 15 / 7 << endl;
    cout << "4. The remainder of 15 % 4" << setw(12) << "= " << 15 % 4 << "\n\n" << endl;

    cout << setfill(' ') << setw(37) << "This is the end of my first Program" << endl;
    cout << setw(24) << "Thank you!" << endl;

    return 0;
}

我只想知道是否有更清洁和/或更简单的方法来执行此操作。我正在按原样提交,但是我仍然想学习最好的方法,以便我可以学习更多。

谢谢!

c++
1个回答
1
投票

非常有趣。基本上,这个任务无法解决。老师用比例字体给出了预期的输出,控制台的输出很可能是等宽的。因此,对齐方式和星数不适合。

另外。您的解决方案中有错误。您的工作宽度应为40的总宽度,而不是39。否则,文本将无法完全对齐。

所以,现在到可能的解决方案:

您可以使用幼稚的方法,只需将期望的文本作为原始字符串输出。请不要使用using namespace std;。您会在stackoverflow上找到大量不应该使用的提示。

请参阅

#include <iostream>

int main() {
    std::cout << R"(
****************************************
            Nathania Tasico
                CSE 100
       Welcome to C++ Programming
****************************************



1. The sum of 2+3                   = 5
2. The multiplication of 5*6        = 30
3. When I divide 15/7, the quotient = 2 
4. The remainder of 15 % 4          = 1



  This is the end of my first program
              Thank you!
)";

    return 0;
}

但这太简单了,因为您想了解IO流的用法。

然后,接下来,我们让编译器计算数学结果(这将在编译时发生,而不是在运行时发生)。此外,我们了解到C ++程序输入是未格式化的,您可以在所需的任何位置添加换行符。

并且一个接一个地写2个字符串文字,它们将被串联在一起。因此,"123" "456"将为"123456"。另请注意,此处不应使用std::endl。每次(不必要)刷新输出缓冲区,可以安全地用“ \ n”或“ \ n”代替。

也不必在每行中重复std::cout

下一个解决方案:

#include <iostream>

int main() {

    std::cout << "****************************************\n"
                 "             Nathania Tasico\n"
                 "                 CSE 100\n"
                 "        Welcome to C++ Programming\n"
                 "****************************************\n\n\n\n"


                 "1. The sum of 2 + 3                 = " << 2 + 3
            << "\n2. The multiplication of 5*6        = " << 5 * 6
            << "\n3. When I divide 15/7, the quotient = " << 15 / 7
            << "\n4. The remainder of 15 % 4          = " << 15 % 4

      <<   "\n\n\n   This is the end of my first Program\n"
            <<   "               Thank you!\n";

    return 0;
}

但是,我想您的老师想解释一下操纵器。

然后下一个解决方案如下所示:

#include <iostream>
#include <iomanip>

int main() {

    std::cout << std::setw(40) << std::setfill('*') << "" << '\n'

        // center = (number of asterisks / 2) + (text size / 2)
        // round up on odd text sizes
        << std::setfill(' ') << std::setw(28) << "Nathania Tasico\n" 
        << std::setw(24) << "CSE 100\n"
        << std::setw(34) << "Welcome to C++ Programming\n"

        << std::setw(40) << std::setfill('*') << "" << "\n\n\n"

        // Right justified = number of asterisks - text size 
        << "1. The sum of 2 + 3" << std::setfill(' ') << std::setw(19) << "= " << 2 + 3
        << "\n2. The multiplication of 5*6" << std::setw(10) << "= " << 5 * 6
        << "\n3. When I divide 15/7, the quotient" << std::setw(3) << "= " << 15 / 7
        << "\n4. The remainder of 15 % 4" << std::setw(12) << "= " << 15 % 4 << "\n\n\n"

        << std::setfill(' ') << std::setw(37) << "This is the end of my first Program\n"
        << std::setw(24) << "Thank you!\n";

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.