fork download
  1. //+------------------------------------------------------------------+
  2. //| StratosZephyr.mq5 |
  3. //| Generated by DeepAI |
  4. //| |
  5. //+------------------------------------------------------------------+
  6. input double TakeProfit = 120; // Take Profit in points
  7. input double LotSize = 0.01; // Lot size
  8. input bool Autolot = false; // Auto lot management
  9. input double Risk = 3.0; // Risk percentage
  10. input double StopLoss = 50; // Stop Loss in points
  11. input int MagicNumber = 937843; // Magic Number
  12. input double LossCapAmount = 1000; // Maximum loss cap amount
  13. input bool ScaleLossCapWithLotSize = true; // Scale loss cap with lot size
  14. input int SpreadAllowed = 300; // Allowed Spread
  15. input bool EnableConcurrentBuyingAndSelling = true; // Allow concurrent orders
  16.  
  17. input int RSI_Period = 14; // RSI period
  18. input double Overbought_Level = 70.0; // Overbought level for RSI
  19. input double Oversold_Level = 30.0; // Oversold level for RSI
  20.  
  21. input int TradingWindowOpenHour = 22; // Trading opens at hour
  22. input int TradingWindowCloseHour = 8; // Trading closes at hour
  23. input bool TradeOnMonday = true; // Trade on Monday
  24. input bool TradeOnTuesday = true; // Trade on Tuesday
  25. input bool TradeOnWednesday = true; // Trade on Wednesday
  26. input bool TradeOnThursday = true; // Trade on Thursday
  27. input bool TradeOnFriday = true; // Trade on Friday
  28. input bool TradeOnSaturday = false; // Trade on Saturday
  29. input bool TradeOnSunday = false; // Trade on Sunday
  30.  
  31. //+------------------------------------------------------------------+
  32. //| Expert tick function |
  33. //+------------------------------------------------------------------+
  34. void OnTick()
  35. {
  36. // Check if trading is allowed
  37. if (!IsTradingAllowed())
  38. {
  39. return; // Exit if trading is not allowed
  40. }
  41.  
  42. double rsiValue = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE);
  43.  
  44. // Buy condition
  45. if (rsiValue < Oversold_Level && PermitBuyOrders())
  46. {
  47. PlaceOrder(TRADE_ACTION_BUY); // Call place order for buy
  48. }
  49.  
  50. // Sell condition
  51. if (rsiValue > Overbought_Level && PermitSellOrders())
  52. {
  53. PlaceOrder(TRADE_ACTION_SELL); // Call place order for sell
  54. }
  55. }
  56.  
  57. //+------------------------------------------------------------------+
  58. //| Place order function |
  59. //+------------------------------------------------------------------+
  60. void PlaceOrder(int orderType)
  61. {
  62. MqlTradeRequest request; // For sending a trading request
  63. MqlTradeResult result; // To hold the trading result
  64.  
  65. // Check that orderType is valid
  66. if (orderType != TRADE_ACTION_BUY && orderType != TRADE_ACTION_SELL)
  67. {
  68. Print("Invalid order type: ", orderType);
  69. return; // Exit if order type is invalid
  70. }
  71.  
  72. // Set up the trade request
  73. request.symbol = Symbol(); // Get the current trading symbol
  74. request.volume = Autolot ? CalculateLotSize() : LotSize; // Define the volume
  75.  
  76. // Cast orderType to the appropriate enum type if needed
  77. request.action = (TRADE_ACTION)orderType;
  78.  
  79. // Calculate Take Profit and Stop Loss
  80. request.tp = (orderType == TRADE_ACTION_BUY) ? NormalizeDouble(Ask + (TakeProfit * _Point), _Digits)
  81. : NormalizeDouble(Bid - (TakeProfit * _Point), _Digits);
  82. request.sl = (orderType == TRADE_ACTION_BUY) ? NormalizeDouble(Ask - (StopLoss * _Point), _Digits)
  83. : NormalizeDouble(Bid + (StopLoss * _Point), _Digits);
  84.  
  85. request.deviation = SpreadAllowed; // Set allowable slippage
  86. request.magic = MagicNumber; // Set unique identifier for the order
  87. request.comment = "Gold Robot Order"; // Set the order comment
  88.  
  89. // Send the order
  90. if (!OrderSend(request, result)) // Execute the trade
  91. {
  92. Print("Error sending order: ", GetLastError()); // Print detailed error info
  93. }
  94. else
  95. {
  96. Print("Order sent successfully: ", result.order); // Print success message
  97. }
  98. }
  99.  
  100. //+------------------------------------------------------------------+
  101. //| Calculate lot size based on risk management |
  102. //+------------------------------------------------------------------+
  103. double CalculateLotSize()
  104. {
  105. double accountRisk = AccountBalance() * (Risk / 100);
  106. double lotSize = accountRisk / (StopLoss * _Point);
  107. return NormalizeDouble(lotSize, 2);
  108. }
  109.  
  110. //+------------------------------------------------------------------+
  111. //| Function to check if trading is allowed |
  112. //+------------------------------------------------------------------+
  113. bool IsTradingAllowed()
  114. {
  115. datetime now = TimeCurrent(); // Get the current time
  116. int hour = TimeHour(now); // Get the current hour
  117. int dayOfWeek = TimeDayOfWeek(now); // Get the current day of the week
  118.  
  119. // Adjust for trading days and hours
  120. if ((dayOfWeek == 0 && !TradeOnSunday) ||
  121. (dayOfWeek == 6 && !TradeOnSaturday) ||
  122. (dayOfWeek == 5 && hour >= TradingWindowCloseHour) ||
  123. (hour < TradingWindowOpenHour) ||
  124. (hour >= TradingWindowCloseHour))
  125. {
  126. return false; // Trading is not allowed
  127. }
  128.  
  129. return true; // Trading is allowed
  130. }
  131.  
  132. // Checks permissions for buy orders
  133. bool PermitBuyOrders()
  134. {
  135. return EnableConcurrentBuyingAndSelling;
  136. }
  137.  
  138. // Checks permissions for sell orders
  139. bool PermitSellOrders()
  140. {
  141. return EnableConcurrentBuyingAndSelling;
  142. }
  143.  
  144. //+------------------------------------------------------------------+/* package whatever; // don't place package name! */
  145. /*
  146.  
  147.   This source code accompanies the article, "Using The Golay Error Detection And
  148.   Correction Code", by Hank Wallace. This program demonstrates use of the Golay
  149.   code.
  150.   Usage: G DATA Encode/Correct/Verify/Test
  151.   where DATA is the data to be encoded, codeword to be corrected,
  152.   or codeword to be checked for errors. DATA is hexadecimal.
  153.   Examples:
  154.   G 555 E encodes information value 555 and prints a codeword
  155.   G ABC123 C corrects codeword ABC123
  156.   G ABC123 V checks codeword ABC123 for errors
  157.   G ABC123 T tests routines, ABC123 is a dummy parameter
  158.   This program may be freely incorporated into your programs as needed. It
  159.   compiles under Borland's Turbo C 2.0. No warranty of any kind is granted.
  160.   */
  161. #include "stdio.h"
  162. #include "conio.h"
  163. #define POLY 0xAE3 /* or use the other polynomial, 0xC75 */
  164.  
  165. /* ====================================================== */
  166.  
  167. unsigned long golay(unsigned long cw)
  168. /* This function calculates [23,12] Golay codewords.
  169.   The format of the returned longint is
  170.   [checkbits(11),data(12)]. */
  171. {
  172. int i;
  173. unsigned long c;
  174. cw&=0xfffl;
  175. c=cw; /* save original codeword */
  176. for (i=1; i<=12; i++) /* examine each data bit */
  177. {
  178. if (cw & 1) /* test data bit */
  179. cw^=POLY; /* XOR polynomial */
  180. cw>>=1; /* shift intermediate result */
  181. }
  182. return((cw<<12)|c); /* assemble codeword */
  183. }
  184.  
  185. /* ====================================================== */
  186.  
  187. int parity(unsigned long cw)
  188. /* This function checks the overall parity of codeword cw.
  189.   If parity is even, 0 is returned, else 1. */
  190. {
  191. unsigned char p;
  192.  
  193. /* XOR the bytes of the codeword */
  194. p=*(unsigned char*)&cw;
  195. p^=*((unsigned char*)&cw+1);
  196. p^=*((unsigned char*)&cw+2);
  197.  
  198. /* XOR the halves of the intermediate result */
  199. p=p ^ (p>>4);
  200. p=p ^ (p>>2);
  201. p=p ^ (p>>1);
  202.  
  203. /* return the parity result */
  204. return(p & 1);
  205. }
  206.  
  207. /* ====================================================== */
  208.  
  209. unsigned long syndrome(unsigned long cw)
  210. /* This function calculates and returns the syndrome
  211.   of a [23,12] Golay codeword. */
  212. {
  213. int i;
  214. cw&=0x7fffffl;
  215. for (i=1; i<=12; i++) /* examine each data bit */
  216. {
  217. if (cw & 1) /* test data bit */
  218. cw^=POLY; /* XOR polynomial */
  219. cw>>=1; /* shift intermediate result */
  220. }
  221. return(cw<<12); /* value pairs with upper bits of cw */
  222. }
  223.  
  224. /* ====================================================== */
  225.  
  226. int weight(unsigned long cw)
  227. /* This function calculates the weight of
  228.   23 bit codeword cw. */
  229. {
  230. int bits,k;
  231.  
  232. /* nibble weight table */
  233. const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
  234.  
  235. bits=0; /* bit counter */
  236. k=0;
  237. /* do all bits, six nibbles max */
  238. while ((k<6) && (cw))
  239. {
  240. bits=bits+wgt[cw & 0xf];
  241. cw>>=4;
  242. k++;
  243. }
  244.  
  245. return(bits);
  246. }
  247.  
  248. /* ====================================================== */
  249.  
  250. unsigned long rotate_left(unsigned long cw, int n)
  251. /* This function rotates 23 bit codeword cw left by n bits. */
  252. {
  253. int i;
  254.  
  255. if (n != 0)
  256. {
  257. for (i=1; i<=n; i++)
  258. {
  259. if ((cw & 0x400000l) != 0)
  260. cw=(cw << 1) | 1;
  261. else
  262. cw<<=1;
  263. }
  264. }
  265.  
  266. return(cw & 0x7fffffl);
  267. }
  268.  
  269. /* ====================================================== */
  270.  
  271. unsigned long rotate_right(unsigned long cw, int n)
  272. /* This function rotates 23 bit codeword cw right by n bits. */
  273. {
  274. int i;
  275.  
  276. if (n != 0)
  277. {
  278. for (i=1; i<=n; i++)
  279. {
  280. if ((cw & 1) != 0)
  281. cw=(cw >> 1) | 0x400000l;
  282. else
  283. cw>>=1;
  284. }
  285. }
  286.  
  287. return(cw & 0x7fffffl);
  288. }
  289.  
  290. /* ====================================================== */
  291.  
  292. unsigned long correct(unsigned long cw, int *errs)
  293. /* This function corrects Golay [23,12] codeword cw, returning the
  294.   corrected codeword. This function will produce the corrected codeword
  295.   for three or fewer errors. It will produce some other valid Golay
  296.   codeword for four or more errors, possibly not the intended
  297.   one. *errs is set to the number of bit errors corrected. */
  298. {
  299. unsigned char
  300. w; /* current syndrome limit weight, 2 or 3 */
  301. unsigned long
  302. mask; /* mask for bit flipping */
  303. int
  304. i,j; /* index */
  305. unsigned long
  306. s, /* calculated syndrome */
  307. cwsaver; /* saves initial value of cw */
  308.  
  309. cwsaver=cw; /* save */
  310. *errs=0;
  311. w=3; /* initial syndrome weight threshold */
  312. j=-1; /* -1 = no trial bit flipping on first pass */
  313. mask=1;
  314. while (j<23) /* flip each trial bit */
  315. {
  316. if (j != -1) /* toggle a trial bit */
  317. {
  318. if (j>0) /* restore last trial bit */
  319. {
  320. cw=cwsaver ^ mask;
  321. mask+=mask; /* point to next bit */
  322. }
  323. cw=cwsaver ^ mask; /* flip next trial bit */
  324. w=2; /* lower the threshold while bit diddling */
  325. }
  326.  
  327. s=syndrome(cw); /* look for errors */
  328. if (s) /* errors exist */
  329. {
  330. for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
  331. {
  332. if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
  333. {
  334. cw=cw ^ s; /* remove errors */
  335. cw=rotate_right(cw,i); /* unrotate data */
  336. return(s=cw);
  337. }
  338. else
  339. {
  340. cw=rotate_left(cw,1); /* rotate to next pattern */
  341. s=syndrome(cw); /* calc new syndrome */
  342. }
  343. }
  344. j++; /* toggle next trial bit */
  345. }
  346. else
  347. return(cw); /* return corrected codeword */
  348. }
  349.  
  350. return(cwsaver); /* return original if no corrections */
  351. } /* correct */
  352.  
  353. /* ====================================================== */
  354.  
  355. int decode(int correct_mode, int *errs, unsigned long *cw)
  356. /* This function decodes codeword *cw in one of two modes. If correct_mode
  357.   is nonzero, error correction is attempted, with *errs set to the number of
  358.   bits corrected, and returning 0 if no errors exist, or 1 if parity errors
  359.   exist. If correct_mode is zero, error detection is performed on *cw,
  360.   returning 0 if no errors exist, 1 if an overall parity error exists, and
  361.   2 if a codeword error exists. */
  362. {
  363. unsigned long parity_bit;
  364.  
  365. if (correct_mode) /* correct errors */
  366. {
  367. parity_bit=*cw & 0x800000l; /* save parity bit */
  368. *cw&=~0x800000l; /* remove parity bit for correction */
  369.  
  370. *cw=correct(*cw, errs); /* correct up to three bits */
  371. *cw|=parity_bit; /* restore parity bit */
  372.  
  373. /* check for 4 bit errors */
  374. if (parity(*cw)) /* odd parity is an error */
  375. return(1);
  376. return(0); /* no errors */
  377. }
  378. else /* detect errors only */
  379. {
  380. *errs=0;
  381. if (parity(*cw)) /* odd parity is an error */
  382. {
  383. *errs=1;
  384. return(1);
  385. }
  386. if (syndrome(*cw))
  387. {
  388. *errs=1;
  389. return(2);
  390. }
  391. else
  392. return(0); /* no errors */
  393. }
  394. } /* decode */
  395.  
  396. /* ====================================================== */
  397.  
  398. void golay_test(void)
  399. /* This function tests the Golay routines for detection and correction
  400.   of various patterns of error_limit bit errors. The error_mask cycles
  401.   over all possible values, and error_limit selects the maximum number
  402.   of induced errors. */
  403. {
  404. unsigned long
  405. error_mask, /* bitwise mask for inducing errors */
  406. trashed_codeword, /* the codeword for trial correction */
  407. virgin_codeword; /* the original codeword without errors */
  408. unsigned char
  409. pass=1, /* assume test passes */
  410. error_limit=3; /* select number of induced bit errors here */
  411. int
  412. error_count; /* receives number of errors corrected */
  413.  
  414. virgin_codeword=golay(0x555); /* make a test codeword */
  415. if (parity(virgin_codeword))
  416. virgin_codeword^=0x800000l;
  417. for (error_mask=0; error_mask<0x800000l; error_mask++)
  418. {
  419. /* filter the mask for the selected number of bit errors */
  420. if (weight(error_mask) <= error_limit) /* you can make this faster! */
  421. {
  422. trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
  423.  
  424. decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
  425.  
  426. if (trashed_codeword ^ virgin_codeword)
  427. {
  428. printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
  429. weight(error_mask),error_mask);
  430. pass=0;
  431. }
  432.  
  433. if (kbhit()) /* look for user input */
  434. {
  435. if (getch() == 27) return; /* escape exits */
  436.  
  437. /* other key prints status */
  438. printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
  439. }
  440. }
  441. }
  442. printf("Golay test %s!\n",pass?"PASSED":"FAILED");
  443. }
  444.  
  445. /* ====================================================== */
  446.  
  447. void main(int argument_count, char *argument[])
  448. {
  449. int i,j;
  450. unsigned long l,g;
  451. const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
  452. " where DATA is the data to be encoded, codeword to be corrected,\n"
  453. " or codeword to be checked for errors. DATA is hexadecimal.\n\n"
  454. "Examples:\n\n"
  455. " G 555 E encodes information value 555 and prints a codeword\n"
  456. " G ABC123 C corrects codeword ABC123\n"
  457. " G ABC123 V checks codeword ABC123 for errors\n"
  458. " G ABC123 T tests routines, ABC123 is a dummy parameter\n\n";
  459.  
  460. if (argument_count != 3)
  461. {
  462. printf(errmsg);
  463. exit(0);
  464. }
  465.  
  466. if (sscanf(argument[1],"%lx",&l) != 1)
  467. {
  468. printf(errmsg);
  469. exit(0);
  470. }
  471.  
  472. switch (toupper(*argument[2]))
  473. {
  474. case 'E': /* encode */
  475. l&=0xfff;
  476. l=golay(l);
  477. if (parity(l)) l^=0x800000l;
  478. printf("Codeword = %lX\n",l);
  479. break;
  480.  
  481. case 'V': /* verify */
  482. if (decode(0,&i,&l))
  483. printf("Codeword %lX is not a Golay codeword.\n",l);
  484. else
  485. printf("Codeword %lX is a Golay codeword.\n",l);
  486. break;
  487.  
  488. case 'C': /* correct */
  489. g=l; /* save initial codeword */
  490. j=decode(1,&i,&l);
  491. if ((j) && (i))
  492. printf("Codeword %lX had %d bits corrected,\n"
  493. "resulting in codeword %lX with a parity error.\n",g,i,l);
  494. else
  495. if ((j == 0) && (i))
  496. printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
  497. else
  498. if ((j) && (i == 0))
  499. printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
  500. else
  501. if ((j == 0) && (i == 0))
  502. printf("Codeword %lX does not require correction.\n",g);
  503. break;
  504.  
  505. case 'T': /* test */
  506. printf("Press SPACE for status, ESC to exit test...\n");
  507. golay_test();
  508. break;
  509.  
  510. default:
  511. printf(errmsg);
  512. exit(0);
  513. }
  514. }
  515.  
  516. /* end of G.C */
  517.  
  518.  
  519. import java.util.*;
  520. import java.lang.*;
  521. import java.io.*;
  522.  
  523. /* Name of the class has to be "Main" only if the class is public. */
  524. class Ideone
  525. {
  526. public static void main (String[] args) throws java.lang.Exception
  527. {
  528. // your code goes here
  529. }
  530. }
