将字符串转换为矩阵数组 [英] Convert string into array of matrix

查看:557
本文介绍了将字符串转换为矩阵数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要开发一个程序来执行Matrix操作。该程序使用定义的字符串格式来表示用户输入和输出部分中的矩阵。对于以下矩阵:examplee:用户输入Matrix的字符串表示形式:ex:[10 2.13 3; -5 0 4; 16.5 1 8]在程序中,用户输入定义的字符串格式的矩阵,然后要求进入运营商



因此我无法将此字符串转换为数组以使操作成为总和或乘以。



我尝试过:



It is required to develop a program to do Matrix operations. The program use a defined string format to represent the matrix in the user input and output sections. For the following matrix: examplee : the user input the string representation of the Matrix : ex:[10 2.13 3;-5 0 4;16.5 1 8] In the program, the user enters a matrix in the defined string format then asked to enter an operator

so i can't convert this string to an array to make operations as sum or multiply.

What I have tried:

using namespace std;

int main() { 

    int i, j, n; 
    string s1; 
    float m1[100][100]; 

    getline(cin, s1);

    for (int i = 0; i < 100; i++) { 
        for (n = 0; n < s1.length(); n++) { 
            if (isspace(s1.at(i)));
        } 
    }
}

推荐答案

首先需要使用半分割字符串冒号作为分隔符。这将为每行提供一串数字。然后使用逗号分隔每个子字符串作为分隔符,它将为每列提供数字。然后,您可以为矩阵分配数组。然后,您需要依次获取每个数字,将其转换为double值并将其存储在矩阵的相关单元格中。请参阅 basic_string类Microsoft Docs [ ^ ]了解如何查找分隔符。
You first need to split the string using the semi-colon as the separator,. That will give you a string of numbers for each row. Then split each substring using the comma as separator which will give the numbers for each column. You can then allocate the array for the matrix. You then need to take each number in turn, convert it to a double value and store it in the relevant cell of the matrix. See basic_string Class | Microsoft Docs[^] to see how to find the separator characters.


如何在C ++中拆分字符串 - Fluent C ++ [ ^ ]


这篇关于将字符串转换为矩阵数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