- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
static double E( double epsilon )
{
int n = 1, nf = 1;
double e = 0;
while (Math.Abs(e - Math.E) > epsilon)
{
e += 1.0 / nf;
nf *= n;
n++;
}
return e;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+117
static double E( double epsilon )
{
int n = 1, nf = 1;
double e = 0;
while (Math.Abs(e - Math.E) > epsilon)
{
e += 1.0 / nf;
nf *= n;
n++;
}
return e;
}
Вычисление числа e как суммы обратных факториалов. По мотивам реальных событий.
+128
#ifndef __KERNEL__
/* Multicast groups - backwards compatiblility for userspace */
#define SELNL_GRP_NONE 0x00000000
#define SELNL_GRP_AVC 0x00000001 /* AVC notifications */
#define SELNL_GRP_ALL 0xffffffff
#endif
enum selinux_nlgroups {
SELNLGRP_NONE,
#define SELNLGRP_NONE SELNLGRP_NONE
SELNLGRP_AVC,
#define SELNLGRP_AVC SELNLGRP_AVC
__SELNLGRP_MAX
};
#define SELNLGRP_MAX (__SELNLGRP_MAX - 1)
Linux 3.3.0
include/linux/selinux_netlink.h
+156
foreach ($items as $item_id)
{
if ((int)$item_id<=0)
die();
// ...
if (!$redis->sIsMember("views_today_stats_sadd:{$views_stats_namespace}",$key))
$redis->sAdd("views_today_stats_sadd:{$views_stats_namespace}",$key);
// ...
}
Пехепешник второго уровня: циклы уже осилил, а вот с множествами пока не разобрался.
+154
escape = function(string) {
return (''+string).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
};
Ваше мнение?
+1004
long base64::rlong (char * source)
{
long res = 0;
for (int i = 0; i < 4; i++) ((char *) &res)[3 - i] = source[i];
return res;
}
void base64::encode (char * source, char * dest)
{
char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int n = 0;
for (int nd = 0; nd < ceil(4*((double)strlen(source))/3); nd++)
dest[n++] = base[rlong(&source[((nd >> 2) << 2)*3 >> 2]) >> (26 - 6*(nd - ((nd >> 2) << 2))) & 0x3F];
for (int i = 0; i < ((int) ceil(((double)n)/4) << 2) - n; i++) dest[n++] = '=';
dest[n] = 0;
}
Ответ преподу по c++. Переводстроки в base64.
+133
#include <stdio.h>
float abs(float a) { return a > 0 ? a : -a; }
int main() {
float x, e, step = 1, summ = step;
int i = 0;
scanf("%f, %f", (printf("x, e: "), &x), &e);
do summ += (abs(step *= x / ++i) > e) ? step : 0; while (abs(step) > e);
printf("summ = %f\n", summ);
return 0;
}
вычисление суммы ряда (1 + x/1! + x^2/2! + ... + x^n/n!), с заданной точностью
+149
$USER_ID = $_GET['user_id'];
$API_METHOD = $_GET['method'];
$Errors = array('Error#001' => 'Undefined data in request!');
switch($API_METHOD)
{
case 'get_user_name_by_id':
echo EncodeJSON(UserAPI::GetUserNameByID($USER_ID));
break;
case 'get_user_status_by_id':
echo EncodeJSON(UserAPI::GetUserStatusByID($USER_ID));
break;
default:
echo $Errors['Error#001'];
break;
}
class UserAPI //test
{
function GetUserNameByID($ID)
{
$SqlQuery = mysql_query("SELECT displayname FROM engine4_users WHERE user_id='$ID'");
$Response = mysql_fetch_array($SqlQuery);
$Result = array('UserName'=>$Response['displayname']);
return $Result;
}
function GetUserStatusByID($ID)
{
$SqlQuery = mysql_query("SELECT status FROM engine4_users WHERE user_id='$ID'");
$Response = mysql_fetch_array($SqlQuery);
$Result = array('UserStatus'=>$Response['status']);
return $Result;
}
}
function EncodeJSON($arr) {
$parts = array();
$is_list = false;
if (!is_array($arr)) return;
if (count($arr)<1) return '{}';
$keys = array_keys($arr);
$max_length = count($arr);
if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {
$is_list = true;
for($i=0; $i<count($keys); $i++) {
if($i != $keys[$i]) {
$is_list = false;
break;
}
}
}
foreach($arr as $key=>$value) {
if(is_array($value)) {
if($is_list) $parts[] = array2json($value);
else $parts[] = '"' . $key . '":' . array2json($value);
} else {
$str = '';
if(!$is_list) $str = '"' . $key . '":';
if(is_numeric($value)) $str .= $value;
elseif($value === false) $str .= 'false';
elseif($value === true) $str .= 'true';
else $str .= '"' . addslashes($value) . '"';
$parts[] = $str;
}
}
$json = implode(',',$parts);
if($is_list) return '[' . $json . ']';
return '{' . $json . '}';
}
Удивительное рядом.
+156
if (!$_GET['nocache']) {
$use_caching=0; // use cache?
} else {
$use_caching=0;
}
+74
for(int i = 0; i < fCount; i++)
{
result += Integer.parseInt(String.valueOf((fPart.charAt(i))), 10)*Math.pow(base, fCount-(i+1));
}
+121
private static string GetShortDisciplineName(string longDsciplineName){
string[] words = longDsciplineName.Split(' ');
switch (words.Count()){
case 1:{
return longDsciplineName;
break;
}