[C++]2ºÎ°­ÁÂ(60)<--manipulator





´Ù. manipulator

manipulator¶õ ÀÔÃâ·Â Çü½ÄÀ» Á¶ÀÛÇÏ´Â ÇÔ¼öµéÀÌ´Ù. manipulator´Â ÀϺΠiost
ream.h¿¡ Á¤ÀǵǾî ÀÖ´Â °Íµµ ÀÖÁö¸¸ ´ëºÎºÐÀº iomanip.h¿¡ Á¤ÀǵǾî ÀÖÀ¸¹Ç·Î
¹Ýµå½Ã iomanip.h¸¦ Æ÷ÇÔ½ÃÅ°µµ·Ï ÇÑ´Ù.
manipulatorÀÇ Á¾·ù¿¡´Â ´ÙÀ½°ú °°Àº °ÍµéÀÌ ÀÖ´Ù.

-------------------------------------------------------------------------
manipulator      ÀÇ ¹Ì
-------------------------------------------------------------------------
dec            ¼ýÀÚ µ¥ÀÌÅ͸¦ 10Áø¼ö·Î Ãâ·ÂÇÑ´Ù.
oct            ¼ýÀÚ µ¥ÀÌÅ͸¦ 8Áø¼ö·Î Ãâ·ÂÇÑ´Ù.
hex            ¼ýÀÚ µ¥ÀÌÅ͸¦ 16Áø¼ö·Î Ãâ·ÂÇÑ´Ù.
endl           '\n'À» Ãâ·ÂÇÏ°í ½ºÆ®¸²À» ºñ¿î´Ù.
ends           ¹®ÀÚ¿­ÀÇ ³¡¿¡ NULL ¹®ÀÚ¸¦ Ãß°¡ÇÑ´Ù.
flush          ½ºÆ®¸²À» ºñ¿î´Ù.
setbase(int n) Áø¹ýÀ» ÁöÁ¤ÇÑ´Ù. nÀº 8,10,16ÀÌ °¡´ÉÇÏ¸ç ±× ¿ÜÀÇ ³ª¸ÓÁö ¼ý
            ÀÚ°¡ ¿À¸é 10Áø¼ö·Î °¡Á¤ÇÑ´Ù.
resetiosflags(long n)  n¿¡¼­ ÁöÁ¤ÇÑ ºñÆ®¸¦ resetÇÑ´Ù.
setiosflags(liong n)   n°ú x_flags¸¦ OR½ÃÄÑ n¿¡¼­ ÁöÁ¤ÇÑ ºñÆ®¸¦ setÇÑ´Ù.
setfill(int n)      fill ¹®ÀÚ¸¦ ÁöÁ¤ÇÑ´Ù.
setw(int n)         µ¥ÀÌÅÍ°¡ Ãâ·ÂµÉ È­¸éÀÇ ÆøÀ» nÀ¸·Î ÁöÁ¤ÇÑ´Ù.
setprecision(int n) ½Ç¼öÀÇ À¯È¿¼ýÀÚ ÀÚ¸®¼ö¸¦ ÁöÁ¤ÇÑ´Ù. ÁöÁ¤ÇÑ À¯È¿¼ýÀÚ ÀÚ
                 ¸®¿¡¼­ ¹Ý¿Ã¸²µÈ´Ù.
-------------------------------------------------------------------------

¹º°¡ º¹ÀâÇغ¸ÀÌÁö¸¸ Çѹø¾¿ »ç¿ëÇغ¸¸é ¾î·ÆÁö ¾Ê´Ù. manipulator Áß¿¡´Â ios
ÀÇ ¸â¹ö ÇÔ¼ö¿Í ±â´É¸é¿¡¼­ Áߺ¹µÇ´Â °Íµéµµ ÀÖ´Ù. setw´Â ios::width¿Í °°°í
setfillÀº ios::fill°ú ±× ±â´ÉÀÌ °°´Ù. Â÷ÀÌÁ¡À̶ó¸é width, fillÀº °³º°ÀûÀ¸
·Î ½ÇÇàÀ» ½ÃÄÑ¾ß ÇÏ°í manipulator´Â << ¿¬»êÀÚÀÇ ÇÇ¿¬»êÀÚ·Î »ç¿ëµÉ ¼ö ÀÖ´Ù
´Â °ÍÀÌ´Ù. ´ÙÀ½¿¡ manipulator¸¦ »ç¿ëÇÏ´Â ¿¹Á¦¿Í ½ÇÇà °á°ú¸¦ º¸ÀδÙ.

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
double x=3.1415;
int i=16;
clrscr();
cout << x << endl;
cout << setw(10) << setfill('0') << x << endl;
cout << setprecision(3) << x << endl;
cout << "dec= " << setbase(10) << i << endl;
cout << "hex= " << setbase(16) << i << endl;
cout << "oct= " << setbase(8) << i << endl;
}

½ÇÇà °á°ú

3.1415
00003.1415
3.142
dec= 16
hex= 10
oct= 20

½ÇÇà °á°ú¸¦ Àß »ìÆ캸°í ¿¹Á¦¿¡ »ç¿ëµÈ manipulator¸¦ Á÷Á¢ »ç¿ëÇØ º¸¾Æ¶ó.


¶ó. Á¿ì Á¤·Ä

