Что тут не так с переменными (С)? (их значения равны, хотя не должны)
Добрый день. Пишу лабораторную на тему стека. Я хочу, чтобы считывались данные из файла (это расписание) и считалась разница между соседними значениями времени. Но почему то в отладке даже при пустом стеке (когда stackOfTrains.count == 0) tmFinish равен tmStart! (хотя его значение задается только при считывании каждой строки или внутри цикла при непустом стеке). Что тут не так, помогите разобраться пожалуйста. Файл для считывания shedule.txt:
12 10.01
14 10.15
11 10.08
Мой код:
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
//#include <iostream>
#include <stdlib.h>
//#include <fstream>
//#include <unistd.h>
#include <stdio.h>
#include <io.h>
#include <time.h>
struct Stack
{
int A[255][3];
int count;
};
//проверка стека на пустоту
int isEmpty(struct Stack *p)
{
if (p -> count ==0 ) return 1;
else if ( p->count == 255) return -1;
else return 0;
}
void pushIntoStek(struct Stack *p, int elem[3]) // включение в стек
{
int j = 0;
for (j = 0; j <3; j++){
p -> A[p -> count][j] = elem[j];
}
p -> count++;
}
int *returnLastElem(struct Stack *p) // возвращение последнего элемента
{
return p -> A[(p -> count) - 1];
}
int deleteFromStek(struct Stack *p) // удаление из стека
{
if (isEmpty(p) == 1) return -1;
else {
p -> count--;
return 0;
}
}
void printStek(struct Stack *p) // вывод содержимого стека на экран
{
int i = 0, j = 0;
if (p -> count == 0) {
printf("Stek is empty\n");
}
else {
for (i = 0; i< p -> count; i++){
for (j = 0; j < 3; j++){
printf("%d ",p -> A[i][j]);
}
}
printf("\n");
}
}
void main(){
char* nameOfFileInput = "shedule.txt";
FILE *fileInput = NULL;
char str[255];
char* strCur = NULL;
int i;
int numOfString = 0;
char firstWord[30], otherString[30];
int firstWordLen = 0, otherStringLen = 0;
char *space;
int isDig = 1;
struct tm *tmStart, *tmFinish;
int *last;
int train[3] ;//номер; часы, минуты
struct Stack stackOfTrains;
double dfTime;
time_t timeFirst, timeLast, t;
stackOfTrains.count = 0;
timeFirst = time(NULL);
timeLast = time(NULL);
t = time(NULL);
for (i = 0; i<255; i++){
str[i] = "";
}
do {
isDig = 1;
printf("Input a time of waiting\n");
gets(str);
for (i = 0; i < strlen(str); i++){
if (isdigit(str[i]) == 0 )
isDig = 0;
}
if (isDig == 0) printf("The entered value must be a number! \n");
}
while (isDig == 0);
fileInput = fopen( nameOfFileInput, "r" );
if(fileInput == NULL){
printf("Can't oprn a file '%s'",nameOfFileInput);
return 2;
}
while( !feof(fileInput) ) {
strCur = fgets(str,255,fileInput);
if (strCur == NULL){
printf ("\nError reading of file \n");//ERROR
break;
}
tmStart = localtime(&t);
tmFinish = localtime(&t);
sscanf(str, "%d%*[^0-9]%d%*[^0-9]%d", &train[0], &train[1], &train[2]);
//printf("%s %d %d %d \n", str, train[0], train[1], train[2]);
tmStart -> tm_min = train[2];//то, что содержится в текущей строке
tmStart -> tm_hour = train[1];
if ( stackOfTrains.count > 0){
last = returnLastElem(&stackOfTrains);
tmFinish -> tm_min = last[2];//то, что содержится в стеке последняя запись
tmFinish -> tm_hour = last[1];
timeFirst = mktime(tmStart);
timeLast = mktime(tmFinish);
dfTime = difftime(timeLast,timeFirst);
//printf("$d %s %s %s %f\n", stackOfTrains.count, ":::", asctime(tmStart), asctime(tmFinish), dfTime);
}
pushIntoStek( &stackOfTrains, train);
getch();
}
fclose(fileInput);
printStek(&stackOfTrains) ;
getch();
}
Источник: Stack Overflow на русском