SlideShare una empresa de Scribd logo
1 de 11
Descargar para leer sin conexión
!

                                     บทที่ 2
                             การแสดงผลและการรับขอมูล

♦! วามหมายของการแสดงผล
 ค
       การแสดงผล หมายถึง การสงใหคอมพวเตอรนําขอมูลและผลลัพธที่มีอยูในหนวยความ จําไป
                               ่ั       ิ    
แสดงผลออกที่อุปกรณแสดงผล (output device) ของคอมพิวเตอร การแสดงผลที่อุปกรณแสดงผลอาจมี
เพยงอปกรณเ ดยว หรอหลาย ๆ อุปกรณพรอมกันก็ได เชน แสดงผลที่จอภาพ เครองพมพ(Printer)
   ี ุ       ี      ื                                                    ่ื ิ 
ลําโพง แผนดิสก เปนตน


♦! ารแสดงผลทางจอภาพดวย cout
 ก                  
         จากรูปแบบโครงสรางของโปรแกรม C++ ในบทที่ 1 จะเห็นตัวอยางการใชประโยคคําสั่ง cout
(อานวา c - out ซีเอาต ยอมาจาก character out) หมายถึง การแสดงผลในลักษณะอักษรหรือขอความ
ทีอยูในเครืองหมาย "…… " หรือคาของตัวแปร(variable)ออกทางจอภาพ
  ่        ่
         cout เปนออบเจ็กต(object) อยูในไฟล iostream.h ซึ่ง cout จะเปน object ที่ทาหนาทดาเนน
                                                                                      ํ  ่ี ํ ิ
การเกี่ยวกับกระแสขอมูล(Stream) ของภาษา C++ ออกไปแสดงผลสูอุปกรณตาง ๆ
         << เปน operator หรอตวดาเนินการ มชอวา put to หรอสงไปท่ี หรอเรยกวา insertion
                                 ื ั ํ          ี ่ื              ื           ื ี 
หมายถึง การแสดงขอความ เครืองหมาย << จะทําหนาทนาคาทอยทางขวาของเครองหมายซงอาจจะ
                                ่                        ่ี ํ  ่ี ู              ่ื        ่ึ
เปนคาคงที่ ขอความหรอ string ทีอยูในเครืองหมาย "……" หรอคาตวแปร(variable) ก็ได สงใหแก
                        ื          ่     ่                     ื  ั
Object ที่อยูทางซายของเครื่องหมาย
         รูปแบบการแสดงผลขอความทางจอภาพ โดยใช cout << มดงน้ี      ีั

                  ! cout << " ขอความ " << "ขอความ " << ตัวแปร;
                  ! cout << ตัวแปร<<"ขอความ"<<ตัวแปร<<…;



           •! ตัวอยางโปรแกรม cout_exp.cpp แสดงการใช cout << แสดงขอความออกทาง
              จอภาพ



ศิริชัย นามบุรี                                                             การแสดงและการรับขอมูล
!
!23

/* Program : cout_exp.cpp
   Process : display cout object in iostream.h */
#include <iostream.h>
void main()
{
  cout << "This is Turbo C++ Program ";
  cout<< "C++ is high language";
}

       จากตวอยางโปรแกรมน้ี cout จะทําหนาที่สงกระแสขอมูลขอความ This is Turbo C++
           ั 
Program C++ is high language ไปแสดงที่จอภาพ ณ ตําแหนงปจจบนท่ี cursor ชี้อยู
                                                      ุ ั

         •! โปรแกรม cout_ex2.cpp แสดงขอความตอกันโดยใช << ออกทางจอภาพ
/* Program : cout_ex2.cpp
   Process : display cout object in iostream.h
*/
#include <iostream.h>
void main()
{
  cout<< "This is Turbo C++ Program."<<" "<<" It is very easy.";
  cout<< "I love C++."<<" "<<"It's high level language.";
}

           จากโปรแกรม cout_ex2.cpp จะแสดงขอความตอไปนี้บนจอภาพ
           This is Turbo C++ Program. It is very easy. I love C++. It's high level language.


♦! ําแหนงแสดงผลทจอภาพดวย endl
 ต             ่ี    

          1. การใช endl เปนโอเปอเรเตอรประเภทตัวผสม (manipulator) ทาหนาทเ่ี ลอนเคอรเ ซอร
                                                                    ํ  ่ื
เพื่อขึ้นบรรทัดใหมและการแสดงผลขอความที่ตามมาจะขึ้นบรรทัดใหมดวย
          •! ตัวอยางโปรแกรม endl_exp.cpp แสดงขอความ และกําหนดใหแสดงขอความขึ้น
             บรรทัดใหมดวย manipulator คือ endl
/* Program : endl_exp.cpp
  Process : display cout object in iostream.h
*/
#include <iostream.h>
#include <conio.h>
void main()
{ clrscr(); //clear screen standard function from conio.h
ศิริชัย นามบุรี                                                                    การแสดงและการรับขอมูล
!24
    cout << "This is Turbo C++ Program"<<endl;
    cout << "It is very easy."<<endl;
    cout <<endl<<endl; // space 2 line
    cout << "I like C++ Program"<<endl<<endl;
    cout << "Press any key to continue..."<<endl;
    getch(); //get character standard function from conio.h
}
         2. การใช setw(n) ทาหนาที่กําหนดความกวางของตัวเลขหรือขอความกอนที่จะแสดงผลใหมีคา
                             ํ
เทากับ n อกษร เรยกคา n วาเปน อารกิวเมนต (argument) เพื่อใชในการแสดงผลที่จอภาพในลักษณะชิด
           ั      ี   
ขวา โดยตองเรียกใช Header file ที่ชื่อ iomanip.h ในขณะ compile ดวย ดังตัวอยางตอไปนี้
                 cout << setw(10) << "World";
         หมายถึง กําหนดใหคาวา World มีความกวางในการแสดงผล 10 ตําแหนง โดยนับจากดานซาย
                             ํ                                             
ของจอภาพ อกษร d จะเรมพมพตําแหนงคอลัมนที่ 10 และเริ่มพิมพอักษร lrow มาทางซายคอลัมนที่
              ั           ่ิ ิ 
9,8,7 และ 6
         •! ตัวอยางโปรแกรม setw.cpp เปนการใช setw กําหนดความกวางในการแสดงผล เพอให   ่ื
             แสดงผล ณ ตําแหนงกลางจอภาพ
/* Program : setw.cpp
  Process : display set width of display to screen
*/
#include <iostream.h>
#include <conio.h>
#include <iomanip.h> //header file of setw()
void main()
{ clrscr(); //clear screen standard function from conio.h
   cout << setw(50)<<"This is Turbo C++ Program"<<endl;
   cout << setw(45)<< "It is very easy."<<endl;
   cout <<endl<<endl; // space 2 line
   cout << setw(45)<< "I like C++ Program"<<endl<<endl;
   cout << setw(50)<<"Press any key to continue..."<<endl;
   getch(); //get character standard function from conio.h
}




