C程序设计:使用结构和函数来确定两个日期之间的天数

如何解决C程序设计:使用结构和函数来确定两个日期之间的天数

我已经做了Craig Estey建议的更正。该代码现在可以编译并运行。 这是修改后的代码:

/* Programme to determine the number of days between two dates ex8.2.c
This is done with the formula
N = ( ((1461 * (f(year,month))) / 4) + ((153 * (g(month))) / 5) + day )

with:
f(year,month) = year - 1 if month <= 2; otherwise year

g(month) = month + 13 if month <=2; otherwise month + 1

The formula is applicable for dates after 1 March 1900;
    add 1 to N for dates between 1 March 1800 to 28 February 1900
    add 2 to N for dates between 1 March 1700 to 28 February 1800

ALGORITHMS
N.B.: Use ternary operators to help with different evaluations

Declare structure(s) to store date
Write functions to evaluate f
Write function to evaluate g
Compare date periods to know if modification of formula will be used

 */

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

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

struct date date1,date2;
int month,year,duration1,duration2,diff;

// Function prototypes
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int year_Func (int month,int year);
int month_Func (int month);
int date_Elapsed (struct date d);

int main(void)
{
    printf("This is a programme to find the number of days between two dates!\n");

    printf("\nEnter first date (dd mm yyyy): ");
    scanf(" %i%i%i",&date1.day,&date1.month,&date1.year);
        month = date1.month;
        year = date1.year;

    duration1 = date_Elapsed (date1);

    printf("\nEnter second date (dd mm yyyy): ");
    scanf(" %i%i%i",&date2.day,&date2.month,&date2.year);
        month = date2.month;
        year = date2.year;

    duration2 = date_Elapsed (date2);

    diff = duration2 - duration1;

    printf("Number of elapsed days are: %i.\n",diff);
}

// Function to find the number of days in a month
int number_of_Days (struct date d)
{
    int days;
    bool is_Leap_Year (struct date d);
    const int days_Per_Month[13] = {0,31,28,30,31};

    if (is_Leap_Year (d) == true && d.month == 2)
    {
        days = 29;
    }
    else
    {
        days = days_Per_Month[d.month];
    }
    return days;
}

// Function to determine if it is a leap year
bool is_Leap_Year (struct date d)
{
    bool leap_Year_Flag;

    if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))
    {
        leap_Year_Flag = true;
    }
    else
    {
        leap_Year_Flag = false;
    }
    return leap_Year_Flag;
}

// Function to find f in formula
int year_Func (int month,int year)
{
    int yrRet;
    yrRet = (month <= 2) ? (year - 1) : (year);
    return yrRet;
}

// Function to find g in formula
int month_Func (int month)
{
    int mntRet;
    mntRet = (month <= 2) ? (month + 13) : (month + 1);
    return mntRet;
}

// Function to calculate N in formula
int date_Elapsed (struct date d)
{
    int number_of_Days (struct date d);
    bool is_Leap_Year (struct date d);
    int year_Func (int month,int year);
    int month_Func (int month);

    int yCalc,mCalc,nCalc;

    yCalc = year_Func (d.month,d.year);
    mCalc = month_Func (d.month);

    // Calculates number of elapsed days
    nCalc = ( ((1461 * (yCalc) / 4) + ((153 * (mCalc))) / 5) + d.day );

    if ((d.day < 1) && (d.month < 3) && (d.year < 1700))
    {
        printf("Invalid date input!\n");
        printf("Date must be from 1 March 1700.\n");
        exit (999);
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1700)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1800)))
    {
        nCalc = nCalc + 2;
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1800)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1900)))
    {
        nCalc = nCalc + 1;
    }
    else
    {
        return nCalc;
    }
}

================================================ ================================

我是学习计算机编程的新手。这是Stephen G. Kochan撰写的C语言编程的练习。我需要帮助来找出到目前为止的错误。我已经呆了大约一个星期。

我的Code :: Blocks编译器显示以下错误:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
|In function ‘N’:|
|134|error: lvalue required as left operand of assignment|
|144|error: lvalue required as left operand of assignment|
|148|error: lvalue required as left operand of assignment|
|152|warning: return makes integer from pointer without a cast [-Wint-conversion]|
|154|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 3 error(s),2 warning(s) (0 minute(s),0 second(s)) ===|

