sunu algo05

Post on 14-May-2015

195 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ALGORİTMA VE PROGRAMLAMAProgram Karar Verme Komutları

SWİTCH

Kullanımı: 

switch ( değişken){

case Sabit1 :

KomutListesi1

break;....case Sabitn :

KomutListesin

break;default:

KomutListesin+1

}

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

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.

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.

Ö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;

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"); }

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

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;

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"); }

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

Ö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

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

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

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"); }

Ö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;

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;

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

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;

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; }

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.

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

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

top related