C++ 为什么';这不是简单的C++;工作

C++ 为什么';这不是简单的C++;工作,c++,math,C++,Math,我需要编写一个程序,根据这些规则计算旅行距离的成本 前100英里(含)的每一英里费用为5.50英镑 超过100英里至500英里(含500英里):每英里4.00英镑 超过500英里:每英里2.50英镑 这是我目前的节目 #include <iostream> //for cin >> and cout << using namespace std; int main() // main algorithm { doubl

我需要编写一个程序,根据这些规则计算旅行距离的成本

  • 前100英里(含)的每一英里费用为5.50英镑
  • 超过100英里至500英里(含500英里):每英里4.00英镑
  • 超过500英里:每英里2.50英镑
这是我目前的节目

#include <iostream>     //for cin >> and cout <<
using namespace std;


int main()          // main algorithm
{
    double distance;
    double Milecost1;
    double Milecost2;
    double Milecost3;
    double totalcost;

    cout << "enter distance";
    cin >> (distance);

    void CFM();

    if (distance >= 100) {
        Milecost1 = 100 * 5.50;
    } else {
        totalcost = distance * 5.50;
    }

    void CSM();

    if ( (distance >100) && (distance <= 500) ) {
        Milecost2 = (distance - 100) *4.00;
    }

    void CTM();
    if (distance > 500) {
        Milecost3 = distance - 500 * 2.50;
    }

    void totalcost1();
    totalcost = Milecost1 + Milecost2 + Milecost3;

    cout << "the total cost for the distance travelled is" << totalcost 

    system("pause");        //to hold the output screen
    return(0);
}
#包括//用于cin>>和cout(距离);
无效CFM();
如果(距离>=100){
Milecost1=100*5.50;
}否则{
总成本=距离*5.50;
}
void CSM();
如果((距离>100)和((距离500){
Milecost3=距离-500*2.50;
}
void totalcost1();
总成本=Milecost1+Milecost2+Milecost3;

我可以这样写:

#include <iostream>     //for cin >> and cout <<
using namespace std;


int main()          // main algorithm
{
    double distance;
    double Milecost1;
    double Milecost2;
    double Milecost3;
    double totalcost;


    cout << "enter distance";
    cin >> (distance);

    if (distance >= 100) {
        Milecost1 = 100 * 5.50;
    } else {
       Milecost1 = distance * 5.50;
    }

    if ( (distance >100) && (distance <= 500) ) {
       Milecost2 = (distance - 100) *4.00;
    } else {
        Milecost2 = 0;
    }

    if (distance > 500) {
        Milecost3 = distance - 500 * 2.50;
    } else {
        Milecost3 = 0;
    }

    totalcost = Milecost1 + Milecost2 + Milecost3;

    cout << "the total cost for the distance travelled is" << totalcost ;

    system("pause");        //to hold the output screen
    return(0);
 }
#包括//用于cin>>和cout(距离);
如果(距离>=100){
Milecost1=100*5.50;
}否则{
Milecost1=距离*5.50;
}
如果((距离>100)和((距离500){
Milecost3=距离-500*2.50;
}否则{
Milecost3=0;
}
总成本=Milecost1+Milecost2+Milecost3;

cout不,数学是不正确的,例如,如果距离=501,您将得到

Milecost1: 550
Milecost2: (unitialised)
Milecost3: 2.50
这是假设您在
Milecost3
上更正运算符优先级,因为现在您将乘以500乘以2.5,然后从
距离中减去该值


Milecost2
仅当
distance
在其相关值范围内时才被分配,相反,如果我正确理解了练习,那么
distance 500
的值应该是
0

我认为这应该可以

void main(void)
{
double distance;
double cost;
if(distance <= 100)
{
    cost = distance * 5.50;
}
else if(distance > 100 && distance <= 500)
{
    cost = 100 * 5.50;
    cost += 4.00 * (distance - 100);
}
else
{
    int temp = 100;
    cost = temp * 5,50;
    temp = 400;
    cost += temp * 4.00;
    temp = distance - 500;
    cost += temp * 2.50;
}
}
void主管道(void)
{
双倍距离;
双重成本;

如果(距离100&&distance个人,我会这样做:

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    double distance;
    cout << "Enter distance (Miles): ";
    cin >> distance;

    double milesOver500   = max(0.0, distance-500.0);
    double milesInBetween = max(0.0, distance-milesOver500-100.0);
    double milesUnder100  = max(0.0, distance-milesInBetween-milesOver500);

    double totalCost = milesOver500 * 2.5 +
                       milesInBetween * 4.0 + 
                       milesUnder100 * 5.5;

    cout << "The total cost for the distance traveled is £" << totalCost << endl;

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
双倍距离;
距离;
双倍里程超过500=最大值(0.0,距离-500.0);
双密耳间隙=最大值(0.0,距离超过500-100.0密耳);
100米以下的双倍里程=最大值(0.0,两米之间的距离大于500米);
双倍总成本=500*2.5英里以上+
Milesinbeween*4.0+
100*5.5米以下的里程;

cout如果距离是99,那么您希望Milecost2的值在
totalcost=Milecost1+Milecost2+Milecost3
中是多少?为了可读性,您应该使用适当的缩进。并且通过在特定的检查点打印内容来调试您的程序。Neil Kirk在声明变量时对可能未初始化的值有很好的看法es,最好初始化它们:double Milecost1=0;double Milecost2=0;double Milecost3=0;如果不初始化变量,它们将采用随机数。在您的情况下,如果距离小于100 Milecost2&Milecost3将具有随机值。如果sidtance小于500,Milecost3将具有随机值。这将为您提供一个ran您认为这四条语句的作用是什么:
void CFM();
void CSM();
void CTM();
void totalcost1();