Useful functions in all languages ​​- Page 2 - Programming - HWzone Forums
adplus-dvertising
Skip to content
  • Create an account
  • About Us

    Hello Guest!

     
    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Useful functions in all languages


Pure-Gold

Recommended Posts

Not exactly a function, but the right way to insert Flash into XHTML pages in a standard way, validation and work in all browsers:

<object
type = "application / x-shockwave-flash"
data = "flash.swf"
width = "300"
height = "120">
<param
name = "movie"
value = "flash.swf" />

That's it.

Without EMBED (which is an HTML tag neglected in XHTML) and without codebase and other junk.

The downside is that IE will not display the flash before it loads completely (ie without streaming), and in large files it can be very annoying (you can not start displaying the flash with a "loading" page). There is a solution to this that I will not enter here.

Explanations, and, in general, credit for this whole issue:

http://www.alistapart.com/articles/flashsatay/

Link to content
Share on other sites

  • 1 month later ...
  • תגובות 43
  • Opens on
  • Last comment

Language: VB

Description: The function checks if the string contains letters only, Hebrew or English, large or small, or spaces. If it returns true, if there is any number or punctuation, false returns.

Public Function isAlpha (str As String) As Boolean
str = Trim (str)
str = LCase (str)
If str = "" Then
isAlpha = False
Exit Function
End If
Dim i%
Dim ch As String
For i = 1 To Len (str)
ch = Mid (str, i, 1)
If (ch <"a" Or ch> "z") And (ch <"a" Or ch> "ת") And (ch <> "") Then
isAlpha = False
Exit Function
End If
Next
isAlpha = True
End Function

Link to content
Share on other sites

Macro-C is very helpful in allowing to know if any number is stronger than two:

#define IS_POWER_OF_TWO (x) ((x) && (! ((x) & ((x) - 1)))

In C ++, of course, an inline function returns bool.

Its great advantage in my opinion is the fact that it lets you know while compiling whether the number is strong of 2 or not.

Link to content
Share on other sites

  • 4 weeks later ...
  • 3 weeks later ...
  • 2 months later ...

C language, checks whether a string is empty or contains only spaces : Has:



// Returns 0 if pStr == NULL or all symbols in * pStr are spaces
int IsStrEmpty (char const * const pStr)
{
int isStrEmpty = pStr == NULL;
if (! isStrEmpty)
{
int strIndex = 0;
int strLen = strlen (pStr);
for (; strIndex <strLen && isStrEmpty; strIndex ++)
{
isStrEmpty = pStr [strIndex]> '';
}
}
return isStrEmpty;
}



Link to content
Share on other sites

C language, checks whether a string is empty or contains only spaces : Has:



// Returns 0 if pStr == NULL or all symbols in * pStr are spaces
int IsStrEmpty (char const * const pStr)
{
int isStrEmpty = pStr == NULL;
if (! isStrEmpty)
{
int strIndex = 0;
int strLen = strlen (pStr);
for (; strIndex <strLen && isStrEmpty; strIndex ++)
{
isStrEmpty = pStr [strIndex]> '';
}
}
return isStrEmpty;
}

please Not used The isStrEmpty function.

1) The function contains a bug (at least one). In my opinion it is also being realized bad (this is already an opinion).

2) It is generally useful to use the isXXXX or iswXXXX functions in the standard directory.

Link to content
Share on other sites

Here's a slightly better solution:

#include

bool isStrEmpty (const char * pStr) {
if (! pStr)
return true;

for (; * pStr! = '\ 0'; ++ pStr) {
if (! isspace (* pStr))
return false;
}

return true;
}

Shorter, more elegant, and no bugs.

post Scriptum. There is no reason to use the const * const, because the pointer also passes by value.

Link to content
Share on other sites

I do not think anything like that belongs to Sticky. This is a basic function and not very useful.

It is equally possible to publish a function that raises a number by force, or a function that returns the number square ...

Link to content
Share on other sites

Here is a program fragment in VB that finds a point of intersection between straight 2 (you need to give the points that create the straight):


Private Sub Command1_Click ()
'made by ofir tadmor. plz dont delete this.
X1 = Text1.Text 'coordinate of point one in line 2
Y1 = Text2.Text
X2 = Text3.Text 'coordinate of point 2 in line 1
Y2 = Text4.Text

