我正在打印4x4矩阵的调试语句。有没有人知道不使用cout
的更好的方法?
// num decimal places to show
void print( int decimals )
{
char fmtString[ 300 ] ;
// I'm thinking this should be able to get smaller.
sprintf(fmtString,
"%%.%df %%.%df %%.%df %%.%df\n"
"%%.%df %%.%df %%.%df %%.%df\n"
"%%.%df %%.%df %%.%df %%.%df\n"
"%%.%df %%.%df %%.%df %%.%df",
decimals, decimals, decimals, decimals,
decimals, decimals, decimals, decimals,
decimals, decimals, decimals, decimals,
decimals, decimals, decimals, decimals ) ;
printf(fmtString,
m[0][0], m[0][1], m[0][2], m[0][3],
m[1][0], m[1][1], m[1][2], m[1][3],
m[2][0], m[2][1], m[2][2], m[2][3],
m[3][0], m[3][1], m[3][2], m[3][3] ) ;
}
超级加分预处理器!
最佳答案
怎么样:
void print( int decimals = 2 )
{
int dimension = 4;
for(int i = 0; i < dimension; i++) {
for(int j = 0; j < dimension; j++) {
printf("%.*f", decimals, matrix[i][j]);
if(j == dimension - 1) printf("\n");
else printf(" ");
}
}
}
关于c - 4x4矩阵的调试语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1516358/