Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何拆分方程式的字符串_C++_String_Split_Calculator_Equation - Fatal编程技术网

C++ 如何拆分方程式的字符串

C++ 如何拆分方程式的字符串,c++,string,split,calculator,equation,C++,String,Split,Calculator,Equation,我是新来的,所以如果我犯了错误,请原谅我 我需要一个将字符串拆分为字符的程序。我的意思是如果用户写下“你好”这个程序应该是“a[]={'H','e','l','l','o'}”或者类似的东西。C++真的有可能吗?我要用它来计算方程式。如果您有更好的主意(没有ax^2+bx+c),请告诉我。谢谢你的帮助。祝您愉快。从字符串中提取字符并将其存储到字符数组中 简单方法(使用字符串索引): 不要忘记使用delete释放内存,该内存是使用new操作符分配给字符数组的,如下所示: delete [] cha

我是新来的,所以如果我犯了错误,请原谅我

我需要一个将字符串拆分为字符的程序。我的意思是如果用户写下“你好”这个程序应该是
“a[]={'H','e','l','l','o'}”
或者类似的东西。C++真的有可能吗?我要用它来计算方程式。如果您有更好的主意(没有
ax^2+bx+c
),请告诉我。谢谢你的帮助。祝您愉快。

从字符串中提取字符并将其存储到字符数组中 简单方法(使用字符串索引):

不要忘记使用
delete
释放内存,该内存是使用
new
操作符分配给字符数组的,如下所示:

delete [] charArray; 删除[]字符; 否则,程序中可能会出现内存泄漏

要从字符串中提取整数字符并将其存储到整数数组中,请执行以下操作: 以下程序应能帮助您:

#include<iostream>

using namespace std;
// Using the namespace std to use string.

int main(int argc, char** argv) {
    string input;
    cout << "Enter a string: ";
    cin>>input;     // To read the input from the user.
    
    int strLength = input.length(); // To compute the length of the string.
    char * charArray = new char[strLength]; //Dynamic memory allocation
    int loopVar = 0;

    /**
     * The following for loop converts the string into an array.
    */
    for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
        charArray[loopVar] = *iterativeVar;
        ++loopVar;
    }

    cout << endl << "The elements of the character array are as follows: {";
    for (int i = 0; i < strLength; i++) {
        if (i != strLength - 1) {
            cout << "\'" << charArray[i] << "\', ";
        } else {
            cout << "\'" << charArray[i] << "\'}";
        }   
    }

    delete [] charArray;
    return 0;
}
#include<iostream>

using namespace std;
// Using the namespace std to use string.

int main(int argc, char** argv) {
    string input;
    cout << "Enter a number: ";
    cin>>input;     // To read the input from the user.
    
    int strLength = input.length(); // To compute the length of the string.
    int * intArray = new int[strLength]; //Dynamic memory allocation
    int loopVar = 0;

    /**
     * The following for loop converts the string into an array.
    */
    for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
        intArray[loopVar] = (*iterativeVar-'0');
        ++loopVar;
    }

    cout << endl << "The elements of the integer array are as follows: {";
    for (int i = 0; i < strLength; i++) {
        if (i != strLength - 1) {
            cout << intArray[i] << ", ";
        } else {
            cout <<intArray[i]<<"}";
        }   
    }

    delete [] intArray;
    return 0;
}
#包括
使用名称空间std;
//使用名称空间std来使用字符串。
int main(int argc,字符**argv){
字符串输入;
cout>input;//从用户处读取输入。
int strLength=input.length();//计算字符串的长度。
int*intArray=newint[strLength];//动态内存分配
int-loopVar=0;
/**
*下面的for循环将字符串转换为数组。
*/
for(const char*iterativeVar=input.c_str();*iterativeVar;iterativeVar++){
intArray[loopVar]=(*iterativeVar-'0');
++loopVar;
}

不能仅仅将字符串存储到
char
数组中吗?您不必做任何特殊的事情来将字符串“拆分”为char。
string s=“Hello”;std::不能谢谢您。这对我帮助很大。
Enter a string: Hello

The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'}
RUN SUCCESSFUL (total time: 2s)
newmain.cpp: In function 'int main(int, char**)':
newmain.cpp:23:42: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
     for (char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
delete [] charArray;
#include<iostream>

using namespace std;
// Using the namespace std to use string.

int main(int argc, char** argv) {
    string input;
    cout << "Enter a number: ";
    cin>>input;     // To read the input from the user.
    
    int strLength = input.length(); // To compute the length of the string.
    int * intArray = new int[strLength]; //Dynamic memory allocation
    int loopVar = 0;

    /**
     * The following for loop converts the string into an array.
    */
    for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
        intArray[loopVar] = (*iterativeVar-'0');
        ++loopVar;
    }

    cout << endl << "The elements of the integer array are as follows: {";
    for (int i = 0; i < strLength; i++) {
        if (i != strLength - 1) {
            cout << intArray[i] << ", ";
        } else {
            cout <<intArray[i]<<"}";
        }   
    }

    delete [] intArray;
    return 0;
}
Enter a number: 123456789 The elements of the integer array are as follows: {1, 2, 3, 4, 5, 6, 7, 8, 9} RUN SUCCESSFUL (total time: 4s)