x3 = Text7.Text 'coordinate of point 1 in line 2
y3 = Text8.Text
x4 = Text9.Text 'coordinate of point 2 in line 2' made by ofir tadmor
y4 = Text10.Text

a1 = Y2 - Y1
b1 = X1 - X2
c1 = - (X1 * a1 + Y1 * b1)
a2 = y4 - y3
b2 = x3 - x4
c2 = - (x3 * a2 + y3 * b2)

If a2 * b1 - a1 * b2 <> 0 Then 'not parellal

x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2)
y = - (a2 * c1 - a1 * c2) / (a2 * b1 - a1 * b2)
else
MsgBox "àéï çéúåê"
Exit Sub

End If


Text5.Text = x
Text6.Text = y



End Sub

More, from the same program:



Private Sub Command2_Click ()
'made by ofir tadmor. plz dont del this

a = Text1.Text 'x coord of line vec
b = Text2.Text 'y coord of line vec

c = Text3.Text 'x coord of move vec
d = Text4.Text 'y coord of move vec

Dim mul As Double
mul = -2 * (a * d - b * c) / (a ​​* a + b * b) 'made by ofir tadmor

Text5.Text = mul * b + c 'new x move vec
Text6.Text = -mul * a + d 'new y move vec

End Sub



You give it a vector vector vector of a spherical object, and it returns the new direction of the object. It's without friction. If you want friction, you have to use what you learn in high school physics.

Use it wisely.

EDIT: I wrote that about two years ago. Nice to remember it ...

Well, I'll bring it all up. I crumpled it now.

Edit 2: I changed something small in the program.

[attachment deleted by admin]

Link to content
Share on other sites

  • 2 months later ...

Function in C Return word

Which receives STRING (array of CHAR) and returns an array New Containing the word number-parameter

When the first word is 1

Second word 2 etc.

The function separates words according to the ASCII value of the "" space.

Include STRING.H


char * strcropword (char str [], int num)
{
int i = 0, j = 0, k = 0, last;
char * tmp = (char *) malloc (sizeof (char) * (strlen (str)));
strcpy (tmp, str);
if (num> 1)
{
for (i = 0; k
while (str [i]! = 32)
i + +;
}
tmp = & tmp [i];
}

while (str [i]! = 32 && str [i]! = 0)
{i ++; j ++;}
tmp [j] = 0;

return tmp;
}

Link to content
Share on other sites

And why not use strtok or strtok_r? It is recommended to use the standard library when possible. C's standard library is not that great, so you should know it or at least know how to search.

There are a couple of bugs here that I've found just in a minute of studying the code:

1) You have not checked if str is NULL

2) There is a possible bug when the str length is 0 meaning "". What will happen is platform-dependent.

And there seems to be a few more.

Link to content
Share on other sites

  • 2 weeks later ...

I guess I'll put it if no one else has bothered:

Quick (and reasonably accurate) 1 X Root Section X for IEEE 754 32bit float

float InvSqrt (float x)
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;
}

Especially useful when FP hardware is limited.

History and Explanations:

http://www.beyond3d.com/articles/fastinvsqrt/

http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf

Link to content
Share on other sites

  • 1 month later ...

JAVASCRIPT

Checking which You have a version (as there is now in the forum):





var browser = navigator.appName
var b_version = navigator.appVersion
var version = parseFloat (b_version)

document.write ("Browser name:" + browser)
document.write (" ")
document.write ("Browser version:" + version)




And here if you want a clock on your site / browser :)




function startTime ()
{
var today = new Date ()
var h = today.getHours ()
var m = today.getMinutes ()
var s = today.getSeconds ()
// add a zero in front of numbers <10
m = checkTime (m)
s = checkTime (s)
document.getElementById ('txt'). innerHTML = h + ":" + m + ":" + s
t = setTimeout ('startTime ()', 500)
}

function checkTime (i)
{
if (i <10)
{i = "0" + i}
return i
}








Link to content
Share on other sites

ארכיון

This discussion has been archived and new comments can not be added.


  • Latest news

  • Buzz-Zone: Everything hot on the net

  • Popular now

×
  • Create new ...

At the top of the news:

new on the site