Sunday, 26 September 2021

Sum two number without arithmetic operator

Sum two number without arithmetic operator, Using C#


using System; 

namespace ConsoleApp1

{

    public class Program

    {

        static void Main()

        {

            int sum = SumWithoutArithmeticOprator(25, 7);

            Console.WriteLine($"Sum of two numbers is {sum}");

            Console.ReadLine();

        }

 

        private static int SumWithoutArithmeticOprator(int v1, int v2)

        {

            if (v2 == 0) return v1;

            if (v1 == 0) return v2;

            // Where v1 ^ v2: XOR bitwise calculation

            // v1 & v2: AND bitwise with carry

            return SumWithoutArithmeticOprator((v1 ^ v2), (v1 & v2) << 1);

        }

    }

}

 

 






Thursday, 23 September 2021

Binary Search Tree

 



Simple & easy way to create binary search tree in C# or .net Core



using System; 

namespace BinarySearchTree

{

    public class Program

    {

        static void Main()

        {

            Node root = null;

            BinaryTree binaryTree = new BinaryTree();

 

            root = binaryTree.Insert(root, 5);

            root = binaryTree.Insert(root, 4);

            root = binaryTree.Insert(root, 2);

            root = binaryTree.Insert(root, 3);

            root = binaryTree.Insert(root, 7);

            root = binaryTree.Insert(root, 6);

            root = binaryTree.Insert(root, 8);

 

            binaryTree.Traverse(root);

 

            Console.ReadLine();

        }

 

    }

 

    class Node

    {

        public int value;

        public Node left;

        public Node right;

    }

 

    class BinaryTree

    {

        internal Node Insert(Node root, int value)

        {

            if (root is null)

            {

                root = new Node

                {

                    value = value

                };

            }           

            else if (value < root.value)

            {

                root.left = Insert(root.left, value);

            }

            else

            {

                root.right = Insert(root.right, value);

            }

 

            return root;

        }

 

        public void Traverse(Node root)

        {

            if (root is null) return; 

            Traverse(root.left);

            Console.Write($"{root.value} ");

            Traverse(root.right);

        }

    }

}

 

 


C# Number double decimal convert into culture spacific or time zone

string value;

            System.Globalization.NumberStyles style;

            System.Globalization.CultureInfo culture;

            float number;

 

            value = "1.345,978";

            style = System.Globalization.NumberStyles.AllowDecimalPoint |

                    System.Globalization.NumberStyles.AllowThousands;

            culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");

            if (Single.TryParse(value, style, culture, out number))

                Console.WriteLine("Converted '{0}' to {1}.", value, number);

            else

                Console.WriteLine("Unable to convert '{0}'.", value);

 

            value = "1345,978";

            if (Single.TryParse(value, style, culture, out number))

                Console.WriteLine("Converted '{0}' to {1}.", value, number);

            else

                Console.WriteLine("Unable to convert '{0}'.", value);

 

            var convertBack = number.ToString(culture);


Tuesday, 28 February 2017

RTF to HTML to RTF converter

Hi,

I tried to find out the project/ code for RTF to HTML converter but no one create project/code for Universal windows application, now end of searching i have to create RTF-to-HTML-RTF converter for UWP.

DOWNLOAD Project
LINK : https://github.com/mhshry/RTF-to-HTML-Converter 

Feature in this project:

Bold Italic Underline
FontFamily
FontSize
Color red yellow


  • Bullet 1
  • bullet 2

  1. Number1
  2. Number2

Link



1. RichEditBox data displayed in below image.



 Input RTF:{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang16393{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil\fcharset0 Rage;}{\f2\fnil Segoe UI;}{\f3\fnil\fcharset2 Symbol;}}
{\colortbl ;\red0\green0\blue0;\red0\green128\blue0;\red255\green0\blue0;\red255\green255\blue0;\red0\green0\blue255;}
{\*\generator Riched20 10.0.10586}\viewkind4\uc1 
\pard\ltrpar\tx720\cf1\f0\fs24\lang1033 Hi,\par
\b Bold \b0\i Italic \ul\i0 Underline \ulnone\par
\f1 FontFamily\f0\par
\fs56 FontSize\fs24\par
\cf2 Color \cf3 red \cf4 yellow\cf1\par

\pard{\pntext\f3\'B7\tab}{\*\pn\pnlvlblt\pnf3\pnindent0{\pntxtb\'B7}}\ltrpar\tx720 Bullet 1\par
{\pntext\f3\'B7\tab}bullet 2\par

\pard 
{\pntext\f0\u-239?)\tab}{\*\pn\pnlvlbody\pnf0\pnindent0\pnstart1\pndecd{\pntxta)}}
\ltrpar\tx720 Number1\par
{\pntext\f0\u-238?)\tab}Number2\par

