看看我这几道程序有什么问题?

ham163 2005-12-30 02:10:27
3,已知两个日期(YYMMDD),编写一函数,计算两个日期相差的天数。
#include "stdio.h"
#include "stdlib.h"
struct data
{int year;int month;int day;};

calculate(struct data* p1,struct data* p2)

void main()
{
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
struct data* p,*p1,*p2;
p1=p=(struct data*)malloc(sizeof(struct data));
scanf("%d%d%d",&p1->year,&p1->month,&p1->day);
p2=p=(struct data*)malloc(sizeof(struct data*));
scanf("%d%d%d",&p2->year,&p2->month,&p2->day);
calculate(struct data* p1,struct data* p2)
}

void calculate(struct data* p1,struct data* p2)
{
int year,sum1,sum2,sum=0;int year_days=365;sum1=0;sum2=0;
if(p1->year>p2-year)
{
sum=(p1->year-p2-year)*year_days;
year=p2->year;
for(year;year<p1->year;year++)
{
if((year%4==0&&year%100!=0)||year%400==0)
sum=sum+1;
}
For(int i=1;i<p1->month;i++)
{sum1+=a[i];
}
For(int i=1;i<p2->month;i++)
{sum2+=a[i];
}
If(((p2->year%4==0&&p2->year%100!=0)||p2->year%400==0)&&p2->month>2)
Sum2=sum2+1;
Sum1=sum1+p1->day;sum2=sum2+p2->day;sum=sum+(sum1-sum2);
}
if(p1->year<p2->year){
sum=(p2->year-p1-year)*year_days;
year=p1->year;
for(year;year<p2->year;year++)
{if((year%4==0&&year%100!=0)||year%400==0)
sum=sum+1;
}
For(int i=1;i<p2->month;i++)
{sum2+=a[i];
}
For(int i=1;i<p1->month;i++)
{sum1+=a[i];
}
If(((p1->year%4==0&&p1->year%100!=0)||p1->year%400==0)&&p1->month>2)
Sum1=sum1+1;
Sum1=sum1+p1->day;sum2=sum2+p2->day;sum=sum+(sum2-sum1);
}
Else {
For(int i=1;i<p1->month;i++)
{
sum1+=a[i];
}
If(((p1->year%4==0&&p1->year%100!=0)||p1->year%400==0)&&p1->month>2)
{
sum1=sum1+1
}
For(int i=1;i<p2->month;i++)
{
sum2+=a[i];
}
If(((p2->year%4==0&&p2->year%100!=0)||p2->year%400==0)&&p2->month>2)
{
{
Sum2=sum2+1;
}
Sum1=sum1+p1->day;sum2=sum2+p2->day;sum=sum+(sum1-sum2);
}

}

...全文
101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ham163 2006-01-03
  • 打赏
  • 举报
回复
问题已经解决,还是谢谢各位
jixingzhong 2005-12-30
  • 打赏
  • 举报
回复
If(((p2->year%4==0&&p2->year%100!=0)||p2->year%400==0)&&p2->month>2)
{
{
Sum2=sum2+1;
}
Sum1=sum1+p1->day;sum2=sum2+p2->day;sum=sum+(sum1-sum2);
}
....
代碼風格太差,看着實在是纍 ...
jianwang_yz 2005-12-30
  • 打赏
  • 举报
回复
另外就你所贴上的代码来看,在括号匹配上还存在问题
建议写出好的代码风格,不然找错都难
jianwang_yz 2005-12-30
  • 打赏
  • 举报
回复
calculate(struct data* p1,struct data* p2) //少了分号
此处为函数声明,应该加上分号的

另外加上返回类型 void
ouyh12345 2005-12-30
  • 打赏
  • 举报
回复
也没有返回类型
屋顶上的老猫 2005-12-30
  • 打赏
  • 举报
回复
calculate(struct data* p1,struct data* p2) //少了逗点
ham163 2005-12-30
  • 打赏
  • 举报
回复
C:\TDdownload\cmpp20capi\sample\2.cpp(8) : warning C4518: 'void ' : storage-class or type specifier(s) unexpected here; ignored
C:\TDdownload\cmpp20capi\sample\2.cpp(8) : error C2146: syntax error : missing ';' before identifier 'main'
C:\TDdownload\cmpp20capi\sample\2.cpp(8) : fatal error C1004: unexpected end of file found

错误提示

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