Skip to content

Instantly share code, notes, and snippets.

@mperlet

mperlet/c_oop_example.c

Last active Nov 3, 2015
Embed
What would you like to do?
#include <stdio.h>
#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
float glob_f = 0.0;
struct component_C; //declaration
struct component_L {
float Strom;
void (*fp)(struct component_L *l, float v);
};
struct component_C //definition
{
float Spannung;
void (*fp)(struct component_C *c, float v);
};
void printValue(float value, char einheit)
{
printf("Zustand: \t%f%c\n", value, einheit);
}
void print_C_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
}
void print_L_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 berechne_wert_des_kondensators(struct component_C *kondensator, float strom) {
kondensator->Spannung = kondensator->Spannung + (-1/C_value * strom * dt);
print_C_value(*kondensator);
usleep(1000*SLEEP_MS);
}
void berechne_wert_der_spule(struct component_L *spule, float spannung) {
(*spule).Strom = spule->Strom + (1/L_value * spannung * dt);
print_L_value(*spule);
}
int main()
{
struct component_C kondensator;
struct component_L spule;
kondensator.fp = &berechne_wert_des_kondensators;
kondensator.Spannung = U_0;
spule.Strom = 0.0;
spule.fp = &berechne_wert_der_spule;
print_C_value(kondensator);
print_L_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.fp(&kondensator, spule.Strom);
spule.fp(&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