Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <cstdio>
#include <unistd.h>
#include <math.h>
#define L_value 20e-6
#define C_value 50e-12
#define dt 10e-10
#define SLEEP_MS 10
#define U_0 5.0
using std::printf;
float glob_f = 0.0;
namespace C {
class component_C //definition
{
public:
float Spannung;
void berechne_wert_des_kondensators(float spannung);
};
void print_value(struct component_C c) {
printf("Z=\t%fV\n", c.Spannung);
// hier malen wir noch lustige + und - auf das terminal
int l;
for(l=0;l<c.Spannung;l++) printf("+");
for(l=0;l<c.Spannung*-1.0;l++) printf("-");
printf("\n");
printf("f_math=\t%fMHz\n", 1/(2*M_PI*sqrt(L_value*C_value))/1000000);
printf("f_calc=\t%fMHz\n", glob_f/1000000);
printf("\e[1;1H\e[2J"); // clear terminal hackkkkk
}
}
namespace L {
class component_L {
public:
float Strom;
void berechne_wert_der_spule(float strom);
};
void print_value(struct component_L l) {
printf("Z=\t%fmA\n", l.Strom*1000);
int l1;
for(l1=0;l1<l.Strom*1000;l1++) printf("+");
for(l1=0;l1<l.Strom*-1000.0;l1++) printf("-");
printf("\n");
}
}
void C::component_C::berechne_wert_des_kondensators(float strom) {
this->Spannung = this->Spannung + (-1/C_value * strom * dt);
C::print_value((*this));
usleep(1000*SLEEP_MS);
}
void L::component_L::berechne_wert_der_spule(float spannung) {
(*this).Strom = (*this).Strom + (1/L_value * spannung * dt);
L::print_value((*this));
}
int main()
{
class C::component_C kondensator;
class L::component_L spule;
kondensator.Spannung = U_0;
spule.Strom = 0.0;
C::print_value(kondensator);
L::print_value(spule);
float delta_t_counter = 0;
float last_spannung = 0.0;
int i;
for(i=0;i<999999999;i++) {
delta_t_counter += dt;
kondensator.berechne_wert_des_kondensators(spule.Strom);
spule.berechne_wert_der_spule(kondensator.Spannung);
if(last_spannung > 0 && kondensator.Spannung < 0.0){
//printf("f = %f", 1/delta_t_counter*2);
glob_f = 1/delta_t_counter; // hier noch ein fehler
delta_t_counter = 0;
}
last_spannung = kondensator.Spannung;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment