Tag Archive for c

Generic Control to Head of Page

HtmlGenericControl si = new HtmlGenericControl();
si.TagName = "script";
si.Attributes.Add("type", "text/javascript");
si.Attributes.Add("language", "JavaScript");
si.Attributes.Add("src", ResolveClientUrl("~/scripts/global.js"));

source

The famous InvSqrt()

float InvSqrt (float x)
{
float xhalf = 0.5F * x;
int i = * (int *) &x;
i = 0x5F3759DF - (i >> 1);
x = * (float *) &i;
x = x * (1.5F - xhalf * x * x);
return x;
}

source

Crazy C++ Code

char *s = "DEHLORW";
char *d = "2133464530";

for(i=0; d[i]; i++)
cout << s[d[i]-'0'];

///////////////////////////

i = 1<<1;
i |= 1;
cout << (i<<2) << ":" << (i>>2) << (i==2);

/////////////

x = 0;
cout << (x==0) << (x=0) << (0);

////////////////////////////

class c{
c(int i=0);
public: int a;
};

c::c(int i): a(i) {}

source

C Console Scripting Framework

#include <stdio.h>
//#include <stdlib.h> //for system pause

//USAGE (in DOS):   dir blah | yourProgramName > outFile.txt
// (use /b for JUST file and folder names)
// (use /b /ad for JUST folder names)

void printsln(char *s) {printf("%s
", s);}

void error(char *s){printsln(s); exit(1);}

bool ngets(char *s, int n) {
int i = 0;
char c;
c = getchar();
if (c==EOF) {s[i] = 0; return false;}
while(c!='
'){
if(i>=n) error("input stream overflowed buffer");
s[i++] = (char)c;
c = getchar();
}
s[i] = 0;
return true;
}

int main(int argc, char *argv[])
{
char s[10000]; //note: possible (in theory) security hole
while(ngets(s, 10000)) { //security hole closed.

printf("/new/%s
", s, s);  //TWEAK THIS LINE!!!

}
//system("pause");
return 0;
}

source

GCC inline Assembly template

asm(
"
"
"
"
:
:
);

source

setjmp

#include <setjmp.h>
#include <stdio.h>

jmp_buf envBuf;												//environment buffer

int main(void)
{
int dividend, divisor;

if ( setjmp(envBuf) )
{
puts("Enter a number.");
getchar();
}
printf("Dividend: ");
if (scanf("%d", &dividend) != 1) longjmp(envBuf, 1);

switch ( setjmp(envBuf) )
{
case 0: break;
case 1: puts("Enter a number."); getchar(); break;
case 2: puts("Divided by zero."); break;
default: puts("Unknown error."); break;
}
printf("Divisor: ");
if (scanf("%d", &divisor) != 1) longjmp(envBuf, 1);
if (divisor == 0) longjmp(envBuf, 2);
printf("%d / %d = %d", dividend, divisor, dividend / divisor);

return 0;
}

source

racing condition

#include <stdio.h>

void printn(int n, pid_t pid)
{
int c;
for (c = 0; c < n; c++)
if (pid)
printf("parent: %d
", c);
else
printf("child: %2d
", c);
}

int main(void)
{
pid_t pid;
pid = fork();
printn(5, pid);
return 0;
}

source

foreach in c++ (#define)

#define foreach(m_itname,m_container)
for( typeof(m_container.begin()) m_itname=m_container.begin() ;
m_itname!=m_container.end() ;
m_itname++ )

source

Reverse a Linked List

NodePtr reversed=NULL, cur=head;

while(cur!=NULL)
{
//detach cur
head=head->next;

//link cur to reversed list
cur->next=reversed;
reversed=cur;

//move cur to next node
cur=head;

head=reversed;
}

source

dropdown states

<asp:DropDownList id="ddlStates" runat="server">
<asp:ListItem Value="">-- Select --</asp:ListItem>
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="AZ">Arizona</asp:ListItem>
<asp:ListItem Value="AR">Arkansas</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CO">Colorado</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="DC">District of Columbia</asp:ListItem>
<asp:ListItem Value="DE">Delaware</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="GA">Georgia</asp:ListItem>
<asp:ListItem Value="HI">Hawaii</asp:ListItem>
<asp:ListItem Value="ID">Idaho</asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
<asp:ListItem Value="KS">Kansas</asp:ListItem>
<asp:ListItem Value="KY">Kentucky</asp:ListItem>
<asp:ListItem Value="LA">Louisiana</asp:ListItem>
<asp:ListItem Value="ME">Maine</asp:ListItem>
<asp:ListItem Value="MD">Maryland</asp:ListItem>
<asp:ListItem Value="MA">Massachusetts</asp:ListItem>
<asp:ListItem Value="MI">Michigan</asp:ListItem>
<asp:ListItem Value="MN">Minnesota</asp:ListItem>
<asp:ListItem Value="MS">Mississippi</asp:ListItem>
<asp:ListItem Value="MO">Missouri</asp:ListItem>
<asp:ListItem Value="MT">Montana</asp:ListItem>
<asp:ListItem Value="NE">Nebraska</asp:ListItem>
<asp:ListItem Value="NV">Nevada</asp:ListItem>
<asp:ListItem Value="NH">New Hampshire</asp:ListItem>
<asp:ListItem Value="NJ">New Jersey</asp:ListItem>
<asp:ListItem Value="NM">New Mexico</asp:ListItem>
<asp:ListItem Value="NY">New York</asp:ListItem>
<asp:ListItem Value="NC">North Carolina</asp:ListItem>
<asp:ListItem Value="ND">North Dakota</asp:ListItem>
<asp:ListItem Value="OH">Ohio</asp:ListItem>
<asp:ListItem Value="OK">Oklahoma</asp:ListItem>
<asp:ListItem Value="OR">Oregon</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="RI">Rhode Island</asp:ListItem>
<asp:ListItem Value="SC">South Carolina</asp:ListItem>
<asp:ListItem Value="SD">South Dakota</asp:ListItem>
<asp:ListItem Value="TN">Tennessee</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem Value="UT">Utah</asp:ListItem>
<asp:ListItem Value="VT">Vermont</asp:ListItem>
<asp:ListItem Value="VA">Virginia</asp:ListItem>
<asp:ListItem Value="WA">Washington</asp:ListItem>
<asp:ListItem Value="WV">West Virginia</asp:ListItem>
<asp:ListItem Value="WI">Wisconsin</asp:ListItem>
<asp:ListItem Value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>

public string SelectedValue
{
get
{
return ddlStates.SelectedValue;
}
set
{
ddlStates.SelectedValue = value;
}
}

<asp:RequiredFieldValidator ID="rfvStates" ControlToValidate="ddlStates"
runat="server" EnableClientScript="false" CssClass="validation">*</asp:RequiredFieldValidator>

public string id
{
set
{
ddlStates.ID = value;
}
}

public string ValidationEnabled
{
set
{
rfvStates.Enabled = Convert.ToBoolean(value);
}
}

public string ValidationGroup
{
set
{
ddlStates.ValidationGroup = value;
rfvStates.ValidationGroup = value;
}
}

public string ValidationErrorMessage
{
set
{
rfvStates.ErrorMessage = value;
}
}

public string SelectedValue
{
get
{
return ddlStates.SelectedValue;
}
set
{
ddlStates.SelectedValue = value;
}
}

public short TabIndex
{
set
{
ddlStates.TabIndex = value;
}
}

source