Cの実装は特定の文字列を処理します

#include <stdio.h>
#include <string.h>

int main()
{
    
    
    char a[100] = "12+5=;45-2=;34*2=;56/3=";
    char b[100] = {
    
     0 };
    char *s = strtok(a, ";");
    while(s)
    {
    
    
        int i, j;
        char c;
        sscanf(s, "%d%c%d=", &i, &c, &j);
        int res = 0;
        switch(c)
        {
    
    
        case '+':
            res = i + j;
            break;
        case '-':
            res = i - j;
            break;
        case '*':
            res = i * j;
            break;
        case '/':
            res = i / j;
            break;
        default:
            res = 0;
        }
        char tmp[10] = {
    
     0 };
        sprintf(tmp, "%s%d;", s, res);

        strcat(b, tmp);
        s = strtok(NULL, ";");
    }
    
    strcpy(a, b);
    printf("%s\n", a);

    return 0;
}


試験結果

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/zxy131072/article/details/108493679