c++ - 如何使用 getline 和 stringstream 来解析格式化的日期和时间输入?

标签 c++ parsing stringstream getline

我只使用 C++ 大约一个月。我不太了解它是如何工作的,但是我需要为学校编写一个程序。我使用了一个 void 函数,到目前为止它似乎工作正常,但我不知道下一步该做什么我在第 44 行迷路了,我不确定如何让它工作,有没有办法从某个字符串?如果值在两个字符串中,我将如何确定哪个值?这是我的作业:

A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. People who park their cars for longer than 24 hours will pay $8.00 per day.

Write a program that calculates and prints the parking charges. The inputs to your program are the date and time when a car enters the parking garage, and the date and time when the same car leaves the parking garage. Both inputs are in the format of YY/MM/DD hh:mm

这是我到目前为止编写的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;

stringstream ss;
string enter_date;
string enter_time;
string exit_date;
string exit_time;
int calculatecharge;
int num;
int i;
int year;
int month;
int ddmmyyChar;
int dayStr;
string line;
int x;

void atk() 
{
    getline (cin,line);             // This is the line entered by the user
    stringstream  ss1(line);        // Use stringstream  to interpret that line
    ss >> enter_date >> enter_time;
    stringstream  ss2(enter_date);  // Use stringstream  to interpret date
    string year, month, day;
    getline (ss2, year, '/');
}

int main()
{
    cout << "Please enter the date and time the car is entering "<< endl
         << "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
    atk();
    cout << "Please enter the date and time the car is exiting "<< endl
         << "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
    atk();
    if (hr - hr < 3)
        cout<<"Parking fee due: $2.00" << endl;

最佳答案

Write a program that calculates and prints the parking charges.

这是我们计划的目标。基本上,这就是输出。

The inputs to your program are the date and time when a car enters the parking garage, and the date and time when the same car leaves the parking garage. Both inputs are in the format of YY/MM/DD hh:mm

因此,我们需要某种方法将作为字符串输入的日期格式转换为时间时差。您可以将时间存储在 int 中,它表示 parking 期间经过的分钟数(我选择分钟,因为这是输入的最小时间段)。这里的挑战是将字符串解析为这个整数。

你可以这样写一个函数:

int parseDate( std::string dateStr )
{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); // Warning: may not be accurate enough
    totalMins += ( month * 30 * 24 * 60 ); // in terms of leap years and the fact
    totalMins += ( day * 24 * 60 );        // that some months have 31 days
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}

小心!我这里的功能只是一个例子,并没有考虑闰年和不同月份长度等细微之处。您可能需要对其进行改进。重要的是要认识到它试图获取一个字符串并返回自 '00 年以来经过的分钟数。这意味着我们只需从两个日期字符串中减去两个整数即可找到耗时:

int startTime = parseDate( startDateString );
int endTime   = parseDate( endDateString );
int elapsedTime = endTime - startTime; // elapsedTime is no. of minutes parked

这可能是问题中最难的部分,一旦你解决了这个问题,剩下的就应该更简单了。我再给你几个提示:

A parking garage charges a $2.00 minimum fee to park for up to three hours.

基本上只是固定费率:无论如何,描述成本的输出变量应该至少等于 2.00

The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours.

计算三个小时过去的小时数 - 从 elapsedTime 中减去 180。如果它大于 0,则将它除以 60 并将结果存储在 float 中(因为它不一定是整数结果) ,称为 excessHours。使用 excessHours = floor( excessHours ) + 1; 将此数字四舍五入。现在将其乘以 0.5;这是额外的费用。 (试着理解为什么这在数学上有效)。

The maximum charge for any given 24-hour period is $10.00. People who park their cars for longer than 24 hours will pay $8.00 per day.

我会把这个留给你去解决,因为这毕竟是家庭作业。希望我在这里提供的内容足以让您了解需要完成的工作。这个问题也有很多可能的方法,这只是一个,可能是也可能不是“最好的”。

关于c++ - 如何使用 getline 和 stringstream 来解析格式化的日期和时间输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10039383/

相关文章:

php - 带有 php 或 python 属性的 xml 到 json

python - Python 中灵活的数字字符串解析

c++ - 为什么在 C++ 中通过空指针 "work"调用方法?

c++ - 如何在 C++ 中将 iso 8601 日期(可选毫秒)解析为 struct tm?

c++ - 未定义行为的定义

c++ - 字符串流到数组

c++ - C++ 标准是否保证当 'rdbuf' 的返回值传递给流输出运算符时,缓冲区的内容被打印出来

c++ - tellp 在空 ostringstream 上的标准行为

c++ - 如何在不使用继承的情况下转换用户定义类的指针

c++ - sscanf_s 在哪里?