我无法在此代码中发现错误。我试图编写一个程序,根据日,月和年[ex:1/3/2000(2000年3月3日)]返回一年中的天数(例如:60)。
编译器给我这些错误:
- 功能的参数太少' day_of_year'
- ' day_of_year'
的冲突类型
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, i, count=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year();
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year, int i, int count){
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[2]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
答案 0 :(得分:1)
宣言:
int day_of_year(int day, int month, int year);
呼叫:
count=day_of_year();
定义:
int day_of_year (int day, int month, int year, int i, int count){
C是强类型语言,意味着函数参数的数量和类型非常匹配。
因此,您需要更正参数列表并将缺少的参数添加到函数调用中。
看起来你并不真正需要定义中的最后两个参数 - 而是将它们声明为局部变量:
int day_of_year (int day, int month, int year){
int i, count;
答案 1 :(得分:1)
我发表了一些评论。
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
// you don't need to declare 'i' here, or a[]
int day, month, year,count;
printf ("\nEnter the day: ");
scanf ("%d", &day);
printf ("\nEnter the month: ");
scanf ("%d", &month);
printf ("\nEnter the year: ");
scanf ("%d", &year);
// you need to pass the parameters to the function
count=day_of_year(day,month,year);
printf ("\nCount: %d", count);
return 0;
}
// here you put in the function signature two more variables.
//they are not used and also they differ from the initial definition.
int day_of_year (int day, int month, int year){
int count=0,i=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[1]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
答案 2 :(得分:1)
感谢所有人。我纠正了错误。 这是新代码。
Arrays.asList(null)
答案 3 :(得分:0)
您尚未将任何参数传递给您的函数day_of_year。
答案 4 :(得分:0)
您的原始代码中存在相当多的错误,但编译的解决方案可能是这样的:
from django import template
from django.utils.text import Truncator
register = template.Library()
@register.filter("custom_truncator")
def custom_truncator(value, max_len, trunc_chars=True):
truncator = Truncator(value)
return truncator.chars(max_len) if trunc_chars else truncator.words(max_len)
首先,在代码的开头声明你的#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, count=0;
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year(day, month, year);
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year){
int i = 0, count = 0; // declaration was missing
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (year%4==0) a[2]++;
count = day;
for (i=0;i<month;i++)
count+=a[i];
return count;
}
函数,但是当你实现它时,你会使用其他被禁止的参数。
您应该在函数体内声明day_of_year
和count
,以便将它们从函数的参数列表中移出。
其次,当您在i
中拨打day_of_year
时,您没有通过任何内容,尽管您已从控制台中读取它们。
除了确定输入年份是否为闰年的逻辑不是一个理想的解决方案,但我还是要由你来纠正。
希望它有所帮助。
答案 5 :(得分:0)
除了原型%定义不匹配外,您也没有将任何参数传递给函数。
你几乎没有一个错误。 C中的索引从0开始。
这是修改后的版本。密切关注day_of_year
功能的变化。特别是,对于2月份,您希望增加a[1]
(不是a[2]
)。同样,for
循环的条件必须为month - 1
(不是month
)。
#include <stdio.h>
int day_of_year(int day, int month, int year);
int main(){
int day, month, year, i, count=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
printf ("Enter the day: ");
scanf ("%d", &day);
printf ("Enter the month: ");
scanf ("%d", &month);
printf ("Enter the year: ");
scanf ("%d", &year);
count=day_of_year(day, month, year);
printf ("Count: %d", count);
return 0;
}
int day_of_year (int day, int month, int year)
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
int count = day, i;
if (year%4==0) a[1]++;
for (i=0;i<month - 1;i++)
count+=a[i];
return count;
}
答案 6 :(得分:0)
编译器会出错:
°函数'day_of_year'的参数太少
原因:没有参数调用day_of_year()函数。
°'day_of_year'的冲突类型
原因:day_of_year()
函数的原型与day_of_year()
定义不同。
Prototype对编译器说它需要3个参数但是函数定义有5个参数。这种不匹配导致错误。