Welcome Guest Search | Active Topics | Members | Log In | Register

การใช้งาน if ,CLOCK และการตรวจสอบค่าใน c++ Options · View
paedotnet
Posted: Monday, June 23, 2008 11:05:54 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 328
Location: bkk

การใช้งาน if ใน C++

รูปแบบ
if( condition )
  statement;

หรือ
if( condition )
{
  statement1;
  statement2;
  ...
  statementn;
}

ตัวอย่างการนับจำนวนตัวอักษร

#include <iostream>
#include <conio.h>
using namespace std;
int main(){
char chr;
int count = 0;
cin>>chr; //ให้ผู้ใช้ใส่ตัวอักษรเข้ามา
while(true){ //วนลูปไปเรื่อยๆ
 if(chr == '#') //ถ้าเจอ เครื่องหมาย #
    break; //ให้ออกจากลูป
count++;//เพิ่มค่าตัวแปร count
cin>>chr; //รับค่าตัวถัดไปในกรณีที่ยังไม่เจอ #
}
cout<<"total characher is  : "<<count<<endl;
getch();
return 0;
}
 

ผลลัพธ์จะได้ดังรุป

การใช้งาน  else
คล้ายๆกับ if แต่ถ้าเงื่อนไขใน if ไม่เป็นจริงก็จะทำงานใน else

การใช้่งาน else if
ก็จะคล้ายๆกับ if แต่ถ้าเงื่อนไขใน if ไม่เป็นจริงก็จะตรวจสอบเงื่อนไขถัดไปใน else if
ตัวอย่าง

#include <iostream>
using namespace std;
int main(){
int number;
int count = 10;
while(1){ //วนลูปไปเรื่อยๆ
cin>>number;
if(number < count)
   cout<<number<<"<"<<count<<endl;
else if(number > count)
   cout<<number<<">"<<count<<endl;
else //ถ้า number == count ให้ ออกจาก ลูป while
{
 break;
}
}
return 0;
}

 

ผลลัพธ์ แสดงดังรูป


ในตัวอย่างนี้ถ้าในเลข 10 ก็จะออกจาก ลูปทันที

การตรวจสอบเงื่อนไขว่าตัวอักษรที่รับเข้ามานั้นเป็นตัวใหญ่หรือเป็นตัวเลขหรือไม่
จะใช้ฟังก์ชั่นในคลาน cctype
ในการตรวจสอบว่าเป็นตัวเลขหรือไม่จะใช้ฟังก์ชั่นที่ชื่อ isdigit
ในการตรวจสอบว่าเป็นตัวอักษรหรือไม่จะใช้ฟังก์ชั่นที่ชื่อ isalpha
ในการตรวจสอบว่ากด ctrl หรือไม่ก็จะใช้ cntrl()

ตัวอย่าง

#include <iostream>
#include <cctype>
#include <conio.h>
using namespace std;
int main(){
int characters = 0;
int digits = 0;
int otherchar = 0;
char chr;
cin.get(chr);
while(cin.fail()==false){
   if(isdigit(chr))
    digits++;
   else if(isalpha(chr))
    characters++;
  
   cin.get(chr);
}
cout<<"charachter :"<<characters<<"\n"<<"digit  :"<<digits<<endl;
getch(); //อยู่ใน conio.h เพือรอการรับค่ามาจาก keyboard ก่อนที่จะจบการทำงานของโปรแกรม
return 0;
}

 

ผลลัพธ์ แสดงดังรูป

การใช้งาน switch
ในกรณีที่มีหลายๆเงื่อนไขเราก็สามารถใช้ switch ได้ถ้าเงื่อนไขตรงกับ case ใดก็จะทำงานตามคำสั่งของ case นั้นๆ
รูปแบบ

switch ( integer/character - expresion )
{
 case label1 : statement1;
 case label2 : statement2;
 case label3 : statement3;
 case labeln : statementn;
 default     : statement;
}

ตัวอย่าง

#include <iostream>
using namespace std;
int main(){
int input;
cin>>input;
switch(input){
case 1 : cout<<"1";break;
case 2 : cout<<"2";break;
case 3 :
case 4 :
      { //กรณีที่รับค่าเข้ามาเป็น 3 หรือ 4
       cout<<"3 and 4";break;
   }
default : cout<<"> 4";break;
}
return 0;
}

 

ผลลัพธ์ จะได้ดังรูป

การใช้งาน CLOCK
ในการใช้งาน CLOCK เพื่อกำหนดเวลา จะต้อง เรียกใช้งาน ฟังก์ชั่นจาก library ที่ชื่อ ctime
ตัวอย่าง ถ้าต้องการให้โปรแกรมหยุดรอเวลาให้ครบ 7 วินาทีก่อนที่จะออกจากโปรแกรมก็สามารถเขียนได้ดังนี้

#include <iostream>
#include <ctime>
using namespace std;
int main(){
clock_t wait_time = 7*CLOCKS_PER_SEC;
clock_t start_time = clock();
while(clock() - start_time < wait_time){ //โปรแกรมจะหน่วงเวลา 7 วินาทีก่อนที่จะออกจากโปรแกรม
}
return 0;
}
 


[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.

Sponsored by