ศิริชัย นามบุรี                                                           การแสดงและการรับขอมูล
!25

♦! ารแสดงผลการคํานวณทางคณตศาสตร
 ก                       ิ
           •! ตัวอยางโปรแกรม cout_mat.cpp แสดงการใช cout เพื่อแสดงผลของการคํานวณทาง
              คณิตศาสตร

/* Program : cout_mat.cpp
   Process : Display mathmetic calculate result*/
#include <iostream.h>
#include <conio.h>
void main()
{ int number1,number2;
  float x,y,z;
  // set value of variable
  number1=20;
  number2=30;
  x=25.25; y=30.05; z=10.75;
  // display calculation result
  clrscr();
  cout<< "Program Display Mathmetic calculation"<<endl;
  cout<< "number1 = " << number1 <<endl;
  cout<< "number2 = " << number2 <<endl;
  cout<< "number1 + number2 = "<<number1+number2 <<endl;
  cout<< "number1 - number2 = "<<number1-number2 <<endl;
  cout<< "x+y+z = "<< x+y+z << endl;
  cout<< "x*y*z = " << x*y*z <<endl;
  cout<< "50*25*12.5 = " << 50*25*12.5 << endl;
  getch();
}

           •! ตัวอยางโปรแกรม set_dec.cpp การใช manipulator ทชอ setprecision(n) รวมกบ cout<<
                                                              ่ี ่ื                ั
              เพื่อกําหนดความละเอียดของตาแหนงทศนยมของจานวนจริง โดยที่ n คือจํานวน
                                         ํ        ิ       ํ
              ตําแหนงทศนยม
                         ิ

/*Program : set_dec.cpp
 process : set decimal precision of float number
*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>


ศิริชัย นามบุรี                                                           การแสดงและการรับขอมูล
!26

void main()
{ float A=125.25125;
 float B=10.7525;
 float C=212.15;
 clrscr(); //function in conio.h for clear screen
 cout<<"Display set precision of float number ...."<<endl;
 cout<<"A+B = "<<setprecision(10)<<A+B<<endl;
 cout<<"A*B*C = "<<setprecision(15)<<A*B*C<<endl;
 cout<<"A*B = "<<setprecision(5)<<A*B<<endl;
 cout<<"A+B+C = "<<setprecision(3)<<A+B+C<<endl;
 cout<<"A+B+C = "<<setprecision(2)<<A+B+C<<endl;
 cout<<"A/B = "<<setprecision(1)<<A/B<<endl;
 getch();

♦! ารคํานวณโดยใชฟงกชนทางคณตศาสตร
 ก                 ั   ิ
!        การคานวณคาฟงกชันทางคณิตศาสตร เชน คายกกาลัง คารากที่สอง คาสัมบูรณ จะตองใช
             ํ                                           ํ
ฟงกชันมาตรฐาน (standard function) ทางดานคณตศาสตร ที่ C++ จดเตรยมไวใหใน
                                                       ิ                         ั ี  
โดยจัดเก็บคลังคําสั่งไวในไฟล Math.h ดงนนในโปรแกรมทตองการใชฟงกชนทางคณตศาสตรจะตอง
                                        ั ้ั                  ่ี       ั       ิ        
มีการ include ไฟล Math.h เปน preprocessor directive ดวย (วิธีการตรวจสอบวามีฟงกชันใดบางใน
                              
ไฟล Math.h ใหใชคาสั่ง Help , Index แลวพิมพ คําวา Math.h เมื่อพบคําวา Math.h แลวใหกดแปน
                      ํ
Enter จะแสดงใหเห็นชื่อฟงกชันตาง ๆ ทางดานคณตศาสตร)
                                                ิ          
!
           •! ตัวอยางโปรแกรม Func_Mat.cpp แสดงการหาคาทางคณิตศาสตรโดยใชฟงกชัน pow()
              และฟงกชัน sqrt() เพื่อหาคาเลขยกกําลังและรากที่สอง

/*Program : Func_Mat.Cpp
 Process : Display using mathmetic function
*/
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
  double x = 9.0, y = 2.0;
  clrscr();
  cout<<"Display using mathmetic functions ...a"<<endl<<endl;
  cout<<x<<" power by "<<y<<" = "<< pow(x, y)<<endl;
  cout<<" SquareRoot "<<x<<" = "<< sqrt(x)<<endl;
ศิริชัย นามบุรี                                                            การแสดงและการรับขอมูล
!27

    cout<<"(x power y)*(sqrt(y)) = "<<setprecision(3)<<pow(x,y)*(sqrt(y));
    cout<<endl<<endl<<"Press any key to exit...";
    getch();
}
!

!

♦! ารแสดงผลดวยการใช escape sequence
 ก
      escape sequence เปนรหสอกขระแบบคอนสแตนต(constant) ชนิดอักษร(character) ซึ่ง
                         ั ั
ประกอบดวยเครืองหมาย  (backslash) และตัวอักษร อยภายในเครองหมาย ' ' เชน 'n' 't' ทาหนาที่
              ่                                  ู      ่ื                       ํ
จดรปแบบการแสดงผลรวมกบ cout เหมอนกับ endl รายละเอยดดงตารางตอไปน้ี
 ั ู                  ั           ื                 ี ั       
ตาราง แสดง escape sequence และความหมาย
       escape sequence                  ชื่อ                          ความหมาย
              a             alarm bell                 สงเสียงบิ๊ป (beep)
              b             backspace                  เหมอนการกดคย backspace
                                                            ื            ี
              f             formfeed                   ขึ้นหนาใหม
              n             newline                    ขึนบรรทัดใหม
                                                          ้
              r             return                     เหมอนการกดคย enter
                                                              ื            ี
              t             tab                        เหมอนการกดคย tab
                                                                ื            ี
                           backslash                  แสดงเครองหมาย 
                                                                  ่ื
               '            single quote               แสดงเครองหมาย '
                                                                  ่ื
              "             double quote               แสดงเครองหมาย "
                                                                  ่ื
              ?             Question mark              แสดงเครองหมาย ?
                                                                  ่ื

           •! ตัวอยางโปรแกรม escape.cpp แสดงการใชเครื่องหมาย escape sequence รวมกบ cout
                                                                                 ั
              สําหรบการแสดงผล
                   ั