Ãâ·ÂµÉ µ¥ÀÌÅÍ°¡ Â÷ÁöÇÒ ÆøÀÌ µ¥ÀÌÅÍÀÇ ±æÀ̺¸´Ù ´õ ±æ °æ¿ì´Â µ¥ÀÌÅ͸¦ ÁÂÃø
Á¤·ÄÇÒ °ÍÀÎÁö ¿ìÃø Á¤·ÄÇÒ °ÍÀÎÁö µÎ °¡Áö¸¦ ¼±ÅÃÇØ¾ß ÇÑ´Ù. ÀÌ·¯ÇÑ Á¤·Ä Á¤
º¸´Â ios Ŭ·¡½ºÀÇ µ¥ÀÌÅÍ ¸â¹ö x_flags¿¡ º¸°üµÇ¸ç x_flagsÀÇ Á¤º¸¸¦ Á¶ÀÛÇÏ
´Â setiosflags¿Í resetiosflags°¡ ÁغñµÇ¾î ÀÖ´Ù.

* setiosflags(long n);
nÀÇ °ªÀÌ 1ÀÎ ºñÆ®¿Í ´ëÀÀµÇ´Â x_flagsÀÇ ºñÆ®¸¦ 1·Î ¸¸µé¾î ÁØ´Ù. Áï ±× ºñ
Æ®°¡ °¡Áø Àǹ̸¦ È°¼ºÈ­½ÃŲ´Ù´Â ¶æÀÌ´Ù. setiosflags(0x0014)´Â x_flagsÀÇ b
2,b4¸¦ 1·Î ¸¸µé¸ç À̶§ Ãâ·Â Çü½ÄÀº 10Áø¼öÀÇ ¿À¸¥ÂÊ Á¤·ÄÀÌ µÈ´Ù.
* resetiosflags(long n);
nÀÇ °ªÀÌ 1ÀÎ ºñÆ®¿Í ´ëÀÀµÇ´Â x_flagsÀÇ ºñÆ®¸¦ 0À¸·Î ¸¸µé¾î ÁØ´Ù. Áï ±×
ºñÆ®°¡ °¡Áø Àǹ̸¦ È°¼ºÈ­½ÃÅ°Áö ¾Ê´Â´Ù´Â ¶æÀÌ´Ù.

setiosflagsÀÇ Àμö·Î »ç¿ëÇϱâ À§ÇÑ ¿­°ÅÇüÀÌ iostream.h¿¡ ´ÙÀ½°ú °°ÀÌ Á¤
ÀǵǾî ÀÖÀ¸¸ç ÀÌ ¿­°ÅÇüÀ» »ìÆ캸¸é x_flagsÀÇ °¢ ºñÆ®°¡ ¾î¶°ÇÑ ÀǹÌÀÎÁö ¾Ë
¼ö ÀÖ´Ù.

// formatting flags
enum {
 skipws = 0x0001,     // skip whitespace on input
 left = 0x0002,       // left-adjust output
 right = 0x0004,      // right-adjust output
 internal = 0x0008,   // padding after sign or base
 indicatordec = 0x0010, // decimal conversion
 oct = 0x0020,     // octal conversion
 hex = 0x0040,     // hexadecimal conversion
 showbase = 0x0080,        // use base indicator on
 outputshowpoint = 0x0100, // force decimal point (floating output)
 uppercase = 0x0200,       // upper-case hex output
 showpos = 0x0400,         // add '+' to positive
 integersscientific =  0x0800, // use 1.2345E2 floating
 notationfixed = 0x1000,       // use 123.45 floating
 notationunitbuf = 0x2000, // flush all streams after
 insertionstdio = 0x4000   // flush stdout, stderr after insertion
};

´ÙÀ½ ¿¹Á¦´Â setiosflags¸¦ »ç¿ëÇØ ¹®ÀÚ¿­À» Á¿ì·Î Á¤·Ä½ÃÄÑ Ãâ·ÂÇغ» °ÍÀÌ
´Ù.

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
char str[]="string";
clrscr();
cout.setf(ios::left);            // ÁÂÃø Á¤·Ä
cout << setw(10) << str << endl;
cout.setf(ios::right);           // ¿ìÃø Á¤·Ä
cout << setw(10) << str << endl;
cout.setf(ios::hex);             // 16Áø Ãâ·Â
cout.setf(ios::showbase);        // 16Áø Ç¥½ÄÀ» º¸ÀδÙ.
cout.setf(ios::left);            // ÁÂÃø Á¤·Ä
cout.setf(ios::uppercase);       // 16Áø¼ö¿¡ ´ë¹®ÀÚ »ç¿ë
cout << setw(10) << 12 << endl;
}

Ãâ·Â °á°ú

string
 string
0XC

¶ÇÇÑ showbase,uppercase µîÀ» »ç¿ëÇØ ¼ýÀÚ µ¥ÀÌÅÍÀÇ base(Áø¹ý)¸¦ Ãâ·ÂÇϵµ
·Ï ÇÏ°í 16Áø¼ö¿¡ »ç¿ëµÇ´Â ¿µ¹®ÀÚ¸¦ ´ë¹®ÀÚ·Î Ãâ·ÂµÇµµ·Ï ÇÏ¿´´Ù.

À̾ °è¼ÓµË´Ï´Ù. ==============>>>>>>>


--------------------------------------------------------------------------------

    

 ´ÙÀ½