\pard\ltrpar\tx720\par
{\cf5\ul{\field{\*\fldinst{HYPERLINK "http://google.com"}}{\fldrslt{Link}}}}\f0\fs24\par

\pard\ltrpar\tx720\f2\lang16393\par
}

2. Full output with HTML text and Html Viewer

3. As given Text in RTF format display in WEb Broser.

Output HTML:<html> <p><span style="color: 000000";> <span style="font-family:  Segoe UI ";><font size=12> Hi,</p><p><strong> Bold </strong><em> Italic <u></em> Underline </u></p><p></font><span style="font-family:  Rage";> FontFamily<span style="font-family:  Segoe UI";></p><p><font size=28> FontSize</font></span></p><p></span><span style="color: 008000"> Color </span><span style="color: ff0000"> red </span><span style="color: ffff00"> yellow<span style="color: 000000 "></p><p>
<p><ul><li>Bullet 1</li><li>bullet 2</li></ul><p><ol> <li> Number1</li><li>Number2</ol><p></p><p></span><span style="color: 0000ff"><u><a href="http://google.com">Link</a></u></span><span style="font-family:  Segoe UI";><font size=12></p><p>
<p></span></p> </html>


Happy Coding :)

Friday, 21 October 2016

WPF MVVM design pattern Example

WPF MVVM design pattern Example:

Create Student class in Model Folder:
public class StudentModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
    }

Create ViewModel StudentViewModel in ViewModel Folder
public class StudentViewModel:INotifyPropertyChanged
    {
        public StudentViewModel()
        {
            PageName = "WPF Demo- MVVM";
        }
              
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string pageName;
        public string PageName
        {
            get { return pageName; }
            set { pageName = value; OnPropertyChanged("PageName"); }
        }

        private ObservableCollection<StudentModel> students;

        public ObservableCollection<StudentModel> Students
        {
            get { if (students == null) GetStudentData(); return students; }
            set { students = value; OnPropertyChanged("Students"); }
        }

        private void GetStudentData()
        {
            students = new ObservableCollection<StudentModel>
            {// Here we call APi data or Database Data
                new StudentModel {Id=1, Name="XYZ", Class="1st" },
                new StudentModel {Id=2, Name="XYZ123", Class="2nd" },
                new StudentModel {Id=3, Name="XYZ1234", Class="1st" },
                new StudentModel {Id=4, Name="XYZ123455", Class="2nd" }
            };
        }
       
    }
 Add StudentView page into the view folder
Now Add View Model in XAMl File

    <!--ViewMOdel Binding-->   
    <Window.DataContext>
        <VM:StudentViewModel/>
    </Window.DataContext>
<Window x:Class="Wpf2.View.StudentView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf2.View"
        xmlns:VM="clr-namespace:Wpf2.ViewModel"
        mc:Ignorable="d"
        Title="StudentView" Height="300" Width="300">
    <!--Namespace call of viewModel, VM is custom name -->

    <!--ViewMOdel Binding-->   
    <Window.DataContext>
        <VM:StudentViewModel/>
    </Window.DataContext>

    <Window.Resources>
        <!--Use in ListView-->
        <!--Also create User control with Dependancy property if data is more complex or resuse, style-->
        <DataTemplate x:Key="KeyStudentRecordDataTemplate">
            <Grid Background="#FFFAFFD7">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding Name}"/> <!--Default Zero index-->
                <TextBlock Grid.Column="1" Text="{Binding Class}"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
   
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Background="Gray">
            <TextBlock Text="{Binding PageName}" TextAlignment="Center" Style="{StaticResource KeyTextBlockStyle}"/> <!-- App.xaml->KeyTextBlockStyle-->
        </Grid>

        <ListView Grid.Row="1" ItemsSource="{Binding Students}" ItemTemplate="{StaticResource KeyStudentRecordDataTemplate}" HorizontalContentAlignment="Stretch"/>

    </Grid>

</Window>

Add Resource Style in App.xaml file
<Application.Resources>
        <Style x:Key="KeyTextBlockStyle" TargetType="TextBlock">
            <Setter Property="FontSize" Value="30"/>
            <Setter Property="Background" Value="Gray"/>
            <Setter Property="Foreground" Value="White"/>
        </Style>
    </Application.Resources>

OUtput

Happy Coding ..

Thanks