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
}
ศิริชัย นามบุรี การแสดงและการรับขอมูล
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 เพอเปลยนเลขฐาน
ก ่ื ่ี
ศิริชัย นามบุรี การแสดงและการรับขอมูล