1

所以它是一个星期五,我一直在使用这个应用程序,我的头快要爆炸了。我在任何地方都找不到问题!!!我是初学者,所以我希望 stackoverflow 的大神可以指导我正确的方向或提供一些反馈!:]

这个控制台应用程序只是一张简单的停车时间票。代码符合正常,没有错误。但是我的数学一团糟!

这是一个示例结果:时间输入为 3:50,出口为 5:29,车辆为 T。

 TIME-IN          -858993460:858993460
 TIME-OUT                -858933460:-858993460

 PARKING TIME             0:-858993460

 TOTAL CHARGE             -214748352.00

这是我的代码

 #include <iostream>   //used for cout/cin
 #include <iomanip>   //used to manipulate data 


 void getData(int* ehour, int* emin, int* exhour, int* exmin);
 void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round);
 void charge(char* vehic, float* rate1, float* rate2, int ehour);
 void result(int exhour, int exmin, int ehour, int emin, int thour, float rate1, float rate2, int round, float total);

 int main(void)
 {
   char vehic;
   int ehour;
   int emin;
   int exhour;
   int exmin;
   int thour;
   int tmin;
   int round;

   float rate1;
   float rate2;
   float total;

   getData(&ehour, &emin, &exhour, &exmin);
   rate(exhour, exmin, ehour, emin, &thour, &tmin, &round);
   charge(&vehic, &rate1, &rate2, ehour);
   total= rate1 + rate2;
   result( exhour, exmin, ehour, emin, thour, rate1, rate2, round, total);
   return 0;
 }

  void getData(int* ehour, int* emin, int* exhour, int* exmin)
 {
   char v;
   printf("Enter C for car, B for bus, T for truck: ");
   scanf("%c", &v);
   printf("\nHour vehicle entered 0-24: ");
   scanf("%d", &ehour);
   printf("\nMinute vehicle entered 0-60: ");
   scanf("%d", &emin);
   printf("\nHour vehicle exited 0-24: ");
   scanf("%d", &exhour);
   printf("\nMinute vehicle exited 0-60: ");
   scanf("%d", &exmin);
   return;
 }
 void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round)
 {
   if(emin < exmin)
   {
          emin= emin + 60;
          exhour= exhour - 1;
   }
   *thour = ehour - exhour;
   *tmin = emin - exmin;
   if ((*tmin > 0 && *tmin <= 60))
   {
          *thour = *thour + 1;
          *round = *tmin * 0;
   }
   return;
 }
 void charge(char* vehic, float* rate1, float* rate2, int ehour)
 {
   switch (*vehic)
   {
   case 'c': if (ehour <= 3)
                   {
                         *rate1 = 0.00;
                         if (ehour > 3)
                              *rate2 = 1.25 * (ehour - 3);
                   }
                   break;

   case 'b': if (ehour <= 2)
                   {
                         *rate1 = 2.00 * ehour;
                         if (ehour > 2)
                                *rate2 = 2.50 * (ehour - 2);
                   }
                   break;
   case 't': if (ehour <= 1)
                   {
                         *rate1 = 3.75 * ehour;
                         if (ehour > 1)
                         *rate2 = 4.50 * (ehour - 1);
                   }
                   break;
   }
   return;
 }
 void result(int exhour, int exmin, int ehour, int emin, int thour, float rate1, float rate2, int round, float total)
 {
   printf("\n\t\t PARKING LOT CHARGE \t\t\n");
   printf("\nType of vehicle: Car or Bus or Truck");
   printf("\nTIME-IN\t\t %d:%d", ehour, emin);
   printf("\nTIME-OUT\t\t %d:%d", exhour, exmin);
   printf("\n\t\t\t --------");
   printf("\nPARKING TIME\t\t %d:%d", thour, round);
   printf("\n\t\t\t --------");
   total= rate1 + rate2;
   printf("\nTOTAL CHARGE\t\t %4.2f\n\n", total);

   return;
   }

对不起,这是很多代码!我简直太疑惑了!!!我的整数格式不正确吗?数学错了吗?

4

6 回答 6

