initial commit
This commit is contained in:
99
src/LAB3.c
Normal file
99
src/LAB3.c
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "../include/LAB3.h"
|
||||
|
||||
double iter_fn(double x, uint32_t i)
|
||||
{
|
||||
return tan(fabs(x)) * sqrt(i);
|
||||
}
|
||||
|
||||
double for_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
{
|
||||
double r = 0.0;
|
||||
|
||||
for (uint32_t i = min; i <= max; ++i)
|
||||
{
|
||||
r += iter_fn(ctx->x, i);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
double while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
{
|
||||
double r = 0.0;
|
||||
|
||||
uint32_t i = min;
|
||||
while (i <= max)
|
||||
{
|
||||
r += iter_fn(ctx->x, i);
|
||||
++i;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
double do_while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
{
|
||||
double r = 0.0;
|
||||
|
||||
uint32_t i = min;
|
||||
do
|
||||
{
|
||||
r += iter_fn(ctx->x, i);
|
||||
++i;
|
||||
}
|
||||
while (i <= max);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void lab_3()
|
||||
{
|
||||
Context ctx = { .x = 0 };
|
||||
uint32_t n = 0;
|
||||
|
||||
printf("Enter [x]: ");
|
||||
READ_EXACT("%lf", ctx.x);
|
||||
|
||||
printf("Enter [N]: ");
|
||||
READ_EXACT("%u", n);
|
||||
|
||||
LoopFn* loop_fn;
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("Select option:\n\t(f|F|1): for\n\t(w|W|2): while\n\t(d|D|3): do-while\n\t(e|E|4): exit\nOption: ");
|
||||
|
||||
uint8_t opt;
|
||||
READ_EXACT(" %c", opt);
|
||||
|
||||
switch (opt)
|
||||
{
|
||||
case 'f':
|
||||
case 'F':
|
||||
case '1':
|
||||
loop_fn = for_fn;
|
||||
break;
|
||||
case 'w':
|
||||
case 'W':
|
||||
case '2':
|
||||
loop_fn = while_fn;
|
||||
break;
|
||||
case 'd':
|
||||
case 'D':
|
||||
case '3':
|
||||
loop_fn = do_while_fn;
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
case '4':
|
||||
return;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("y(x=%lf,N=%u) = %lf\n", ctx.x, n, tan(fabs(ctx.x)) * loop_fn(&ctx, 1, n, iter_fn));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user