注册 登录
编程论坛 C++教室

请问,计算错在那里?

SwanK 发布于 2013-02-27 13:50, 465 次点击
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    int i;
    for (i=0; i<=9;i++){

        double price;
        double lastPrice;
        double inflationRate;

        cout<<"Price in "<<2005+i<<":";
        cin>>price;
        lastPrice=price;
        cout<<"last price"<<lastPrice<<endl;
        cout<<" price"<<price<<endl;


        if (i>=1)

        inflationRate=(price-lastPrice)/lastPrice;

        cout<<inflationRate;
        cout<<fixed<<showpoint;
        cout<<setprecision(2);
        cout<<"from "<< 2005+i <<"  to " <<2005+i+1<<": ";
        cout<<inflationRate*100<<"%  inflation"<<endl;




    }


    cout << "Hello world!" << endl;
    return 0;
}
9 回复
#2
peach54602013-02-27 17:16
太长了,描述不清晰
#3
Susake2013-02-27 17:19
kk.
#4
锋了2013-02-27 17:50
首先,在for语句中声明的变量
double inflationRate;
没有初始化,而当i=0时输出
cout<<inflationRate;?????
出错。c++语言一再强调声明变量或者类最好都初始化他们,否则可能出现不可预料的结果。
还有inflationRate在i>0的时候,这里的结果应该是等于零的吧,不知你想要的是不是这个值
inflationRate=(price-lastPrice)/lastPrice;
还有就是,这里应该加个括号吧,“+”号的优先级小于“()”号
cout<<"Price in "<<2005+i<<":";
后面的涉及到cmath的没看到

(放假这么久了,都没练到,也没来论坛学习,如果我记错了,那不好意思啊)

[ 本帖最后由 锋了 于 2013-2-27 17:52 编辑 ]
#5
SwanK2013-02-28 08:28
谢谢你的回复!
我改成这样:但是题目要求1位小数
例如2005 到2006年是1。8%
我设置了cout<<setprecision(1)<<answer<<endl;
为什么得数还是1。80%呢?
输入是2005: 5.0
      2006:  5.09


#include <iostream>
 #include <iomanip>
#include <cmath>
#include <string>

using namespace std;
void double inflation(double price1,double price2,double &inflationRate){
inflationRate=((price2-price1)/price1)*100;
}

int main()
{
    int i;
    for (i=0; i<=5;i=i+2){

        double price1;
        double price2;
        double inflationRate;

        cout<<"Price in "<<2005+i<<":";
         cin>>price1;

        cout<<"Price in "<<2005+i+1<<":";
         cin>>price2;
         double answer=0.0;
        inflation(price1,price2,answer)
 

        if (i>=1)

        //cout<<inflationRate;
        cout<<fixed<<showpoint;
        cout<<setprecision(1);
        cout<<"from "<< 2005+i <<"  to " <<2005+i+1<<": ";
         cout<<setprecision(1)<<answer<<"%  inflation"<<endl;
    }
    return 0;
}
#6
SwanK2013-02-28 15:24
查出问题是:
cout<<fixed<<setprecision(1)<<answer<<"%  inflation"<<endl;
#7
锋了2013-02-28 15:27
回复 6楼 SwanK
查出就好,自己查出错误是最好的学习方法,那样才能记忆深刻
#8
SwanK2013-02-28 15:34
谢谢,问题解决了一个,很开心!
但是另外还有解决不出,主要都是关于函数的问题,对这个概念还是有点含糊,我觉得我很的不好。你们可帮忙吗?

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>

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


//a)
/*@distance.
 *@double distance(double x1, double x2,double y1, double y2)
 */


 double distance(double x1,double x2,double y1,double y2)

{
    double distance;
    distance=sqrt(pow((x2-x1),2)+ pow((y2-y1),2));
    return distance;
}


//b)
/*@mid distance.
 *@double distance(double x1, double x2,double y1, double y2,double &midX, double &midY);
  */
void midpoint(double x1,double x2,
              double y1,double y2,
              double *midX, double *midY)
{
    *midX=(x2-x1)/2.0;
    *midY=(y2-y1)/2.0;
    //cout<<(x2-x1)/2.0<<","<< (y2-y1)/2.0;
}

//c)
/*@diameter.
 *@double radius(double x1, double y1,
 *               double x2, double y2)
 */


void radius(double x1, double y1,
                   double x2, double y2)
{
//double radius;
    double diameter;
    double r;
    distance(x1,x2,y1,y2);
    r=diameter/2.0;
    return radius(x1,x2,y1,y2);

}


//d)

/*@circumference(double r)
  *@double radius(double x1, double y1,
 *               double x2, double y2)
 */
//double circumference(double r);
double circumference(double r){
    const double PI=3.14;
    double circumference;
    circumference=2*PI* r;
    //return circumference(r);
    return circumference;

}

//e)
/*@circumference.
 *@double circumference(double x1, double y1,
                         double x2, double y2)
 */
double circumference(double x1, double y1, double x2, double y2)
{

    const double PI=3.14;
    distance(x1,x2,y1,y2);

    return circumference(x1,x2,y1,y2);

}

int main()
{

    double x1,x2,y1,y2;
    cout<<"enter x1,x2,y1,y2:"<<endl;
    cin>> x1>>x2>>y1>>y2;
    distance(x1,x2,y1,y2);
    cout<<" The distance between them is:"<< distance <<endl;
    cout<<"midpoint is:"<<x2-x1<<",y2-y1"<endl;

    cout<<"the circle's radius is:"<<

    return 0;
}


#9
风雨1232013-03-07 21:31
这个真的有好长
#10
小菜B2013-03-08 10:58
你是想算通货膨胀率么?
#include <iostream>
 #include <iomanip>
 #include <cmath>
 #include <string>
 
using namespace std;
 double  inflation(double price1,double price2){
 return ((price2-price1)/price1)*100;
 }
 
int main()
 {double price1,price2,rate;
     int i;
     for (i=0; i<=5;i=i+1)
     {
     if(i==0)
     {     cout<<"请输入2005年的价格:";
         cin>>price1;}
     else
     {cout<<"请输入"<<2005+i<<"年的价格";
cin>>price2;
 rate=inflation(price1,price2);
 price1=price2;
     cout<<"通货膨胀率"<<rate<<"%";}
     }
}
1