| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2296 人关注过本帖
标题:结构{复合字面量问题}
只看楼主 加入收藏
huangfengchu
Rank: 1
等 级:新手上路
威 望:2
帖 子:274
专家分:0
注 册:2007-5-22
收藏
 问题点数:0 回复次数:7 
结构{复合字面量问题}

这是一个计算下一天日期程序。问题出现在函数部分复合字面量地方出现语法错误,找了很久,没发现解决办法,请帮我指出一下。
struct date
{
int month;
int year;
int day;
};
struct date update(struct date today)
{
struct date tomorrow;
const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if(today.year%4==0&&today.year%100!=0||today.year%400==0&&today.month==2)
days=29;
else
days=dayspermonth[today.month-1];
if(today.day!=days)
tomorrow=(struct date){today.month,today.day+1,today.year};

else if (today.month==12)
tomorrow=(struct date){1,1,today.year+1};

else
tomorrow=(struct date){today.monnth+1,1,today.year};
return tomorrow;
}
int main(void)
{
struct date thisdate,nextdate;
struct date update(struct date today);
printf("Input date's date (mm dd yyyy)");
scanf("%i:%i:%i",&thisdate.month,&thisdate.day,&thisdate.year);
nextdate=update(thisdate);
printf("%i:%i:%i",nextdate.month,nextdate.day,nextdate.year);
return 0;
}

搜索更多相关主题的帖子: 字面量 int today 结构 year 
2007-06-11 01:06
killer_l
Rank: 2
等 级:新手上路
威 望:3
帖 子:1139
专家分:0
注 册:2007-5-25
收藏
得分:0 


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

struct date
{
int month;
int year;
int day;
};

struct date update(struct date today)
{
struct date tomorrow;
const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if( (today.year%4 == 0 && today.year%100 != 0) || (today.year %400 == 0 && today.month == 2))
{
days=29;
}
else
{
days=dayspermonth[today.month-1];
}
if(today.day!=days)
{
tomorrow=(struct date){today.month,today.day+1,today.year};
}
else
{
if (today.month == 12)
{
tomorrow=(struct date){1,1,today.year+1};
}
else
{
tomorrow=(struct date){today.month+1,1,today.year};
}
}
return tomorrow;
}


int main()
{
struct date thisdate,nextdate;
struct date update(struct date today);
printf(\"Input date's date (mm dd yyyy)\");
scanf(\"%i:%i:%i\",&thisdate.month,&thisdate.day,&thisdate.year);
nextdate = update(thisdate);
printf(\"%i:%i:%i\",nextdate.month,nextdate.day,nextdate.year);
return 0;
}


2007-06-11 17:50
huangfengchu
Rank: 1
等 级:新手上路
威 望:2
帖 子:274
专家分:0
注 册:2007-5-22
收藏
得分:0 
楼上,先谢你指出的这个month错误,但是还是运行不了。我用的的WIN-TC,不知道是不是编译器问题,你能运行吗?

深山苦学C语言,终年不见外面世界。
2007-06-11 18:05
killer_l
Rank: 2
等 级:新手上路
威 望:3
帖 子:1139
专家分:0
注 册:2007-5-25
收藏
得分:0 
我可以运行,MINGW....那个括号看见了么?

[此贴子已经被作者于2007-6-11 18:13:56编辑过]



2007-06-11 18:13
huangfengchu
Rank: 1
等 级:新手上路
威 望:2
帖 子:274
专家分:0
注 册:2007-5-22
收藏
得分:0 

括号看见的,那个都没问题。可有可无,只是可读性好点,我用WIN-TC就报错结构中表达式错误。就那三表达式错误,我想应该是编译器问题了,谢谢你。


深山苦学C语言,终年不见外面世界。
2007-06-11 18:45
飞翔的寂寞
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-7-30
收藏
得分:0 
回复:(huangfengchu)括号看见的,那个都没问题。可...
看看你定义的结构体的成员变量顺序好像跟下面输出的时候不同哦。呵呵~~
struct date
{
int month;
int day;
int year;
} ;
2007-07-30 17:13
jackys2006
Rank: 1
等 级:新手上路
帖 子:195
专家分:0
注 册:2006-3-10
收藏
得分:0 
#include <conio.h>
这个头文件是什么用的?
为哪个函数提供原型还是?

2007-07-30 17:58
cora0911
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-8-9
收藏
得分:0 

#include <stdio.h>

struct date
{
int month;
int day;
int year;
};

int main()
{

struct date today, tomorrow;
int numbersOfMonth(struct date);

printf("Please enter today's date (month day year):");
scanf("%d%d%d",&today.month,&today.day,&today.year);

tomorrow = (struct date){today.day, today.day, today.year};

if(today.day != numbersOfMonth(today))//非月末判断是否是闰年的2月
{
tomorrow.day = today.day + 1;
}
else if(today.month == 12)//年末
{
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
}
else//月末
{
tomorrow.day = 1;
tomorrow.month = today.month + 1;
}
printf("Tomorrow's date is(month/day/year):%d/%d/%.2d\n",
tomorrow.month,tomorrow.day,tomorrow.year%100);
return 0;
}

int numbersOfMonth(struct date today)
{
int days;
bool isLeapYear(struct date);

const int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

if(isLeapYear(today) == true && today.month == 2)
days = 29;
else
days = daysPerMonth[today.month - 1];
return days;
}

bool isLeapYear(struct date today)
{
bool leapYearFlag;

if((today.year % 4 ==0 && today.year % 100 != 0) || (today.year % 400 == 0))
leapYearFlag = true;
else
leapYearFlag = false;
return leapYearFlag;

}

为了试一下复合字面量的用法我只用了一个语句,但是没有编译通过,用的是Microsoft Visual C++ 6.0
提示是这样的:
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
都是那一句的错误
请问是编译器的问题,还是别的问题

2007-08-09 14:37
快速回复:结构{复合字面量问题}
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019118 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved