【发布时间】:2014-11-05 10:32:45
【问题描述】:
所以我得到了我需要做的关于制作数字时钟的工作。 用户设置点数,每个点数等于 1/n 秒,N 是用户点数。之后我被要求做 3 个指针:
- 一秒
- 一分钟
- 一个小时
秒指针必须运行在循环链接中的所有节点上。对于完整的旋转,分钟指针应该开始移动到下一个节点,并且在分钟指针总共移动 60 次之后,小时指针应该紧随其后。 时钟需要做以下事情:
用户可以将时钟设置回零:00:00:00;
用户可以设置精确到秒的时钟。
程序需要在每次活动后显示时钟。
用户可以将时钟编程为闹钟,其中时钟向用户显示消息。
它需要找到它的指针(在时钟而不是程序上)对齐的下一个小时(例如:12:00,01:05,02:10,03: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 N\n");
printf("2. Make Time Zero\n");
printf("3. Set Clock\n");
int query;
scanf("%d",&query);
if(query==1)
{
int data,i,n;
printf("Posa n thes\n");
scanf("%d",&n);
for (i = 0; i < 60*n; i++)
{
data = i;
insert(start,data);
printf("%d\n",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;
}
}
【问题讨论】:
-
学习如何使用调试器的时间(不是双关语)。使用调试器,您可以逐行执行程序并观察变量的值。这将帮助您了解实际发生的情况。
-
'我遇到了一些困难' - 你能不能再含糊一点?
-
@MartinJames 它说在我进行了一些编辑后,分段错误核心转储了。
-
@JoachimPileborg 你想和我分享吗?将不胜感激!
-
您的意思是您在编译您发布的程序时遇到问题?我是。
标签: c pointers linked-list circular-list