用C语言实现输入人员姓名并在之后实时签到
时间: 2023-05-28 12:04:09 浏览: 64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_NAME_LENGTH 50
#define MAX_RECORDS 1000
struct Record {
char name[MAX_NAME_LENGTH];
time_t timestamp;
};
int main() {
struct Record records[MAX_RECORDS];
int num_records = 0;
char name[MAX_NAME_LENGTH];
while (1) {
printf("Please enter your name (or 'q' to quit): ");
scanf("%s", name);
if (strcmp(name, "q") == 0) {
break;
}
time_t current_time = time(NULL);
struct tm* tm_info = localtime(¤t_time);
char timestamp[20];
strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", tm_info);
strncpy(records[num_records].name, name, MAX_NAME_LENGTH);
records[num_records].timestamp = current_time;
num_records++;
printf("%s signed in at %s\n", name, timestamp);
}
printf("Sign-in records:\n");
for (int i = 0; i < num_records; i++) {
struct tm* tm_info = localtime(&records[i].timestamp);
char timestamp[20];
strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s signed in at %s\n", records[i].name, timestamp);
}
return 0;
}