- 1
- 2
- 3
- 4
if( state != !val )
{
state = !val;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+62
if( state != !val )
{
state = !val;
}
Переключение. Обе переменные булевские.
+51
void main() {
system("color 04");
setlocale(LC_ALL, "rus");
if (start == false) {
logos();
loading();
start = true;
system("cls");
}
if (get_hero_s == false) {
get_hero();
get_hero_s = true;
}
menu();
map();
if (go_went_gone == 1) {
system("cls");
cout << "\nВы напали на оборотня в тёмном лесу\n";
loading();
Sleep(1400);
fight("werewolf");
go_went_gone = 0;
main();
}
else if (go_went_gone == 3) {
system("cls");
cout << "\nВы напали на лучника в мрачном поле\n";
loading();
Sleep(1400);
fight("archer");
go_went_gone = 0;
main();
}
if (go_went_gone == 2) {
system("cls");
cout << "\nВы напали на гоблина в тёмном лесу\n";
loading();
Sleep(1400);
fight("werewolf");
go_went_gone = 0;
main();
}
else if (go_went_gone == 4) {
system("cls");
cout << "\nВы напали на лучника на костяного лучника в подземелье \n";
loading();
Sleep(1400);
fight("archer");
go_went_gone = 0;
main();
}
Рекурсия мейна, и это курсовая работа!!
+55
bool validateIp(std::string& ip) {
if( ip.length() == 0 ) {
return false;
}
if( ip[0] == '.' ) {
return false;
}
// Проверка на наличие 3 точек
int cp = 0;
for( int i = 0; i < ip.length(); i++ ) {
if( ip[i] == '.' ) {
cp++;
}
}
if( cp != 3 ) {
std::cout << "проверка на 3 точки" << std::endl;
return false;
}
//=====================
// Проверка на 2 точки подряд
for( int i = 0; i < ip.length()-1; i++ ) {
if( ip[i] == '.' && ip[i+1] == '.' ) {
std::cout << "проверка на 2 точки подряд" << std::endl;
return false;
}
}
//===========================
//Проверка на больше 3 цифр подряд
int i = 0;
int j = 0;
for( i = 0; i < ip.length(); i++ ) {
for( j = i; j < i+4 && j < ip.length(); j++ ) {
if( j == i+3 && ip[j] != '.' ) {
std::cout << "проверка на 4 цифры подряд" << std::endl;
return false;
}
if( ip[j] == '.' ) {
i = j;
break;
}
}
}
return true;
//============================
}
Валидация IP-адреса
+51
while (w.pollEvent(event)) {
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Period)) {
ip += ".";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num0)) {
ip += "0";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num1)) {
ip += "1";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num2)) {
ip += "2";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num3)) {
ip += "3";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num4)) {
ip += "4";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num5)) {
ip += "5";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num6)) {
ip += "6";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num7)) {
ip += "7";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num8)) {
ip += "8";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num9)) {
ip += "9";
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::BackSpace) && ip.length() > 0) {
ip.erase(ip.end() - 1, ip.end());
}
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Return)) {
if (validateIp(ip)) {
Text c("Connection...", f);
c.setColor(Color::Black);
c.setPosition(100, 20);
w.draw(c);
w.display();
return ip;
} else {
ip.erase(ip.begin(), ip.end());
wrongAnswer = true;
}
}
if (event.type == Event::Closed) {
w.close();
return 0;
}
}
Ввод IP-адреса в интерфейсе игры
+49
#include <iostream>
int get_number() {
return 5;
}
int magic_number(int foo()) {
return foo();
}
int main(void)
{
std::cout << magic_number(get_number) << std::endl;
}
http://ideone.com/TbV0jD
+51
//Sets the color(background and foreground)
void Console::SetColor(){
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, FGColor | BGColor);
#else
string clr = "\033[";
clr += BGColor;
clr += ";";
clr += FGColor;
clr += "m";
cout << clr;
#endif
}
Изменение цвета текста и фона консоли
+52
// round up the blockSize to fit an integer number of pointers...
m_blockSize = static_cast<QMPoolSize>(sizeof(QFreeBlock));//start with one
uint_fast16_t nblocks = uf16_1; //# free blocks in a memory block
while (m_blockSize < static_cast<QMPoolSize>(blockSize)) {
m_blockSize += static_cast<QMPoolSize>(sizeof(QFreeBlock));
++nblocks;
}
в догонку к #17616. делим на 4 с округлением, с помощью цикла.
P.S. касты и цикл само собой разумеется в ж не нужны:
m_blockSize = (blockSize + sizeof(QFreeBlock)-1) & ~(sizeof(QFreeBlock)-1);
nblocks = m_blockSize / sizeof(QFreeBlock);
+53
{
...
_tswfstring contentID = fileName;
_tswfstring::size_type index = fileName.find_last_of ( _T("\\") );
if ( index != -1 )
contentID.erase(0, index + 1);
TCHAR name[10] = {0};
memcpy(name, contentID.c_str() + contentID.length() - 9, 9 * sizeof(TCHAR));
if(name[6] == _T('B') || name[6] == _T('b')) //to upper case if .bmp
{
name[6] = _T('B');
name[7] = _T('M');
name[8] = _T('P');
}
}
+55
uint8_t const Q_ROM QF_div8Lkup[65] = {
static_cast<uint8_t>(0), // unused location
static_cast<uint8_t>(0), static_cast<uint8_t>(0), static_cast<uint8_t>(0),
static_cast<uint8_t>(0), static_cast<uint8_t>(0), static_cast<uint8_t>(0),
static_cast<uint8_t>(0), static_cast<uint8_t>(0),
static_cast<uint8_t>(1), static_cast<uint8_t>(1), static_cast<uint8_t>(1),
static_cast<uint8_t>(1), static_cast<uint8_t>(1), static_cast<uint8_t>(1),
static_cast<uint8_t>(1), static_cast<uint8_t>(1),
static_cast<uint8_t>(2), static_cast<uint8_t>(2), static_cast<uint8_t>(2),
static_cast<uint8_t>(2), static_cast<uint8_t>(2), static_cast<uint8_t>(2),
static_cast<uint8_t>(2), static_cast<uint8_t>(2),
static_cast<uint8_t>(3), static_cast<uint8_t>(3), static_cast<uint8_t>(3),
static_cast<uint8_t>(3), static_cast<uint8_t>(3), static_cast<uint8_t>(3),
static_cast<uint8_t>(3), static_cast<uint8_t>(3),
static_cast<uint8_t>(4), static_cast<uint8_t>(4), static_cast<uint8_t>(4),
static_cast<uint8_t>(4), static_cast<uint8_t>(4), static_cast<uint8_t>(4),
static_cast<uint8_t>(4), static_cast<uint8_t>(4),
static_cast<uint8_t>(5), static_cast<uint8_t>(5), static_cast<uint8_t>(5),
static_cast<uint8_t>(5), static_cast<uint8_t>(5), static_cast<uint8_t>(5),
static_cast<uint8_t>(5), static_cast<uint8_t>(5),
static_cast<uint8_t>(6), static_cast<uint8_t>(6), static_cast<uint8_t>(6),
static_cast<uint8_t>(6), static_cast<uint8_t>(6), static_cast<uint8_t>(6),
static_cast<uint8_t>(6), static_cast<uint8_t>(6),
static_cast<uint8_t>(7), static_cast<uint8_t>(7), static_cast<uint8_t>(7),
static_cast<uint8_t>(7), static_cast<uint8_t>(7), static_cast<uint8_t>(7),
static_cast<uint8_t>(7), static_cast<uint8_t>(7)
};
// ....
//! the function evaluates to TRUE if the priority set has the element n.
bool hasElement(uint_fast8_t const n) const {
uint_fast8_t const m =
static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_div8Lkup[n]));
return ((m_bits[m]
& static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_pwr2Lkup[n])))
!= static_cast<uint_fast8_t>(0));
}
//! insert element \a n into the set, n = 1..64
void insert(uint_fast8_t const n) {
uint_fast8_t m =
static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_div8Lkup[n]));
m_bits[m] |= static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_pwr2Lkup[n]));
m_bytes |=
static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_pwr2Lkup[m
+ static_cast<uint_fast8_t>(1)]));
}
делим на 8 в индустриальном С++. это такой специальный вариант крестов где пользователям сначала лоботомию делают.
из реализации bitset'а. insert() приведен в качестве примера.
+53
static uint8_t const FREE = static_cast<uint8_t>(0);
static uint8_t const USED = static_cast<uint8_t>(1);
static char_t const * const THINKING = &"thinking"[0];
static char_t const * const HUNGRY = &"hungry "[0];
static char_t const * const EATING = &"eating "[0];
Из демы QP/C++ библиотеки. Вот в таком духе очень много кода.
Индустриальщики, после перехода на С++, похоже очень сильно страдают по отсутствию pre-ANSI C какашек, и изобретают новые.