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

11
ศิริชัย นามบุรี การแสดงและการรับขอมูล บทที่ 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_exp.cpp แสดงการใช cout << แสดงขอความออกทาง จอภาพ cout << " ขอความ " << "ขอความ " << ตัวแปร; cout << ตัวแปร<<"ขอความ"<<ตัวแปร<<…;

Transcript of บทที่ 02 การแสดงผลและการรับข้อมูล

Page 1: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

บทที่ 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_exp.cpp แสดงการใช cout << แสดงขอความออกทางจอภาพ

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

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

!

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

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

Page 3: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

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}

Page 4: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

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>

Page 5: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

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;

Page 6: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

27

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

!

♦ !การแสดงผลดวยการใช escape sequenceescape 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

Page 7: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

28

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

♦ !การรับขอมูลจากคีบอรดดวย cinในการเขียนโปรแกรมเพือ่การประมวล มีความจํ าเปนอยางยิ่งที่โปรแกรมดวยทั่วไป

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

โดยที ่ 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 : ";

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

Page 8: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

29

cin>>number2; //enter integer from keyboard cout<< "\nEnter 1 character : "; cin>>ch; //enter 1 character from keyboard cout<< "\n\nPress any key to display..."; getch(); //wait press any key clrscr(); // process display value from variable cout<< "You enter number and character : \n\a"; cout<< "Value of number1 : " <<number1; cout<< "\nValue of number 2 : " <<number2; cout<< "\nValue of character : " <<ch; cout<< "\n\nPress 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<< "\n\nRadius of circle = "<<rad; cout<< "\n\nArea of circles = \a\n"<<area; getch();}

วิธีการกํ าหนดใหโปรแกรมรับขอมูลหลาย ๆ ตัวแปร ตอเน่ืองกันมีวิธีการเขียนประโยค ดังน้ี

! cin >> number1 >> number2 >> number3;

Page 9: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

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 : \n\a"; cout<< "Value of number1 : " <<number1; cout<< "\nValue of number 2 : " <<number2; cout<< "\nValue of number 3 : " <<number3; cout<< "\n\nPress any key to exit..."; getch(); //wait press any key}

♦ !การใช manipulator เพ่ือเปล่ียนเลขฐาน

Page 10: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

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(); }

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

Page 11: บทที่ 02 การแสดงผลและการรับข้อมูล

!

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

32

1. จงเขียนโปรแกรมแสดงขอความรายละเอียดเก่ียวกับตัวทานเอง ดังตอไปน้ี โดยใหแสดงขอความบริเวณกลางจอภาพ

This is C++ program.I love to study C++ language.

My name is Mr……..Code ………..

Major ………….2. จากสตูรการคํ านวณหาดอกเบ้ีย ดังน้ี

I = PinS = P+I

โดยที ่ I คือ ดอกเบ้ีย P คือ เงินตน

i คือ อัตราดอกเบ้ียตอปn คือ จํ านวนปS คือ เงินรวม

ใหเขียนโปรแกรมเพ่ือคํ านวณหาคาดอกเบ้ีย ( I ) และเงินรวม (S) โดยใหผูใชกรอกขอมูลทางแปนพิมพ ท่ีจํ าเปนตองใชในการคํ านวณ

3. จงเขียนโปรแกรมเพ่ือคํ านวณหาผลลัพธและแสดงผลลัพธนิพจนทางคณิตศาสตรตอไปนี้โดยกํ าหนดใหA , B เปนจํ านวนจริง (float) โดย A = 2.25 B = 3.0X,Y,Z เปนจํ านวนเต็ม (integer) โดย X = 4 Y = 5 Z = 8คํ านวณหานิพจนตอไปน้ีในโปรแกรม โดยกํ าหนดการแสดงผลทศนิยม 2 ตํ าแหนงA+B+X+Y+ZZ*Y/A(A+B)*Xpow(A,2)pow(A,2)*pow(X,2)sqrt(X+Y+Z)sqrt(X*Y*Z)/B