타임 스탬프를 초 단위로 수집하는 스크립트를 만들었습니다. 그런 다음이 값의 차이를 계산하려고합니다. 정수가 양수이면 결과는 예상대로입니다.음수를 문자로 연결하는 방법 C 언어
다음 단계로 정수를 문자열로 변환하고 사이에 점으로 연결합니다. 사람이 읽을 수있는 형식으로 읽는 것이 훨씬 쉽습니다.
정수가 음수 일 때 문제가 나타납니다. 무엇보다 먼저 음수 int를 문자열로 변환 할 때 음수 부호를 출력하는 방법을 알아낼 수 없습니다. (예 : 3 초 -3 마이크로 초)
둘째 정수가 모두 음수이고 두 개의 음수 부호가있는 경우 두 번째로 악화 될 수 있습니다. (예 : -3 초 -3 마이크로 초)
내 질문은 현실에서 두 개의 정수를 빼기 위해 부동 소수점으로 연결 한 다음 뺄셈을 적용 할 수있는 다른 대안이 있습니까? (예 : int 3sec int 3 마이크로 초 연결하여 3.3 초 부동)
나는 이것이 가능하면 잘 모르겠다. 그래서이 모든 과정을 거쳤다. (int 문자열에 연결 한 다음 연결). 실행 코드의
샘플 : 출력의
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <sys/timeb.h>
#include <inttypes.h>
#include <assert.h>
#define TIME_CHAR 22
#define PROCESS_CHARACTERS 32
typedef struct rec {
char time_1[TIME_CHAR];
char time_2[TIME_CHAR];
char time_3[TIME_CHAR];
char process[PROCESS_CHARACTERS];
}RECORD;
char *u2s(long unsigned int number) {
RECORD *ptr_record = malloc (sizeof(RECORD));
if (ptr_record == NULL) {
printf("Out of memory!\nExit!\n");
exit(0);
}
const int n = snprintf(NULL, 0, "%lu", number);
assert(n > 0);
char buf[n+1];
snprintf(buf, n+1, "%lu", number);
assert(buf[n] == '\0');
memcpy(ptr_record->process, buf , sizeof buf);
return ptr_record->process;
}
uint32_t ClockGetTime() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (int32_t)ts.tv_sec * 1000000LL + (int32_t)ts.tv_nsec/1000LL;
}
void cleanup(RECORD** ptr_record) {
free(*ptr_record);
*ptr_record = NULL;
}
int main(int argc, char *argv[]) {
char *dot = ".";
RECORD *ptr_record;
ptr_record = malloc (sizeof(RECORD));
if (ptr_record == NULL) {
printf("Out of memory!\nExit!\n");
exit(0);
}
/* Originate Time Stamps for time_1 */
time_t time_1_sec = time(NULL);
uint32_t client_1_sec = time_1_sec;
uint32_t client_1_microsec = ClockGetTime();
char *client_1_sec_string = u2s(client_1_sec);
char *client_1_microsec_string = u2s(client_1_microsec);
memset(ptr_record->time_1 , '\0' , sizeof ptr_record->time_1);
strncat(ptr_record->time_1 , client_1_sec_string , strlen(client_1_sec_string));
strncat(ptr_record->time_1 , dot , strlen(dot));
strncat(ptr_record->time_1 , client_1_microsec_string , strlen(client_1_microsec_string));
/* Originate Time Stamps for time_2 */
time_t time_2_sec = time(NULL);
uint32_t client_2_sec = time_2_sec;
uint32_t client_2_microsec = ClockGetTime();
char *client_2_sec_string = u2s(client_2_sec);
char *client_2_microsec_string = u2s(client_2_microsec);
memset(ptr_record->time_2 , '\0' , sizeof ptr_record->time_2);
strncat(ptr_record->time_2 , client_2_sec_string , strlen(client_2_sec_string));
strncat(ptr_record->time_2 , dot , strlen(dot));
strncat(ptr_record->time_2 , client_2_microsec_string , strlen(client_2_microsec_string));
printf("This is buffer time_2: %s\n",ptr_record->time_2);
int32_t d_positive_sec = client_2_sec - client_1_sec;
int32_t d_positive_microsec = client_2_microsec - client_1_microsec;
printf("This is the positive difference in sec: %"PRId32"\n",d_positive_sec);
printf("This is the positive difference in microsec: %"PRId32"\n",d_possitive_microsec);
int32_t d_negative_sec = client_1_sec - client_2_sec;
int32_t d_negative_microsec = client_1_microsec - client_2_microsec;
printf("This is the negative difference in sec: %"PRId32"\n",d_negative_sec);
printf("This is the negative difference in microsec: %"PRId32"\n",d_negative_microsec);
char *n_sec_string = u2s(d_negative_sec);
char *n_microsec_string = u2s(d_negative_microsec);
memset(ptr_record->time_3 , '\0' , sizeof ptr_record->time_3);
strncat(ptr_record->time_3 , n_sec_string , strlen(n_sec_string));
strncat(ptr_record->time_3 , dot , strlen(dot));
strncat(ptr_record->time_3 , n_microsec_string , strlen(n_microsec_string));
printf("This is negative concatenated %s\n",ptr_record->time_3);
cleanup(&ptr_record);
return 0;
} /* End of main(){} */
샘플 :
This is the positive difference in sec: 0
This is the positive difference in microsec: 37
This is the negative difference in sec: 0
This is the negative difference in microsec: -37
This is negative concatenated 0.18446744073709551579
최고의 연결 한 후 우리는 (0-37)를 볼 것으로 예상한다고 말할 쉽다 가능한 결과는 (-0.37)입니다.
0s + 37us는 0.37s가 아니라 0.000037을 제공합니다! – alk
이것은 사실입니다.이 문제를 해결하려면 어떻게해야합니까? 어떤 제안? – Thanos
caclulation을 수행 할 때 더 작은 해상도로 전환하십시오. 작업이 끝나면 필요한 장치로 다시 확장하십시오. – alk