| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3071 人关注过本帖
标题:关于setw的问题
只看楼主 加入收藏
nofarewell
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-5-14
收藏
 问题点数:0 回复次数:5 
关于setw的问题
编写了一个矩阵转置的程序,想让输入和转置后的数组在屏幕上显示成矩阵的形状,没用setw之前运行正常,用了setw却编译出错,怎么回事?程序如下,麻烦大家帮我看看:

#include <iostream>
using namespace std;
#define M 3
#define N 4
void invert(int a[M][N])
{
int i,j,b[N][M],cnt;
cout<<"转置前:"<<endl;
for(i=0;i!=M;++i)
for(j=0;j!=N;++j)
{
cout<<setw(5)<<a[i][j];
b[j][i]=a[i][j];
cnt++;
if(cnt%N==0)
cout<<endl;
}
cnt=0;
cout<<"转置后:"<<endl;
for(i=0;i!=N;++i)
for(j=0;j!=M;++j)
{
cout<<setw(5)<<b[i][j];
cnt++;
if(cnt%M==0)
cout<<endl;
}
}
int main()
{
cout<<"请输入"<<M*N<<"个整数:"<<endl;
int i,j,array[M][N];
for(i=0;i!=M;++i)
for(j=0;j!=N;++j)
cin>>array[i][j];
invert(array);
return 0;
}

编译器提示:cpp(13) : error C2065: 'setw' : undeclared identifier
setw不是iostream库的函数吗?怎么会这样呢?请教大家!

[此贴子已经被作者于2007-10-31 11:19:07编辑过]

搜索更多相关主题的帖子: setw define cnt 矩阵 屏幕 
2007-10-31 11:00
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

#include<iomanip>

////////////////////////////////////////////////////////////////////////
for example:
// iomanip_setw.cpp
// compile with: /EHsc
// Defines the entry point for the console application.
//
// Sample use of the following manipulators:
// resetiosflags
// setiosflags
// setbase
// setfill
// setprecision
// setw

#include <iostream>
#include <iomanip>

using namespace std;

const double d1 = 1.23456789;
const double d2 = 12.3456789;
const double d3 = 123.456789;
const double d4 = 1234.56789;
const double d5 = 12345.6789;
const long l1 = 16;
const long l2 = 256;
const long l3 = 1024;
const long l4 = 4096;
const long l5 = 65536;
int base = 10;

void DisplayDefault( )
{
cout << endl << "default display" << endl;
cout << "d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl;
cout << "d3 = " << d3 << endl;
cout << "d4 = " << d4 << endl;
cout << "d5 = " << d5 << endl;
}

void DisplayWidth( int n )
{
cout << endl << "fixed width display set to " << n << ".\n";
cout << "d1 = " << setw(n) << d1 << endl;
cout << "d2 = " << setw(n) << d2 << endl;
cout << "d3 = " << setw(n) << d3 << endl;
cout << "d4 = " << setw(n) << d4 << endl;
cout << "d5 = " << setw(n) << d5 << endl;
}

void DisplayLongs( )
{
cout << setbase(10);
cout << endl << "setbase(" << base << ")" << endl;
cout << setbase(base);
cout << "l1 = " << l1 << endl;
cout << "l2 = " << l2 << endl;
cout << "l3 = " << l3 << endl;
cout << "l4 = " << l4 << endl;
cout << "l5 = " << l5 << endl;
}

int main( int argc, char* argv[] )
{
DisplayDefault( );

cout << endl << "setprecision(" << 3 << ")" << setprecision(3);
DisplayDefault( );

cout << endl << "setprecision(" << 12 << ")" << setprecision(12);
DisplayDefault( );

cout << setiosflags(ios_base::scientific);
cout << endl << "setiosflags(" << ios_base::scientific << ")";
DisplayDefault( );

cout << resetiosflags(ios_base::scientific);
cout << endl << "resetiosflags(" << ios_base::scientific << ")";
DisplayDefault( );

cout << endl << "setfill('" << 'S' << "')" << setfill('S');
DisplayWidth(15);
DisplayDefault( );

cout << endl << "setfill('" << ' ' << "')" << setfill(' ');
DisplayWidth(15);
DisplayDefault( );

cout << endl << "setprecision(" << 8 << ")" << setprecision(8);
DisplayWidth(10);
DisplayDefault( );

base = 16;
DisplayLongs( );

base = 8;
DisplayLongs( );

base = 10;
DisplayLongs( );

return 0;
}
///////////////////////////////////////////////////

Output:
default display
d1 = 1.23457
d2 = 12.3457
d3 = 123.457
d4 = 1234.57
d5 = 12345.7

setprecision(3)
default display
d1 = 1.23
d2 = 12.3
d3 = 123
d4 = 1.23e+003
d5 = 1.23e+004

setprecision(12)
default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setiosflags(4096)
default display
d1 = 1.234567890000e+000
d2 = 1.234567890000e+001
d3 = 1.234567890000e+002
d4 = 1.234567890000e+003
d5 = 1.234567890000e+004

resetiosflags(4096)
default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setfill('S')
fixed width display set to 15.
d1 = SSSSS1.23456789
d2 = SSSSS12.3456789
d3 = SSSSS123.456789
d4 = SSSSS1234.56789
d5 = SSSSS12345.6789

default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setfill(' ')
fixed width display set to 15.
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setprecision(8)
fixed width display set to 10.
d1 = 1.2345679
d2 = 12.345679
d3 = 123.45679
d4 = 1234.5679
d5 = 12345.679

default display
d1 = 1.2345679
d2 = 12.345679
d3 = 123.45679
d4 = 1234.5679
d5 = 12345.679

setbase(16)
l1 = 10
l2 = 100
l3 = 400
l4 = 1000
l5 = 10000

setbase(8)
l1 = 20
l2 = 400
l3 = 2000
l4 = 10000
l5 = 200000

setbase(10)
l1 = 16
l2 = 256
l3 = 1024
l4 = 4096
l5 = 65536


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-10-31 12:11
hegj
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-9-5
收藏
得分:0 
收获不少,感谢

2007-10-31 12:47
nofarewell
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-5-14
收藏
得分:0 

谢谢啊!原来是要包含iomanip头文件!

2007-10-31 14:45
negatlov
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-10-29
收藏
得分:0 

有好多东西要我学的啊!


我爱你--老婆
2007-10-31 17:21
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 
没头文件,呵呵~

★孤独的人是可耻的★
2007-11-01 23:09
快速回复:关于setw的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026575 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved