Top.Mail.Ru
Ответы

Решите задачу по C#

Напишите программу, которая преобразовывает число из десятеричной системы в шестнадцатеричную.
using System;

public class MainClass
{
public static void Main()
{
string line = Console.ReadLine(); // ввод числа в десятеричной системе

int x = int.Parse(line);

string answer = x;

//Запишите тут Ваш код

Console.WriteLine(answer);
}
}

По дате
По рейтингу
Аватар пользователя
Знаток
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
 class DecimalToLetters 
    { 
        private readonly List<string> listText; 
        public DecimalToLetters(List<int> listValue)  
        { 
            listText = []; 
            for (int i = 0; i < listValue.Count; i++) 
            { 
                if (listValue[i] == 10) 
                { 
                    listText.Add("A"); 
                } 
                else 
                if (listValue[i] == 11) 
                { 
                    listText.Add("B"); 
                } 
                else 
                if (listValue[i] == 12) 
                { 
                    listText.Add("C"); 
                } 
                else 
                if (listValue[i] == 13) 
                { 
                    listText.Add("D"); 
                } 
                else 
                if (listValue[i] == 14) 
                { 
                    listText.Add("E"); 
                } 
                else 
                if (listValue[i] == 15) 
                { 
                    listText.Add("F"); 
                } 
                else 
                { 
                    listText.Add(listValue[i].ToString()); 
                } 
            } 
        } 
 
        public List<string> GetListHex() 
        { 
            listText.Reverse(); 
            return listText; 
        } 
    }
class DecimalToHexadecimal(int value) 
    { 
        private readonly int _value = value; 
        private const int hex = 16; 
         
 
        public string Convert() 
        { 
            List<int> listValue = []; 
             
            int myValue = _value; 
            int finalValue = 0; 
            int minValue = 0; 
 
            bool IsShare = true; 
 
            while(IsShare) 
            { 
                finalValue = myValue/hex; 
 
                if (myValue > 0) 
                { 
                    minValue = myValue - finalValue * hex; 
                    listValue.Add(minValue); 
                } 
                else 
                { 
                    IsShare = false; 
                } 
                myValue /= hex; 
            } 
            var letters = new DecimalToLetters(listValue); 
            string finalText = string.Join("", [.. letters.GetListHex()]); 
            return finalText; 
        } 
    }
class Program 
{ 
    private static void Main(string[] args) 
    { 
 
        Console.Write("Ввод числа в десятеричной системе: "); 
         
        try 
        { 
             int number = Convert.ToInt32(Console.ReadLine());
             DecimalToHexadecimal decimalToHexadecimal = new(number);
             Console.WriteLine(decimalToHexadecimal.Convert());
        } 
        catch (Exception ex) 
        { 
            Console.WriteLine(ex.ToString()); 
        } 
    } 
} 
Аватар пользователя
Знаток

Да