C++简单的改写问题,在线等!!!下午用!!

来源:百度知道 编辑:UC知道 时间:2024/05/10 19:35:24
#include<iostream>
#include<cmath>
using namespace std;
void main()
{
int x,y;
float c,q;
bool flag=true;
cout<<"请输入直角坐标(x,y):"<<endl;
cin>>x>>y;
bool convert(int x,int y,float*c,float*q);
flag=convert(x,y,&c,&q);
if(flag)

{
cout<<"转换成功!";
cout<<"直角坐标值("<<x<<","<<y<<")"<<"对应的极坐标是("<<c<<","<<q<<")"<<endl;
}
else
{
cout<<"x不能为0值,转换失败!";
}
}
bool convert(int x,int y,float *c,float*q)
{
if(x==0)
return false;
else
{
*c=sqrt(pow(x,2)+pow(y,2));
*q=atan(y/x);
return true;
}
}
改写要求:c值以指针形参返回,q值以引用形参返回,而函数的函数值反映x值是否合法。

只改写convert函数及其调用方式就可以
#include<iostream>
#include<cmath>
using namespace std;
void main()
{
int x,y;
float c,q;
bool flag=true;
cout<<"请输入直角坐标(x,y):"<<endl;
cin>>x>>y;
bool convert(int x,int y,float*c,float& q); //修改声明
flag=convert(x,y,&c,q); //修改调用语句
if(flag)

{
cout<<"转换成功!";
cout<<"直角坐标值("<<x<<","<<y<<")"<<"对应的极坐标是("<<c<<","<<q<<")"<<endl;
}
else
{
cout<<"x不能为0值,转换失败!";
}
}
bool convert(int x,int y,float *c,float&q) //修改函数实现
{
if(x==0)
return false;
else
{
*c=sqrt(pow(x,2)+pow(y,2));
q=atan(y/x); //q的赋值,修改
return true;
}
}

讲清楚有完成什么功能