You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C语言中使用strtok从多项式提取系数与指数?

多项式系数与指数提取问题

给定多项式字符串(例如"-4x^0 + x^1 + 4x^3 - 4x^5 - 3x^7"),需要将其解析为两个数组,最终结果需满足:

coef = [-4, 1, 0, 4, 0, -4, 0, -3]
expo = [ 0, 1, 2, 3, 4,  5, 6,  7]

其中coef[i]对应指数expo[i]的系数,缺失的指数项系数填0。

现有代码问题

以下是当前尝试的代码,以及修改后未成功的版本:

初始代码

int main()
{
    char userInput[100];
    char temp[100];
    printf("Enter the polynomial: ");
    scanf("%[^\n]%*c", userInput);
    strcpy(temp, userInput);
    printf("\n");
    
    int coef[100];
    int expo[100];
    for (int i = 0; i < 100; i++) {
        coef[i] = 0;
        expo[i] = 0;
    }
    
    char *tok = strtok(temp, "x^"); 
    int counter = 0;
    
    while (tok) { 
        printf("*%s*\n", tok); 
        tok = strtok(NULL, "x^"); 
        counter++; 
    } 
    
    return 0;
}

修改后未成功的代码片段

int counter = 0;
while (tok) { 
    printf("*%s*\n", tok);        
    expo[counter] = atoi(tok);

    tok = strtok(NULL, "x^"); 
    counter++; 
} 

问题根源

直接用"x^"作为分隔符会破坏符号、系数与指数的对应关系——原字符串中的空格、加减号未被正确处理,导致提取的token混乱(比如拆分出"-4"、"0 "、"+ "这类无效内容),无法精准匹配每一项的系数和指数。

解决方案

正确思路是先拆分出多项式的每一项,再对单独项提取系数和指数:

  1. 预处理字符串,给开头补加号(如果首字符不是加减号),实现统一分割逻辑;
  2. 用strtok按"+-"分割每一项,同时通过指针偏移获取项的符号;
  3. 对每一项判断是否包含x^:
    • 包含则拆分系数(处理x^n这类系数为1/-1的特殊情况)和指数;
    • 不包含则视为常数项,指数设为0;
  4. 记录最大指数,初始化数组后填充对应位置的系数,缺失项保持初始0值。

完整实现代码

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

int main() {
    char userInput[100];
    char temp[100];
    printf("Enter the polynomial: ");
    scanf("%[^\n]%*c", userInput);
    
    // 预处理:统一项的开头符号
    if (userInput[0] != '+' && userInput[0] != '-') {
        snprintf(temp, sizeof(temp), "+%s", userInput);
    } else {
        strcpy(temp, userInput);
    }
    
    int coef[100] = {0};
    int expo[100];
    int max_expo = 0;
    
    // 分割每一项,分隔符为+和-
    char *tok = strtok(temp, "+-");
    while (tok != NULL) {
        // 跳过空token
        if (strlen(tok) == 0) {
            tok = strtok(NULL, "+-");
            continue;
        }
        
        // 获取当前项的符号
        char sign = *(tok - 1);
        int coefficient = 1;
        int exponent = 0;
        
        // 查找x^的位置
        char *x_pos = strstr(tok, "x^");
        if (x_pos != NULL) {
            // 提取系数
            char coef_str[20] = {0};
            int coef_len = x_pos - tok;
            if (coef_len == 0) {
                // 处理x^n形式,系数为1
                coefficient = 1;
            } else {
                strncpy(coef_str, tok, coef_len);
                coefficient = atoi(coef_str);
            }
            // 提取指数
            exponent = atoi(x_pos + 2);
        } else {
            // 常数项,指数为0
            coefficient = atoi(tok);
            exponent = 0;
        }
        
        // 应用符号
        if (sign == '-') {
            coefficient *= -1;
        }
        
        // 更新最大指数
        if (exponent > max_expo) {
            max_expo = exponent;
        }
        
        // 填充系数数组
        coef[exponent] = coefficient;
        
        tok = strtok(NULL, "+-");
    }
    
    // 填充连续的指数数组
    for (int i = 0; i <= max_expo; i++) {
        expo[i] = i;
    }
    
    // 输出结果
    printf("coef = [");
    for (int i = 0; i <= max_expo; i++) {
        if (i > 0) printf(", ");
        printf("%d", coef[i]);
    }
    printf("]\n");
    
    printf("expo = [");
    for (int i = 0; i <= max_expo; i++) {
        if (i > 0) printf(", ");
        printf("%d", expo[i]);
    }
    printf("]\n");
    
    return 0;
}

代码说明

  • 预处理字符串:确保所有项都以加减号开头,简化分割逻辑;
  • 分割每一项:通过"+-"拆分出单个项的内容,利用指针偏移获取项的正负符号;
  • 项解析逻辑:区分含变量项和常数项,处理x^n这类系数隐含为1的特殊情况;
  • 数组填充:根据指数位置精准填充系数,缺失项保持初始0值,最后生成连续的指数数组。

内容的提问来源于stack exchange,提问作者hmmmquan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:10:28