Success #stdin #stdout 0.03s 25280KB
stdin
Standard input is empty
stdout
//+------------------------------------------------------------------+
//| StratosZephyr.mq5 |
//| Generated by DeepAI |
//| |
//+------------------------------------------------------------------+
input double TakeProfit = 120; // Take Profit in points
input double LotSize = 0.01; // Lot size
input bool Autolot = false; // Auto lot management
input double Risk = 3.0; // Risk percentage
input double StopLoss = 50; // Stop Loss in points
input int MagicNumber = 937843; // Magic Number
input double LossCapAmount = 1000; // Maximum loss cap amount
input bool ScaleLossCapWithLotSize = true; // Scale loss cap with lot size
input int SpreadAllowed = 300; // Allowed Spread
input bool EnableConcurrentBuyingAndSelling = true; // Allow concurrent orders

input int RSI_Period = 14; // RSI period
input double Overbought_Level = 70.0; // Overbought level for RSI
input double Oversold_Level = 30.0; // Oversold level for RSI

input int TradingWindowOpenHour = 22; // Trading opens at hour
input int TradingWindowCloseHour = 8; // Trading closes at hour
input bool TradeOnMonday = true; // Trade on Monday
input bool TradeOnTuesday = true; // Trade on Tuesday
input bool TradeOnWednesday = true; // Trade on Wednesday
input bool TradeOnThursday = true; // Trade on Thursday
input bool TradeOnFriday = true; // Trade on Friday
input bool TradeOnSaturday = false; // Trade on Saturday
input bool TradeOnSunday = false; // Trade on Sunday

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if trading is allowed
if (!IsTradingAllowed())
{
return; // Exit if trading is not allowed
}