7
char v;
printf("Enter C for car, B for bus, T for truck: ");
scanf("%c", &v);
printf("\nHour vehicle entered 0-24: ");
scanf("%d", ehour);
printf("\nMinute vehicle entered 0-60: ");
scanf("%d", emin);
printf("\nHour vehicle exited 0-24: ");
scanf("%d", exhour);
printf("\nMinute vehicle exited 0-60: ");
scanf("%d", exmin);

您获取了参数的地址,这些地址已经是指针


作为一般的手指练习,您可以使用更典型的 C++ 风格进行以下操作:

/////////////////////////////////////
// timepoint classes (booking.hpp)

struct timepoint
{
    int hour, minute;

    timepoint normalized()                     const; 
    int totalMinutes    ()                     const; 
    int roundedHours    ()                     const; 
    timepoint operator- (timepoint const& rhs) const; 
};

struct booking_t
{
    char vehicle;
    timepoint enter, exit;

    timepoint parked() const { return exit - enter; }
};

/////////////////////////////////////
// main program (main.cpp)
booking_t inputData();
void displayBill(booking_t const& booking);

int main(void)
{
    auto booking = inputData();
    displayBill(booking);
}

/////////////////////////////////////
// timepoint methods (booking.cpp)

timepoint timepoint::normalized() const
{
    timepoint tmp { (hour + minute/60) % 24, minute % 60 };
    while (tmp.minute < 0) tmp.hour--, tmp.minute+=60;
    while (tmp.hour   < 0) tmp.hour+=24;
    return tmp;
}

int timepoint::roundedHours() const
{
    return (totalMinutes()-1) / 60 + 1; // TODO check rounding logic
}

int timepoint::totalMinutes() const
{
    return hour*60 + minute;
}

timepoint timepoint::operator-(timepoint const& rhs) const
{
    return timepoint { 0, totalMinutes() - rhs.totalMinutes() } .normalized();
}

#include <iostream>   //used for cout/cin

timepoint getTime(std::string label)
{
    int hour, minute;
    std::cout  << "\nHour "   << label << " 0-24: ";
    std::cin >> hour;
    std::cout  << "\nMinute " << label << " 0-60: ";
    std::cin >> minute;
    return { hour, minute };
}

/////////////////////////////////////
// global functions - input
booking_t inputData()
{
    std::cout << "Enter C for car, B for bus, T for truck: ";
    char v;
    std::cin >> v;
    auto entered = getTime("vehicle entered");
    auto exited  = getTime("vehicle exited");
    return { v, entered.normalized(), exited.normalized() };
}

/////////////////////////////////////
// calculation + billing
#include <sstream>
#include <iomanip>   //used to manipulate data 
#include <map>

static std::ostream& operator <<(std::ostream& os, timepoint const& tp)
{
    std::ostringstream oss;
    oss << std::setw(2) << std::setfill('0') << tp.hour << ':' 
        << std::setw(2) << std::setfill('0') << tp.minute;
    return os << oss.str();
}

std::pair<float,float> charge(booking_t const& booking)
{
    struct tariff_t { int threshold; float rate1, rate2; };
    const static auto tariffs = std::map<char, tariff_t> {
        { 'c', { 3, 0   , 1.25 } },
        { 'b', { 2, 2.  , 2.5  } },
        { 't', { 1, 3.75, 4.5 } } ,
    };

    auto& tariff = tariffs.at(std::tolower(booking.vehicle));
    auto  parked = booking.parked().roundedHours();

    return std::make_pair(
            tariff.rate1 * std::min(tariff.threshold, parked)    ,
            tariff.rate2 * std::max(0, parked - tariff.threshold));
}

void displayBill(booking_t const& booking)
{
    std::cout << "\n\n    PARKING LOT CHARGE\n";
    std::cout << "Type of vehicle: Car or Bus or Truck\n";
    std::cout << "TIME-IN         " << booking.enter << "\n";
    std::cout << "TIME-OUT        " << booking.exit  << "\n";
    std::cout << "                " << "--------\n";
    std::cout << "PARKING TIME    " << booking.parked() << "\n";
    std::cout << "        ROUNDED " << booking.parked().roundedHours() << "\n";
    std::cout << "                " << "--------\n";

    auto  rates = charge(booking);
    float total = rates.first + rates.second;
    std::cout << "TOTAL CHARGE    " << std::fixed << std::setw(7) << std::setprecision(2) << total << "\n\n";
}
于 2012-11-09T21:51:46.020 回答
3

