C++ 计算C+中两次的差值+;

C++ 计算C+中两次的差值+;,c++,C++,我正在处理的一个问题需要输入两个24小时格式的时间值(以小时、分钟和秒为单位)。我已经声明了关于主程序的变量、输入和输出语句。我的void语句CalcDiff在计算两个时间输入的差时遇到了麻烦。所有值都已声明为int类型。在计算差值之前,是否应首先比较输入(如sec和sec2)以确定哪个值更大?我假设变量的顺序也很重要(计算分钟之前的小时数和秒之前的小时数的差值)?我对C++很陌生,所以如果我没有任何明显的东西,我会道歉。p> // Computes time difference of

我正在处理的一个问题需要输入两个24小时格式的时间值(以小时、分钟和秒为单位)。我已经声明了关于主程序的变量、输入和输出语句。我的void语句CalcDiff在计算两个时间输入的差时遇到了麻烦。所有值都已声明为int类型。在计算差值之前,是否应首先比较输入(如sec和sec2)以确定哪个值更大?我假设变量的顺序也很重要(计算分钟之前的小时数和秒之前的小时数的差值)?我对C++很陌生,所以如果我没有任何明显的东西,我会道歉。p>
    // Computes time difference of two time periods in 24h00 format
    // Time periods are input

    #include <iostream>
    using namespace std;

    int seconds,seconds2, minutes, minutes2, hours, hours2;


    void CalcDiff(int seconds, int seconds2 int minutes, int minutes2, int 
    hours, int hours2);

    int main()
    {
        int sec, sec2, min, min2, hr, hr2, diff;

        cout << "Enter the first time." << endl;
        cout << "Enter hours, minutes and seconds: ";
        cin >> hr >> min >> sec;

        cout << "Enter the second time." << endl;
        cout << "Enter hours, minutes and seconds: ";
        cin >> hr2 >> min2 >> sec2;

        CalcDiff(int sec, int sec2, int min, int min2, int hr, int hr2, 
        diff);

        cout << endl << "Difference in times: " << hr << ":" << min << ":" 
        << sec;
        cout << " - " << hr2 << ":" << min2 << ":" << sec2;

    return 0;
    }
    void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int 
    hour, int hour2, diff)
//以24h00格式计算两个时段的时差
//输入时间段
#包括
使用名称空间std;
整数秒,秒2,分,分2,小时,小时2;
无效CalcDiff(整数秒,整数秒2整数分钟,整数分钟2,整数
小时,国际时2);
int main()
{
内秒,秒2,分钟,分钟2,小时,小时2,差异;
cout>min>>sec;
cout>min2>>sec2;
CalcDiff(整数秒、整数秒2、整数分钟、整数分钟2、整数小时、整数小时2、,
差异);

cout除了已经存在处理这个问题的类之外,下面是一个解决方案,并给出了解释

<首先,代码> SEC,SEC2,min,MI2 …这是一个不同变量的列表。如果你有这么长的列表,这是一个不好的信号。这是C++,所以使用OOP。也就是说,使用类。< /P> 此类类的一个头可以是

class Time {

private:
        unsigned char seconds;
        unsigned char minutes;
        unsigned char hours;

public:
        Time(const unsigned char _seconds, const unsigned char _minutes,
          const unsigned char __hours);
        Time(const unsigned int _seconds);

        Time difference(const Time& other) const;

        unsigned int to_total_seconds() const;
}
这是一种更干净的方法-您不需要在使用它的地方使用所有代码。实现:

Time Time::difference(const Time& other) const {

    int difference_seconds = (int) this.to_total_seconds() - (int) other.to_total_seconds();
    return Time((unsigned int) std::abs(difference_seconds));
}

unsigned int Time::to_total_seconds() const{
    return seconds + 60.*minutes + 60*60*hours;
}
Time::Time(const unsigned int _seconds){

    seconds = _seconds % (60*60);
    minutes = (_seconds / 60) % 60;
    hours = _seconds / (60*60);
}
Time::Time(const unsigned char _seconds, const unsigned char _minutes,
          const unsigned char __hours) :
    seconds(_seconds), minutes(_minutes), hours(_hours) {
    assert(seconds < 60);
    assert(minutes < 60);
}
时间::差异(常数时间和其他)常数{
int差分_秒=(int)此。至_总_秒()-(int)其他。至_总_秒();
返回时间((无符号整数)std::abs(差秒));
}
无符号整数时间::到总秒数()常量{
返回秒数+60.*分钟+60*60*小时;
}
时间::时间(常数无符号整数_秒){
秒=_秒%(60*60);
分钟=(_秒/60)%60;
小时=秒/(60*60);
}
时间:时间(常量无符号字符秒,常量无符号字符分钟,
常量无符号字符(小时):
秒(秒)、分钟(分钟)、小时(小时){
断言(秒<60);
断言(分钟<60);
}
另一种方法是直接存储总秒数

我的意思是,你可以做一些实际的减法运算,比如做6:12到4:50的差,在几分钟内从12减去50,得到38个余数,然后6减(4+余数)=1->1:38的差。但是,当你可以简单地减去秒数并取它们的绝对值时,为什么要这样做呢

但更重要的是,保持主程序的整洁。大型过程代码是缺少类的明显标志


(当然,您必须在代码中添加一些内容以获取值,最好是打印机。由于存在不一致的可能性,因此不建议在此处使用公共成员。)

首先,函数调用的语法应该是

 CalcDiff( sec,  sec2,  min,  min2,  hr,  hr2);       
void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int 
    hour, int hour2, diff)
{
  \\ write the code for subtraction here

}
而不是

CalcDiff(int sec, int sec2, int min, int min2, int hr, int hr2, 
        diff);
在主要部分

函数定义代码应该是

 CalcDiff( sec,  sec2,  min,  min2,  hr,  hr2);       
void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int 
    hour, int hour2, diff)
{
  \\ write the code for subtraction here

}
尝试使用std::chrono

