Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [C++] Сортировка списка по дате рождения (файл)


Автор: battlemanls 29.3.2014, 16:01
Очень нужна помощь умельцев. Помогите пожалуйста отсортировать по дате рождения. На этом примере идет сортировка по году, а мне нужно по дате рождения. 
Код

#include <math.h>
#include <string.h>
#include <tchar.h>
#include <conio.h>
#include <locale>
#include <stdio.h>
#include <iostream>
 
using namespace std;
 
struct student_list
    {
    char first_name[15];
    char second_name[15];
    char two_name[15];
        struct
        {
         unsigned int day,month,year;
        } brithday;
    };
 
int main()
{
    FILE *input, *output;
    int i,n=5;
    char s[255];
    struct student_list bd [10];
    student_list sort[10];
    input=fopen("test.txt","r");
    i=0;
    while(i<n)
    {
        fscanf(input,"%s",&bd[i].first_name);
        fscanf(input,"%s",&bd[i].second_name);
        fscanf(input,"%s",&bd[i].two_name);
        fscanf(input,"%d",&bd[i].brithday.day);
        fscanf(input,"%d",&bd[i].brithday.month);
        fscanf(input,"%d",&bd[i].brithday.year);
        i++;
    }
    setlocale(LC_ALL,"Russian");
    cout<<"\tСписок группы:"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    setlocale(LC_ALL,"Russian");
    for(i=0;i<n;i++)
        {
        cout<<"№ "<<i+1<<"| Ф.И.О. студента: ";setlocale(LC_ALL,"Russian");
        cout<<bd[i].second_name<<" "<<bd[i].first_name<<" "<<bd[i].two_name;setlocale(LC_ALL,"Russian");
        cout<<"|Дата рождения: "<<bd[i].brithday.day<<"/"<<bd[i].brithday.month<<"/"<<bd[i].brithday.year<<"|"<<endl;
        }
    cout<<"------------------------------------------------------------------------------";
 
 
for (i=0;i<n-1;i++)
{
    for (int g=i+1;g<n;g++)
    {
        if(bd[i].brithday.year>bd[g].brithday.year)
        {
        sort[1]=bd[i];
        bd[i]=bd[g];
        bd[g]=sort[1];
        }
    }
}
 
 
 
 
cout<<"\tСписок группы упорядоченный по возрастанию году рождения :"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
    setlocale(LC_ALL,"Russian");
    for(i=0;i<n;i++)
    {
        cout<<"№ "<<i+1<<"| Ф.И.О. студента: ";
        setlocale(LC_ALL,"Russian");
        cout<<bd[i].second_name<<bd[i].first_name<<bd[i].two_name;
        setlocale(LC_ALL,"Russian");
        cout<<"|Дата рождения: "<<bd[i].brithday.day<<"/"<<bd[i].brithday.month<<"/"<<bd[i].brithday.year<<"|"<<endl;
    }
cout<<"------------------------------------------------------------------------------";
 _getch();
}

Автор: Guinness 1.4.2014, 07:55
Цитата(battlemanls @  29.3.2014,  17:01 Найти цитируемый пост)
if(bd[i].brithday.year>bd[g].brithday.year)

Тебе нужно всего лишь исправить это условие под свое задание.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)