C++中cout.width(4)ってどういう意味ですか?

9048 ワード

           4,         。       “12”,                “ 12”。        ! 

cin cout 
 :      cin 
              ,           --  。      ,             。   :cin>>  ; 
       ,            , :cin>>x>>y>>z; 
        ,     ,          ,         。   ,            ,  ,          ,      
      ,   ×××,        ,           。 
  ,            "&" ,         ,     scanf  。                。 : 

int i; 
cout<<"please input a number:" 
cin>>i; 
cout<<"i="<<i<<endl; 

            'a'       ,            ,         。     scanf  ,                ,         。  : 

/*                 */ 
#include <iostream.h> 
main() 
{ 
int i; 
while(i!=-1) 
{ 
cout<<"i=" 
cin>>i; /*          'a'  */ 
cout<<endl; 
} 
} 

      ,         ,        。        , cin>>i;            ,  ,             ,      。 
cin          。        : 

/*                   */ 
#include <iostream.h> 
main() 
{ 
char str[20]; 
cout<<"please input a string:"; 
cin>>str; /*     "hello word"*/ 
cout<<endl<<"str="<<str; 
} 

         ?      str=hello,    ?  cin        ,         ,                , 
           。  ,                ,        。  ,         ,          ,      。 

 、      cout 
 cout   ,  cin  ,      。           --   。                。                     ,              , printf     。 
  ,     16  ,8   10          ,  : 

/*          */ 
#include<iostream.h> 
void main() 
{ 
int x=30, y=300, z=1024; 
cout<<x<<' '<<y<<' '<<z<<endl; //       
cout.setf(ios::showbase | ios::uppercase); //                    
cout<<x<<' '<<y<<' '<<z<<endl; 
cout.unsetf(ios::showbase | ios::uppercase); //                    
cout.setf(ios::oct); //        ,           
cout<<x<<' '<<y<<' '<<z<<endl; //       
cout.setf(ios::showbase | ios::uppercase); //                    
cout<<x<<' '<<y<<' '<<z<<endl; 
cout.unsetf(ios::showbase | ios::uppercase); //                    
cout.unsetf(ios::oct); //         ,         
cout.setf(ios::hex); //          
cout<<x<<' '<<y<<' '<<z<<endl; 
cout.setf(ios::showbase | ios::uppercase); //                    
cout<<x<<' '<<y<<' '<<z<<endl; 
cout.unsetf(ios::showbase | ios::uppercase); //                    
cout.unsetf(ios::hex); //          ,         
cout<<x<<' '<<y<<' '<<z<<endl; 
} 

   cout.setf()       , cout.unsetf()    。    10                   ios:: 
showbase,   ,8            0, 16       0X。            ,  16    ,          
    。  ,        ,                ,         ,  , 

/*          */ 
#include<iomanip.h> 
void main() 
{ 
int x=30, y=300, z=1024; 
cout<<x<<' '<<y<<' '<<z<<endl; //       
cout<<oct<<x<<' '<<y<<' '<<z<<endl; //       
cout<<setiosflags(ios::showbase); //       
cout<<x<<' '<<y<<' '<<z<<endl; //        
cout<<resetiosflags(ios::showbase); //       
cout<<hex<<x<<' '<<y<<' '<<z<<endl; //        
cout<<setiosflags(ios::showbase | ios::uppercase); 
//                 , 
cout<<x<<' '<<y<<' '<<z<<endl; //         
cout<<resetiosflags(ios::showbase | ios::uppercase); 
//                  
cout<<x<<' '<<y<<' '<<z<<endl; //         
cout<<dec<<x<<' '<<y<<' '<<z<<endl; //       
} 

                  ,      。            : 

    
1.1    C  ...........................1 
1.11 C     ..........................58 
    

      ,       : 

