การแสดงผลและการรับข้อมูล • ความหมายของการแสดงผล การแสดงผล หมายถึง การสั่งให้คอมพิวเตอร์นำข้อมูลและผลลัพธ์ที่มีอยู่ในหน่วยความ จำไป แสดงผลออกที่อุปกรณ์แสดงผล (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 << แสดงข้อความออกทางจอภาพ /* Program : cout_exp.cpp Process : display cout object in iostream.h */ #include <iostream> int main() using namespace std; { cout << "This is Turbo C++ Program "; cout<< "C++ is high language"; return 0; } จากตัวอย่างโปรแกรมนี้ 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> int main() using namespace std; { cout<< "This is Turbo C++ Program."<<" "<<" It is very easy."; cout<< "I love C++."<<" "<<"It's high level language."; return 0; } จากโปรแกรม cout_ex2.cpp จะแสดงข้อความต่อไปนี้บนจอภาพ This is Turbo C++ Program. It is very easy. I love C++. It's high level language.
Go to Top
ตำแหน่งแสดงผลที่จอภาพด้วย endl การใช้ endl เป็นโอเปอเรเตอร์ประเภทตัวผสม (manipulator) ทำหน้าที่เลื่อนเคอร์เซอร์ เพื่อขึ้นบรรทัดใหม่และการแสดงผลข้อความที่ตามมาจะขึ้นบรรทัดใหม่ด้วย ตัวอย่างโปรแกรม endl_exp.cpp แสดงข้อความ และกํ าหนดให้แสดงข้อความขึ้น บรรทัดใหม่ด้วย manipulator คือ endl /* Program : endl_exp.cpp Process : display cout object in iostream.h */ #include <iostream> #include <conio.h> int main() using namespace std; { clrscr(); //clear screen standard function from conio.h 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 return 0; } Go to Top
การแสดงผลการคำนวณทางคณิตศาสตร์ ตัวอย่างโปรแกรม cout_mat.cpp แสดงการใช้ cout เพื่อแสดงผลของการคำนวณทาง คณิตศาสตร์ /* Program : cout_mat.cpp Process : Display mathmetic calculate result*/ #include <iostream> #include <conio.h> int main() using namespace std; { 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(); return 0; }
ตัวอย่างโปรแกรม set_dec.cpp การใช้ manipulator ที่ชื่อ setprecision(n) ร่วมกับ cout<< เพื่อกําหนดความละเอียดของตำแหน่งทศนิยมของจำนวนจริง โดยที่ n คือจำนวน ตำแหน่งทศนิยม /*Program : set_dec.cpp process : set decimal precision of float number */ #include <iostream> #include <iomanip.h> #include <conio.h> int main() using namespace std; { 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(); return 0; }
Go to Top
การคำนวณโดยใช้ฟังก์ชันทางคณิตศาสตร์ การคำนวณค่าฟังก์ชันทางคณิตศาสตร์ เช่น ค่ายกกํ าลัง ค่ารากที่สอง ค่าสัมบูรณ์ จะต้องใช้ ฟังก์ชันมาตรฐาน (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> #include <math.h> #include <conio.h> #include <iomanip.h> int main() using namespace std; { 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; cout<<"(x power y)*(sqrt(y)) = "<<setprecision(3)<<pow(x,y)*(sqrt(y)); cout<<endl<<endl<<"Press any key to exit..."; getch(); return 0; }
Go to Top
การแสดงผลด้วยการใช้ escape sequence escape sequence เป็นรหัสอักขระแบบคอนสแตนต์(constant) ชนิดอักษร(character) ซึ่ง ประกอบด้วยเครื่องหมาย \ (backslash) และตัวอักษร อยู่ภายในเครื่องหมาย ' ' เช่น '\n' '\t' ทำหน้าที่ จัดรูปแบบการแสดงผลร่วมกับ cout เหมือนกับ endl รายละเอียดดังตารางต่อไปนี้ ตาราง แสดง escape sequence และความหมาย
ตัวอย่างโปรแกรม escape.cpp แสดงการใช้เครื่องหมาย escape sequence ร่วมกับ cout สำหรับการแสดงผล /*Program : escape.cpp Process : display with escape sequence */ #include <iostream> #include <conio.h> int main() using namespace std; { clrscr(); //clear screen cout<< "Hello Program C++" << '\n'; //new line 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 return 0; }
Go to Top
การรับข้อมูลจากคีบอร์ดด้วย 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> #include <conio.h> int main() using namespace std; { 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>>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 return 0; }
ตัวอย่างโปรแกรม area_cir.cpp แสดงการคำนวณพื้นที่วงกลมและแสดงผลลัพธ์ ออกมาทางจอภาพ โดยรับค่ารัศมีของวงกลมจากคีย์บอร์ดกํ าหนดให้มีตัวแปรประเภท float และมี constant ในโปรแกรมด้วย /*Program : area_cir.cpp Process : calculate circle area by input radius */ #include <iostream> #include <conio.h> int main() using namespace std; { 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(); return 0; } วิธีการกําหนดให้โปรแกรมรับข้อมูลหลาย ๆ ตัวแปร ต่อเนื่องกันมีวิธีการเขียนประโยค ดังนี้ cin >> number1 >> number2 >> number3; วิธีป้อนข้อมูลเมื่อ 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> #include <conio.h> int main() using namespace std; { 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 return 0; }
|