用c语言1. 显示(系统)当前的日期 (年、月、日、星期)、时间(时、分、秒) 2. 初始化(空)链表(创建哨兵结点) 3. 显示用户(功能)选择菜单,并获得用 户的(功能)选择 5、创建一个有2022个节点的(无序)链 表,链表每个结点存放的数据为 11922,20251之问的日期 (年、月、日) 数据(随机产生) 6、输出链表的前n个结点或整个链表所 有结点的数据 了、将整个链表的所有数据都存入一个文件中 8、(分屏、加行号)显示链表数据文件的 (全部)内容 ,、显示(所有)不能构成合法日期的 结点的代码
时间: 2023-04-09 07:01:23 浏览: 164
time_C语言时间获取函数_
1. 显示(系统)当前的日期(年、月、日、星期)、时间(时、分、秒):
这可以使用C语言中的time.h头文件中的函数来实现。具体代码如下:
```c
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("当前日期:%d年%d月%d日 星期%d\n", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday);
printf("当前时间:%d:%d:%d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
return 0;
}
```
2. 初始化(空)链表(创建哨兵结点):
链表的哨兵结点是链表中的一个特殊结点,它不存储任何数据,只是用来占据链表中的第一个位置,方便链表的操作。具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
int main() {
Node *head = (Node *) malloc(sizeof(Node));
head->next = NULL;
return 0;
}
```
3. 显示用户(功能)选择菜单,并获得用户的(功能)选择:
这可以使用C语言中的scanf函数来实现。具体代码如下:
```c
#include <stdio.h>
int main() {
int choice;
printf("请选择功能:\n");
printf("1. 功能1\n");
printf("2. 功能2\n");
printf("3. 功能3\n");
scanf("%d", &choice);
printf("您选择了功能%d\n", choice);
return 0;
}
```
5. 创建一个有2022个节点的(无序)链表,链表每个结点存放的数据为 11922,20251之问的日期(年、月、日)数据(随机产生):
具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node {
int year;
int month;
int day;
struct Node *next;
} Node;
int main() {
Node *head = (Node *) malloc(sizeof(Node));
head->next = NULL;
Node *p = head;
srand(time(NULL));
for (int i = 0; i < 2022; i++) {
Node *node = (Node *) malloc(sizeof(Node));
node->year = rand() % 100 + 1920;
node->month = rand() % 12 + 1;
node->day = rand() % 31 + 1;
p->next = node;
p = node;
}
return 0;
}
```
6. 输出链表的前n个结点或整个链表所有结点的数据:
具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int year;
int month;
int day;
struct Node *next;
} Node;
void printList(Node *head, int n) {
Node *p = head->next;
int i = 0;
while (p != NULL && i < n) {
printf("%d年%d月%d日\n", p->year, p->month, p->day);
p = p->next;
i++;
}
}
int main() {
Node *head = (Node *) malloc(sizeof(Node));
head->next = NULL;
Node *p = head;
for (int i = 0; i < 2022; i++) {
Node *node = (Node *) malloc(sizeof(Node));
node->year = 2022;
node->month = 1;
node->day = i + 1;
p->next = node;
p = node;
}
printList(head, 10);
return 0;
}
```
7. 将整个链表的所有数据都存入一个文件中:
具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int year;
int month;
int day;
struct Node *next;
} Node;
void saveList(Node *head, char *filename) {
FILE *fp = fopen(filename, "w");
Node *p = head->next;
while (p != NULL) {
fprintf(fp, "%d年%d月%d日\n", p->year, p->month, p->day);
p = p->next;
}
fclose(fp);
}
int main() {
Node *head = (Node *) malloc(sizeof(Node));
head->next = NULL;
Node *p = head;
for (int i = 0; i < 2022; i++) {
Node *node = (Node *) malloc(sizeof(Node));
node->year = 2022;
node->month = 1;
node->day = i + 1;
p->next = node;
p = node;
}
saveList(head, "list.txt");
return 0;
}
```
8. (分屏、加行号)显示链表数据文件的(全部)内容:
具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
void showFile(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
int line = 1;
char buffer[1024];
while (fgets(buffer, 1024, fp) != NULL) {
printf("%d: %s", line, buffer);
line++;
if (line % 10 == 0) {
printf("按任意键继续...");
getchar();
}
}
fclose(fp);
}
int main() {
showFile("list.txt");
return 0;
}
```
9. 显示所有不能构成合法日期的结点的代码:
具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int year;
int month;
int day;
struct Node *next;
} Node;
void printInvalidNodes(Node *head) {
Node *p = head->next;
while (p != NULL) {
if (p->year < 1900 || p->year > 2100 || p->month < 1 || p->month > 12 || p->day < 1 || p->day > 31) {
printf("%d年%d月%d日\n", p->year, p->month, p->day);
}
p = p->next;
}
}
int main() {
Node *head = (Node *) malloc(sizeof(Node));
head->next = NULL;
Node *p = head;
for (int i = 0; i < 2022; i++) {
Node *node = (Node *) malloc(sizeof(Node));
node->year = 2022;
node->month = 1;
node->day = i + 1;
p->next = node;
p = node;
}
Node *node1 = (Node *) malloc(sizeof(Node));
node1->year = 2022;
node1->month = 2;
node1->day = 30;
p->next = node1;
p = node1;
Node *node2 = (Node *) malloc(sizeof(Node));
node2->year = 2022;
node2->month = 4;
node2->day = 31;
p->next = node2;
p = node2;
printInvalidNodes(head);
return 0;
}
```
阅读全文