Используйте упорядоченный список Затем вычислите соотношение между двумя значениями времени и, наконец, отобразите температуру и шум
Для 2014-02-13T13: 40:00 получить
Время: 20140213134000 Температура: 6,666667 Гул: 67,333336
#include <stdio.h>
#include <stdlib.h>
struct metrhseis{
long long int time;
int temp;
int hum;
struct metrhseis *next;
};
long long int string_To_long(const char arr[]);
void interpolation_search(struct metrhseis *list, const char * in);
void add_metrhseis(struct metrhseis **list, const char *time, int temp, int hum);
void list_delete(struct metrhseis **list);
void list_print(struct metrhseis *list);
int main(){
char in[25];
struct metrhseis *list = NULL;
add_metrhseis(&list, "2014-02-13T06:20:00", 3.0, 93);
add_metrhseis(&list, "2014-02-13T13:50:00", 7.0, 66);
add_metrhseis(&list, "2014-02-13T06:00:00", 2, 91);
add_metrhseis(&list, "2014-02-13T03:00:00", 3, 84);
add_metrhseis(&list, "2014-02-13T13:00:00", 6, 62);
add_metrhseis(&list, "2014-02-13T18:50:00", 4.0, 75);
add_metrhseis(&list, "2014-02-13T13:20:00", 6.0, 70);
add_metrhseis(&list, "2014-02-13T15:00:00", 6, 56);
add_metrhseis(&list, "2014-02-13T08:50:00", 4.0, 87);
add_metrhseis(&list, "2014-02-13T21:50:00", 4.0, 75);
add_metrhseis(&list, "2014-02-13T08:00:00", 3, 88);
list_print(list);
printf("Date:");
scanf("%s",in);
interpolation_search(list, in);
list_delete(&list);
}
void list_print(struct metrhseis *m) {
printf("List\n");
do {
printf("%lld:%d,%d\n", m->time, m->temp, m->hum);
m = m->next;
} while (m);
}
void list_delete(struct metrhseis **list) {
struct metrhseis *m = *list;
if (m->next) {
list_delete(&m->next);
}
free(m);
*list = NULL;
}
void add_metrhseis(struct metrhseis **list, const char *time, int temp, int hum)
{
struct metrhseis *current = *list;
struct metrhseis *m = malloc(sizeof(struct metrhseis));
m->time = string_To_long(time);
m->temp = temp;
m->hum = hum;
if (*list == NULL || (*list)->time > m->time) {
m->next = *list;
*list = m;
return;
}
while (current->next && current->next->time <= m->time) {
current = current->next;
}
m->next = current->next;
current->next = m;
}
void interpolation_search(struct metrhseis *list, const char * in) {
long long int time = string_To_long(in);
float ratio;
if (time < list->time) {
printf("Time: %s Temperatura: %d Hum: %d\n", in, list->temp, list->hum);
return;
}
while(list->next && list->next->time < time) {
list = list->next;
}
if (!list->next) {
printf("Time: %s Temperatura: %d Hum: %d\n", in, list->temp, list->hum);
return;
}
ratio = (float)(time - list->time) / (list->next->time - list->time);
printf("Time: %s Temperatura: %f Hum: %f\n",
in,
list->temp + ratio * (list->next->temp - list->temp),
list->hum + ratio * (list->next->hum - list->hum )
);
return;
}
long long string_To_long(const char arr[]) {
long long int n = 0;
for (int i = 0; arr[i] != '\0'; i++) {
char c = arr[i];
if (c >= '0' && c <= '9')
n = n * 10 + (c - '0');
}
return n;
}