/*Program : escape.cpp
 Process : display with escape sequence
*/
#include <iostream.h>
#include <conio.h>
void main()
{ clrscr(); //clear screen
  cout<< "Hello Program C++" << 'n'; //new line

ศิริชัย นามบุรี                                                              การแสดงและการรับขอมูล
!28

    cout<< "C++ is very easy a" <<'n'<<'a'; //new line and bell
    cout<< "ttC++ is very interest language n"; // 2 tab and newline
    cout<< "tC++ is OOP Languageb n"; //tab, backspace and new line
    getch(); //wait for press any key
}

♦! ารรบขอมลจากคบอรดดวย cin
 ก ั  ู       ี  
        ในการเขียนโปรแกรมเพือการประมวล มีความจําเปนอยางยิ่งที่โปรแกรมดวยทั่วไป
                               ่
จะตองมีการรับขอมูลจากผูใช (user) ผานทางคยบอรดหรอแปนพมพ เพื่อความยืดหยุนในการใชงาน
                                           ี  ื  ิ
โปรแกรม ใน C++ สามารถใชออปเจ็กต cin ที่อยูในไฟล iostream.h เพื่อรับขอมูลจากคียบอรดและ
อุปกรณอน ๆ ได มรปแบบดงน้ี
         ่ื       ีู        ั

                    ! cin >> ชอตวแปร;
                               ่ื ั
                    ! cin>>ชอตวแปร>>ชอตวแปร>>ชอตวแปร>>…;
                             ่ื ั     ่ื ั    ่ื ั


         โดยที่ cin อานวา ซีอิน ยอมาจาก character in ซึ่งหมายถึงการรับขอมูลในลักษณะ
ของอักษร cin เปนออปเจ็กตที่สรางอยูในไฟล iostream.h
         >> เปนโอเปเรเตอร ซึ่งมีชื่อวา เอกซแทรกชน (extraction - รับเขามา) หรอ get from จะทํา
                                              ็ ั                               ื
หนาที่รับคาที่อยูทางซายของเครื่องหมายสงใหแกตัวแปรที่อยูทางขวาของเครื่องหมาย(ถาไมระบุอุปกรณ
หมายถึงรับขอมูลเขาทางคียบอรด)
         •! ตัวอยางโปรแกรม cin_exp.cpp แสดงการรบคาจานวนตัวเลขและตัวอักษรทางแปนพิมพ
                                                        ั  ํ
             เพื่อเก็บไวในตัวแปร โดยใช cin>>

/* Program : cin_exp.cpp
  Process : input number and character to variable, and display value*/
#include <iostream.h>
#include <conio.h>
void main()
{ int number1,number2; //declared integer variable
 char ch;          //declared character variable
 //start statement
 clrscr();
 cout<< " Please Enter number and character : n";
 cout<< " Enter number1 : ";
 cin>>number1; //enter integer from keyboard
 cout<< "nEnter number 2 : ";
ศิริชัย นามบุรี                                                                การแสดงและการรับขอมูล
!29

    cin>>number2; //enter integer from keyboard
    cout<< "nEnter 1 character : ";
    cin>>ch;         //enter 1 character from keyboard
    cout<< "nnPress any key to display...";
    getch(); //wait press any key
    clrscr();
    // process display value from variable
    cout<< "You enter number and character : na";
    cout<< "Value of number1 : " <<number1;
    cout<< "nValue of number 2 : " <<number2;
    cout<< "nValue of character : " <<ch;
    cout<< "nnPress any key to exit...";
    getch(); //wait press any key
}

           •! ตัวอยางโปรแกรม area_cir.cpp แสดงการคํานวณพื้นที่วงกลมและแสดงผลลัพธ
              ออกมาทางจอภาพ โดยรับคารัศมีของวงกลมจากคียบอรดกําหนดใหมตวแปรประเภท
                                                                          ี ั
              float และมี constant ในโปรแกรมดวย
/*Program : area_cir.cpp
 Process : calculate circle area by input radius
*/
#include <iostream.h>
#include <conio.h>

void main()
{ float rad; //declared real variable
  const float PI = 3.14159; // defined constant PI
  //start statement
  clrscr();
  cout<< "Please enter radius of circle : ";
  cin>>rad;
  float area = PI*rad*rad; //declared variable at position that want to use
  cout<< "nnRadius of circle = "<<rad;
  cout<< "nnArea of circles = an"<<area;
  getch();
}

           วิธีการกําหนดใหโปรแกรมรบขอมลหลาย ๆ ตัวแปร ตอเนองกนมวธการเขยนประโยค ดังนี้
                                 ั  ู                ่ื ั ี ิ ี   ี
                  ! cin >> number1 >> number2 >> number3;


ศิริชัย นามบุรี                                                               การแสดงและการรับขอมูล
!30

           วิธีปอนขอมูลเมื่อ run โปรแกรม มี 2 วิธี คือ
           - กรอกเลข 3 จานวน แตละจานวนเวนวรรค แลว Enter ทจานวนสุดทาย เชน
                             ํ           ํ                ่ี ํ           
             50 30 40 <Enter> หรือ
           - กรอกเลขแตละจํานวน แลวกดแปน Enter ตาม เชน 
                    50 <Enter>
                    30 <Enter>
                    40 <Enter>

           •! ตัวอยางโปรแกรม cin_cout.cpp แสดงการกรอกขอมูลตัวเลข 3 จานวน ทางแปนพิมพ
                                                                      ํ

/* Program : cin_cont.cpp
  Process : input 3 number and display value*/
#include <iostream.h>
#include <conio.h>

void main()
{ int number1,number2,number3; //declared 3 integer variable
  //start statement
  clrscr();
  cout<< "Please enter 3 integer number : n";
  cin>>number1>>number2>>number3; //enter 3 amount integer from keyboard
  cout<< "nPress any key to display...";
  getch(); //wait press any key
  clrscr();
  // process display value from variable
  cout<< "You enter 3 number : na";
  cout<< "Value of number1 : " <<number1;
  cout<< "nValue of number 2 : " <<number2;
  cout<< "nValue of number 3 : " <<number3;
  cout<< "nnPress any key to exit...";
  getch(); //wait press any key
}




♦! ารใช manipulator เพอเปลยนเลขฐาน
 ก                     ่ื ่ี
ศิริชัย นามบุรี                                                            การแสดงและการรับขอมูล
!31

       ในการเปลี่ยนการแสดงผลเลขฐานตาง ๆ ใน C++ ไดแก ฐานสิบ(Decimal) ฐานแปด (Octal)
ฐานสิบหก(Hexadecimal) เราสามารถใช แมนิพวเลเตอร (manipulator) รวมกับ cout เพือแสดงจํานวน
                                                                              ่
