使用 C++设计一个程序,功能如下: 从文件“input.txt”中读入数据(一组二维坐标(x,y)),文件格式如下: 大 34.1 45,44 -323.67 493.56 42.55 -89.22 34.11 99.56 ...... 每行第一个数为 x,第二个数为y。 读取数据后,通过按键控制程序: 1.输入字符为's’时,在屏幕上显示所有坐标; 2.输入字符为'x’时,依据x对所有坐标进行排序并输出到屏幕; 3.输入字符为'y’时,依据y对所有坐标进行排序并输出到屏幕; 4.输入字符为'n’时,在屏幕上输出坐标数目; 5.输入字符为'c’时,在屏幕上输出所有点的质心位置(假设每个点质量相同); 6.输入字符为'l’时,对坐标进行最小二乘法拟合,输出拟合的斜率、截距 和误差; 7.输入字符为'i’时,要求用户再输入一个整数 n,输入后屏幕显示第n个 发给 坐标值; 8.输入字符为'v’时,所有坐标沿x轴进行镜像变换,并将变换后的坐标屏幕上输出; 9.输入字符为'h’时,所有坐标沿y轴进行镜像变换,并将变换后的坐标在屏幕上输出; 10.输入字符为'r’时,要求用户再输入一个数 a,输入后所有坐标绕原点旋转a度; 11.输入字符为'm’时,要求用户再输入两个数 x0,y0,输入后将所有坐标沿 x和y轴分别平移 x0,y0,并在屏幕上输出所有坐标;12.输入字符为'e’时,退出程序。
时间: 2024-03-18 14:42:17 浏览: 28
好的,我会根据您的要求进行回答,以下是C++代码实现:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
//定义二维坐标结构体
struct Point {
double x;
double y;
};
//按照x进行排序
bool cmp_x(Point a, Point b) {
return a.x < b.x;
}
//按照y进行排序
bool cmp_y(Point a, Point b) {
return a.y < b.y;
}
//计算质心
Point center_of_mass(vector<Point> points) {
Point center;
center.x = 0;
center.y = 0;
int n = points.size();
for (int i = 0; i < n; i++) {
center.x += points[i].x;
center.y += points[i].y;
}
center.x /= n;
center.y /= n;
return center;
}
//计算最小二乘法拟合的斜率、截距和误差
void least_square_method(vector<Point> points, double& k, double& b, double& error) {
int n = points.size();
double sum_x = 0, sum_y = 0, sum_xy = 0, sum_xx = 0;
for (int i = 0; i < n; i++) {
sum_x += points[i].x;
sum_y += points[i].y;
sum_xy += points[i].x * points[i].y;
sum_xx += points[i].x * points[i].x;
}
double x_mean = sum_x / n;
double y_mean = sum_y / n;
k = (sum_xy - n * x_mean * y_mean) / (sum_xx - n * x_mean * x_mean);
b = y_mean - k * x_mean;
error = 0;
for (int i = 0; i < n; i++) {
error += pow(points[i].y - k * points[i].x - b, 2);
}
error = sqrt(error / n);
}
//沿x轴进行镜像变换
void mirror_x(vector<Point>& points) {
int n = points.size();
for (int i = 0; i < n; i++) {
points[i].y = -points[i].y;
}
}
//沿y轴进行镜像变换
void mirror_y(vector<Point>& points) {
int n = points.size();
for (int i = 0; i < n; i++) {
points[i].x = -points[i].x;
}
}
//绕原点旋转
void rotate(vector<Point>& points, double a) {
int n = points.size();
double radian = a * M_PI / 180.0;
for (int i = 0; i < n; i++) {
double x = points[i].x;
double y = points[i].y;
points[i].x = x * cos(radian) - y * sin(radian);
points[i].y = x * sin(radian) + y * cos(radian);
}
}
//平移
void translate(vector<Point>& points, double x0, double y0) {
int n = points.size();
for (int i = 0; i < n; i++) {
points[i].x += x0;
points[i].y += y0;
}
}
int main() {
//从文件中读取数据
ifstream fin("input.txt");
vector<Point> points;
double x, y;
while (fin >> x >> y) {
Point p;
p.x = x;
p.y = y;
points.push_back(p);
}
fin.close();
//循环控制程序
char ch;
while (true) {
cout << "请输入指令:";
cin >> ch;
switch (ch) {
case 's':
//显示所有坐标
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'x':
//依据x进行排序
sort(points.begin(), points.end(), cmp_x);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'y':
//依据y进行排序
sort(points.begin(), points.end(), cmp_y);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'n':
//输出坐标数目
cout << "坐标数目为:" << points.size() << endl;
break;
case 'c':
//输出质心位置
Point center = center_of_mass(points);
cout << "质心位置为:(" << center.x << ", " << center.y << ")" << endl;
break;
case 'l':
//最小二乘法拟合
double k, b, error;
least_square_method(points, k, b, error);
cout << "斜率为:" << k << endl;
cout << "截距为:" << b << endl;
cout << "误差为:" << error << endl;
break;
case 'i':
//输出第n个坐标值
int n;
cout << "请输入n:";
cin >> n;
if (n >= 1 && n <= points.size()) {
cout << "第" << n << "个坐标为:" << "(" << points[n-1].x << ", " << points[n-1].y << ")" << endl;
}
else {
cout << "输入错误!" << endl;
}
break;
case 'v':
//沿x轴进行镜像变换
mirror_x(points);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'h':
//沿y轴进行镜像变换
mirror_y(points);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'r':
//绕原点旋转
double a;
cout << "请输入a:";
cin >> a;
rotate(points, a);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'm':
//沿x和y轴分别平移
double x0, y0;
cout << "请输入x0和y0:";
cin >> x0 >> y0;
translate(points, x0, y0);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ")" << endl;
}
break;
case 'e':
//退出程序
return 0;
default:
cout << "输入错误!" << endl;
break;
}
}
return 0;
}
```
代码中实现了上述功能,您可以根据需要进行调整或扩展。
阅读全文