double rsiValue = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE);

// Buy condition
if (rsiValue < Oversold_Level && PermitBuyOrders())
{
PlaceOrder(TRADE_ACTION_BUY); // Call place order for buy
}

// Sell condition
if (rsiValue > Overbought_Level && PermitSellOrders())
{
PlaceOrder(TRADE_ACTION_SELL); // Call place order for sell
}
}

//+------------------------------------------------------------------+
//| Place order function |
//+------------------------------------------------------------------+
void PlaceOrder(int orderType)
{
MqlTradeRequest request; // For sending a trading request
MqlTradeResult result; // To hold the trading result

// Check that orderType is valid
if (orderType != TRADE_ACTION_BUY && orderType != TRADE_ACTION_SELL)
{
Print("Invalid order type: ", orderType);
return; // Exit if order type is invalid
}

// Set up the trade request
request.symbol = Symbol(); // Get the current trading symbol
request.volume = Autolot ? CalculateLotSize() : LotSize; // Define the volume

// Cast orderType to the appropriate enum type if needed
request.action = (TRADE_ACTION)orderType;

// Calculate Take Profit and Stop Loss
request.tp = (orderType == TRADE_ACTION_BUY) ? NormalizeDouble(Ask + (TakeProfit * _Point), _Digits)
: NormalizeDouble(Bid - (TakeProfit * _Point), _Digits);
request.sl = (orderType == TRADE_ACTION_BUY) ? NormalizeDouble(Ask - (StopLoss * _Point), _Digits)
: NormalizeDouble(Bid + (StopLoss * _Point), _Digits);

request.deviation = SpreadAllowed; // Set allowable slippage
request.magic = MagicNumber; // Set unique identifier for the order
request.comment = "Gold Robot Order"; // Set the order comment

// Send the order
if (!OrderSend(request, result)) // Execute the trade
{
Print("Error sending order: ", GetLastError()); // Print detailed error info
}
else
{
Print("Order sent successfully: ", result.order); // Print success message
}
}

