我有一个项目要编写一个对矩阵执行数学运算的代码。用户以字符串形式输入一个矩阵,然后输入一个运算符,如果它是+或-或*或/,则用户必须输入另一个矩阵... 因此,我想创建一个执行每个操作的函数,并从ADD函数开始,但是在主函数中调用它时出现错误。 输入矩阵的示例:[3 4 9; 2 5 8; 1 2 50] 注意:不得打印出多余的空格或分号。
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
string ADD (float matrix1,float matrix2,int arraySize);
int main()
{
string s1; //first matrix
char op; //operator
string s2;//second matrix
int y; //for power operation
int n = 0; //number of rows of first matrix
int m = 0; //number of columns of first matrix
int o = 0; //number of rows of second matrix
int p = 0;//number of columns of second matrix
getline(cin, s1);
for (int i = 0; i < s1.size(); i++)
{
if (s1[i] == ';') n++;
}
for (int j = 0; j < s1.size(); j++)
{
if (s1[j] == ' ') m++;
}
n = n+1;
m = (m/n)+1;
s1.erase(0, 1); //to remove first bracket
for (int z = 0; z < s1.size(); z++) //to replace characters with a space
{
if (s1[z] == ';' || s1[z] == ']') s1[z] = ' ';
}
string token1;
float matrix1 [n*m];
for (int x = 0; x < n*m; x++)
{
token1 = s1.substr(0, s1.find(" "));
float v = atof(token1.c_str());
matrix1 [x] = v;
s1.erase(0, s1.find(" ")+1);
}
cout <<"Please Enter An Operator From The Following List: '+ - * ^ T D I /'" <<endl;
cin >> op;
if (op == '+' || op == '-' || op == '*' || op == '/')
{
cin.ignore(); getline (cin,s2);
}
else if (op == '^') cin >> y;
for (int f = 0; f < s2.size(); f++)
{
if (s2[f] == ';') o++;
}
for (int q = 0; q < s2.size(); q++)
{
if (s2[q] == ' ') p++;
}
o = o+1;
p = (p/o)+1;
s2.erase(0, 1); //to remove first bracket
for (int e = 0; e < s2.size(); e++) //to replace characters with a space
{
if (s2[e] == ';' || s2[e] == ']') s2[e] = ' ';
}
string token2;
float matrix2 [o*p];
float h;
for (int c = 0; c <o*p; c++)
{
token2 = s2.substr(0, s2.find(" "));
h = atof(token2.c_str());
matrix2 [c] = h;
s2.erase(0, s2.find(" ")+1);
}
if ( n == o && m == p && op == '+')
{
ADD(matrix1,matrix2,m*n) //Where I got the error Cannot convert 'float*' to 'float' for argument '1'
}
return 0;
}
string ADD (float matrix1[],float matrix2[],int arraySize)
{
string str;
for (int u = 0; u < arraySize; u++)
{
float matrix3[arraySize];
matrix3[u] = matrix1[u] + matrix2[u];
ostringstream ss;
ss << matrix3;
str = ss.str();
return str;
}
cout <<str;
}
答案 0 :(得分:2)
您的前向声明与您的函数定义不符:
string ADD (float matrix1,float matrix2,int arraySize);
和
string ADD (float matrix1[],float matrix2[],int arraySize)
{
...
}
只需更改您的前向声明以匹配该定义。