Ir para conteúdo
Fórum Script Brasil

iTiago019

Membros
  • Total de itens

    1
  • Registro em

  • Última visita

Sobre iTiago019

iTiago019's Achievements

0

Reputação

  1. Olá, estou tentando finalizar uma atividade de C, contudo por conta algum erro de lógica eu não consigo organizar um conjunto de struct. O objetivo é realizar a organização em ordem crescente (dd/mm/yyyy index) e para facilitar o processo eu transformo a data em um inteiro, para assim aplicar o select sort, mas por algum motivo a organização falha... #include <stdio.h> #define MAX 2 typedef struct{ //Type row int day; int month; int year; float index; }row; typedef struct{ //Array of rows row arr[MAX]; //Number of elements int count; } table; inicializeTable(table *edit_table){ //Inicialize a table edit_table->count = 0; } insertItem(table * edit_table, row new_row){ //Update a table edit_table->arr[edit_table->count] = new_row; //Update number of rows edit_table->count++; } void select_sort(table *sort_table){ int i, j, min_idx, d1, m1, y1, d2, m2, y2, days1, days2; row swap; for (i = 0; i < MAX; i++){ min_idx = i; for (j = i+1; j < MAX; j++){ d1 = sort_table->arr[j].day; d2 = sort_table->arr[min_idx].day; m1 = sort_table->arr[j].month; m2 = sort_table->arr[min_idx].month; y1 = sort_table->arr[j].year; y2 = sort_table->arr[min_idx].year; days1 = date_to_days(d1, m2, y1); days2 = date_to_days(d2, m2, y2); if(days1 < days2){ min_idx = j; swap = sort_table->arr[min_idx]; sort_table->arr[min_idx] = sort_table->arr[j]; sort_table->arr[j] = swap; } } } for(i = 0; i <= MAX; i++){ printf("%d %d %d \n", sort_table->arr[i].day, sort_table->arr[i].month, sort_table->arr[i].year); } //dd = sort_table->arr[i].day; //mm = sort_table->arr[i].month; //yyyy = sort_table->arr[i].year; //days = date_to_days(dd, mm, yyyy); } int isLeap(year){ int days = 365; if(year % 400 == 0){ //true, the year is leap days = 366; } else{ if((year % 4 == 0 )&& (year % 100 != 0)){ //true, the year is leap days = 366; } } return days; } int date_to_days(int d, int m, int y){ int months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //check if the year is leap if(isLeap(y) == 366){ //February has 29 days months[1] = 29; } //add days int days = d; int i; //months x days for(i = 0; i <= (m-1); i++){ days += months[i]; } //year x days //Stating year = 2000 for(i = 2000; i < y; i++){ //get days of the year and add days += isLeap(i); } return days; } int main(){ row row1; table table1; inicializeTable(&table1); int i; for(i = 0; i <= MAX; i++){ printf("Enter date (d m Y) and index: "); scanf("%d %d %d %f", &row1.day, &row1.month, &row1.year, &row1.index); insertItem(&table1, row1); } select_sort(&table1); return 0; }
×
×
  • Criar Novo...