有没有更干净或更简单的方法来重写我编写的代码? 我是新手,我现在正在C++中学习一个入门课。我的第二个实验室告诉我复制。 我试着让我的代码尽可能简单,而不会对课堂上还没有涉及到的内容过于超前。我只是想做些不同的事情,这样我就能自己学到更多。在我向男朋友寻求帮助之前,我得找上好几个小时。他精通C++,但我保证不做任何太高级的事情。p>
这就是我所做的:有没有更干净或更简单的方法来重写我编写的代码? 我是新手,我现在正在C++中学习一个入门课。我的第二个实验室告诉我复制。 我试着让我的代码尽可能简单,而不会对课堂上还没有涉及到的内容过于超前。我只是想做些不同的事情,这样我就能自己学到更多。在我向男朋友寻求帮助之前,我得找上好几个小时。他精通C++,但我保证不做任何太高级的事情。p>,c++,C++,这就是我所做的: #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { cout << setw(39) << setfill('*') << "*" << endl; // center = (number of asterisks / 2) + (text si
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout非常有趣。基本上这个任务无法解决。老师以比例字体给出预期的输出,而控制台输出很可能是单间隔的。因此,对齐和星星数不能匹配
此外,您的解决方案中有一个错误。您的工作宽度应为40,而不是39。否则,您的文本未完全重新对齐
因此,现在来看看可能的解决方案:
您可以使用简单的方法,将预期的文本作为原始字符串输出。
请不要使用命名空间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;
}
但是,我猜,你的老师想解释操纵者
接下来的解决方案如下所示:
#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;
}
#包括
#包括
int main(){
我只是想知道是否有更干净和/或更简单的方法来做到这一点不完全是。这可能是我要做的——或者至少是非常接近于此的事情。一切都应该尽可能简单,但不简单。-阿尔伯特·爱因斯坦,更干净的解决方案是打印出多行文本,用空格/*代替setfill和setw。
#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;
}