void calcDiff(int h1, int m1, int s1, int h2, int m2, int s2){
  std::chrono::seconds d = std::chrono::hours(h2-h1) 
                           + std::chrono::minutes(m2-m1)
                           + std::chrono::seconds(s2-s1);

  std::cout << std::chrono::duration_cast<std::chrono::hours>(d).count() << "h" <<
    std::chrono::duration_cast<std::chrono::minutes>(d % std::chrono::hours(1)).count() << "m" <<
    std::chrono::duration_cast<std::chrono::seconds>(d % std::chrono::minutes(1)).count() << "s" << std::endl;
}


int main(){
    calcDiff(13, 30, 45, 18, 40, 20); // 5h9m35s
    calcDiff(20, 30, 45, 18, 40, 20); // -1h-50m-25s
    return 0;
}
void calcDiff(int h1、int m1、int s1、int h2、int m2、int s2){
标准时间:秒d=标准时间:小时(h2-h1)
+标准时间分钟数(m2-m1)
+标准时间:秒(s2-s1);
std::不能使用
plus

#包括“date.h”
#包括
int
main()
{
标准时间:秒t1,t2;
std::cout>date::parse(“%T”,t1);
if(std::cin.fail())
返回1;
std::cout>date::parse(“%T”,t2);
if(std::cin.fail())
返回1;

std::cout我决定继续将分和小时转换为秒,并通过一个名为CalcDiff的void函数计算时间段的差异。在将新的DiffSec转换为新值后,我使用了三个引用变量FinSec、FinMin和FinHr来存储差异。 我的作废声明程序是:

void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int 
hour, int hour2, int& FinSec, int& FinMin, int& FinHr)
{
   int TotalSec1, TotalSec2, DiffSec, tempMin;

   TotalSec1= (hour*3600)+(minutes*60)+seconds;
   TotalSec2= (hour2*3600)+(minutes2*60)+seconds2;

    if(TotalSec1>TotalSec2)
        DiffSec = TotalSec1 - TotalSec2;
    else
        DiffSec = TotalSec2 - TotalSec1;

    FinSec = DiffSec%60;
    tempMin = DiffSec/60;
    FinMin = tempMin%60;
    FinHr = FinMin/60;

}

我也有同样的问题,我的解决方案是这段代码,我的观点是以秒为单位计算两次,然后减去它们以了解差异。时间计算是在IF语句中进行的。代码的其余部分是打印结果,我添加了许多变量,试图使其更加明确和易懂

#include <iostream>

using namespace std;

int main()
{
    int h1,h2,m1,m2,s1,s2;
    long TotalSeconds = 0;

    cout << "Enter the first time." << endl;
    cout << "Enter hours, minutes and seconds: ";
    cin >> h1 >> m1 >> s1;

    cout << "Enter the second time." << endl;
    cout << "Enter hours, minutes and seconds: ";
    cin >> h2 >> m2 >> s2;

    // Difference Between Times IN Seconds
    // this code must be in your function
    if( h1 > h2 ){
        TotalSeconds += 86400 - ((h1*3600)+(m1*60)+(s1));
        TotalSeconds +=  ((h2*3600)+(m2*60)+(s2));
    }else{
        //                Time 2            -        Time 1
        TotalSeconds = ((h2*3600)+(m2*60)+(s2)) - ((h1*3600)+(m1*60)+(s1));
    }
    
    cout << "Total Seconds: " << TotalSeconds << endl;
    cout << "Time Difference :\t";
    long hours, minutes, seconds;
    hours = TotalSeconds / 3600;
    cout  << hours << ":";
    TotalSeconds -= hours*3600;

    minutes = TotalSeconds / 60;
    cout  << minutes << ":";
    TotalSeconds -= minutes*60;

    seconds = TotalSeconds;
    cout  << seconds << endl;
    return 0;
}
#包括
使用名称空间std;
int main()
{
int h1、h2、m1、m2、s1、s2;
长总秒=0;
cout>m1>>s1;
cout>m2>>s2;
//以秒为单位的时间差
//此代码必须在您的函数中
如果(h1>h2){
总秒数+=86400-((h1*3600)+(m1*60)+(s1));
总秒数+=((h2*3600)+(m2*60)+(s2));
}否则{
//时间2-时间1
总秒数=((h2*3600)+(m2*60)+(s2))-((h1*3600)+(m1*60)+(s1));
}

cout和我只是对如何处理hour>hour2但minuse chrono的场景感到困惑……您还没有提供CalcDiff()的主体函数,而您调用它是错误的。调用函数时不应提供类型信息。欢迎使用堆栈溢出!请使用您的代码将其简化为您的问题的一部分。您当前的代码包含许多与您的问题无关的内容-最小的示例通常与良好的单元测试类似:仅执行一个任务,为再现性指定输入值。如何将输入转换为秒,然后计算差值,然后重新转换回小时、分钟和秒?也有可能使
-
过载(减段)接线员,哪一个更合适natural@HatsuPointerKun我开始用它写,但我会认为这是一个误用的符号形式。(A -B)+(B -A)应该是零,这将不是。