使用 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 08:42:10 浏览: 54
好的,以下是 C++ 设计的程序。请注意,我已经按照您的要求将输入文件名设置为“input.txt”,并将所有输出打印在屏幕上。程序中的注释应该能够帮助您理解代码。
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
// 二维坐标结构体
struct Point {
double x;
double y;
};
// 输出所有坐标
void printAllPoints(vector<Point>& points) {
for (const auto& p : points) {
cout << "(" << p.x << "," << p.y << ") ";
}
cout << endl;
}
// 按照 x 坐标排序并输出
void sortByX(vector<Point>& points) {
sort(points.begin(), points.end(), [](const Point& p1, const Point& p2) {
return p1.x < p2.x;
});
printAllPoints(points);
}
// 按照 y 坐标排序并输出
void sortByY(vector<Point>& points) {
sort(points.begin(), points.end(), [](const Point& p1, const Point& p2) {
return p1.y < p2.y;
});
printAllPoints(points);
}
// 输出所有坐标的数量
void printNumPoints(vector<Point>& points) {
cout << "Number of points: " << points.size() << endl;
}
// 输出所有坐标的质心位置
void printCentroid(vector<Point>& points) {
double sumX = 0.0;
double sumY = 0.0;
for (const auto& p : points) {
sumX += p.x;
sumY += p.y;
}
double centroidX = sumX / points.size();
double centroidY = sumY / points.size();
cout << "Centroid: (" << centroidX << "," << centroidY << ")" << endl;
}
// 最小二乘法拟合
void leastSquaresFit(vector<Point>& points) {
double sumX = 0.0;
double sumY = 0.0;
double sumXY = 0.0;
double sumX2 = 0.0;
double sumY2 = 0.0;
for (const auto& p : points) {
sumX += p.x;
sumY += p.y;
sumXY += p.x * p.y;
sumX2 += p.x * p.x;
sumY2 += p.y * p.y;
}
double n = points.size();
double slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
double intercept = (sumY - slope * sumX) / n;
double error = 0.0;
for (const auto& p : points) {
double residual = p.y - (slope * p.x + intercept);
error += residual * residual;
}
error = sqrt(error / (n - 2));
cout << "Slope: " << slope << endl;
cout << "Intercept: " << intercept << endl;
cout << "Error: " << error << endl;
}
// 输出第 n 个坐标
void printNthPoint(vector<Point>& points, int n) {
if (n >= 1 && n <= points.size()) {
cout << "(" << points[n-1].x << "," << points[n-1].y << ")" << endl;
} else {
cout << "Invalid index." << endl;
}
}
// 沿 x 轴进行镜像变换并输出
void mirrorX(vector<Point>& points) {
for (auto& p : points) {
p.y = -p.y;
}
printAllPoints(points);
}
// 沿 y 轴进行镜像变换并输出
void mirrorY(vector<Point>& points) {
for (auto& p : points) {
p.x = -p.x;
}
printAllPoints(points);
}
// 绕原点逆时针旋转 a 度并输出
void rotate(vector<Point>& points, double a) {
double radians = a * M_PI / 180.0;
for (auto& p : points) {
double newX = p.x * cos(radians) - p.y * sin(radians);
double newY = p.x * sin(radians) + p.y * cos(radians);
p.x = newX;
p.y = newY;
}
printAllPoints(points);
}
// 沿 x 轴和 y 轴分别平移 x0 和 y0 并输出
void translate(vector<Point>& points, double x0, double y0) {
for (auto& p : points) {
p.x += x0;
p.y += y0;
}
printAllPoints(points);
}
int main() {
// 读取数据
ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
cout << "Error: unable to open input file." << endl;
return 0;
}
vector<Point> points;
double x, y;
while (inputFile >> x >> y) {
Point p = { x, y };
points.push_back(p);
}
inputFile.close();
// 控制程序
char input;
while (true) {
cout << "Enter a command: ";
cin >> input;
switch (input) {
case 's':
printAllPoints(points);
break;
case 'x':
sortByX(points);
break;
case 'y':
sortByY(points);
break;
case 'n':
printNumPoints(points);
break;
case 'c':
printCentroid(points);
break;
case 'l':
leastSquaresFit(points);
break;
case 'i':
int n;
cout << "Enter an index: ";
cin >> n;
printNthPoint(points, n);
break;
case 'v':
mirrorX(points);
break;
case 'h':
mirrorY(points);
break;
case 'r':
double a;
cout << "Enter an angle of rotation: ";
cin >> a;
rotate(points, a);
break;
case 'm':
double x0, y0;
cout << "Enter x and y translations: ";
cin >> x0 >> y0;
translate(points, x0, y0);
break;
case 'e':
return 0;
default:
cout << "Invalid command." << endl;
break;
}
}
return 0;
}
```
阅读全文