所以我得到了关于制作数字时钟的工作。用户设置点头次数,每个点头等于1/n秒,n为用户点头次数。在那之后,我被叫去做三个指针:
- 一秒
- 一分钟
- 一个小时
Seconds指针必须运行循环链接中的所有点。对于完整的旋转,分钟指针应开始移动到下一个节点,在分钟指针总共移动60次之后,小时指针应紧随其后。时钟需要做以下事情:
-
用户可以将时钟设置回零:00:00:00;
-
用户可以设置秒精度的时钟。
-
程序需要在每次活动后显示时钟。
-
用户可以为闹钟编程,在闹钟中时钟向用户显示消息。
-
它需要找到指针(在时钟上而不是程序上)对齐的下一个小时(例如:12:00,01:05,02:10,3:15等)
- 它在释放了列表使用的剩余内存后完成程序
这是我的代码,但我有一些困难。
#include<stdio.h>
#include<stdlib.h>
int counter=0;
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node *pointer, int data)
{
node *start = pointer;
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=start)
{
pointer = pointer -> next;
}
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
pointer = pointer->next;
pointer->data = data;
pointer->next = start;
}
void print(node *start,node *pointer)
{
if(pointer==start)
{
return;
}
printf("%d ",pointer->data);
print(start,pointer->next);
}
int main()
{
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = start;
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/
node *sec,*min,*hour;
int v,c,n;
printf("1. Insert Nn");
printf("2. Make Time Zeron");
printf("3. Set Clockn");
int query;
scanf("%d",&query);
if(query==1)
{
int data,i,n;
printf("Posa n thesn");
scanf("%d",&n);
for (i = 0; i < 60*n; i++)
{
data = i;
insert(start,data);
printf("%dn",i);
}
node *sec_copy;
sec_copy=start;
min=start;
hour=start;
while(n>0)
{
sec_copy=sec_copy->next;
n--;
c++;
if(c == 59*n)
{
min=min->next;
c=0;
v++;
}
if(v == 60)
{
hour=hour->next;
v=0;
}
}
}
printf("%d",sec->data);
if(query==2)
{
int timer;
timer=0;
printf("%.2d:%.2d:%.2d",timer,timer,timer);
}
if(query==3)
{
int h,m,s;
printf("Set me hours");
scanf("%d",&h);
h = h%24;
printf("Set me min");
scanf("%d",&m);
h = h+m/60;
m = m%60;
printf("Set me secs");
scanf("%d",&s);
h = h + s/3600;
m = m + s%3600;
s = s%60;
}
}
正如Jochaim Pileborg所指出的,您确实需要使用调试器来实现这一点。
我看到segfault的唯一原因是您调用:printf("%d",sec->data);
而没有初始化sec
您也不初始化"v"one_answers"c",并且解决方案中有两个"n"变量。