//เป็นกรณีศึกษาเกรดของมหาวิทยาลัยรามคำแหงครับ ถ้าจะตัดเป็น A,B,C..
//ก็ทำได้เหมือนกันแต่ว่าต้องมีการเพิ่มและเปลี่ยนค่าบางอย่างนิดหน่อย ลองๆดูน่ะครับ
//สังเกตว่าจะไม่มี if แม้แต่ตัวเดียวเลย
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int Error,G,P,Total,N;
float score;
cout<<"Please put your score : ";
cin>>score;
N=score;
Error=(int)(N/101)*3;//More than 100 then Error
G=(int)(N%101/90)*2;//Get grade 'G' if score between 90 and 100
P=N%101%90/60; //Get grade 'P' if score between 60 and 89;
Total=Error+G+P;//other 'F'(less than 50)
switch(Total){
case 0 : cout<<"You get grade F"; break;
case 1 : cout<<"You get grade P"; break;
case 2 : cout<<"You get grade G"; break;
default : cout<<"Error!";
}
getch();
}
//Please put your score : 92.3
//You get grade G
วันพุธที่ 17 พฤศจิกายน พ.ศ. 2553
switch-case ตัดเกรด ด้วยภาษา C
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ยากมาก
ตอบลบรันแล้วมันผิดค่ะ
ตอบลบ