60 likes | 186 Vues
This lab exercise focuses on demonstrating base number conversion, the use of break and continue statements, and the switch control structure in C++. Students will study examples including conversion between decimal, octal, and hexadecimal formats. They will also learn how to use break within loops to terminate execution based on conditions, and utilize continue to skip iterations. Additional focus will be placed on creating a menu-driven program using switch statements for various user commands, enhancing programming logic and control structures.
E N D
LAB # 7 CASE SWITCH CONTINUE
ให้นักศึกษา ศึกษาตัวอย่างการแปลงค่าเลขฐานต่างๆและทำความเข้าใจ /*Program : base_num.cpp Process : display change base number of decimal,octal,hexadecimal*/ #include <iostream.h> void main() { 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; }
ให้นักศึกษา ศึกษาตัวอย่างการใช้คำสั่ง Break และทำความเข้าใจ /*Program : break.cpp Process : display using 'break' in for loop */ #include <iostream.h> void main() { int x,row,sum; row=0;sum=0; for(x=1;x<=100;++x) //condition for 100 loop { cout<<"x="<<x<<'\n'; sum+=x; row++; if(row>23) //condition of break break; } cout<<"summation 1-"<<x<<" = "<<sum; }
ให้นักศึกษา ศึกษาตัวอย่างการใช้คำสั่ง Continue และทำความเข้าใจ /*Program : continue.cpp Process : display keyword 'continue' in do...while loop */ #include<iostream.h> void main() { float x,y; char choice; do{ cout<< "\nProgram Divide Calculation x/y"; cout<< "\n******************************"; cout<< "\nEnter X : "; cin>>x; cout<< "\nEnter Y : "; cin>>y; if(y==0) { cout<< "\a\n\nCan't divide by zero !!!"; cout<< "\npress any key to continue..."; continue; } cout<< "\n*** Result "<<x<<"/"<<y<<" = "<<x/y; cout<< "\n\nCalculate another <y or n> ? "; cin>>choice; }while((choice!='n')&&(choice!='N')); }
ให้นักศึกษา ศึกษาตัวอย่างการใช้คำสั่ง Switch และทำความเข้าใจ /*Program : switch.cpp Process : test statement switch...case */ #include <iostream.h> void main() { int first,second; char choice; //begin statement cout<<"Program Calcurate Area\n"; cout<<"1. Circle\n"; cout<<"2. Square\n"; cout<<"3. Triangle\n"; cout<<"Please select your choice <1-3>: "; cin>>choice; //begin switch statement switch(choice) { case '1': cout<<"\nYou select choice "<<choice<< " calculate Circle Area\n"; cout<<"Press any key to end program\n"; break; case '2': cout<<"\nYou select choice "<<choice<< " calculate Square Area\n"; cout<<"Press any key to end program\n"; break; case '3': cout<<"\nYou select choice "<<choice<< " calculate Triangle Area\n"; cout<<"Press any key to end program\n"; break; default: cout<<"\nYou select Another choice \a\a\n"; cout<<"Press any key to end program\n"; } }
จงเขียนโปรแกรมต่อไปนี้ โดยใช้คำสั่ง Switch และ CASE 1. โปรแกรมนี้เป็นโปรแกรมเมนูที่รับการคีย์อักขระจากคีย์บอร์ด • กด L เมื่อผู้ใช้ต้องการ Load • กด S เมื่อผู้ใช้ต้องการ Save • กด E เมื่อผู้ใช้ต้องการ Edit • กด P เมื่อผู้ใช้ต้องการ Print • กด Q เมื่อผู้ใช้ต้องการ Quit • หากผู้ใช้ไม่กดอักขระ Q โปรแกรมจะวนรอบให้ผู้ใช้ป้อนข้อมูลไปเรื่อย Lab7-1.cpp 2. จงเขียนโปรแกรมเพื่อสร้างเมนูรายการร้านขายอาหารอิตาลี ซึ่งมีอาหาร 2 ชนิดคือ SPAGHETTI และ PASTA ให้เมนูมีตัวเลือก 3 อย่างได้แก่ • 1. SPAGHETTI ราคา 60 บาท • 2. PIZZA ราคา 120 บาท • 3. PASTA ราคา 40 บาท • 4. SOAUP ราคา 80 บาท • 5. Quit • โดยจะทำการวนซ้ำรับข้อมูลการซื้ออาหารของลูกค้าทีละรายเพื่อคำนวณรายรับสะสมแล้วแสดงผลเมื่อปิดร้าน(กด 5) Lab7-2.cpp