程序设计思维

例题:编写一个程序,不断要求用户输入两个数,直到其中一个为0,对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数=2.0*x*y/(x+y)

include<iostream>
double t_av(double x,double y);
int main()
{
using namespace std;
double x, y;
double result;
cout<<"please enter two numbers (0 to stop);
while ((cin>>x>>y)&&x!=0  &&y!=0)
{
result=t_av(x,y);
cout<<"调和平均数=”<<result<<endl;
cout<<"please enter two number (0 to stop): ";
}
return 0;
}
double t_av(double x,double y)
{
return 2.0*x*y/(x+y);
}


 #include<iostream>
   using namespace std;
   void average(double,double);
   int main()
   {
     double A,B;
     cout<,"please enter two numbers:";
     while (cin>>A>>B)
     

 {
 if(A==0||B==0)
    break;
 else
    average(A,B);
    cout<<please enter two number:";
    }
    cout<<"Bye!\n";
    return 0;
}
void average(double x,double y)
{
cout<<"The average is:"
       <<2.0*x*y/(x+y)
       <<endl;
 }

关于编程思想:如何写出代码:
1:按照程序的角度去拆解问题,知识点已经在书上,而且有代码实例,但是综合程序却是将多个知识点结合起来。
先分析问题,这个题目表达的是什么意思,目前已经学习了8章,将知识点作为工具(例导数就是工具)用旧知识解决新问题。
2:划流程图:要使用几个函数体,开头要做什么,还有栈,堆等等。有序的逻辑思维是表达出程序的关键。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43360397/article/details/85011925