Hallo zusammen
ich möchte eines meiner ino's in eine Klasse verwandeln und scheitere kläglich.
ino:
#include "MyToolz.h"
MyToolz robard();
/* an example of struct */
struct sensors{
char name[16];
float value;
};
void setup() {
Serial.begin(9600);
while(!Serial);
}
void loop() {
struct sensors structs[] = {
{"top", 1.0},
{"left", 2.0},
{"right", 3.0},
{"back", 9.9},
{"bottom", 0.2 }
};
// moechte den Name des sensors an position x nach Sortierung des Wertes nach Grösse
Serial.println(robard.getSortedNameByPosition(sensors, 3));
}
.h:
/*
MyToolz.h - Library MyToolz sensor.
Released into the public domain.
*/
#ifndef MyToolz_h
#define MyToolz_h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class MyToolz
{
public:
MyToolz();
int mypos;
struct mystruct;
char* getSortedNameByPosition();
int struct_cmp_by_value();
private:
};
#endif
cpp:
#include "MyToolz.h"
MyToolz::MyToolz() {}
/* an example of struct */
//struct my{
// char name[16];
// float value;
//};
/* qsort struct comparision function (value float field) */
int MyToolz::struct_cmp_by_value(struct _mystruct,const void *a, const void *b)
{
/*
float comparison: returns negative if b > a
and positive if a > b. We multiplied result by 100.0
to preserve decimal fraction
*/
struct mystruct *ia = (struct mystruct *)a;
struct mystruct *ib = (struct mystruct *)b;
return (int)(100.f*ia->value - 100.f*ib->value);
}
char* MyToolz::getSortedNameByPosition(struct mystruct *_array, int mypos)
{
size_t structs_len = sizeof(structs) / sizeof(struct mystruct);
qsort(structs, structs_len, sizeof(struct mystruct), struct_cmp_by_value(mystruct));
return _array[pos].name;
}
Im Prinzip möchte ich folgendes *.ino in eine Class umwandeln:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* an example of struct */
struct sensors{
char name[16];
float value;
};
/* qsort struct comparision function (value float field) */
int struct_cmp_by_value(const void *a, const void *b)
{
struct sensors *ia = (struct sensors *)a;
struct sensors *ib = (struct sensors *)b;
return (int)(100.f*ia->value - 100.f*ib->value);
/* float comparison: returns negative if b > a
and positive if a > b. We multiplied result by 100.0
to preserve decimal fraction */
}
char *pos_struct_array(struct sensors *array, int pos)
{
return array[pos].name;
}
void setup() {
Serial.begin(9600);
}
void loop() {
struct sensors structs[] = {
{"top", 1.0},
{"left", 2.0},
{"right", 3.0},
{"back", 9.9},
{"bottom", 0.2 }
};
size_t structs_len = sizeof(structs) / sizeof(struct sensors);
qsort(structs, structs_len, sizeof(struct sensors), struct_cmp_by_value);
Serial.println(pos_struct_array(structs, 0));
}
Die float Werte im Struct varieren.
ich möchte es am ende so verwenden können:
struct sensors structs[] = {
{"top", 1.0},
{"left", 2.0},
{"right", 3.0},
{"back", 9.9},
{"bottom", 0.2 }
};
robard.getSortedNameByPosition(sensors,5)
--> raus kommt der Name des fünft grössten Wert oder der kleinste jeh nach dem wie man es sieht
robard.getSortedNameByPosition(sensors,1)
--> raus kommt der Name des zweit grössten Wertes
Wenn mir jemand helfen könnte wäre ich sehr froh ... und bitte macht Beispiele - ich bin neu in c++ zu viel Fach Chinesisch verstehe ich nicht Vielen Dank.
Es geht mir ausdrücklich nicht um min(), max() Werte - es geht mir um den namen des x-ten Wertes, oder key des x grössten values.