年月日转换-指针法

starcat 2009-08-02 11:03:42
想用数组指针试试。不想用参考答案里的字符指针方法。
不知道怎么摆平编译时的两个报错消息,请帮帮忙,谢谢。

#include <stdio.h>
static char daytab[][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static char (*arrptr)[13];
arrptr = daytab;


int day_of_year(int year, int month, int day)
{
int i, leap;
leap = year % 4 == 0 && year %100 != 0 || year % 400 == 0;
for (i = 1; i < month; i++)
day += *(*(arrptr+leap) + i);
return day;
}

void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
leap = year % 4 == 0 && year %100 != 0 || year % 400 == 0;
for (i = 1; yearday > *(*(arrptr+leap) + i); i++)
yearday -= *(*(arrptr+leap) + i);
*pmonth = i;
*pday = yearday;
}

int main()
{
int year, month, yearday, day;
int *pmonth, *pday;
printf("Enter the year, month and day:\n");
scanf("%d%d%d", &month, &day, &year);
printf("the %dth day of the year.\n", day_of_year(year, month, day));

printf("Enter the year and yearday:\n");
scanf("%d%d", &year, &yearday);
printf("the %dth day of month %d.\n", *pmonth, *pday);

return 0;
}
...全文
130 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mu_yang 2009-08-03
static char daytab[][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
=========
这个看着就别扭
回复
ies_sweet 2009-08-03
[Quote=引用 3 楼 allen1986 的回复:]
这个是网上找的解释:

我的观点是这样的:
由于i是全局变量,所以它将静态分配到内存的全局静态区;
所以当你
int i;
i=10;
编译器会默认是两个不同的全局变量,所以先打印一条“error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int“,然后编译器因为两个变量的名字相同,接着有第二条”error C2086: “int q”: 重定义”

所以我们一般定义全局变量都是在声明的同时赋初值!

[/Quote]

更倾向于在运行时赋初值。
回复
FigoZhu 2009-08-03
[Quote=引用 4 楼 vshuang 的回复:]
printf("Enter theyear, month and day:\n");
scanf("%d%d%d",&month, &day, &year);

亲自动手调试了好一会儿,发现卡在这里
[/Quote]

scanf("%d-%d-%d",&month, &day, &year);

输入的时候,2009-8-3
回复
wanghao111 2009-08-03
[Quote=引用 1 楼 baihacker 的回复:]
C/C++ code#include<stdio.h>staticchar daytab[][13]= {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};staticchar (*arrptr)[13]= daytab;//像这样int day_of_yea¡­
[/Quote]
hacker
回复
starcat 2009-08-03
[Quote=引用 8 楼 mu_yang 的回复:]
static char daytab[][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
=========
这个看着就别扭
[/Quote]

这是照搬Dennis Ritchie的写法
回复

printf("Enter the year, month and day:\n");
scanf("%d%d%d", &month, &day, &year);

亲自动手调试了好一会儿,发现卡在这里
回复
allen1986 2009-08-02
这个是网上找的解释:

我的观点是这样的:
由于i是全局变量,所以它将静态分配到内存的全局静态区;
所以当你
int i;
i=10;
编译器会默认是两个不同的全局变量,所以先打印一条“error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int“,然后编译器因为两个变量的名字相同,接着有第二条”error C2086: “int q”: 重定义”

所以我们一般定义全局变量都是在声明的同时赋初值!
回复
mstlq 2009-08-02
只要搞定编译错误就可以了吗^_^?
那么请把arrptr = daytab这一句语句放入main函数中……

好了,楼主可以开始调试了,good luck^_^
回复
baihacker 2009-08-02
#include <stdio.h> 
static char daytab[][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static char (*arrptr)[13] = daytab; //像这样


int day_of_year(int year, int month, int day)
{
int i, leap;
leap = year % 4 == 0 && year %100 != 0 || year % 400 == 0;
for (i = 1; i < month; i++)
day += *(*(arrptr+leap) + i);
return day;
}

void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
leap = year % 4 == 0 && year %100 != 0 || year % 400 == 0;
for (i = 1; yearday > *(*(arrptr+leap) + i); i++)
yearday -= *(*(arrptr+leap) + i);
*pmonth = i;
*pday = yearday;
}

int main()
{
int year, month, yearday, day;
int *pmonth, *pday;
printf("Enter the year, month and day:\n");
scanf("%d%d%d", &month, &day, &year);
printf("the %dth day of the year.\n", day_of_year(year, month, day));

printf("Enter the year and yearday:\n");
scanf("%d%d", &year, &yearday);
printf("the %dth day of month %d.\n", *pmonth, *pday);

return 0;
}
回复
发动态
发帖子
C语言
创建于2007-09-28

6.2w+

社区成员

C语言相关问题讨论
申请成为版主
社区公告
暂无公告