โครงสร้างข้อมูล C++ |
|
อ้างอิง
อ่าน 770 ครั้ง / ตอบ 0 ครั้ง
|
sanookzaa
|
บทความเกี่บวกับการใช่ if else , if elseif , switch , random , for , function เป็น Code กรณีศึกษา วิชาโครงสร้างข้อมูล
Lecture 1
1. เขียนโปรแกรมหารเลขธรรมดา รับค่า 2 ตัว ถ้าตัวหารไม่เท่ากับ 0 ค่อยทำการหาร แต่ถ้าเป็น 0 ให้จบโปรแกรมไปเลย (เรื่อง if else)
01. #include <iostream.h>
02. int main(){
03. int in1,in2,sum;
04.
05. cout << "Enter a number : " ;
06. cin >> in1 ;
07.
08. cout << "Enter a number is Divided( != 0): " ;
09. cin >> in2 ;
10.
11. if ( in2 != 0 ) {
12. sum = in1 / in2 ;
13. cout << in1 << " / " << in2 << " = " << sum ;
14. } else {
15. cout << "end program." ;
16. }
17. }
2. เขียนโปรแกรมตัดเกรด โดยนักศึกษากำหนดช่วงเองตามใจ (ตัดอิงเกณฑ์) (เรื่อง if else if)
01. #include <iostream.h>
02. int main() {
03.
04. int score ;
05.
06. cout << "Enter your score : " ;
07. cin >> score ;
08.
09. if ( score >= 80 ) { cout << "Your grad A" ; }
10. else if (score >= 70 ) { cout << "Your grad B " ; }
11. else if (score >= 60 ) { cout << "Your grad C " ; }
12. else if (score >= 50 ) { cout << "Your grad D " ; }
13. else if (score <= 49 ) { cout << "Your grad F " ; }
14. else { cout << "End Program." ; }
15.
16. }
3. เขียนโปรแกรมเครื่องคิดเลข บวก ลบ คูณ หาร โดยรับค่าเข้าไป 2 ตัว แล้วเลือกว่าจะให้ทำอะไร (เรื่อง switch case)
01. #include <iostream.h>
02. int main() {
03. double n1 , n2 ;
04. char op ;
05.
06. cout << "Enter a number is 1 : " ;
07. cin >> n1 ;
08.
09. cout << "Enter a number is 2 : " ;
10. cin >> n2 ;
11.
12. cout << "\nnumber 1 = " << n1 << "\tnumber 2 = " << n2 << "\n" ;
13. cout << "You want to do ? ( + , - , * , / ) : " ;
14. cin >> op ;
15.
16. switch (op) {
17. case '' + '' : cout << n1 << " + " << n2 << " = " << n1+n2 ; break ;
18. case '' - '' : cout << n1 << " - " << n2 << " = " << n1-n2 ; break ;
19. case '' * '' : cout << n1 << " * " << n2 << " = " << n1*n2 ; break ;
20. case '' / '' : cout << n1 << " / " << n2 << " = " << n1/n2 ; break ;
21. default : cout << "End Program. " ;
22. }
23.
24. }
4. เขียนโปรแกรมสูตรคูณ เลือกแม่ได้ และเลือกได้ว่าให้ run จาก1 ไปจนถึงเท่าไหร่ (เรื่อง for)
01. # include <iostream.h>
02. int main() {
03. int st,row;
04.
05. cout << "Do You Want to Multiplication table : " ;
06. cin >> st ;
07.
08. cout << "Enter the number you want to multiply : " ;
09. cin >> row ;
10.
11. for ( int count = 1 ; count <= row ; count ++ ){
12. cout << st << " * " << count << " = " << st * count << "\n" ;
13. }
14.
15. }
5. เขียนโปรแกรมสุ่มเลข (random) เลขขึ้นมา 1 ตัวตั้งแต่ 0 - 100 แล้วให้เราทาย จนกว่าจะถูกต้อง (เรื่อง while)
01. # include <iostream.h>
02. # include <stdlib.h>
03. # include <time.h>
04. int main() {
05. srand(time(0)) ;
06. int number = rand() % 100 , i_n ;
07.
08. cout << "Mind games mate !! \n" ;
09. cout << "Enter a number ( 0 - 100 ) : " ;
10. cin >> i_n ;
11.
12. while (1){
13. if ( i_n == number ){
14. cout << "Your Win !! " ;
15. break ;
16. } else {
17. cout << "No, Please enter numbers again : " ;
18. cin >> i_n ;
19. }
20. }
21. }
Lecture 2
1. เขียนโปรแกรมตัดเกรด โดยนิสิตกำหนดช่วงเองตามใจ (ตัดอิงเกณฑ์)
01. # include <iostream.h>
02. char Grad(int);
03. int main() {
04.
05. int score ;
06.
07. cout << "Enter your score : " ;
08. cin >> score ;
09.
10. cout << "Your Grad " << Grad(score) ;
11.
12. }
13. char Grad( int score ){
14. if ( score >= 80 ) { return '' A '' ; }
15. else if (score >= 70 ) { return '' B '' ; }
16. else if (score >= 60 ) { return '' C '' ; }
17. else if (score >= 50 ) { return '' D '' ; }
18. else if (score <= 49 ) { return '' F '' ; }
19. else { return '' I '' ; }
20.
21. }
Note : ใน Code ด้านบน ตรงที่ return "A" ให้เปลี่ยนเป็น return ''A'' นะครับ และเปลี่ยนให้ครบทุกตัว จนถึง I ครับ
2. เขียนโปรแกรมสูตรคูณ เลือกแม่ได้ และเลือกได้ว่าให้ run จาก1 ไปจนถึงเท่าไหร่ก็ได้
01. # include <iostream.h>
02. void MT(int,int);
03. int main() {
04. int st,row;
05.
06. cout << "Do You Want to Multiplication table : " ;
07. cin >> st ;
08.
09. cout << "Enter the number you want to multiply : " ;
10. cin >> row ;
11.
12. MT(st,row);
13.
14.
15. }
16. void MT(int st, int row){
17. for ( int count = 1 ; count <= row ; count ++ ){
18. cout << st << " * " << count << " = " << st * count << "\n" ;
19. }
20. }
3. ทั้งสองข้อ เคยทำไปแล้วเมื่อคราวก่อน แต่คราวนี้ให้นิสิตเขียนโปรแกรมแบบฟังก์ชัน คือ ให้แยกงานออกเป็นส่วนๆ ไม่ให้เขียนให้ทำทุกอย่างใน main()
|
|
sanookzaa sanookzaweb@hotmail.co.th [110.49.250.xxx] เมื่อ 14/01/2017 13:57
|