长沙的士的方法计算费用 从键盘读入计算乘车的距离,计算所需付的乘车费用。
#include <iostream>
#include <cmath>
using namespace std;
double calc(double d)
{
double money = 0.0;
if (d >= 10.0) {
money += d * 1.8 * 1.5;
}
else {
money += 3.0;
if (d > 0 && d <= 1.0) {
money += 0.0;
}
else if (d > 1.0 && d <= 1.5) {
money += 4.0;
}
else if (d > 1.5 && d <= 2.0) {
money += 5.0;
}
else if (d > 2.0 && d <= 2.5) {
money += 6.0;
}
else if (d > 2.5 && d <= 3.0) {
money += 7.0;
}
else {
money += ceil(d - 3.0) * 1.8;
}
}
money = ceil(money / 0.5) * 0.9;
return money;
}
int main()
{
double d;
cout << "请输入公里数: ";
cin >> d;
double money = calc(d);
cout << "车费为: " << money << " 元" << endl;
return 0;
}
这个就是利用if else判断就行了
不知道你这个问题是否已经解决, 如果还没有解决的话:基类只打印自己的,派生类将重写的不打印,只打印自己的
基类大小不变,派生类的大小和无重写几乎一样,不过多了一个用于区分对象的0,这个0加进了派生类数据成员和指向基类虚表的指针之间
#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
b = 10;
}
virtual void fun0()
{
cout << "Base::fun0()" << endl;
}
virtual void fun1()
{
cout << "Base::fun1()" << endl;
}
virtual void fun2()
{
cout << "Base::fun2()" << endl;
}
int b;
};
class Derived :virtual public Base
{
public:
Derived()
{
d = 20;
}
virtual void fun0()
{
cout << "Derived::fun0()" << endl;
}
virtual void fun1()
{
cout << "Derived::fun1()" << endl;
}
virtual void fun5()
{
cout << "Derived::fun5()" << endl;
}
int d;
};
typedef void(*vpf)();
void Printvpf()
{
Base b;
Derived d;
//Base* p = &d;
cout << "Base::vpf" << endl;
vpf* n = (vpf*)*(int *)(&b);
while (*n)
{
(*n)();
n++;
}
cout << "Derived::vpf" << endl;
vpf* pn = (vpf*)*(int *)(&d);
while (*pn)
{
(*pn)();
pn++;
}
cout << sizeof(Base) << endl;
cout << sizeof(Derived) << endl;
}
int main()
{
Printvpf();
system("pause");
return 0;
}