/*      ,  ,       */ 
#include <iostream.h> 
void main() 
{ 
cout<<"   "<<endl; 
cout<<" "; 
cout.setf(ios::left); //       left 
cout.width(7); //     7,        
cout<<"1.1"; 
cout<<"   C  "; 
cout.unsetf(ios::left); //      ,   right   
cout.fill('.'); //       
cout.width(30); //    ,         
cout<<1<<endl; 
cout<<" "; 
cout.width(7); //     
cout.setf(ios::left); //       left 
cout.fill(' '); //    ,      
cout<<"1.11"; 
cout<<"C     "; 
cout.unsetf(ios::left); //       
cout.fill('.'); 
cout.width(30); 
cout<<58<<endl; 
cout.fill(' '); 
cout<<"   "<<endl; 
} 

         ,            ,        ,              ,     。          。                 。 

/*      ,  ,       */ 
#include <iomanip.h> 
void main() 
{ 
cout<<"   "<<endl; 
cout<<" "; 
cout<<setiosflags(ios::left)<<setw(7); //     7,left     
cout<<"1.1"; 
cout<<"   C  "; 
cout<<resetiosflags(ios::left); //       
cout<<setfill('.')<<setw(30)<<1<<endl; //   30,   '.'   
cout<<setfill(' '); //        
cout<<" "; 
cout<<setw(7)<<setiosflags(ios::left); //     7,left     
cout<<"1.11"; 
cout<<"C     "; 
cout<<resetiosflags(ios::left); //       
cout<<setfill('.')<<setw(30)<<58<<endl; //   30,   '.'   
cout<<setfill(' ')<<"   "<<endl; 
} 
          ,       ,                 。              ,   : 

/*        */ 
#include <iostream.h> 
void main() 
{ 
float f=2.0/3.0,f1=0.000000001,f2=-9.9; 
cout<<f<<' '<<f1<<' '<<f2<<endl; //     
cout.setf(ios::showpos); //       +  
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout.unsetf(ios::showpos); //      +  
cout.setf(ios::showpoint); //           0 
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout.unsetf(ios::showpoint); //           0 
cout.setf(ios::scientific); //      
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout.unsetf(ios::scientific); //        
cout.setf(ios::fixed); //       
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout.unsetf(ios::fixed); //         
cout.precision(18); //   18,   6 
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout.precision(6); //     6 
} 
  ,                  : 

/*        */ 
#include <iomanip.h> 
void main() 
{ 
float f=2.0/3.0,f1=0.000000001,f2=-9.9; 
cout<<f<<' '<<f1<<' '<<f2<<endl; //     
cout<<setiosflags(ios::showpos); //       +  
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout<<resetiosflags(ios::showpos); //      +  
cout<<setiosflags(ios::showpoint); //           0 
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout<<resetiosflags(ios::showpoint); //           0 
cout<<setiosflags(ios::scientific); //      
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout<<resetiosflags(ios::scientific); //        
cout<<setiosflags(ios::fixed); //       
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout<<resetiosflags(ios::fixed); //         
cout<<setprecision(18); //   18,   6 
cout<<f<<' '<<f1<<' '<<f2<<endl; 
cout<<setprecision(6); //     6 
} 

 c/c++             ,           。       getch(),getche(), 
getchar 
(),cin.get(),putch(),putchar(),cout.put(),gets(),cin.getline(),puts()。   
                          , :cin.putback(),fflush(stdin),cout.flush().   
        。 
1、getch() getche(),      ,         。getch()       。 conio.h  。 
2、cin.get(),getchar(),     ,         ,   。getchar() stdio.h  ,cin.get() iostream.h  。 
3、putch() putchar(),      ,          。putch() conio.h  ,putchar() stdio.h  。 
4、cout.put(),     ,          。 iostream.h  。 
5、gets() cin.geline(),     ,      (    ,        ),gets() stdio.h  ,cin.getline() iostream.h  。 
6、puts(),     ,       , stdio.h  。 
7、cin.putback(),            。 
8、fflush(stdin),         。    cin.get()      。 
9、cout.flush(),       。 
            /     ,          io           。            ,        。     : 
1、      
a,       ,      。 
b,      。 
c,         。 
d,          
e,      
2、      
a,        
b,          
c,            
d,    。 
                   ,     scanf(),        ,               。            。