ภาษา C++ Lesson2

Page 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 << แสดงขอความออกทาง จอภาพ

ศิริชัย นามบุรี

!

การแสดงและการรับขอมูล


!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 \b \f \n \r \t \\ \' \" \?

ชื่อ alarm bell backspace formfeed newline return tab backslash single quote double quote Question mark

ความหมาย สงเสียงบิ๊ป (beep) เหมือนการกดคีย backspace ขึ้นหนาใหม ขึน้ บรรทัดใหม เหมือนการกดคีย enter เหมือนการกดคีย tab แสดงเครือ่ งหมาย \ แสดงเครือ่ งหมาย ' แสดงเครือ่ งหมาย " แสดงเครือ่ งหมาย ?

•! ตัวอยางโปรแกรม 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<< "\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 >> ชือ่ ตัวแปร; ! 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<< "\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; ศิริชัย นามบุรี

การแสดงและการรับขอมูล


!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 เพือ่ เปลีย่ นเลขฐาน ศิริชัย นามบุรี

การแสดงและการรับขอมูล


!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

ศิริชัย นามบุรี

การแสดงและการรับขอมูล


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.