fix ctx
This commit is contained in:
@@ -5,19 +5,21 @@
|
||||
|
||||
#include "shared.h"
|
||||
|
||||
typedef double (IterFn)(double, uint32_t);
|
||||
typedef struct
|
||||
{
|
||||
double x;
|
||||
uint32_t i;
|
||||
uint32_t n;
|
||||
IterFn* iter_fn;
|
||||
} Context;
|
||||
|
||||
typedef double (IterFn)(double, uint32_t);
|
||||
typedef double (LoopFn)(Context* ctx, uint32_t, uint32_t max, IterFn* iter_fn);
|
||||
typedef double (LoopFn)(Context* ctx);
|
||||
|
||||
double iter_fn(double x, uint32_t i);
|
||||
|
||||
double for_fn(Context* ctx, uint32_t min, uint32_t max, IterFn* iter_fn);
|
||||
double while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn* fiter_fn);
|
||||
double do_while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn* iter_fn);
|
||||
double for_fn(Context* ctx);
|
||||
double while_fn(Context* ctx);
|
||||
double do_while_fn(Context* ctx);
|
||||
|
||||
void lab_3();
|
||||
|
||||
|
||||
22
src/LAB3.c
22
src/LAB3.c
@@ -8,11 +8,11 @@ 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 for_fn(Context* ctx)
|
||||
{
|
||||
double r = 0.0;
|
||||
|
||||
for (uint32_t i = min; i <= max; ++i)
|
||||
for (uint32_t i = ctx->i; i <= ctx->n; ++i)
|
||||
{
|
||||
r += iter_fn(ctx->x, i);
|
||||
}
|
||||
@@ -20,12 +20,12 @@ double for_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
return r;
|
||||
}
|
||||
|
||||
double while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
double while_fn(Context* ctx)
|
||||
{
|
||||
double r = 0.0;
|
||||
|
||||
uint32_t i = min;
|
||||
while (i <= max)
|
||||
uint32_t i = ctx->i;
|
||||
while (i <= ctx->n)
|
||||
{
|
||||
r += iter_fn(ctx->x, i);
|
||||
++i;
|
||||
@@ -34,31 +34,31 @@ double while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
return r;
|
||||
}
|
||||
|
||||
double do_while_fn(Context* ctx, uint32_t min, uint32_t max, IterFn *iter_fn)
|
||||
double do_while_fn(Context* ctx)
|
||||
{
|
||||
double r = 0.0;
|
||||
|
||||
uint32_t i = min;
|
||||
uint32_t i = ctx->i;
|
||||
do
|
||||
{
|
||||
r += iter_fn(ctx->x, i);
|
||||
++i;
|
||||
}
|
||||
while (i <= max);
|
||||
while (i <= ctx->i);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void lab_3()
|
||||
{
|
||||
Context ctx = { .x = 0 };
|
||||
Context ctx = { .i = 1, .x = 0 };
|
||||
uint32_t n = 0;
|
||||
|
||||
printf("Enter [x]: ");
|
||||
READ_EXACT("%lf", ctx.x);
|
||||
|
||||
printf("Enter [N]: ");
|
||||
READ_EXACT("%u", n);
|
||||
READ_EXACT("%u", ctx.n);
|
||||
|
||||
LoopFn* loop_fn;
|
||||
|
||||
@@ -94,6 +94,6 @@ void lab_3()
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("y(x=%lf,N=%u) = %lf\n", ctx.x, n, tan(fabs(ctx.x)) * loop_fn(&ctx, 1, n, iter_fn));
|
||||
printf("y(x=%lf,N=%u) = %lf\n", ctx.x, n, tan(fabs(ctx.x)) * loop_fn(&ctx));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user