Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The following function is implemented in C:
function(struct_XX *p)
{
if(p->A)
{
if(p->B)
{
do something0;
}
if(p->C)
{
do something1;
}
if(p->D && p->E)
{
do something2;
}
if(p->Z)
{
do something3;
}
}
}
each branch has different things to do,It doesn't satisfy Open-Closed Principle(because the struct which p points at is not stable ,new fields will be added into it often,that means new process codes will be added into function frequently); how can it be modified to satisfy OCP?
you haven't given much to go, you have a bunch of conditions, and a bunch of "do"s based on those conditions.
So given that... you can make something that stores a collections of conditions and callbacks....
void ocpfunction(struct struct_XX *p)
{
int i;
for(i=0; i<p->conditions_count; i++)
{
if(p->conditions[i].evaluate(p))
{
p->conditions[i].callback(p, p->conditions[i].context);
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
int frequency(string note)
{
int i;
float f;
int n=0;
float octave= note[strlen(note)-1];
if(strlen(note)==3)
{
if(note[1]=='#')
{
n+=1;
}
else if(note[1]=='b')
{
n-=1;
}
}
if(note[0]=='B')
{
n+=2;
}
else if(note[0]=='C')
{
n-=9;
}
else if(note[0]=='D')
{
n-=7;
}
else if(note[0]=='E')
{
n-=5;
}
else if(note[0]=='F')
{
n-=4;
}
else if(note[0]=='G')
{
n-=2;
}
n+=(octave-4.0)*12.0;
float p= n/12.0;
f=(int)(round(pow(2.0,p)*440.0));
return f;
}
So basically whenever I run this code I get an error stating "runtime error: value 7.3641e+16 is outside the range of representable values of type 'int'"
Then the value returned is just-2147483648. I've looked it up online and haven't found an answer that helps me with my code. Also this was made in the cs50 IDE so there are a bunch of commands and things that are imported. My program compiles properly and it can run so how do I fix this?
Frequencies of notes of the chromatic scale are not integers in the first place.
If you are passed a two character string like Cb, you get a stupidly huge octive with a frequency way too high.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
threaded programming
I want to write simple multi-thread app.
where each thread when it open I increment (using InterlockedIncrement) member by one ,
and decrements it (using InterlockedDecrement) when thread finishes
I know about Mutex/Semaphore/event
but I would more clean /simple way to implement comparison similar to the Interlocked function .
What I need next is implement comparison function [if(member == x)]
simple example:
Thread 1 function:
{
//do somthing
InterlockedDecrement(member);
}
Thread 2 function:
{
//do something else
InterlockedDecrement(member);
}
main thread function :
{
while(member)//<--how can it be done in thread safe fashion
{
//do yet another something
}
}
Use InterlockedAdd and add 0. This will lock the member and return the value without changing it:
while (InterlockedAdd(&member, 0) == someValue)
{
//do yet another something
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
void fade(ImagenBMP *imagen1, ImagenBMP *imagen2, int f) {
float v = (float)f/255;
for (int i = 0; i < imagen1->tamanyo; i++) {
imagen1->datos[i] = (imagen1->datos[i] - imagen2->datos[i])*v + imagen2->datos[i];
}}; //end of fade
It's supose to fade two images into a single one.
If you rewrite the equation, you will end up with:
imagen1->datos[i] = v*imagen1->datos[i] + (1-v)*imagen2->datos[i];
This is how the blend works. You are specifying how much of the first image (out of 255 parts) should be included in the function parameter f, 255-f parts will be included from the second image.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why is this code not writing to different files?
---file name: "file.c". c
int main(){
// skipped rest of the code
FILE * pfile;
while(i<25)
{
sprintf(mytext,"%d.txt", i); // trying to make mytext1.txt, mytext2.txt ...
pfile = fopen ("mytextd.txt","w"); // trying to write in each files "confuse here"
printf("eneter in server recieve");
if(pfile != NULL)
{
// while(i<25)
// {
read(connfd,sendBuff,sizeof(sendBuff));
fputs(sendBuff,pfile);
fputs(sendBuff,stdout);
i++;
// }
fclose (pfile);
}
}
}
You loop keeps writing to the same file "mytestd.txt" because you are using that string constant instead of the variable mytext that you just wrote the desired filename into.
You probably mean
pfile = fopen (mytext,"w");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can someone explain the following code?
what is the use of the variable ort?
void squart_root(double a)
{
if (a>0.0){
double root = 1, ort = 0;
while(ort!=root)
{
ort = root;
root = ((a/root) + root) / 2;
}
printf("Root is : %lf",root);
}else if(a==0.00000){
printf("Root is : %lf",a);
}else{
printf("Cannot find the square root of a negative number");
}
}
This looks similar to Newton's method of calculating square root (derived from Newton-Raphson's method).
You can read more about this here.
It looks like:
X(n+1) = (A/X(n) + X(n))/2
It converges when X(n) = X(n+1) (that is in your case) or under some precision.
At each iteration, X(n) should get closer to the real root.
The purpose of ort is to hold X(n) and check for this convergence. When root converges, loop terminates. That is why ort is here.
You can read about Newton-Raphsone method (just google it) and you will be able to derive this equation.