//+------------------------------------------------------------------+
//| Calculate lot size based on risk management |
//+------------------------------------------------------------------+
double CalculateLotSize()
{
double accountRisk = AccountBalance() * (Risk / 100);
double lotSize = accountRisk / (StopLoss * _Point);
return NormalizeDouble(lotSize, 2);
}

//+------------------------------------------------------------------+
//| Function to check if trading is allowed |
//+------------------------------------------------------------------+
bool IsTradingAllowed()
{
datetime now = TimeCurrent(); // Get the current time
int hour = TimeHour(now); // Get the current hour
int dayOfWeek = TimeDayOfWeek(now); // Get the current day of the week

// Adjust for trading days and hours
if ((dayOfWeek == 0 && !TradeOnSunday) ||
(dayOfWeek == 6 && !TradeOnSaturday) ||
(dayOfWeek == 5 && hour >= TradingWindowCloseHour) ||
(hour < TradingWindowOpenHour) ||
(hour >= TradingWindowCloseHour))
{
return false; // Trading is not allowed
}

return true; // Trading is allowed
}

// Checks permissions for buy orders
bool PermitBuyOrders()
{
return EnableConcurrentBuyingAndSelling;
}

// Checks permissions for sell orders
bool PermitSellOrders()
{
return EnableConcurrentBuyingAndSelling;
}