对于您的原始代码,我找不到太多可取之处,因此这本身并不是一个答案,但也许您会发现我如何用“纯 C++”为您的问题编写代码很有趣:

#include <string>
#include <iostream>
#include <sstream>

bool parse_time(std::string const & s, int & t)
{
    int h, m;
    char c;

    std::istringstream iss(s);

    if (iss >> h >> c >> m >> std::ws && c == ':' && iss.get() == EOF)
    {
        t = 60 * h + m;
        return true;
    }

    return false;
}

int main()
{
    int t_in, t_out;
    std::string line;

    if (!(std::cout << "Enter arrival time: "   &&
          std::getline(std::cin, line)          &&
          parse_time(line, t_in)                &&
          std::cout << "Enter departure time: " &&
          std::getline(std::cin, line)          &&
          parse_time(line, t_out)))
    {
        std::cerr << "Input error! Aborting.\n";
        return 0;
    }

    std::cout << "You spent " << t_out - t_in << " minutes.\n";
}

这是一个典型的会话:

Enter arrival time: 5:14
Enter departure time: 8:41
You spent 207 minutes.
于 2012-11-09T22:00:22.387 回答
1

规则 1:在您首次使用它们的位置附近声明变量。并在创建它们时初始化所有变量:

int main(void)
{
  int ehour = 0;
  int emin = 0;
  int exhour = 0;
  int exmin = 0;
  getData(&ehour, &emin, &exhour, &exmin);

  int thour = 0;
  int tmin = 0;
  int round = 0;
  rate(exhour, exmin, ehour, emin, &thour, &tmin, &round);

  char vehic = 0;
  float rate1 = 0;
  float rate2 = 0;
  charge(&vehic, &rate1, &rate2, ehour);

  float total = 0;
  total= rate1 + rate2;
  result( exhour, exmin, ehour, emin, thour, rate1, rate2, round, total);
  return 0;
}

现在看到了float total吗?将变量的声明和初始化移到同一行:

  float total = rate1 + rate2;

规则 2:如果不需要,不要使用指针。如果您有一个整数要传入和传出函数,请将参数设置为引用参数,如下所示:

 void getData(int& ehour, int& emin, int& exhour, int& exmin)

主要:

  getData(ehour, emin, exhour, exmin);

在获取数据中:

void getData(int& ehour, int& emin, int& exhour, int& exmin)
{
  char v;
  printf("Enter C for car, B for bus, T for truck: ");
  scanf("%c", &v);
  printf("\nHour vehicle entered 0-24: ");
  scanf("%d", &ehour);
  printf("\nMinute vehicle entered 0-60: ");
  scanf("%d", &emin);
  printf("\nHour vehicle exited 0-24: ");
  scanf("%d", &exhour);
  printf("\nMinute vehicle exited 0-60: ");
  scanf("%d", &exmin);
  return;
}

现在,我发现了你的第一个错误。您不是在阅读整数,而是阅读指向整数的指针。现在我已经对它们进行了引用(想想“别名”或传入的变量),&操作符得到一个指向整数的指针,而不是指向整数指针的指针!

接下来要做的就是不要使用 scanf。Scanf 很难使用。为什么要用难用的东西?

第 3 步:您的程序应该帮助您调试它。

当您从输入中读取某些内容时,请重复一遍以确保您正确阅读。即,在您调用 getData 之后,您应该做的下一件事就是重复您阅读的内容。最终,当您的代码可靠时,您可以删除“重复它”,但是在开发程序时,这种反馈是必不可少的。

请参阅 Kerrek SB 以获得更好的阅读方式,而不是使用scanf.

于 2012-11-09T22:32:09.007 回答
1

我注意到一件事——你传递&ehour和朋友,它是一个指向你的scanf调用的指针(你可以&scanf调用 when ehouris 中删除int*)。

于 2012-11-09T21:49:43.777 回答
1

取出scanf中的&!他们已经是指针!

于 2012-11-09T21:52:04.420 回答
1

代码中的另一个错误是变量vehic永远不会得到值。看起来你打算给它一个价值,getData但不知何故搞砸了。

于 2012-11-09T21:59:22.843 回答