下面的代码应该从用户char s1[] = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
输入
并只取其中的数字,然后将其转换为浮点数并将其放入数组中
但是当我尝试退出数组时,它只打印数字
char s1[] = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
char s2 [100] ;
float matrix [2][2];
char* cutter(char s[])
{
char delim[] = " ;][";
char *token = strtok(s,delim);
while (token)
{
cout << token << endl;
token = strtok(NULL,delim);
for(int i=0;i<2;i++){
for(int j=0;j< 2;j++){
matrix [i][j]=strtof(token, NULL);
}
}
}
return token;
}
void showmatrix(float m[2][2]){
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cout << m[i][j];
}
}
}
int main(){
cutter(s1);
showmatrix(matrix);
//cin.getline(s2,100);
//cutter(s2);
}
答案 0 :(得分:0)
您的代码未在我的计算机上运行。您应该按照其他人的建议更改循环条件,并在I link against these libraries:
- PhysX_32.lib
- PhysXCommon_32.lib
- PhysXCooking_32.lib
- PhysXFoundation_32.lib
I redistribute these DLL files:
- PhysX_32.dll
- PhysXCommon_32.dll
- PhysXCooking_32.dll
- PhysXFoundation_32.dll
- PhysXGpu_32.dll
函数中移动token = strtok(NULL,delim);
代码的位置。
此代码运行:
cutter
另外,请尝试发布完整的代码示例,包括任何#include <iostream>
using namespace std;
char s1[] = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
//char s2 [100] ;
float matrix [2][2];
char* cutter(char s[])
{
char delim[] = " ;][";
char *token = strtok(s,delim);
while (token)
{
cout << token << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
matrix[i][j] = strtof(token, NULL);
}
}
token = strtok(NULL,delim);
}
return token;
}
void showmatrix(float m[2][2]){
for(int i=0;i<2;i++){
for(int j=0;j< 2;j++){
cout << m[i][j];
}
}
}
int main(){
cutter(s1);
showmatrix(matrix);
//cin.getline(s2,100);
//cutter(s2);
}
声明和随附的标头。