เลขฐานตาง ๆ ได โดยใช manipulator ตอไปน้ี
                                      
              dec ทําหนาทีเ่ ปลียนเลขฐานแปดหรือฐานสิบหกเปนเลขฐานสิบ
                                 ่
              oct ทําหนาทีเ่ ปลียนเลขฐานสิบหรือฐานสิบหกเปนเลขฐานแปด
                                     ่
              hex ทําหนาทีเ่ ปลียนเลขฐานแปดหรือฐานสิบเปนเลขฐานสิบหก
                                   ่
       ใน C++ กําหนดจํานวนเลขในฐานตาง ๆ ดังนี้
                                             
              ถามี 0 (ศนย) นําหนาจานวนเลข แสดงวาเปนจํานวนฐานแปด เชน 065,0125
                          ู             ํ
              ถามี ox (ศูนยเอกซ) นําหนาจานวนเลข แสดงวาเปนจํานวนฐานสิบหก เชน
                                               ํ
                        0x125
              ถาไมมี 0 หรอ 0x นําหนาจานวนเลข แสดงวาเปนฐานสบ เชน 50, 95
                            ื              ํ                     ิ

           •! ตัวอยางโปรแกรม base_num.cpp แสดงการใช manipulator เปลียนเลขฐาน dec, oct, hex
                                                                      ่
              ตาง ๆ

/*Program : base_num.cpp
 Process : display change base number of decimal,octal,hexadecimal*/
 #include <iostream.h>
 #include <conio.h>

 void main()
 {
   clrscr();
   cout<< "Display change base number..."<<endl<<endl;
   cout<< "10 decimal to hexadecimal = "<<hex<<10<<endl;
   cout<< "5865 decimal to hexadecimal = "<<hex<<5865<<endl;
   cout<< "1250 decimal to octal = "<<oct<<1250<<endl;
   cout<< "02342 octal to decimal = "<<dec<<02342<<endl;
   cout<< "0xabc125 hexadecimal to decimal = "<<dec<<0xabc125<<endl;
   cout<< "0xf hexadecimal to decimal = "<<dec<<0xf<<endl;
   getch();
 }

♦! บบฝกหัดทายบท
 แ

ศิริชัย นามบุรี                                                           การแสดงและการรับขอมูล
!32

1. จงเขยนโปรแกรมแสดงขอความรายละเอยดเกยวกบตวทานเอง ดงตอไปน้ี โดยใหแสดงขอ ความ
       ี                        ี ่ี ั ั         ั 
บรเิ วณกลางจอภาพ
                                                         This is C++ program.
                                                   I love to study C++ language.
                                                         My name is Mr……..
                                                            Code ………..
                                                          Major ………….
2. จากสตรการคานวณหาดอกเบีย ดังนี้
          ู           ํ                        ้
         I = Pin
         S = P+I
    โดยที่              I คือ ดอกเบย        ้ี
                        P คือ เงินตน
                        i คือ อัตราดอกเบียตอป  ้
                        n คือ จํานวนป
                        S คือ เงินรวม
    ใหเ ขยนโปรแกรมเพอคานวณหาคาดอกเบย ( I ) และเงินรวม (S) โดยใหผูใชกรอกขอมูลทาง
              ี                      ่ื ํ                    ้ี
    แปนพิมพ ทจาเปนตองใชในการคานวณ
                    ่ี ํ                          ํ
3. จงเขยนโปรแกรมเพอคานวณหาผลลัพธและแสดงผลลัพธนิพจนทางคณิตศาสตรตอไปนี้
            ี                    ่ื ํ
    โดยกําหนดให
    A , B เปนจํานวนจรง (float)
                                  ิ                      โดย A = 2.25           B = 3.0
    X,Y,Z เปนจํานวนเต็ม (integer)
                                                         โดย X = 4              Y=5     Z=8
    คํานวณหานิพจนตอไปนีในโปรแกรม โดยกําหนดการแสดงผลทศนิยม 2 ตําแหนง
                                         ้                                              
    A+B+X+Y+Z
    Z*Y/A
    (A+B)*X
    pow(A,2)
    pow(A,2)*pow(X,2)
    sqrt(X+Y+Z)
    sqrt(X*Y*Z)/B




ศิริชัย นามบุรี                                                              การแสดงและการรับขอมูล

Más contenido relacionado

La actualidad más candente

การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา CWarawut
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซีminkky04
 
การโปรแกรมDevc++
การโปรแกรมDevc++การโปรแกรมDevc++
การโปรแกรมDevc++minkky04
 
คำสั่งรับข้อมูลจากคีย์บอร์ด
คำสั่งรับข้อมูลจากคีย์บอร์ดคำสั่งรับข้อมูลจากคีย์บอร์ด
คำสั่งรับข้อมูลจากคีย์บอร์ดเทวัญ ภูพานทอง
 
PHP Tutorial (introduction)
PHP Tutorial (introduction)PHP Tutorial (introduction)
PHP Tutorial (introduction)Tinnakorn Puttha
 
2D Graphics and Animations in Java World
2D Graphics and Animations in Java World2D Graphics and Animations in Java World
2D Graphics and Animations in Java Worldkunemata
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 
เริ่มต้นการเขียนโปรแกรมหุ่นยนต์
เริ่มต้นการเขียนโปรแกรมหุ่นยนต์เริ่มต้นการเขียนโปรแกรมหุ่นยนต์
เริ่มต้นการเขียนโปรแกรมหุ่นยนต์Wittayakorn Yasingthong
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานNookky Anapat
 

La actualidad más candente (18)

การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
12
1212
12
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
การโปรแกรมDevc++
การโปรแกรมDevc++การโปรแกรมDevc++
การโปรแกรมDevc++
 
คำสั่งรับข้อมูลจากคีย์บอร์ด
คำสั่งรับข้อมูลจากคีย์บอร์ดคำสั่งรับข้อมูลจากคีย์บอร์ด
คำสั่งรับข้อมูลจากคีย์บอร์ด
 
PHP Tutorial (introduction)
PHP Tutorial (introduction)PHP Tutorial (introduction)
PHP Tutorial (introduction)
 
compromint
compromintcompromint
compromint
 
05 Loops
05  Loops05  Loops
05 Loops
 
2D Graphics and Animations in Java World
2D Graphics and Animations in Java World2D Graphics and Animations in Java World
2D Graphics and Animations in Java World
 
C lang
C langC lang
C lang
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 
3.5 การแสดงผลและการรับข้อมูล
3.5 การแสดงผลและการรับข้อมูล3.5 การแสดงผลและการรับข้อมูล
3.5 การแสดงผลและการรับข้อมูล
 
ภาษา C#
ภาษา C#ภาษา C#
ภาษา C#
 
เริ่มต้นการเขียนโปรแกรมหุ่นยนต์
เริ่มต้นการเขียนโปรแกรมหุ่นยนต์เริ่มต้นการเขียนโปรแกรมหุ่นยนต์
เริ่มต้นการเขียนโปรแกรมหุ่นยนต์
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
 
