私は RPN 電卓のコードを作成しました。基本的な演算子 (+、*、/、^) だけでなく、浮動小数点数と負数でも問題なく動作します。また、 (x^2 + x*4/-2) : 1 -> 5 :0.5 のような式も評価します (x は 1 から 5 まで、0.5 のステップで評価されます)。
char スタックを使用しました。
ここで、cos(x)、tan(x) などの関数のサポートを追加したいと考えています。その目的を達成するには、解析後に sin、cos、sqrt などの単語を格納するchar* スタックを構築する必要があります。
問題は、スタックを初期化するときに、「アクセス違反: アドレス 0x01 の書き込み」エラーが発生することです。
正確な理由はわかりません。malloc() の使用でしょうか?
これらは、スタックを使用するための関数です。
typedef struct nodo{
char *operador;
struct nodo *next;
}tipo;
typedef tipo *elemento;
typedef tipo *top;
int push(top*,char*) ;
void init(top *);
void libera(top*);
char* pop(top*);
int main(){
(...)
top op;
init(&op);
(...)
}
void init(top *pila) {
*pila = malloc(sizeof(**pila));
(*pila)->operador = NULL;
(*pila)->next = NULL;
}
void libera(top *pila) {
free(*pila);
*pila = NULL;
}
int push (top *last,char *dato){
elemento new1;
int j=strlen(dato);
new1 = (elemento)malloc(sizeof(tipo));
strncpy(new1->operador, dato,j);
new1->next=*last;
*last=new1;
;}
char* pop(top *last){
elemento aux;
char* caract;
aux = (elemento)malloc(sizeof(tipo));
aux=*last;
if (!aux)
return 0;
*last=aux->next;
strcpy(caract,aux->operador);
free(aux);
return caract;
}