我的教授希望我们在不使用数组或向量的情况下编写一个程序:
使用函数编写了一个程序,用于计算和打印每个将汽车停在车库里的n名顾客的停车费。
停车费:
你应该为每个顾客输入停放的时间。你的程序应该以整洁的表格格式打印结果,并计算和打印你的收据的总数。
程序输出应该如下所示:
汽车-小时-收费1
我只走了这么远:
include <iostream>
include <conio.h>
include <cmath>
include <iomanip>
using namespace std;
double calculate(double);
int main()
{
double hours,charge;
int finish;
double sumhours;
sumhours=0;
finish=0;
charge=0;
int cars;
cars=0;
do
{
cout<<"Enter the number of hours the vehicle has been parked: "<<endl;
cin>>hours;
cars++;
sumhours+=hours;
finish=cin.get();
if(hours>24)
{
cout<<"enter a time below 24hrs."<<endl;
cars--;
sumhours=sumhours-hours;
}
}
while(finish!=EOF);
double total=calculate(hours);
cout<<total<<": "<<(cars-1)<<": "<<sumhours;
while(!_kbhit());
return 0;
}
double calculate(double time)
{
double calculate=0;
double fees;
if(time<=5)
return 5;
if(time>15)
return 10;
time=ceil(time);
fees=5+(.5*(time-5));
return calculate;
}发布于 2011-08-10 00:13:26
在每次迭代中,生成相关的输出,但不要将其流到std::cout。相反,将其流到std::stringstream对象。最后,将该对象流到std::cout。数学可以简单地通过保持输入值的不断积累来完成。
当然,这假设在本作业练习中使用std::stringstream并不被认为是“作弊”。
发布于 2011-08-10 00:13:11
因为这是作业,这里有一个算法:
3.1阅读记录。
3.2打印记录内容
3.3 向运行总变量的添加记录字段值(提示!暗示!)
3.4。运行总变量的end-while
您可能需要对正在运行的总变量进行一些额外的计算,特别是对于平均值。
编辑1:运行总变量的示例
int sum = 0; // This is the running total variable.
const unsigned int QUANTITY = 23;
for (unsigned int i = 0; i < QUANTITY; ++i)
{
cout << "Adding " << i << " to sum.\n";
sum += i;
}
cout << "Sum is: " << sum << "\n";
cout.flush();在本例中,数据'i‘不仅存储在使用中。sum变量是一个正在运行的总计。在作业中寻找相似之处。
编辑2: cin输入端检测实例
char reply = 'n';
while (tolower(reply) != 'y')
{
cout << "Do you want to quit? (y/n)";
cout.flush();
cin >> reply;
cin.ignore(1000, '\n'); // Eat up newline.
}
cout << "Thanks for the answer.\n";
cout.flush();发布于 2011-08-10 00:14:09
因为你不能使用数组或向量,我认为你应该打印每辆车的停车数据,因为它正在被处理。伪码:
While more cars:
Read data for next car
Calculate cost
Print data
Add to running totals
End while
Print totalshttps://stackoverflow.com/questions/7004495
复制相似问题