c++在不知道行列数的情况下输入矩阵元素

之前做一些笔试题碰到矩阵元素输入不知道行列数的情况,没法事先设定矩阵的大小及简单使用for循环嵌套输入,这边可以使用c++中的getline()语句,按行读入,然后根据空格分割元素。

getline(cin, str, '#'); //遇到‘#’停止读入,默认为‘\n’

我们首先将每行以string的格式读入,然后以空格为标志将分隔的字符转换为int整数类型,放入vector数组中。
完整的代码如下所示

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
    vector<int> input;
    string temp;
    vector<vector<int>> matrix;
    while ((getline(cin, temp))&&temp!=""){
    	for (int i=0;i<temp.size();++i){
    		int num=0;
    		while (temp[i]!=' '&&temp[i]!='\0'){
    			num=num*10+(temp[i]-'0');
    			++i;
    		}
    		input.push_back(num);
    	}
    	matrix.push_back(input);
    	input.clear(); //每次循环需要将input数组清空
    }
    cout << "matrix:"<<endl;
    for (int i=0;i<matrix.size();++i){
    	for (int j=0;j<matrix[i].size();++j){
    		cout<<matrix[i][j]<<' ';
    	}
    	cout<<endl;
    }
    return 0;
}

这边特别需要注意的是跳出循环的条件,如果只是while (getline(cin, temp)),判断的是cin是否成功,只要有输入(包括回车)一定为true,所以可以加上&&temp!="",当读取的行为空时就跳出循环。

要定义行列未知的复矩阵,需要使用动态组来实现。在C++中,可以使用指针和new运算符动态分配内存空间。下面是一个示例代码: ```c++ #include <iostream> using namespace std; int main() { int rows, cols; // 行列未知 cout << "请输入矩阵的行和列:" << endl; cin >> rows >> cols; // 动态分配内存空间 double **mat = new double *[rows]; for(int i = 0; i < rows; i++) { mat[i] = new double[cols]; } // 输入矩阵元素 cout << "请依次输入矩阵元素:" << endl; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { cin >> mat[i][j]; } } // 打印矩阵 cout << "输入矩阵为:" << endl; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { cout << mat[i][j] << " "; } cout << endl; } // 释放内存空间 for(int i = 0; i < rows; i++) { delete[] mat[i]; } delete[] mat; return 0; } ``` 计算复矩阵的普通乘法也类似,只需要在输入矩阵的时候将元素类型改为复类型即可。具体实现可以参考下面的代码: ```c++ #include <iostream> #include <complex> // 复计算库 using namespace std; int main() { int rows1, cols1, rows2, cols2; cout << "请输入第一个矩阵的行与列:" << endl; cin >> rows1 >> cols1; cout << "请输入第二个矩阵的行与列:" << endl; cin >> rows2 >> cols2; if(cols1 != rows2) { cout << "矩阵无法相乘,请重新输入!" << endl; return 0; } // 动态分配内存空间 complex<double> **mat1 = new complex<double> *[rows1]; for(int i = 0; i < rows1; i++) { mat1[i] = new complex<double>[cols1]; } complex<double> **mat2 = new complex<double> *[rows2]; for(int i = 0; i < rows2; i++) { mat2[i] = new complex<double>[cols2]; } complex<double> **res = new complex<double> *[rows1]; for(int i = 0; i < rows1; i++) { res[i] = new complex<double>[cols2]; } // 输入矩阵元素 cout << "请依次输入第一个矩阵元素:" << endl; for(int i = 0; i < rows1; i++) { for(int j = 0; j < cols1; j++) { double real, imag; cout << "请输入第一个矩阵的第 " << i+1 << " 行第 " << j+1 << " 列元素的实部和虚部:" << endl; cin >> real >> imag; mat1[i][j] = complex<double>(real, imag); } } cout << "请依次输入第二个矩阵元素:" << endl; for(int i = 0; i < rows2; i++) { for(int j = 0; j < cols2; j++) { double real, imag; cout << "请输入第二个矩阵的第 " << i+1 << " 行第 " << j+1 << " 列元素的实部和虚部:" << endl; cin >> real >> imag; mat2[i][j] = complex<double>(real, imag); } } // 矩阵乘法 for(int i = 0; i < rows1; i++) { for(int j = 0; j < cols2; j++) { res[i][j] = 0; for(int k = 0; k < cols1; k++) { res[i][j] += mat1[i][k] * mat2[k][j]; } } } // 打印结果 cout << "矩阵乘积为:" << endl; for(int i = 0; i < rows1; i++) { for(int j = 0; j < cols2; j++) { cout << res[i][j].real() << "+" << res[i][j].imag() << "i "; } cout << endl; } // 释放内存空间 for(int i = 0; i < rows1; i++) { delete[] mat1[i]; } delete[] mat1; for(int i = 0; i < rows2; i++) { delete[] mat2[i]; } delete[] mat2; for(int i = 0; i < rows1; i++) { delete[] res[i]; } delete[] res; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值