คอมโปรเฟรม
คอมโปรเฟรมคอมโปรเฟรม
คอมโปรเฟรม
 
06 for loops
06 for loops06 for loops
06 for loops
 

Similar a Lesson2

2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซีmansuang1978
 
C Programming
C ProgrammingC Programming
C ProgrammingWarawut
 
Computer programming
Computer  programmingComputer  programming
Computer programmingPreaw Jariya
 
Computer programming
Computer programmingComputer programming
Computer programmingJariyaa
 
โปรแกรม
โปรแกรมโปรแกรม
โปรแกรมminkky04
 
ภาษาซีเบื้องต้น
ภาษาซีเบื้องต้นภาษาซีเบื้องต้น
ภาษาซีเบื้องต้นPopinut Gara
 
ภาษา C
ภาษา Cภาษา C
ภาษา C0872671746
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีNattapon
 
Programming
ProgrammingProgramming
Programmingsa
 
คำสั่งในโปรแกรมภาษาซี
คำสั่งในโปรแกรมภาษาซีคำสั่งในโปรแกรมภาษาซี
คำสั่งในโปรแกรมภาษาซีSupaksorn Tatongjai
 
59170249 ธิดารัตน์
59170249 ธิดารัตน์59170249 ธิดารัตน์
59170249 ธิดารัตน์Beam Suna
 
Algorithm Flowchart
Algorithm FlowchartAlgorithm Flowchart
Algorithm FlowchartWarawut
 
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานโปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานchanamanee Tiya
 
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานโปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานchanamanee Tiya
 
Java week2
Java week2Java week2
Java week2Asa Thai
 

Similar a Lesson2 (20)

2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี
 
C Programming
C ProgrammingC Programming
C Programming
 
Chapter1
Chapter1Chapter1
Chapter1
 
การแสดงผลออกทางหน้าจอ
การแสดงผลออกทางหน้าจอการแสดงผลออกทางหน้าจอ
การแสดงผลออกทางหน้าจอ
 
Computer programming
Computer  programmingComputer  programming
Computer programming
 
Computer programming
Computer programmingComputer programming
Computer programming
 
โปรแกรม
โปรแกรมโปรแกรม
โปรแกรม
 
ภาษาซีเบื้องต้น
ภาษาซีเบื้องต้นภาษาซีเบื้องต้น
ภาษาซีเบื้องต้น
 
Introduction toc
Introduction tocIntroduction toc
Introduction toc
 
ภาษา C
ภาษา Cภาษา C
ภาษา C
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
 
Programming
ProgrammingProgramming
Programming
 
03 input math
03 input math03 input math
03 input math
 
คำสั่งในโปรแกรมภาษาซี
คำสั่งในโปรแกรมภาษาซีคำสั่งในโปรแกรมภาษาซี
คำสั่งในโปรแกรมภาษาซี
 
Week5
Week5Week5
Week5
 
59170249 ธิดารัตน์
59170249 ธิดารัตน์59170249 ธิดารัตน์
59170249 ธิดารัตน์
 
Algorithm Flowchart
Algorithm FlowchartAlgorithm Flowchart
Algorithm Flowchart
 
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานโปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
 
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐานโปรแกรมย่อยและฟังก์ชันมาตรฐาน
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
 
Java week2
Java week2Java week2
Java week2
 

Más de โทโม๊ะจัง นานะ

บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookโทโม๊ะจัง นานะ
 
บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookโทโม๊ะจัง นานะ
 
บทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็วบทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็วโทโม๊ะจัง นานะ
 
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์โทโม๊ะจัง นานะ
 

Más de โทโม๊ะจัง นานะ (20)

บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e book
 
บทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e bookบทที่ 4 เรื่องทำงานกับไฟล์ e book
บทที่ 4 เรื่องทำงานกับไฟล์ e book
 
บทที่ 3 การควบคุม e book
บทที่ 3 การควบคุม e bookบทที่ 3 การควบคุม e book
บทที่ 3 การควบคุม e book
 
บทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็วบทที่ 2 สร้าง e book ง่ายและเร็ว
บทที่ 2 สร้าง e book ง่ายและเร็ว
 
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
บทที่ 1 ลักษณะทั่วไปของหนังสืออิเล็กทรอนิกส์
 
Chapter1
Chapter1Chapter1
Chapter1
 
เฉลยแบบฝึกหัดบทที่ 1
เฉลยแบบฝึกหัดบทที่ 1เฉลยแบบฝึกหัดบทที่ 1
เฉลยแบบฝึกหัดบทที่ 1
 
Lesson1
Lesson1Lesson1
Lesson1
 
Lesson6
Lesson6Lesson6
Lesson6
 
Lesson5
Lesson5Lesson5
Lesson5
 
Lesson4
Lesson4Lesson4
Lesson4
 
Lesson3
Lesson3Lesson3
Lesson3
 
Lesson1
Lesson1Lesson1
Lesson1
 
บทที่1
บทที่1บทที่1
บทที่1
 
บท1
บท1บท1
บท1
 
บท2
บท2บท2
บท2
 
บท3
บท3บท3
บท3
 
บท4
บท4บท4
บท4
 
บท5
บท5บท5
บท5
 
บท6
บท6บท6
บท6
 

