你好我需要一些帮助我的程序的这一部分是得到一个输入字符串像2x³+2y²并将它分为2个数组termos = terms和exp = exponential,但我似乎无法让它工作比较字符串与变量类型
#include <stdio.h>
#include <math.h>
int main()
{
char poly[50];
int termos[10];
int exp[10];
int contt=0, conte=0, i=0;
char var1, var2, var3;
printf("Introduza o polinómio\n");
scanf("%s", &poly);
for(i=0; i<50; i++)
{
if(poly[i-1]==char && poly[i]==int && poly[i-1]!='+')
{
exp[conte]=poly[i];
conte++;
}
if(poly[i]==int)
{
termos[contt]=poly[i];
contt++;
}
if(poly[i]=='x')
var1=poly[i];
if(poly[i]=='y')
var2=poly[i];
if(poly[i]=='z')
var3=poly[i];
}
请查看您的for循环中的索引。从i = 0开始,尝试检索[i - 1]是不正确的。 – AlexG
代码可能不应循环到数组的末尾,而是字符串的结尾:for(i = 0; i <50; i ++)' - >'for(i = 0; poly [i]; i ++ )对于(i = 0; i <50; i ++),' – chux
'不正确,因为输入字符串可能更短。使用:'我= 0; while(poly [i]!='\ 0'){... i ++;}' –