这是我的代码

/* Programme to determine the number of days between two dates ex8.2.c
This is done with the formula
N = ( ((1461 * (f(year,month) = year - 1 if month <= 2; otherwise year

g(month) = month + 13 if month <=2; otherwise month + 1

The formula is applicable for dates after 1 March 1900;
    add 1 to N for dates between 1 March 1800 to 28 February 1900
    add 2 to N for dates between 1 March 1700 to 28 February 1800

ALGORITHMS
N.B.: Use ternary operators to help with different evaluations

Declare structure(s) to store date
Write functions to evaluate f
Write function to evaluate g
Compare date periods to know if modification of formula will be used

*/

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

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

struct date date1,N1,N2,diff;

// Function prototypes
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int F (int month,int year);
int G (int month);
int N (struct date d);

int main(void)
{
    //
    printf("This is a programme to find the number of days between two dates!\n");

    printf("\nEnter first date (dd mm yyyy): ");
    scanf(" %i%i%i",&date1.year);
        month = date1.month;
        year = date1.year;

    N1 = N (date1);

    printf("\nEnter second date (dd mm yyyy): ");
    scanf(" %i%i%i",&date2.year);
        month = date2.month;
        year = date2.year;

    N2 = N (date2);

    diff = N2 - N1;

    printf("Number of elapsed days are: %i.\n",31};

    if (is_Leap_Year (d) == true && d.month == 2)
    {
        days = 29;
    }
    else
    {
        days = days_Per_Month[d.month];
    }
    return days;
}

// Function to determine if it is a leap year
bool is_Leap_Year (struct date d)
{
    bool leap_Year_Flag;

    if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))
    {
        leap_Year_Flag = true;
    }
    else
    {
        leap_Year_Flag = false;
    }
    return leap_Year_Flag;
}

// Function to find f
int F (int month,int year)
{
    int F;
    F = (month <= 2) ? (year - 1) : (year);
    return F;
}

// Function to find g
int G (int month)
{
    int G;
    G = (month <= 2) ? (month + 13) : (month + 1);
    return G;
}

// Function to calculate N
int N (struct date d)
{
    int number_of_Days (struct date d);
    bool is_Leap_Year (struct date d);
    int F (int month,int year);
    int G (int month);

    int f,g;

    f = F (d.month,d.year);
    g = G (d.month);

    //N = ( ((1461 * (f(d.month,d.year))) / 4) + ((153 * (g(d.month))) / 5) + d.day );
    N = ( ((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day );

    if ((d.day < 1) && (d.month < 3) && (d.year < 1700))
    {
        printf("Invalid date input!\n");
        printf("Date must be from 1 March 1700.\n");
        exit (999);
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1700)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1800)))
    {
        N = N + 2;
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1800)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1900)))
    {
        N = N + 1;
    }
    else
    {
        return N;
    }
}

解决方法

违规行是:

  Message=Attempt to invoke virtual method 'void android.support.design.internal.BottomNavigationItemView.setTextColor(android.content.res.ColorStateList)' on a null object reference 

但是,这是函数 N = (((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day); 中的

您不能分配给函数名称。

要么更改函数名称(例如N-> N),要么为包含返回值的变量使用其他名称(例如Ncalc)。

ret [IIRC]中,将N包含为包含函数的赋值给N来设置函数的返回值。在fortran中不能很好地工作。

而且,您必须提供变量的定义(例如):

c

一些样式项...

按照惯例,在int ret; 中,使用所有大写字母通常保留给常量(例如):

c

尽管通过 value 传递#define X 12345 是完全合法的,但是大多数代码将 pointer 传递给结构(可能是struct),因为更快。

const

想象一下,如果您的int N(struct date *d) { } 是:

struct

它将把大约40,000个字节压入堆栈。

此外,使用(更长)更具描述性的名称比使用单个字符的名称会有所帮助。您已经有了一些函数来“计算” struct date { ... int array[10000]; }; fg,分别名为nFG

嗯,什么是[an] N [或fg] [其他]阅读您的代码的人都想知道。

使用更具描述性的评论,等同于:

n

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-