我想在C++中創建一個通話計費程序。這個小程序的特點之一是可以在折扣時間內更新折扣分鐘,在營業時間內更新正常價格的分鐘數,具體取決於進入哪個時間段。根據循環內的兩個不同條件更新計數器
用戶首先進入時間這樣22:00
字符串然後,我有一個函數,它接受字符串,並把它變成一個整數。例如,上面的22點變成2200
然後我有另一個幫助函數,它接受一個int,上面的int並將其轉換爲小數時間。
double turnTimeToDecimal(int timeRaw){
double decimalTime;
decimalTime = timeRaw * 0.01;
return decimalTime;
}
營業時間其中承擔任何折扣,是爲了對付這個功能我更新內部的兩個計數器for循環,從0到1417分鐘上午8時及18.30am之間(24小時):
double myStartDecimal = 0.0;
double myStopDecimal = 0.0;
myStartDecimal = turnTimeToDecimal(myStartRaw);
myStopDecimal = turnTimeToDecimal(myStopRaw);
//hours and minutes start
int hourStart = (int)floor(myStartDecimal);
int minuteStart = (int)round(100*(myStartDecimal - hourStart));
//hours and minutes stop
int hourStop = (int)floor(myStopDecimal);
int minuteStop = (int) round(100*(myStopDecimal - hourStop));
int totalMinutesPremium = 0;
int totalMinutesDiscount = 0;
int i = 0;
int k = 0;
for(k = (hourStart* 60) + minuteStart; k < (hourStop * 60) + minuteStop + round(((double)minuteStop/100)); k++){
//will update the corresponding counter depending
//on the time stretch, business hours 8.00 - 18.30.
if(hourStart >= 8 && hourStop < 18.5){
totalMinutesPremium++;
}else{
totalMinutesDiscount++;
}
}
//will give total minutes
cout << " k is: " << k << endl;
//will give the total number of minutes during the business hours and
//the total number of minutes during the discount hours(non-bussiness hours)
cout << "Total minutes premium " << round(totalMinutesPremium) <<
" Total Minutes discount " << round(totalMinutesDiscount) << endl;
但是,程序確實檢測輸入的時間延長是否在營業時間內,除非有一種情況。例如,如果時間在7:30到8:30之間(營業時間在7:59-18:30之間),它不會在營業時間內返回分鐘的組合,我希望在營業時間內的分鐘數爲30分鐘,我預計會有30分鐘的折扣,因爲折扣在07:59結束,並在18:31重新開始。
希望我自己清楚。
哪裏是'hourStop'的聲明? (更具體地說,它是什麼類型?) – Mike 2013-04-08 13:23:37
是小時開始一個浮點變量?我猜這是int?我會建議在時間上做數學運算,並且使用時間上的差異而不是循環...這是一種非常昂貴的方法 – 2013-04-08 13:24:00
您不在循環中使用'k',所以您只需添加到'totalMinutesDiscount'或'totalMinutesPremium',這可能不是你想要的。你應該儘量擺脫完整的循環,因爲你可以通過簡單的減法找到時間差異。 – riv 2013-04-08 13:29:00