2017-03-16 107 views
1

你好我需要一些幫助我的程序的這一部分是得到一個輸入字符串像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]; 
    } 
+1

請查看您的for循環中的索引。從i = 0開始,嘗試檢索[i - 1]是不正確的。 – AlexG

+0

代碼可能不應循環到數組的末尾,而是字符串的結尾:for(i = 0; i <50; i ++)' - >'for(i = 0; poly [i]; i ++ )對於(i = 0; i <50; i ++),' – chux

+0

'不正確,因爲輸入字符串可能更短。使用:'我= 0; while(poly [i]!='\ 0'){... i ++;}' –

回答

0

根本不可能,中沒有運行時類型信息。也許你應該閱讀The XY Problem並在稍後再提問。

0

if(poly[i-1]==char && poly[i]==int && poly[i-1]!='+') 

你想知道poly[i-1]爲英文字母的線,例如一個'a',或者是一個數字。您可以使用ctype.h中的以下函數:

// among the includes 
#include <ctype.h> 

// later 
if(isalpha(poly[i-1]) && isdigit(poly[i]) && poly[i-1]!='+') 

代碼中存在更多問題。我們將發佈這些評論。

+0

就像將保存到'exp []'改成'exp [conte] = poly [i] - '0'; //從ASCII數字轉換爲數字' – infixed

+0

btw,使用'exp []'可能是一個令人困惑的名字,因爲'exp(n)'是一個常用的反對數函數 – infixed