键盘上的C++输入
C++ Input from Keyboard
#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;
仍然有效。
还要注意,有更有效的方法来处理多维数组的问题。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type