Lesson2

  • 1. ! บทที่ 2 การแสดงผลและการรับขอมูล ♦! วามหมายของการแสดงผล ค การแสดงผล หมายถึง การสงใหคอมพวเตอรนําขอมูลและผลลัพธที่มีอยูในหนวยความ จําไป ่ั  ิ  แสดงผลออกที่อุปกรณแสดงผล (output device) ของคอมพิวเตอร การแสดงผลที่อุปกรณแสดงผลอาจมี เพยงอปกรณเ ดยว หรอหลาย ๆ อุปกรณพรอมกันก็ได เชน แสดงผลที่จอภาพ เครองพมพ(Printer) ี ุ ี ื  ่ื ิ  ลําโพง แผนดิสก เปนตน ♦! ารแสดงผลทางจอภาพดวย cout ก  จากรูปแบบโครงสรางของโปรแกรม C++ ในบทที่ 1 จะเห็นตัวอยางการใชประโยคคําสั่ง cout (อานวา c - out ซีเอาต ยอมาจาก character out) หมายถึง การแสดงผลในลักษณะอักษรหรือขอความ ทีอยูในเครืองหมาย "…… " หรือคาของตัวแปร(variable)ออกทางจอภาพ ่  ่ cout เปนออบเจ็กต(object) อยูในไฟล iostream.h ซึ่ง cout จะเปน object ที่ทาหนาทดาเนน  ํ  ่ี ํ ิ การเกี่ยวกับกระแสขอมูล(Stream) ของภาษา C++ ออกไปแสดงผลสูอุปกรณตาง ๆ << เปน operator หรอตวดาเนินการ มชอวา put to หรอสงไปท่ี หรอเรยกวา insertion  ื ั ํ ี ่ื  ื  ื ี  หมายถึง การแสดงขอความ เครืองหมาย << จะทําหนาทนาคาทอยทางขวาของเครองหมายซงอาจจะ ่  ่ี ํ  ่ี ู ่ื ่ึ เปนคาคงที่ ขอความหรอ string ทีอยูในเครืองหมาย "……" หรอคาตวแปร(variable) ก็ได สงใหแก  ื ่  ่ ื  ั Object ที่อยูทางซายของเครื่องหมาย รูปแบบการแสดงผลขอความทางจอภาพ โดยใช cout << มดงน้ี ีั ! cout << " ขอความ " << "ขอความ " << ตัวแปร; ! cout << ตัวแปร<<"ขอความ"<<ตัวแปร<<…; •! ตัวอยางโปรแกรม cout_exp.cpp แสดงการใช cout << แสดงขอความออกทาง จอภาพ ศิริชัย นามบุรี การแสดงและการรับขอมูล !
  • 2. !23 /* Program : cout_exp.cpp Process : display cout object in iostream.h */ #include <iostream.h> void main() { cout << "This is Turbo C++ Program "; cout<< "C++ is high language"; } จากตวอยางโปรแกรมน้ี cout จะทําหนาที่สงกระแสขอมูลขอความ This is Turbo C++ ั  Program C++ is high language ไปแสดงที่จอภาพ ณ ตําแหนงปจจบนท่ี cursor ชี้อยู   ุ ั •! โปรแกรม cout_ex2.cpp แสดงขอความตอกันโดยใช << ออกทางจอภาพ /* Program : cout_ex2.cpp Process : display cout object in iostream.h */ #include <iostream.h> void main() { cout<< "This is Turbo C++ Program."<<" "<<" It is very easy."; cout<< "I love C++."<<" "<<"It's high level language."; } จากโปรแกรม cout_ex2.cpp จะแสดงขอความตอไปนี้บนจอภาพ This is Turbo C++ Program. It is very easy. I love C++. It's high level language. ♦! ําแหนงแสดงผลทจอภาพดวย endl ต  ่ี  1. การใช endl เปนโอเปอเรเตอรประเภทตัวผสม (manipulator) ทาหนาทเ่ี ลอนเคอรเ ซอร  ํ  ่ื เพื่อขึ้นบรรทัดใหมและการแสดงผลขอความที่ตามมาจะขึ้นบรรทัดใหมดวย •! ตัวอยางโปรแกรม endl_exp.cpp แสดงขอความ และกําหนดใหแสดงขอความขึ้น บรรทัดใหมดวย manipulator คือ endl /* Program : endl_exp.cpp Process : display cout object in iostream.h */ #include <iostream.h> #include <conio.h> void main() { clrscr(); //clear screen standard function from conio.h ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 3. !24 cout << "This is Turbo C++ Program"<<endl; cout << "It is very easy."<<endl; cout <<endl<<endl; // space 2 line cout << "I like C++ Program"<<endl<<endl; cout << "Press any key to continue..."<<endl; getch(); //get character standard function from conio.h } 2. การใช setw(n) ทาหนาที่กําหนดความกวางของตัวเลขหรือขอความกอนที่จะแสดงผลใหมีคา ํ เทากับ n อกษร เรยกคา n วาเปน อารกิวเมนต (argument) เพื่อใชในการแสดงผลที่จอภาพในลักษณะชิด ั ี    ขวา โดยตองเรียกใช Header file ที่ชื่อ iomanip.h ในขณะ compile ดวย ดังตัวอยางตอไปนี้ cout << setw(10) << "World"; หมายถึง กําหนดใหคาวา World มีความกวางในการแสดงผล 10 ตําแหนง โดยนับจากดานซาย ํ  ของจอภาพ อกษร d จะเรมพมพตําแหนงคอลัมนที่ 10 และเริ่มพิมพอักษร lrow มาทางซายคอลัมนที่ ั ่ิ ิ  9,8,7 และ 6 •! ตัวอยางโปรแกรม setw.cpp เปนการใช setw กําหนดความกวางในการแสดงผล เพอให ่ื แสดงผล ณ ตําแหนงกลางจอภาพ /* Program : setw.cpp Process : display set width of display to screen */ #include <iostream.h> #include <conio.h> #include <iomanip.h> //header file of setw() void main() { clrscr(); //clear screen standard function from conio.h cout << setw(50)<<"This is Turbo C++ Program"<<endl; cout << setw(45)<< "It is very easy."<<endl; cout <<endl<<endl; // space 2 line cout << setw(45)<< "I like C++ Program"<<endl<<endl; cout << setw(50)<<"Press any key to continue..."<<endl; getch(); //get character standard function from conio.h } ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 4. !25 ♦! ารแสดงผลการคํานวณทางคณตศาสตร ก ิ •! ตัวอยางโปรแกรม cout_mat.cpp แสดงการใช cout เพื่อแสดงผลของการคํานวณทาง คณิตศาสตร /* Program : cout_mat.cpp Process : Display mathmetic calculate result*/ #include <iostream.h> #include <conio.h> void main() { int number1,number2; float x,y,z; // set value of variable number1=20; number2=30; x=25.25; y=30.05; z=10.75; // display calculation result clrscr(); cout<< "Program Display Mathmetic calculation"<<endl; cout<< "number1 = " << number1 <<endl; cout<< "number2 = " << number2 <<endl; cout<< "number1 + number2 = "<<number1+number2 <<endl; cout<< "number1 - number2 = "<<number1-number2 <<endl; cout<< "x+y+z = "<< x+y+z << endl; cout<< "x*y*z = " << x*y*z <<endl; cout<< "50*25*12.5 = " << 50*25*12.5 << endl; getch(); } •! ตัวอยางโปรแกรม set_dec.cpp การใช manipulator ทชอ setprecision(n) รวมกบ cout<< ่ี ่ื  ั เพื่อกําหนดความละเอียดของตาแหนงทศนยมของจานวนจริง โดยที่ n คือจํานวน ํ  ิ ํ ตําแหนงทศนยม  ิ /*Program : set_dec.cpp process : set decimal precision of float number */ #include <iostream.h> #include <iomanip.h> #include <conio.h> ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 5. !26 void main() { float A=125.25125; float B=10.7525; float C=212.15; clrscr(); //function in conio.h for clear screen cout<<"Display set precision of float number ...."<<endl; cout<<"A+B = "<<setprecision(10)<<A+B<<endl; cout<<"A*B*C = "<<setprecision(15)<<A*B*C<<endl; cout<<"A*B = "<<setprecision(5)<<A*B<<endl; cout<<"A+B+C = "<<setprecision(3)<<A+B+C<<endl; cout<<"A+B+C = "<<setprecision(2)<<A+B+C<<endl; cout<<"A/B = "<<setprecision(1)<<A/B<<endl; getch(); ♦! ารคํานวณโดยใชฟงกชนทางคณตศาสตร ก    ั ิ ! การคานวณคาฟงกชันทางคณิตศาสตร เชน คายกกาลัง คารากที่สอง คาสัมบูรณ จะตองใช ํ  ํ ฟงกชันมาตรฐาน (standard function) ทางดานคณตศาสตร ที่ C++ จดเตรยมไวใหใน  ิ ั ี   โดยจัดเก็บคลังคําสั่งไวในไฟล Math.h ดงนนในโปรแกรมทตองการใชฟงกชนทางคณตศาสตรจะตอง ั ้ั ่ี     ั ิ   มีการ include ไฟล Math.h เปน preprocessor directive ดวย (วิธีการตรวจสอบวามีฟงกชันใดบางใน  ไฟล Math.h ใหใชคาสั่ง Help , Index แลวพิมพ คําวา Math.h เมื่อพบคําวา Math.h แลวใหกดแปน ํ Enter จะแสดงใหเห็นชื่อฟงกชันตาง ๆ ทางดานคณตศาสตร)  ิ  ! •! ตัวอยางโปรแกรม Func_Mat.cpp แสดงการหาคาทางคณิตศาสตรโดยใชฟงกชัน pow() และฟงกชัน sqrt() เพื่อหาคาเลขยกกําลังและรากที่สอง /*Program : Func_Mat.Cpp Process : Display using mathmetic function */ #include <iostream.h> #include <math.h> #include <conio.h> #include <iomanip.h> void main() { double x = 9.0, y = 2.0; clrscr(); cout<<"Display using mathmetic functions ...a"<<endl<<endl; cout<<x<<" power by "<<y<<" = "<< pow(x, y)<<endl; cout<<" SquareRoot "<<x<<" = "<< sqrt(x)<<endl; ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 6. !27 cout<<"(x power y)*(sqrt(y)) = "<<setprecision(3)<<pow(x,y)*(sqrt(y)); cout<<endl<<endl<<"Press any key to exit..."; getch(); } ! ! ♦! ารแสดงผลดวยการใช escape sequence ก escape sequence เปนรหสอกขระแบบคอนสแตนต(constant) ชนิดอักษร(character) ซึ่ง  ั ั ประกอบดวยเครืองหมาย (backslash) และตัวอักษร อยภายในเครองหมาย ' ' เชน 'n' 't' ทาหนาที่ ่ ู ่ื  ํ จดรปแบบการแสดงผลรวมกบ cout เหมอนกับ endl รายละเอยดดงตารางตอไปน้ี ั ู  ั ื ี ั  ตาราง แสดง escape sequence และความหมาย escape sequence ชื่อ ความหมาย a alarm bell สงเสียงบิ๊ป (beep) b backspace เหมอนการกดคย backspace ื ี f formfeed ขึ้นหนาใหม n newline ขึนบรรทัดใหม ้ r return เหมอนการกดคย enter ื ี t tab เหมอนการกดคย tab ื ี backslash แสดงเครองหมาย ่ื ' single quote แสดงเครองหมาย ' ่ื " double quote แสดงเครองหมาย " ่ื ? Question mark แสดงเครองหมาย ? ่ื •! ตัวอยางโปรแกรม escape.cpp แสดงการใชเครื่องหมาย escape sequence รวมกบ cout  ั สําหรบการแสดงผล ั /*Program : escape.cpp Process : display with escape sequence */ #include <iostream.h> #include <conio.h> void main() { clrscr(); //clear screen cout<< "Hello Program C++" << 'n'; //new line ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 7. !28 cout<< "C++ is very easy a" <<'n'<<'a'; //new line and bell cout<< "ttC++ is very interest language n"; // 2 tab and newline cout<< "tC++ is OOP Languageb n"; //tab, backspace and new line getch(); //wait for press any key } ♦! ารรบขอมลจากคบอรดดวย cin ก ั  ู ี   ในการเขียนโปรแกรมเพือการประมวล มีความจําเปนอยางยิ่งที่โปรแกรมดวยทั่วไป ่ จะตองมีการรับขอมูลจากผูใช (user) ผานทางคยบอรดหรอแปนพมพ เพื่อความยืดหยุนในการใชงาน  ี  ื  ิ โปรแกรม ใน C++ สามารถใชออปเจ็กต cin ที่อยูในไฟล iostream.h เพื่อรับขอมูลจากคียบอรดและ อุปกรณอน ๆ ได มรปแบบดงน้ี ่ื ีู ั ! cin >> ชอตวแปร; ่ื ั ! cin>>ชอตวแปร>>ชอตวแปร>>ชอตวแปร>>…; ่ื ั ่ื ั ่ื ั โดยที่ cin อานวา ซีอิน ยอมาจาก character in ซึ่งหมายถึงการรับขอมูลในลักษณะ ของอักษร cin เปนออปเจ็กตที่สรางอยูในไฟล iostream.h >> เปนโอเปเรเตอร ซึ่งมีชื่อวา เอกซแทรกชน (extraction - รับเขามา) หรอ get from จะทํา   ็ ั ื หนาที่รับคาที่อยูทางซายของเครื่องหมายสงใหแกตัวแปรที่อยูทางขวาของเครื่องหมาย(ถาไมระบุอุปกรณ หมายถึงรับขอมูลเขาทางคียบอรด) •! ตัวอยางโปรแกรม cin_exp.cpp แสดงการรบคาจานวนตัวเลขและตัวอักษรทางแปนพิมพ ั  ํ เพื่อเก็บไวในตัวแปร โดยใช cin>> /* Program : cin_exp.cpp Process : input number and character to variable, and display value*/ #include <iostream.h> #include <conio.h> void main() { int number1,number2; //declared integer variable char ch; //declared character variable //start statement clrscr(); cout<< " Please Enter number and character : n"; cout<< " Enter number1 : "; cin>>number1; //enter integer from keyboard cout<< "nEnter number 2 : "; ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 8. !29 cin>>number2; //enter integer from keyboard cout<< "nEnter 1 character : "; cin>>ch; //enter 1 character from keyboard cout<< "nnPress any key to display..."; getch(); //wait press any key clrscr(); // process display value from variable cout<< "You enter number and character : na"; cout<< "Value of number1 : " <<number1; cout<< "nValue of number 2 : " <<number2; cout<< "nValue of character : " <<ch; cout<< "nnPress any key to exit..."; getch(); //wait press any key } •! ตัวอยางโปรแกรม area_cir.cpp แสดงการคํานวณพื้นที่วงกลมและแสดงผลลัพธ ออกมาทางจอภาพ โดยรับคารัศมีของวงกลมจากคียบอรดกําหนดใหมตวแปรประเภท  ี ั float และมี constant ในโปรแกรมดวย /*Program : area_cir.cpp Process : calculate circle area by input radius */ #include <iostream.h> #include <conio.h> void main() { float rad; //declared real variable const float PI = 3.14159; // defined constant PI //start statement clrscr(); cout<< "Please enter radius of circle : "; cin>>rad; float area = PI*rad*rad; //declared variable at position that want to use cout<< "nnRadius of circle = "<<rad; cout<< "nnArea of circles = an"<<area; getch(); } วิธีการกําหนดใหโปรแกรมรบขอมลหลาย ๆ ตัวแปร ตอเนองกนมวธการเขยนประโยค ดังนี้  ั  ู  ่ื ั ี ิ ี ี ! cin >> number1 >> number2 >> number3; ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 9. !30 วิธีปอนขอมูลเมื่อ run โปรแกรม มี 2 วิธี คือ - กรอกเลข 3 จานวน แตละจานวนเวนวรรค แลว Enter ทจานวนสุดทาย เชน ํ ํ  ่ี ํ  50 30 40 <Enter> หรือ - กรอกเลขแตละจํานวน แลวกดแปน Enter ตาม เชน  50 <Enter> 30 <Enter> 40 <Enter> •! ตัวอยางโปรแกรม cin_cout.cpp แสดงการกรอกขอมูลตัวเลข 3 จานวน ทางแปนพิมพ ํ /* Program : cin_cont.cpp Process : input 3 number and display value*/ #include <iostream.h> #include <conio.h> void main() { int number1,number2,number3; //declared 3 integer variable //start statement clrscr(); cout<< "Please enter 3 integer number : n"; cin>>number1>>number2>>number3; //enter 3 amount integer from keyboard cout<< "nPress any key to display..."; getch(); //wait press any key clrscr(); // process display value from variable cout<< "You enter 3 number : na"; cout<< "Value of number1 : " <<number1; cout<< "nValue of number 2 : " <<number2; cout<< "nValue of number 3 : " <<number3; cout<< "nnPress any key to exit..."; getch(); //wait press any key } ♦! ารใช manipulator เพอเปลยนเลขฐาน ก ่ื ่ี ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 10. !31 ในการเปลี่ยนการแสดงผลเลขฐานตาง ๆ ใน C++ ไดแก ฐานสิบ(Decimal) ฐานแปด (Octal) ฐานสิบหก(Hexadecimal) เราสามารถใช แมนิพวเลเตอร (manipulator) รวมกับ cout เพือแสดงจํานวน  ่ เลขฐานตาง ๆ ได โดยใช manipulator ตอไปน้ี   dec ทําหนาทีเ่ ปลียนเลขฐานแปดหรือฐานสิบหกเปนเลขฐานสิบ ่ oct ทําหนาทีเ่ ปลียนเลขฐานสิบหรือฐานสิบหกเปนเลขฐานแปด ่ hex ทําหนาทีเ่ ปลียนเลขฐานแปดหรือฐานสิบเปนเลขฐานสิบหก ่ ใน C++ กําหนดจํานวนเลขในฐานตาง ๆ ดังนี้  ถามี 0 (ศนย) นําหนาจานวนเลข แสดงวาเปนจํานวนฐานแปด เชน 065,0125 ู  ํ ถามี ox (ศูนยเอกซ) นําหนาจานวนเลข แสดงวาเปนจํานวนฐานสิบหก เชน ํ 0x125 ถาไมมี 0 หรอ 0x นําหนาจานวนเลข แสดงวาเปนฐานสบ เชน 50, 95 ื ํ   ิ •! ตัวอยางโปรแกรม base_num.cpp แสดงการใช manipulator เปลียนเลขฐาน dec, oct, hex ่ ตาง ๆ /*Program : base_num.cpp Process : display change base number of decimal,octal,hexadecimal*/ #include <iostream.h> #include <conio.h> void main() { clrscr(); cout<< "Display change base number..."<<endl<<endl; cout<< "10 decimal to hexadecimal = "<<hex<<10<<endl; cout<< "5865 decimal to hexadecimal = "<<hex<<5865<<endl; cout<< "1250 decimal to octal = "<<oct<<1250<<endl; cout<< "02342 octal to decimal = "<<dec<<02342<<endl; cout<< "0xabc125 hexadecimal to decimal = "<<dec<<0xabc125<<endl; cout<< "0xf hexadecimal to decimal = "<<dec<<0xf<<endl; getch(); } ♦! บบฝกหัดทายบท แ ศิริชัย นามบุรี การแสดงและการรับขอมูล
  • 11. !32 1. จงเขยนโปรแกรมแสดงขอความรายละเอยดเกยวกบตวทานเอง ดงตอไปน้ี โดยใหแสดงขอ ความ ี  ี ่ี ั ั  ั  บรเิ วณกลางจอภาพ This is C++ program. I love to study C++ language. My name is Mr…….. Code ……….. Major …………. 2. จากสตรการคานวณหาดอกเบีย ดังนี้ ู ํ ้ I = Pin S = P+I โดยที่ I คือ ดอกเบย ้ี P คือ เงินตน i คือ อัตราดอกเบียตอป ้ n คือ จํานวนป S คือ เงินรวม ใหเ ขยนโปรแกรมเพอคานวณหาคาดอกเบย ( I ) และเงินรวม (S) โดยใหผูใชกรอกขอมูลทาง ี ่ื ํ  ้ี แปนพิมพ ทจาเปนตองใชในการคานวณ ่ี ํ    ํ 3. จงเขยนโปรแกรมเพอคานวณหาผลลัพธและแสดงผลลัพธนิพจนทางคณิตศาสตรตอไปนี้ ี ่ื ํ โดยกําหนดให A , B เปนจํานวนจรง (float)  ิ โดย A = 2.25 B = 3.0 X,Y,Z เปนจํานวนเต็ม (integer)  โดย X = 4 Y=5 Z=8 คํานวณหานิพจนตอไปนีในโปรแกรม โดยกําหนดการแสดงผลทศนิยม 2 ตําแหนง  ้  A+B+X+Y+Z Z*Y/A (A+B)*X pow(A,2) pow(A,2)*pow(X,2) sqrt(X+Y+Z) sqrt(X*Y*Z)/B ศิริชัย นามบุรี การแสดงและการรับขอมูล