键盘上的C++输入

C++ Input from Keyboard

本文关键字:输入 C++ 键盘      更新时间:2024-03-29
#include <iostream>
using namespace std;
const int N = 3;
void swap(double matrix[N][N + 1], int i, int j)
{
...
}
int forwardElim(double mat[N][N + 1])
{
...
}
void backSub(double mat[N][N + 1])
{
...
}
void gaussianElimination(double mat[N][N + 1])
{
...
}
int main()
{
int i, j;
double matrix[N][N+1];
int k = 1;
for (i = 0; i < N; i++)
{
cout << "Enter value for equation " << k << ": " << endl;
for (j = 0; j < N +1; j++)
{
cout << "[" << i << "]" << "[" << j << "] = ";
cin >> matrix[i][j];
}
k++;
}
cout << "Your equation is: " << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N+1; j++) {
cout << matrix[i][j] << "   ";
}
cout << endl;
}
gaussianElimination(matrix);
system("pause");
}

我是一个新手,所以有人能帮我解决这个基本问题吗。如何从键盘上获得任何N值,但不在主函数之外?因为我使用N 仍有一些主功能之外的功能

您可以使用std::vector而不是静态数组轻松完成所需的操作。使用std::vector::size可以获得n。您可以通过使用基于范围的for循环来简化代码。避免使用using namespace std;。以下是一些不言自明的代码:

#include <iostream>
#include <vector>
/// some other function
void foo(std::vector<std::vector<double>>
&matrix) {  // <-- make function arguments like this, instead of
// double matrix[N][N+1]
int n = matrix.size(); // <-- put this line inside the function if you need
// the value of n
std::cout << "n = " << n;
// do something here
}
int main() {
int n;
std::cout << "Enter n: ";
std::cin >> n;
std::vector<std::vector<double>> matrix(n, std::vector<double>(n + 1));
for (int i = 0; i < n; ++i) {
std::cout << "Enter value for equation " << i + 1 << ":n";
for (int j = 0; j < n + 1; ++j) {
std::cout << "[" << i << "]"
<< "[" << j << "] = ";
std::cin >> matrix[i][j];
}
}
std::cout << "Your equation is:n";
for (auto &&row : matrix) {
for (auto &&ele : row)
std::cout << ele << 't';
std::cout << 'n';
}
foo(matrix); // <-- function call remains the same
}

使用全局变量n的方法(如果您计划使用此方法,则只需要更改代码的4-5行(:

#include <iostream>
#include <vector>
int n; // make n global, non-const
/// some other function
void foo(std::vector<std::vector<double>>
&matrix) { // <-- make function arguments like this, instead of
// double matrix[N][N+1]
// no need for int n = n.size(); here
std::cout << "n = " << n;
// do something here
}
int main() {
// int n; <-- no need to declare it here, it's available globally
std::cout << "Enter n: ";
std::cin >> n;
std::vector<std::vector<double>> matrix(n, std::vector<double>(n + 1));
// rest code is same as the above...
}

正如评论中所建议的那样,使用向量会更好。因此,由于N是全局声明的,main可以更改N的值,您只需要确保它不是const。由于变量N的作用域是全局的,所以它可以在任何函数中使用和编辑。

#include <iostream>
#include <vector>
using namespace std;
int N = 3;  // Putting some default value
void swap(std::vector<std::vector<double>> &matrix, int i, int j)
{
/*Wherever in the function you are using N , you can use it as normal or you can also get the size as */
// Alternative way to get size if using vectors
int size= matrix.size();
...
}
int forwardElim(std::vector<std::vector<double>> &matrix)
{
...
}
void backSub(std::vector<std::vector<double>> &matrix)
{
...
}
void gaussianElimination(std::vector<std::vector<double>> &matrix)
{
...
}
int main()
{
int i, j;
cout<<"Enter value of N"<<endl;
cin>>N;  // Taking value of N from user 

/* Declaring 2D matrix using vectors of size n*n+1 */
vector<vector<double> > matrix (N,vector<double> (N+1)); 
/* Rest of the program stays the same */
int k = 1;
for (i = 0; i < N; i++)
{
cout << "Enter value for equation " << k << ": " << endl;
for (j = 0; j < N +1; j++)
{
cout << "[" << i << "]" << "[" << j << "] = ";
cin >> matrix[i][j];
}
k++;
}
cout << "Your equation is: " << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N+1; j++) {
cout << matrix[i][j] << "   ";
}
cout << endl;
}
gaussianElimination(matrix);
system("pause");
return 0;
}

否则,你可以简单地这样做,这将起作用,但不建议

#include <iostream>
#include <vector>
using namespace std;
int N; //Removed Const
int main()
{
int i, j;
cout<<"Enter value of N"<<endl;
cin>>N;
double matrix[N][N+1];
//Rest of your code which stays the same

在C++中,数组具有编译时固定的大小。如果你想要一个可变大小,那么你需要使用动态内存。这意味着您可以使用指针手动分配,如下所示:

int N;
int main()
{
cin >> N;
int** matrix = new int*[N];
for(int i = 0; i < N; i++)
{
matrix[i] = new int[N + 1];
}
}

注意,在这里你还必须手动释放这个内存

for(int i = 0; i < N; i++)
{
delete[] matrix[i];
}
delete[] matrix;

显然,这会使事情变得复杂,所以人们建议不要新建/删除。处理这个问题的一种更方便的方法是使用一个动态容器类,如std::vector或智能指针类。

#include <iostream>
#include <vector>
int N;
int main()
{
cin >> N;
std::vector<std::vector<int>> matrix; // this made us a matrix with dimensions [0][0] so we need to reserve the dimensions
for(int i = 0; i < N; i++)
{
// this adds a row of size (N + 1)
matrix.push_back(std::vector<int>(N+1));
}
}

它不需要手动删除或类似的东西,它的工作方式类似于数组的工作方式,所以像这个

matrix[0][0] += 10;

仍然有效。

还要注意,有更有效的方法来处理多维数组的问题。