sunu algo05

27
ALGORİTMA VE PROGRAMLAMA Program Karar Verme Komutları

Upload: eyuep-oral

Post on 14-May-2015

202 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Sunu algo05

ALGORİTMA VE PROGRAMLAMAProgram Karar Verme Komutları

Page 2: Sunu algo05

SWİTCH

Page 3: Sunu algo05

Kullanımı: 

switch ( değişken){

case Sabit1 :

KomutListesi1

break;....case Sabitn :

KomutListesin

break;default:

KomutListesin+1

}

Page 4: Sunu algo05

değişken, tamsayı veya tamsayı uyumlu değişken

Sabiti, tamsayı veya tamsayı uyumlu bir değer

default, seçeneğe bağlı (olması zorunlu değil)

KomutListesii, komutlar dizisi

Page 5: Sunu algo05

switch deyimi çalıştırıldığında değişken değerlendirilir. değişken’in değeri case listesinde varsa KomutListesii, break deyimine, return deyimine veya switch deyimi sonuna kadar çalıştırılır.

Page 6: Sunu algo05

değişken’in değeri case listesinde yoksa default deyimindeki KomutListesin+1 çalıştırılır.

default deyimi bulunmuyorsa çalıştırma işlemi switch bloğundan sonra devam eder.

Page 7: Sunu algo05

ÖRNEK: HERHANGİ BİR AYIN NUMARASI GİRİLDİĞİNDE (1-12) O AYIN ADINI YAZAN

PROGRAM.

// swicthDeyimi.cpp : main project file.

#include "stdafx.h" #include<iostream> #include<stdlib.h> using namespace std;

int main () { int AyNo; cout<<"Kacinci ay: "; cin>>AyNo;

Page 8: Sunu algo05

switch (AyNo) { case 1: cout<<"Ocak"; break; case 2: cout<<"Subat"; break; case 3: cout<<"Mart"; break; case 4: cout<<"Nisan"; break; case 5: cout<<"Mayis"; break; case 6: cout<<"Haziran"; break; case 7: cout<<"Temmuz"; break; case 8: cout<<"Agustos"; break; case 9: cout<<"Eylul"; break; case 10: cout<<"Ekim"; break; case 11: cout<<"Kasim"; break; case 12: cout<<"Aralik"; break; default: cout<<"Yanlis giris!..."<<endl; } cout<<endl; system("PAUSE"); }

Page 9: Sunu algo05

Ekran çıktısı: Kacinci ay: 5Mayis

Page 10: Sunu algo05

YUKARIDAKİ PROGRAMDA SWİTCH BLOĞU AŞAĞIDAKİ GİBİ (BREAK KOMUTLARI UNUTULMUŞ OLSUN) YAZILMIŞ OLSUN.

#include<iostream> #include<stdlib.h> using namespace std;

int main () { int AyNo; cout<<"Kacinci ay: "; cin>>AyNo;

Page 11: Sunu algo05

YUKARIDAKİ PROGRAMDA SWİTCH BLOĞU AŞAĞIDAKİ GİBİ (BREAK KOMUTLARI UNUTULMUŞ OLSUN) YAZILMIŞ OLSUN.

switch (AyNo) { case 1: cout<<"Ocak"; case 2: cout<<"Subat"; case 3: cout<<"Mart"; case 4: cout<<"Nisan"; case 5: cout<<"Mayis"; case 6: cout<<"Haziran"; case 7: cout<<"Temmuz"; case 8: cout<<"Agustos"; case 9: cout<<"Eylul"; case 10: cout<<"Ekim"; case 11: cout<<"Kasim"; case 12: cout<<"Aralik"; default: cout<<"Yanlis giris!..."; } cout<<endl; system("PAUSE"); }

Page 12: Sunu algo05

Bu durumda ekran çıktısı: Kacinci ay: 10EkimKasimArlikYanlis giris!...

Page 13: Sunu algo05
Page 14: Sunu algo05

ÖRNEK:

Öğrencinin ortalama notu (0-100) girildiğinde harf cinsinden karşılık gelen notunu bulup yazan program.

  Ortalama Harf --------------- ------ Ort>=90 A 80<=Ort<90 B 70<=Ort<80 C 60<=Ort<70 D Ort<60 F

Page 15: Sunu algo05

#include "stdafx.h" #include<iostream> #include<stdlib.h> using namespace std;

int main () { float Ort; char HarfNot; cout<<"Ortalama not: "; cin>>Ort;

Page 16: Sunu algo05

switch ( int (Ort/10) ) // Sonucu int yapmak icin { case 10: case 9: HarfNot = 'A'; break; case 8: HarfNot = 'B'; break; case 7: HarfNot = 'C'; break; case 6: HarfNot = 'D'; break; default: HarfNot = 'F'; } cout<<"Harf not: "<<HarfNot<<endl; system("PAUSE"); }

Page 17: Sunu algo05
Page 18: Sunu algo05

ÖRNEK: BASİT 4 İŞLEM YAPAN PROGRAMI YAZINIZ.

// notlar9.cpp : main project file.

#include "stdafx.h" #include <iostream> #include <conio.h>

using namespace std;

int main(array<System::String ^> ^args) { float a, b, sonuc; char islem;

Page 19: Sunu algo05

cout << "1. sayi: "; cin >> a; cout << "2. sayi: "; cin >> b; cout << "Istenen islem (+, -, *, /): "; cin >> islem; switch( islem ) { case '+': sonuc = a + b; break; case '-': sonuc = a - b; break; case '*': sonuc = a * b; break;

Page 20: Sunu algo05

case '/': sonuc = a / b; break; } cout << a << " " << islem << " " << b << " = " << sonuc; getch(); return 0; }

Page 21: Sunu algo05
Page 22: Sunu algo05

AYNI PROGRAMIN FARKLI YAZIMI

#include "stdafx.h" #include <iostream> #include <conio.h>

using namespace std;

int main(array<System::String ^> ^args) { float a, b, sonuc; char islem; cout << "birinci sayiyi, islemi(+, -, *, /) ve ikinci sayiyi

giriniz "; cin >> a >> islem >> b;

Page 23: Sunu algo05

switch( islem ) { case '+': sonuc = a + b; break; case '-': sonuc = a - b; break; case '*': sonuc = a * b; break; case '/': sonuc = a / b; break; } cout << a << " " << islem << " " << b << " = " << sonuc; getch(); return 0; }

Page 24: Sunu algo05
Page 25: Sunu algo05

SORULAR:

1. Fahrenheit’ten Celsius’a, Celsius’tan Fahrenheit’a, Fahrenheit’tan Kelvin’e, Kelvin’den Fahrenheit’e, Celsius’tan Kelvine’e veya kelvin’den Celsius’a sıcaklık dönüşümü yapan bir program yazınız.

 Not: switch kullanılacak ve aşağıdaki ekran çıktısı görümüne sahip olacak.

Page 26: Sunu algo05

Ekran: Dönüştürmek istediğiniz sıcaklık değeri: 212 Seçenekler:A- Fahrenheit’tan Celsius’aB- Celsius’tan Fahrenheit’aC- Fahrenheit’tan Kelvin’eD- Kelvin’den Fahrenheit’aE- Kelvin’den Celsius’aF- Celsius’tan Kelvine’eSeçiminiz: A Dönüştürülen sıcaklık: 100

Page 27: Sunu algo05

Dönüşümler: C = (F-32) / 1.8C = K – 273;K = (F-32)/1.8 + 273