Advertisement
MBrendecke

Coding Challange 6 - C#

Aug 5th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. /*
  2.  * The Morpheus Tutorials:  https://www.youtube.com/channel/UCLGY6_j7kZfA1dmmjR1J_7w
  3.  * Coding Challange 6:      https://www.youtube.com/watch?v=sdAXkE39b9k
  4.  * Version:                 2018.08.11.1954
  5.  */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Net.Http;
  11. using System.Text;
  12.  
  13. using static System.Console;
  14. using static System.Math;
  15.  
  16. namespace Challange_6 {
  17.  
  18.   internal class Program {
  19.     private const string CHALLANGES = "https://cc.the-morpheus.de/challenges/6/";
  20.     private const string SOLUTIONS = "https://cc.the-morpheus.de/solutions/6/";
  21.  
  22.     private static readonly HttpClient client = new HttpClient();
  23.     private static readonly Stopwatch watch = new Stopwatch();
  24.  
  25.     private static void Main(string[] args) {
  26.       try {
  27.         do {
  28.           string numStr = client.GetStringAsync(CHALLANGES).Result;
  29.  
  30.           watch.Restart();                                                  // Start Konvertierung
  31.           ulong number = MyConverter.Str2Dec(numStr);
  32.           watch.Stop();
  33.           double timeToNumber = watch.Elapsed.TotalMilliseconds;
  34.  
  35.           watch.Restart();
  36.           string binary = MyConverter.Dec2Bin(number);
  37.           watch.Stop();                                                     // Ende Konvertierung
  38.           double timeToBinary = watch.Elapsed.TotalMilliseconds;
  39.  
  40.           string response = client.PostAsync(SOLUTIONS, new StringContent(
  41.             $@"{{ ""token"": ""{binary}"" }}",
  42.             Encoding.UTF8,
  43.             "application/json")).Result.Content.ReadAsStringAsync().Result;
  44.  
  45.           string message = $"Number: {number} ({timeToNumber}ms)\nBinary: {binary} ({timeToBinary}ms)\nResult: {response}\n";
  46.           WriteLine(message);
  47.         } while (ReadKey().Key != ConsoleKey.Escape);
  48.       } catch (Exception ex) {
  49.         WriteLine(ex);
  50.         ReadKey();
  51.       }
  52.     }
  53.   }
  54.  
  55.   internal static class MyConverter {
  56.  
  57.     public static ulong Str2Dec(string numStr) {
  58.       ulong number = 0;
  59.       ulong power = 1;
  60.       for (int i = numStr.Length - 1; i >= 0; i--) {
  61.         int num = numStr[i] - '0';
  62.         if (num >= 0 && num <= 9) {
  63.           number += (ulong)num * power;
  64.           power *= 10;
  65.         }
  66.       }
  67.  
  68.       return number;
  69.     }
  70.  
  71.     public static string Dec2Bin(ulong number) {
  72.       int count = (int)Ceiling(Log10(number) / Log10(2));                   // Anzahl der Bits bestimmen
  73.       char[] numbers = new char[count];
  74.  
  75.       ulong temp = number;
  76.       for (int i = count - 1; i >= 0; i--) {
  77.         numbers[i] = (char)('0' + (temp & 1));                              // Bit fΓΌr Position bestimmen
  78.         temp >>= 1;                                                         // Bitshift um eine Position (entspricht Division durch 2)
  79.       }
  80.  
  81.       return new string(numbers);
  82.     }
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement