lab.cpp/ex3/Operator.h

60 lines
1.2 KiB
C++

#ifndef OPERATOR_H
#define OPERATOR_H
#include <string>
#include <stdlib.h>
class Operator{
public:
void init(int size);
void addOperand(float x);
bool missingOperand();
virtual std::string getLibelle();
virtual float* calc();
int size;
protected:
float *result;
float *operands;
};
/* (1) Initialise les operandes */
void Operator::init(int size){
this->size = size;
this->operands = (float*) malloc(size*sizeof(float));
// On preremplis les operandes
for( int i = 0 ; i < size ; i++ )
this->operands[i] = '\0';
}
/* (2) Ajoute une operande */
void Operator::addOperand(float x){
// On rempit la prochaine operande non-vide
for( int i = 0 ; i < this->size ; i++ )
if( this->operands[i] == '\0' ){ // si vide
this->operands[i] = x;
break;
}
}
/* (3) Verification qu'aucune operande ne manque */
bool Operator::missingOperand(){
// On verifie que toutes les operandes ont une valeur
for( int i = 0 ; i < this->size ; i++ )
if( this->operands[i] == '\0' ) return true; // operandes ont une valeur
return false;
}
std::string Operator::getLibelle(){ return "Operator"; };
float* Operator::calc(){ return this->result; };
#endif