本文介绍了这是一个很好的...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我尝试编译以下程序时,我收到以下错误:


strata:/ home / dcrudy / c / exercise 1131 $$ $ - > cc -g -oex7-2 ex7-2.c

ex7-2.c:函数`conv_julian'':

ex7-2.c :102:'MONTHLENGTH'之前的语法错误'

ex7-2.c:顶层:

ex7-2.c:103:`MONTHLEAPLENGTH'之前的语法错误'

ex7-2.c:110:'年''未声明在这里(不在函数中)

ex7-2.c:110:初始化元素不是常数

ex7-2.c:110:警告:数据定义没有类型或存储类

ex7-2.c:111:警告:数据定义没有类型或存储类

ex7-2.c:112:'day''未声明此处(不在函数中)

ex7-2.c:112:警告:数据定义没有类型或存储类

ex7-2.c:116:在'while''之前语法错误

strata:/ home / dcrudy / c / exercise 1132 $$ $ - >


对于我的生活,我似乎无法弄清楚为什么如此我的这些是

错误。我正在使用的编译器是gcc 2.95.3。那些非常令人费解的是110和112的错误。它说''年'是

不在函数中。它绝对是* IS *在一个函数中,它是在该函数中使用的
。它还说这些错误处于最高水平。我勒个去?我按照书中的例子,所以我的问题是什么。


这是'程序:


strata:/ home / dcrudy / c / exercise 1135 $$$ - > cat -n ex7-2.c

1 / *

2

3编程练习7-2 Page 111

4

5编写一个程序来执行日期算术,如6/6/90和4/3/92之间有多少

6天。

7

8 * /

9

10 #include< stdio.h>

11 #include< string.h>

12

13

14 const int YEAR = 365; / *一年中的天数* /

15 int LEAP = 366; / *闰年的天数* /

16

17

18 char line [100]; / *用户数据输入行* /

19 int sday; / *天开始输入* /

20 int smonth; / *月开始输入* /

21 int syear; / *年份开始输入* /

22 int eday; / *日结束输入* /

23 int emonth; / *月末输入* /

24 int eyear; / *年终输入* /

25 int jstart; / *朱利安开始日期* /

26 int jend; / *朱利安结束数据* /

27结果; / *计算结果* /

28

29

30 / *检查给定年份是否为闰年* /

31 int check_leap(int year)

32 {

33 / *我们需要检查3个案例。

34 1.每年可被4整除是闰年。

35 2.但是,如果年份可以被100整除,那么它就是b $ b 36不是a闰年。

37 3.除非年份也可被400整除,否则

38它仍然是闰年。

39 * /



41 const int LEAPYEAR = 4; / *闰年模数* /

42 const int LEAPNONE = 100; / *飞跃1世纪模数* /

43 const int LEAPCENT = 400; / *飞跃4世纪模数* /

44 int result1;

45 int result2;

46 int result3;

47

48 / *检查年份是否> 399.用于每400个

49年的规则。 * /

50 if(year> 399)

51 {

52 result1 = year%LEAPYEAR;

53 result2 =年%LEAPNONE;

54结果3 =年度%LEAPCENT;

55}

56其他如果(年> 99)

57 {

58 result1 =年%LEAPYEAR;

59 result2 =年%LEAPNONE;

60 result3 = 1;

61}

62其他

63 {

64 result1 =年%LEAPYEAR;

65 result2 = 1;

66 result3 = 1;

67}

68

69 / *检查给定年份是否可被4整除。如果是,那么

70我们需要检查年份是否可被100整除。如果是这样,

71然后我们还需要检查年份是否可被400整除。

72如果是,那么它是闰年。如果没有,那么我们没有闰年73美元。 * /

74 if(result1 == 0)

75 {

76 if(result2 == 0)

77 {

78 if(result3 == 0)

79 {

80 return(1);

81}

82其他

83 {

84返回(0);

85}

86}

87其他

88 {

89返回(1);

90}

91}

92其他

93 {

94返回(0);

95}

96}

97

98

99 / *收取指定日期和将其转换为朱利安格式* /

100 int conv_julian(int month,int day,int year)