//+------------------------------------------------------------------+/* package whatever; // don't place package name! */
/*
    
    This source code accompanies the article, "Using The Golay Error Detection And
    Correction Code", by Hank Wallace. This program demonstrates use of the Golay
    code.
      Usage: G DATA Encode/Correct/Verify/Test
        where DATA is the data to be encoded, codeword to be corrected,
        or codeword to be checked for errors. DATA is hexadecimal.
      Examples:
        G 555 E      encodes information value 555 and prints a codeword
        G ABC123 C   corrects codeword ABC123
        G ABC123 V   checks codeword ABC123 for errors
        G ABC123 T   tests routines, ABC123 is a dummy parameter
    This program may be freely incorporated into your programs as needed. It
    compiles under Borland's Turbo C 2.0. No warranty of any kind is granted.
    */
    #include "stdio.h"
    #include "conio.h"
    #define POLY  0xAE3  /* or use the other polynomial, 0xC75 */
    
    /* ====================================================== */
    
    unsigned long golay(unsigned long cw)
    /* This function calculates [23,12] Golay codewords.
      The format of the returned longint is
      [checkbits(11),data(12)]. */
    {
      int i;
      unsigned long c;
      cw&=0xfffl;
      c=cw; /* save original codeword */
      for (i=1; i<=12; i++)  /* examine each data bit */
        {
          if (cw & 1)        /* test data bit */
            cw^=POLY;        /* XOR polynomial */
          cw>>=1;            /* shift intermediate result */
        }
      return((cw<<12)|c);    /* assemble codeword */
    }
    
    /* ====================================================== */
    
    int parity(unsigned long cw)
    /* This function checks the overall parity of codeword cw.
      If parity is even, 0 is returned, else 1. */
    {
      unsigned char p;
    
      /* XOR the bytes of the codeword */
      p=*(unsigned char*)&cw;
      p^=*((unsigned char*)&cw+1);
      p^=*((unsigned char*)&cw+2);
    
      /* XOR the halves of the intermediate result */
      p=p ^ (p>>4);
      p=p ^ (p>>2);
      p=p ^ (p>>1);
    
      /* return the parity result */
      return(p & 1);
    }
    
    /* ====================================================== */
    
    unsigned long syndrome(unsigned long cw)
    /* This function calculates and returns the syndrome
      of a [23,12] Golay codeword. */
    {
      int i;
      cw&=0x7fffffl;
      for (i=1; i<=12; i++)  /* examine each data bit */
        {
          if (cw & 1)        /* test data bit */
            cw^=POLY;        /* XOR polynomial */
          cw>>=1;            /* shift intermediate result */
        }
      return(cw<<12);        /* value pairs with upper bits of cw */
    }
    
    /* ====================================================== */
    
    int weight(unsigned long cw)
    /* This function calculates the weight of
      23 bit codeword cw. */
    {
      int bits,k;
    
      /* nibble weight table */
      const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    
      bits=0; /* bit counter */
      k=0;
      /* do all bits, six nibbles max */
      while ((k<6) && (cw))
        {
          bits=bits+wgt[cw & 0xf];
          cw>>=4;
          k++;
        }
    
      return(bits);
    }
    
    /* ====================================================== */
    
    unsigned long rotate_left(unsigned long cw, int n)
    /* This function rotates 23 bit codeword cw left by n bits. */
    {
      int i;
    
      if (n != 0)
        {
          for (i=1; i<=n; i++)
            {
              if ((cw & 0x400000l) != 0)
                cw=(cw << 1) | 1;
              else
                cw<<=1;
            }
        }
    
      return(cw & 0x7fffffl);
    }
    
    /* ====================================================== */
    
    unsigned long rotate_right(unsigned long cw, int n)
    /* This function rotates 23 bit codeword cw right by n bits. */
    {
      int i;
    
      if (n != 0)
        {
          for (i=1; i<=n; i++)
            {
              if ((cw & 1) != 0)
                cw=(cw >> 1) | 0x400000l;
              else
                cw>>=1;
            }
        }
    
      return(cw & 0x7fffffl);
    }
    
    /* ====================================================== */
    
    unsigned long correct(unsigned long cw, int *errs)
    /* This function corrects Golay [23,12] codeword cw, returning the
      corrected codeword. This function will produce the corrected codeword
      for three or fewer errors. It will produce some other valid Golay
      codeword for four or more errors, possibly not the intended
      one. *errs is set to the number of bit errors corrected. */
    {
      unsigned char
        w;                /* current syndrome limit weight, 2 or 3 */
      unsigned long
        mask;             /* mask for bit flipping */
      int
        i,j;              /* index */
      unsigned long
        s,                /* calculated syndrome */
        cwsaver;          /* saves initial value of cw */
    
      cwsaver=cw;         /* save */
      *errs=0;
      w=3;                /* initial syndrome weight threshold */
      j=-1;               /* -1 = no trial bit flipping on first pass */
      mask=1;
      while (j<23) /* flip each trial bit */
        {
          if (j != -1) /* toggle a trial bit */
            {
              if (j>0) /* restore last trial bit */
                {
                  cw=cwsaver ^ mask;
                  mask+=mask; /* point to next bit */
                }
              cw=cwsaver ^ mask; /* flip next trial bit */
              w=2; /* lower the threshold while bit diddling */
            }
    
          s=syndrome(cw); /* look for errors */
          if (s) /* errors exist */
            {
              for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
                {
                  if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
                    {
                      cw=cw ^ s;              /* remove errors */
                      cw=rotate_right(cw,i);  /* unrotate data */
                      return(s=cw);
                    }
                  else
                    {
                      cw=rotate_left(cw,1);   /* rotate to next pattern */
                      s=syndrome(cw);         /* calc new syndrome */
                    }
                }
              j++; /* toggle next trial bit */
            }
          else
            return(cw); /* return corrected codeword */
        }
    
      return(cwsaver); /* return original if no corrections */
    } /* correct */
    
    /* ====================================================== */
    
    int decode(int correct_mode, int *errs, unsigned long *cw)
    /* This function decodes codeword *cw in one of two modes. If correct_mode
      is nonzero, error correction is attempted, with *errs set to the number of
      bits corrected, and returning 0 if no errors exist, or 1 if parity errors
      exist. If correct_mode is zero, error detection is performed on *cw,
      returning 0 if no errors exist, 1 if an overall parity error exists, and
      2 if a codeword error exists. */
    {
      unsigned long parity_bit;
    
      if (correct_mode)               /* correct errors */
        {
          parity_bit=*cw & 0x800000l; /* save parity bit */
          *cw&=~0x800000l;            /* remove parity bit for correction */
    
          *cw=correct(*cw, errs);     /* correct up to three bits */
          *cw|=parity_bit;            /* restore parity bit */
    
          /* check for 4 bit errors */
          if (parity(*cw))            /* odd parity is an error */
            return(1);
          return(0); /* no errors */
        }
      else /* detect errors only */
        {
          *errs=0;
          if (parity(*cw)) /* odd parity is an error */
            {
              *errs=1;
              return(1);
            }
          if (syndrome(*cw))
            {
              *errs=1;
              return(2);
            }
          else
            return(0); /* no errors */
        }
    } /* decode */
    
    /* ====================================================== */
    
    void golay_test(void)
    /* This function tests the Golay routines for detection and correction
      of various patterns of error_limit bit errors. The error_mask cycles
      over all possible values, and error_limit selects the maximum number
      of induced errors. */
    {
      unsigned long
        error_mask,         /* bitwise mask for inducing errors */
        trashed_codeword,   /* the codeword for trial correction */
        virgin_codeword;    /* the original codeword without errors */
      unsigned char
        pass=1,             /* assume test passes */
        error_limit=3;      /* select number of induced bit errors here */
      int
        error_count;        /* receives number of errors corrected */
    
      virgin_codeword=golay(0x555); /* make a test codeword */
      if (parity(virgin_codeword))
        virgin_codeword^=0x800000l;
      for (error_mask=0; error_mask<0x800000l; error_mask++)
        {
          /* filter the mask for the selected number of bit errors */
          if (weight(error_mask) <= error_limit) /* you can make this faster! */
            {
              trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
    
              decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
    
              if (trashed_codeword ^ virgin_codeword)
                {
                  printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
                    weight(error_mask),error_mask);
                  pass=0;
                }
    
              if (kbhit()) /* look for user input */
                {
                  if (getch() == 27) return; /* escape exits */
    
                  /* other key prints status */
                  printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
                }
            }
        }
      printf("Golay test %s!\n",pass?"PASSED":"FAILED");
    }
    
    /* ====================================================== */
    
    void main(int argument_count, char *argument[])
    {
      int i,j;
      unsigned long l,g;
      const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
                 "  where DATA is the data to be encoded, codeword to be corrected,\n"
                 "  or codeword to be checked for errors. DATA is hexadecimal.\n\n"
                 "Examples:\n\n"
                 "  G 555 E      encodes information value 555 and prints a codeword\n"
                 "  G ABC123 C   corrects codeword ABC123\n"
                 "  G ABC123 V   checks codeword ABC123 for errors\n"
                 "  G ABC123 T   tests routines, ABC123 is a dummy parameter\n\n";
    
      if (argument_count != 3)
        {
          printf(errmsg);
          exit(0);
        }
    
      if (sscanf(argument[1],"%lx",&l) != 1)
        {
          printf(errmsg);
          exit(0);
        }
    
      switch (toupper(*argument[2]))
        {
          case 'E': /* encode */
            l&=0xfff;
            l=golay(l);
            if (parity(l)) l^=0x800000l;
            printf("Codeword = %lX\n",l);
            break;
    
          case 'V': /* verify */
            if (decode(0,&i,&l))
              printf("Codeword %lX is not a Golay codeword.\n",l);
            else
              printf("Codeword %lX is a Golay codeword.\n",l);
            break;
    
          case 'C': /* correct */
            g=l; /* save initial codeword */
            j=decode(1,&i,&l);
            if ((j) && (i))
              printf("Codeword %lX had %d bits corrected,\n"
                     "resulting in codeword %lX with a parity error.\n",g,i,l);
            else
              if ((j == 0) && (i))
                printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
              else
                if ((j) && (i == 0))
                  printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
                else
                  if ((j == 0) && (i == 0))
                    printf("Codeword %lX does not require correction.\n",g);
            break;
    
          case 'T': /* test */
            printf("Press SPACE for status, ESC to exit test...\n");
            golay_test();
            break;
    
          default:
            printf(errmsg);
            exit(0);
        }
    }
    
    /* end of G.C */


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
	}
}