- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以今天是星期五,我一直在使用这个应用程序,我的脑袋快要爆炸了。我在任何地方都找不到问题!!!我是一个初学者,所以我希望stackoverflow的大神们能指导我正确的方向或提供一些反馈!! :]
这个控制台应用程序只是一个简单的 parking 时间票。该代码没有任何错误。但是我的数学一团糟!
这是一个示例结果:时间输入为 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;
}
抱歉,代码太多了!我只是很困惑!我的整数格式不正确吗?数学错了吗?
最佳答案
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";
}
关于c++ - 遇到 C++ 整数和数学问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316490/
Based on Deep Learning (2017, MIT) book. 本文基于Deep Learning (2017, MIT),推导过程补全了所涉及的知识及书中推导过程中跳跃和省
因此,我需要一种方法来弄清楚如何获得5个数字,并且当您将它们中的任意两个相加时,将得出一个总和,您只能通过将这两个特定的数字相加而得到。 这是我正在谈论的示例,但有3个数字: 1个 3 5 1 + 3
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
如何将 a 和 b 之间的数字线性映射到 c 和 d 之间。 也就是说,我希望 2 到 6 之间的数字映射到 10 到 20 之间的数字...但我需要广义的情况。 我的脑子快炸了。 最佳答案 如果您的
嘿,我有一个方程式,我需要弄清楚它是基于图表的数学,其中图表上有两个点,需要获取其余值: 我正在构建一个 javascript 页面,它获取图表上的两个点,但需要吐出图表上的任何位置。 它用于根据了解
有谁知道如何用 Doxygen 得到实复场或射影平面的符号,i.o.w 符号,如 IR、IC、IP 等? 例如,我尝试了\f$\field{R}\f$,但无法识别。 非常感谢您的帮助,G. 最佳答案
我正在使用 Segment to Segment 最接近方法,该方法将输出两个长度段之间的最近距离。每个段对应一个球体对象的起点和终点。速度只是从一个点到另一个点。 即使没有真正的碰撞,最近的方法也可
我有一个 arduino 连接到 Stradella 系统钢琴 Accordion 。我在左手和弦的 12 个音符中的每一个上都有光学传感器。当我弹奏和弦时,它会触发三个传感器。如果我想让合成器演奏和
我正在开发一个具有一些简单功能的新包。现在我可以使用已经存在的“math-vectors”库中的函数;特别是“插值”和“反转”。如何在我的新包中使用这些?编写 y:=reverse(...) 显然是不
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Integer division in JavaScript 希望这是一个简单的问题,基本上我需要这样做: 分隔线
我有一张表格,上面有学校类(class)。此表单上可以有任意数量的类,每个类有 2 个字段。书本费和学费。 我有一个名为总计的第三个字段,当他们在其他字段中输入成本时,我想更新该字段。 这就是我的设置
今天早些时候我问了一个类似的问题,结果发现我只是数学很烂,因为我也无法解决这个问题。 我通过宽度/高度计算屏幕比例。我需要一个函数来将结果数字转换为新的比例。 例如 function convertN
我有一个起始数字,因此必须仅在开始循环时将该数字乘以一个因子,然后将结果乘以另一个因子的 X 倍,然后必须将循环乘以 Y 次,最后我需要总金额...我认为最好查看数字来了解我需要什么 例如,如果我从数
现在我用 JAVA 遇到了一些问题,但不记得如何获取坐标系之间的长度。 例如。A 点 (3,7)B点(7,59) 我想知道如何计算a点和b点之间的距离。非常感谢您的回答。 :-) 最佳答案 A = (
我有两种类型的文本输入,积极的和可疑的。在将输入到这两种类型的输入中的所有数字相加后,我需要显示多组这些输入的总数。例如:2 个阳性 + 2 个可疑 = 总计:4 然后,我需要从总数中找出积极与可疑的
我正在尝试将输入金额乘以 3.5%,任何人都可以给我任何想法如何做到这一点吗? $("#invest_amount").keyup(function() { $('#fee').va
有谁知道返回a的最大数的Math方法 给定的位数。 例如,使用1位数字的最大数字是9,2是99,3是999,4是9999......等等。 使用字符串很容易实现,但这并不完全 我在找什么。 pri
我是 Knockout 的新手,但仍对它一头雾水,我想知道如何使用两个 KO 变量进行简单的数学运算(加法和乘法)。 此刻我有: self.popInc1 = ko.observable('0.3')
我在谷歌地图应用程序中有以下内容,并希望显示转换为英尺的海拔高度,但如何向上/向下舍入到最接近的数字? (消除小数点后的数字)我尝试了 number.toFixed(x) 方法,但似乎什么也没做。 f
我最近开始使用 JavaScript 编写小型 Canvas 游戏,并试图全神贯注于 Vector 2d 数学。我了解 Vectors 的基础知识(比如它们代表 2d 空间中具有方向的点,您可以对它们
我是一名优秀的程序员,十分优秀!