• 29-03-2013, 00:43:14
    #1
    Merhaba r10.net halkı c++ yaptığım uygulamada sıralama sorun yaşıyorum.Ogrenci bilgileri giriliyor bu uygulamada.Bu kodların üzerinde ben verileri giriyorum ekranda yazdırıyorum aşağıdaki kodlar siralamanın başladığı nokta.Sıralama secime göre olacak ben sec değişkeni tanımladım ve ona göre sıralıyorum.

    /*siralama*/

    printf("Siralamak istediginiz alani seciniz Okul No =1 , Ad = 2 , Soyad=3,Dogum Tarihi=\t");
    scanf("%d",&sec);
    for(i=0;i<ogrSayi-1;i++)
    for(j=i+1;j<ogrSayi;j++)
    
    
    
    if(sec==1)/*1 secili ise ögrencinin okul numarasına göre listeyecek*/
    	{
    	 if(okulNo[i]>okulNo[j])
    		   
    				sayitut=okulNo[i];
    				okulNo[i]=okulNo[j];
    				okulNo[j]=sayitut;
    	}
    		 
    	if(sec==2)/*2 secili ise ögrencinin adina göre listeyecek*/
    	{
    		if(strcmp(ogrAdi[i],ogrAdi[j])>=0)
    		
    				strcpy(adtut,ogrAdi[i]);
    				strcpy(ogrAdi[i],ogrAdi[j]);
    				strcpy(ogrAdi[j],adtut);
    		
    	
    	}
    ------- aşağıya dogruda soyadi ve dogum tarihi ile ilerliyor.
    İf kontrolunu mantıklı sağlayamadığım için sıralama başarılı olmuyor yardımcı olurmusunuz.
  • 29-03-2013, 19:26:26
    #2
    öncelikle bi yapı oluşturursanız bilgileri tutmak için daha iyi olur.
    bu yapıları bi dizi içinde tutarsınız.
    sonra sıralama için kullanacağınız algoritmayı seçin.
    en sonda da isteğe göre sıralama yaptırırsınız. misal

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <iostream>
    
    struct ogrenci {
    	int			no;
    	char		ad[64];
    	char		soyad[64];
    	ogrenci() {
    	}
    	ogrenci(int _no, const char* _ad, const char* _soyad) {
    		no = _no;
    		strcpy_s(ad, _ad);
    		strcpy_s(soyad, _soyad);
    	}
    	friend std::ostream& operator<<(std::ostream& os, const ogrenci& o) {
    		os << o.no << " - " << o.ad << ", " << o.soyad;
    		return os;
    	}
    };
    
    struct numaraya_gore_sirala {
    	bool operator()(const ogrenci& o1, const ogrenci& o2) {
    		return o1.no > o2.no;
    	}
    };
    
    struct ada_gore_sirala {
    	bool operator()(const ogrenci& o1, const ogrenci& o2) {
    		return strcmp(o1.ad, o2.ad) > 0;
    	}
    };
    
    //http://www.baskent.edu.tr/~tkaracay/etudio/ders/prg/dataStructures/sorting/BubbleSort/BubbleSort.pdf
    template<class _func>
    void bbSort(std::vector<ogrenci>& arr, _func fnc)
    {
    	bool takas = true;
    	int j = 0;
    	ogrenci yedek;
    	while (takas) {
    		takas = false;
    		j++;
    		for (size_t i = 0; i < arr.size() - j; i++) {
    			if (fnc(arr[i], arr[i + 1])) {
    				yedek = arr[i];
    				arr[i] = arr[i + 1];
    				arr[i + 1] = yedek;
    				takas = true;
    			}
    		}
    	}
    }
    
    
    int main()
    {
    	std::vector<ogrenci> ogrenciler;
    	ogrenciler.push_back(ogrenci(12,"Ahmet", "Mutlu"));
    	ogrenciler.push_back(ogrenci(9,	"Didier", "Drogba"));
    	ogrenciler.push_back(ogrenci(10, "Burak", "Yilmaz"));
    	//***********************************************************
    	for (size_t i = 0; i < ogrenciler.size(); ++i) {
    		std::cout << ogrenciler[i] << std::endl;
    	}
    	//***********************************************************
    	bbSort(ogrenciler, ada_gore_sirala());
    	for (size_t i = 0; i < ogrenciler.size(); ++i) {
    		std::cout << ogrenciler[i] << std::endl;
    	}
    	//***********************************************************
    	bbSort(ogrenciler, numaraya_gore_sirala());
    	for (size_t i = 0; i < ogrenciler.size(); ++i) {
    		std::cout << ogrenciler[i] << std::endl;
    	}
    	return 0;
    }