101 {

102 const int array MONTHLENGTH [12] =

{31,28,31,30,31,30,31,31,30,31,30,31};

103 const int array MONTHLEAPLENGTH [12] =

{31,29,31,30,31,30,31,31,30,31,30,31};

104

105 int leapflag;如果给定日期是闰年,那么/ *设置为true。 * /

106 int count; / *月份柜台* /

107 int daycount; / *日柜台* /

108

109 / *初始化我们的变量* /

110 leapflag = check_leap(year);

111 count = 0;

112 daycount = day;

113

114 / *循环加起来的日子这个月,当月减去

115。 * /

116 while(count< month - 1)

117 {

118 if(leapflag)

119 {

120 daycont = daycount + MONTHLEAPLENGTH [count];

121}

122 else

123 {

124 daycont = daycount + MONTHLENGTH [count];

125}

126 ++ count;

127}

128

129 / *返回我们的结果* /

130返回(daycount);

131 }

132

133

134

135 int main()

136 {

137 / *获取用户输入* /

138 printf(输入开始日期和结束日期(月份日期));

139 fgets(line,sizeof(line),stdin);

140 sscanf(line,"%d%d%d%d%d%d"& smonth,& sday,& syear,

141& emonth,&am p; eday,& eyear);

142

143 / *将给定日期转换为朱利安格式* /

144 jstart = conv_julian( smonth,sday,syear);

145 jend = conv_julain(emon,eday,eyear);

146

147 / *我们有一个几个特殊情况需要处理* /

148

149 / *同一年检查* /

150 if(syear == eyear )

151 {

152结果= jend - jstart;

153}

154其他

155 {

156 int leap; / *标志,如果开始年份是一个

跳跃* /

157

158 leap = check_leap(syear);

159 if(leap)

160 {

161 result =(LEAP - jstart)+ jend;

162}

163其他

164 {

165结果=(年 - jstart)+ jend;

166}

167 if(syear + 1> eyear)

168 {

169 int count; / *通用计数器* /



171 count = syear + 1;

172 while(count< eyear)

173 {

174 leap = check_leap(count);

175 if(leap)

176 {

177结果+ = LEAP;

178}

179其他

180 {

181结果+ =年;

182}

183 ++计算;

184}

185

186}

187}

188

189 / *打印结果* /

190 printf( 天数差异是%d。\ n,结果);

191

192 / *返回操作系统* /

193返回(0);

194}

195

196

strata:/ home / dcrudy / c / exercise 1136 $$$ - >

- -

Daniel Rudy


电子邮件地址已被编码以减少垃圾邮件。

删除所有号码,然后删除无效的电子邮件,不,垃圾回复。


When I try to compile the program below, I get these errors:

strata:/home/dcrudy/c/exercise 1131 $$$ ->cc -g -oex7-2 ex7-2.c
ex7-2.c: In function `conv_julian'':
ex7-2.c:102: syntax error before `MONTHLENGTH''
ex7-2.c: At top level:
ex7-2.c:103: syntax error before `MONTHLEAPLENGTH''
ex7-2.c:110: `year'' undeclared here (not in a function)
ex7-2.c:110: initializer element is not constant
ex7-2.c:110: warning: data definition has no type or storage class
ex7-2.c:111: warning: data definition has no type or storage class
ex7-2.c:112: `day'' undeclared here (not in a function)
ex7-2.c:112: warning: data definition has no type or storage class
ex7-2.c:116: syntax error before `while''
strata:/home/dcrudy/c/exercise 1132 $$$ ->

For the life of me, I cannot seem to figure out why some of these are
errors. The compiler that I''m using is gcc 2.95.3. The ones that are
really puzzling is the errors on 110 and 112. It says that ''year'' is
not in a function. It most definatly *IS* in a function, and it is
being used in that function. It also says these errors are at the top
level. What the hell? I followed the example in the book, so I''m at a
loss as to what the problem is.

Here''s the program:

strata:/home/dcrudy/c/exercise 1135 $$$ ->cat -n ex7-2.c
1 /*
2
3 Programming Exercise 7-2 Page 111
4
5 Write a program to perform date arithmetic such as how many
6 days there are between 6/6/90 and 4/3/92.
7
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12
13
14 const int YEAR=365; /* Days in a year */
15 int LEAP=366; /* Days in a leap year */
16
17
18 char line[100]; /* user data input line */
19 int sday; /* day start input */
20 int smonth; /* month start input */
21 int syear; /* year start input */
22 int eday; /* day end input */
23 int emonth; /* month end input */
24 int eyear; /* year end input */
25 int jstart; /* julian start date */
26 int jend; /* julian end data */
27 int result; /* calculation result */
28
29
30 /* Checks to see if the given year is a leap year */
31 int check_leap(int year)
32 {
33 /* We need to check on 3 cases for this.
34 1. Every year divisible by 4 is a leap year.
35 2. But, if the year is divisible by 100, then it
36 is not a leap year.
37 3. Unless the year is also divisible by 400, then
38 it is still a leap year.
39 */
40
41 const int LEAPYEAR = 4; /* leap year modulus */
42 const int LEAPNONE = 100; /* leap 1 century modulus */
43 const int LEAPCENT = 400; /* leap 4 century modulus */
44 int result1;
45 int result2;
46 int result3;
47
48 /* Check if the year is > 399. Used for the every 400
49 years rule. */
50 if (year > 399)
51 {
52 result1 = year % LEAPYEAR;
53 result2 = year % LEAPNONE;
54 result3 = year % LEAPCENT;
55 }
56 else if (year > 99)
57 {
58 result1 = year % LEAPYEAR;
59 result2 = year % LEAPNONE;
60 result3 = 1;
61 }
62 else
63 {
64 result1 = year % LEAPYEAR;
65 result2 = 1;
66 result3 = 1;
67 }
68
69 /* Check if the given year is divisible by 4. If so, then
70 we need to check if the year is divisible by 100. If so,
71 then we also need to check if the year is divisible by 400.
72 If so, then it is a leap year. If not, then we do not have
73 a leap year. */
74 if (result1 == 0)
75 {
76 if (result2 == 0)
77 {
78 if (result3 == 0)
79 {
80 return(1);
81 }
82 else
83 {
84 return(0);
85 }
86 }
87 else
88 {
89 return(1);
90 }
91 }
92 else
93 {
94 return(0);
95 }
96 }
97
98
99 /* Takes the given date and converts it to the julian format */
100 int conv_julian(int month, int day, int year)
101 {
102 const int array MONTHLENGTH[12] =
{31,28,31,30,31,30,31,31,30,31,30,31};
103 const int array MONTHLEAPLENGTH[12] =
{31,29,31,30,31,30,31,31,30,31,30,31};
104
105 int leapflag; /* set to true if the given date is a
leap year. */
106 int count; /* counter for the month */
107 int daycount; /* day counter */
108
109 /* Init our variables */
110 leapflag = check_leap(year);
111 count = 0;
112 daycount = day;
113
114 /* Loop to add up the days of the months, minus
115 the current month. */
116 while (count < month - 1)
117 {
118 if (leapflag)
119 {
120 daycont = daycount + MONTHLEAPLENGTH[count];
121 }
122 else
123 {
124 daycont = daycount + MONTHLENGTH[count];
125 }
126 ++count;
127 }
128
129 /* Return our result */
130 return(daycount);
131 }
132
133
134
135 int main()
136 {
137 /* Get User Input */
138 printf("Input begining and ending dates (month day year) ");
139 fgets(line, sizeof(line), stdin);
140 sscanf(line, "%d %d %d %d %d %d", &smonth, &sday, &syear,
141 &emonth, &eday, &eyear);
142
143 /* convert given dates to julian format */
144 jstart = conv_julian(smonth, sday, syear);
145 jend = conv_julain(emonth, eday, eyear);
146
147 /* We have a couple of special cases to deal with */
148
149 /* Check for same year */
150 if (syear == eyear)
151 {
152 result = jend - jstart;
153 }
154 else
155 {
156 int leap; /* flag for if the begining year is a
leap */
157
158 leap = check_leap(syear);
159 if (leap)
160 {
161 result = (LEAP - jstart) + jend;
162 }
163 else
164 {
165 result = (YEAR - jstart) + jend;
166 }
167 if (syear + 1 > eyear)
168 {
169 int count; /* generic counter */
170
171 count = syear + 1;
172 while (count < eyear)
173 {
174 leap = check_leap(count);
175 if (leap)
176 {
177 result += LEAP;
178 }
179 else
180 {
181 result += YEAR;
182 }
183 ++count;
184 }
185
186 }
187 }
188
189 /* printout result */
190 printf("Difference in days is %d.\n", result);
191
192 /* return to operating system */
193 return(0);
194 }
195
196
strata:/home/dcrudy/c/exercise 1136 $$$ ->
--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.

推荐答案




这篇关于这是一个很好的...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:59