如何将控制台输出的数字对齐在同一位置 我试图编写一个非常简单的C++程序,它输出一个查找表,其相应的 x和y窦房结函数值。我编写的代码如下: #include "stdafx.h" #include <iostream> #include <cmath> using namespace std; int main() { double hw = 4.0; int nsteps = 30; const double PI = 3.14159; const double maxx = hw * PI; const double deltax = maxx / nsteps; double x = 0.0; for (int i = 0; i < nsteps; i++) { const double f = sin(x); cerr << x << "\t" << f << endl; x = x + deltax; } return 0; } #包括“stdafx.h” #包括 #包括 使用名称空间std; int main() { 双hw=4.0; int nsteps=30; 常数双PI=3.14159; 常数double maxx=hw*PI; const double deltax=maxx/nsteps; 双x=0.0; 对于(int i=0;i
使用如何将控制台输出的数字对齐在同一位置 我试图编写一个非常简单的C++程序,它输出一个查找表,其相应的 x和y窦房结函数值。我编写的代码如下: #include "stdafx.h" #include <iostream> #include <cmath> using namespace std; int main() { double hw = 4.0; int nsteps = 30; const double PI = 3.14159; const double maxx = hw * PI; const double deltax = maxx / nsteps; double x = 0.0; for (int i = 0; i < nsteps; i++) { const double f = sin(x); cerr << x << "\t" << f << endl; x = x + deltax; } return 0; } #包括“stdafx.h” #包括 #包括 使用名称空间std; int main() { 双hw=4.0; int nsteps=30; 常数双PI=3.14159; 常数double maxx=hw*PI; const double deltax=maxx/nsteps; 双x=0.0; 对于(int i=0;i,c++,console,alignment,text-alignment,C++,Console,Alignment,Text Alignment,使用std::setprecision()设置小数点后的计数,使用std::setw()设置输出长度的宽度。包括所需,例如: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double hw = 4.0; int nsteps = 30; const double PI = 3.14159;
std::setprecision()
设置小数点后的计数,使用std::setw()
设置输出长度的宽度。包括
所需,例如:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double hw = 4.0;
int nsteps = 30;
const double PI = 3.14159;
const double maxx = hw * PI;
const double deltax = maxx / nsteps;
double x = 0.0;
cerr << std::setprecision(8);
for (int i = 0; i < nsteps; i++) {
const double f = sin(x);
cerr << std::setw(20) << x << std::setw(20) << f << endl;
x = x + deltax;
}
return 0;
}
使用
std::setprecision()
设置小数点后的计数,使用std::setw()
设置输出长度的宽度。包括所需的
,例如:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double hw = 4.0;
int nsteps = 30;
const double PI = 3.14159;
const double maxx = hw * PI;
const double deltax = maxx / nsteps;
double x = 0.0;
cerr << std::setprecision(8);
for (int i = 0; i < nsteps; i++) {
const double f = sin(x);
cerr << std::setw(20) << x << std::setw(20) << f << endl;
x = x + deltax;
}
return 0;
}
上述答案提供了一个不正确的解决方案,因为对齐设置不正确。我将使用函数来处理格式设置:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void printxy(double x, double y, int width){
cout << setw(width) << x << "\t";
if (y < 0) cout << "\b";
cout << setw(width) << y << "\n";
}
int main(){
double hw = 4.0;
int nsteps = 30;
const double PI = 3.14159;
const double maxx = hw * PI;
const double deltax = maxx / nsteps;
double x = 0.0;
int decimals = 6;
int width = 8; //Adjust as needed for large numbers/many decimals
cout << std::setprecision(decimals);
cout << std::setw(width);
cout.setf(ios::left);
for (int i = 0; i < nsteps; i++) {
const double y = sin(x);
printxy(x, y, width);
x = x + deltax;
}
}
我也不鼓励在这类打印操作中使用cerr
我还应该提到
endl
是一个定时炸弹:它刷新输出,这意味着流的内部缓冲区被写入(无论是控制台、文件还是其他)。当应用程序扩展并变得更加IO密集时,这可能会成为一个严重的性能问题:由于频繁插入endl
,用于提高IO性能的缓冲区可能未被使用。解决方案是使用换行符'\n'
上述答案提供了一个不正确的解决方案打开,因为对齐方式设置不正确。我将使用函数处理格式设置:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void printxy(double x, double y, int width){
cout << setw(width) << x << "\t";
if (y < 0) cout << "\b";
cout << setw(width) << y << "\n";
}
int main(){
double hw = 4.0;
int nsteps = 30;
const double PI = 3.14159;
const double maxx = hw * PI;
const double deltax = maxx / nsteps;
double x = 0.0;
int decimals = 6;
int width = 8; //Adjust as needed for large numbers/many decimals
cout << std::setprecision(decimals);
cout << std::setw(width);
cout.setf(ios::left);
for (int i = 0; i < nsteps; i++) {
const double y = sin(x);
printxy(x, y, width);
x = x + deltax;
}
}
我也不鼓励在这类打印操作中使用cerr
我还应该提到
endl
是一个定时炸弹:它刷新输出,这意味着流的内部缓冲区被写入(无论是控制台、文件还是其他)。当应用程序扩展并变得更加IO密集时,这可能会成为一个严重的性能问题:由于频繁插入endl
而导致用于提高IO性能的缓冲区可能未使用。解决方案是使用换行符'\n'
,但这并不能解决问题。下一步是es(您的答案中没有包含)的格式不正确。这并不能解决问题。接下来的19行(您的答案中没有包含)格式不正确。很漂亮。非常感谢。你能解释一下为什么endl
是个定时炸弹吗?@Max很抱歉,我不知怎么忘了。我编辑了答案来解释这一点。另外,还称之为“定时炸弹”有点夸张。很漂亮。非常感谢。你能解释一下为什么endl
是一个定时炸弹吗?@Max很抱歉,我不知怎么忘了。我编辑了答案来解释这一点。而且,称它为“定时炸弹”也有点夸张。