在//********和//========填好程序源文件使其达到目的功能。
由键盘输入n个坐标类数据,计算出重心后,应用文件流对象保存在文本文件oop9_1.out中。第一行存放数据n,接着将n个坐标数据(每行一个坐标,以空格间隔),最后一行存放...
由键盘输入n个坐标类数据,计算出重心后,应用文件流对象保存在文本文件oop9_1.out中。第一行存放数据n,接着将n个坐标数据(每行一个坐标,以空格间隔),最后一行存放重心。
样例输入:
3
1 1
2 2
3 3
样例输出:
3
1 1
2 2
3 3
2 2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
typedef struct _XY
{ float x,y;
}XY;
class point
{ int n;
XY *p;
public:
point(int nn);
XY heart();
void input();
void save(ofstream &out);
~point(){ delete[]p;}
};
//*********************
//=====================
int main()
{ FILE *fp;
if((fp=fopen("oop9_1.in","r"))!=NULL)
freopen("oop9_1.in", "r", stdin);
int n;
cin>>n;
ofstream out;
out.open("oop9_1.out");
point p(n);
p.input();
p.save(out);
out.close();
return 0;
} 展开
样例输入:
3
1 1
2 2
3 3
样例输出:
3
1 1
2 2
3 3
2 2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
typedef struct _XY
{ float x,y;
}XY;
class point
{ int n;
XY *p;
public:
point(int nn);
XY heart();
void input();
void save(ofstream &out);
~point(){ delete[]p;}
};
//*********************
//=====================
int main()
{ FILE *fp;
if((fp=fopen("oop9_1.in","r"))!=NULL)
freopen("oop9_1.in", "r", stdin);
int n;
cin>>n;
ofstream out;
out.open("oop9_1.out");
point p(n);
p.input();
p.save(out);
out.close();
return 0;
} 展开
1个回答
展开全部
代码如下:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
typedef struct _XY
{
float x,y;
}XY;
class point
{
int n;
XY *p;
public:
point(int nn);
XY heart();
void input();
void save(ofstream &out);
~point(){ delete[]p;}
};
//*********************
point::point(int nn)
{
p = new XY[nn];
n = nn;
}
void point::input()
{
for(int i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
}
}
void point::save(ofstream &out)
{
char str[20];
float x=0,y=0;
sprintf(str,"%d\n",n);
out.write(str,strlen(str));
for(int i=0;i<n;i++)
{
sprintf(str,"%f %f\n",p[i].x,p[i].y);
out.write(str,strlen(str));
x += p[i].x;
y += p[i].y;
}
sprintf(str,"%f %f\n",x/n,y/n);
out.write(str,strlen(str));
}
//=====================
int main()
{
FILE *fp;
if((fp=fopen("oop9_1.in","r"))!=NULL)
freopen("oop9_1.in", "r", stdin);
int n;
cin>>n;
ofstream out;
out.open("oop9_1.out");
point p(n);
p.input();
p.save(out);
out.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
typedef struct _XY
{
float x,y;
}XY;
class point
{
int n;
XY *p;
public:
point(int nn);
XY heart();
void input();
void save(ofstream &out);
~point(){ delete[]p;}
};
//*********************
point::point(int nn)
{
p = new XY[nn];
n = nn;
}
void point::input()
{
for(int i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
}
}
void point::save(ofstream &out)
{
char str[20];
float x=0,y=0;
sprintf(str,"%d\n",n);
out.write(str,strlen(str));
for(int i=0;i<n;i++)
{
sprintf(str,"%f %f\n",p[i].x,p[i].y);
out.write(str,strlen(str));
x += p[i].x;
y += p[i].y;
}
sprintf(str,"%f %f\n",x/n,y/n);
out.write(str,strlen(str));
}
//=====================
int main()
{
FILE *fp;
if((fp=fopen("oop9_1.in","r"))!=NULL)
freopen("oop9_1.in", "r", stdin);
int n;
cin>>n;
ofstream out;
out.open("oop9_1.out");
point p(n);
p.input();
p.save(out);
out.close();
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询