Jump to content

Need help with a C# Programm

Go to solution Solved by madknight3,

Is there no other way because I will need to get something so I can still use my Base system

the example is what is also in my code;

 

mZ1 = Convert.ToInt32(value, 2);

 

I'm not sure what you're looking for. Convert.ToSByte, and all other Convert methods will throw an OverflowException. They are all internally using a cast like how I showed you. For example, here's how Convert.ToSByte is implemented.

[CLSCompliant(false)]public static sbyte ToSByte(long value) {    if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));     Contract.EndContractBlock();    return (sbyte)value; } 

As you can see, the method does (sbyte)value, which is what I showed you, but it also does the overflow check which you don't want.

Hello guys,

 

so to start some background. Right now I am going to a kinda High school and need help with a program for beginners (that's what I think it is :D, expect the part were I get fucked) we are doing right now. What we need to do is to create a calculator with different actions to do. Now the actions are not really the problem till yet, the main problem is that he wants us to do a kind of Registry. For example if we type in the number x = 128 and we use the Registry Signed 8Bit that we get a overflow and it is -128 if I am correct what happens. 

 

 

So the question is how to do that :D

 

Well to begin with I tried it in a Console application just reading in a value, then doing this:

 

Int64 x = Convert.ToInt64(ReadLine());

 

well then I tried to change it for example to Signed 8bit, so I did this:

 

var y = Convert.ToIntSByte(x);                                  //I wrote var because this variable would be in an if() or switch(), so I can change it to the correct Type I want :D

 

 

Well that didn't work, so now I am here I hope you guys can help me. Code and stuff is down there if you got questions ask them, I know comments are in german but if you need it translated ask them as said :D

Btw I was looking to implement it in the class so I have less coding on the MainWindow!!

 

Hope you guys can help me out,

 

Regards Rocky

 

 

Pictures

Picture1:

Yes I just realized Unsigned 65Bit I changed it don't worry :D

952653a553936232d9fab014433730fb.png

 

SL and SR stand for Sift Right and Shift left

 

Picture2:

c65f450bad557e91b5b5a5e8f1a3d0f7.png

 

 

Code of Mainwindow.xaml

 

//Sry guys if its in German but if you have any question feel free to ask them and I will answer them

<Window x:Class="Calculator.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Calculator" Height="255" Width="588">    <Window.Resources>        <BooleanToVisibilityConverter x:Key="b2v" />        <!-- Wird verwendet um einen Button zu verdecken oder aufscheinen zu lassen-->        <!-- https://msdn.microsoft.com/de-de/library/system.windows.controls.booleantovisibilityconverter(v=vs.110).aspx -->    </Window.Resources>    <Grid>        <Button Name ="Add" Content="+" HorizontalAlignment="Left" Margin="207,130,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="Add_Click"/>        <Button Name ="Subb" Content="-" HorizontalAlignment="Left" Margin="262,130,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="Subb_Click"/>        <Button Name="Mul" Content="*" HorizontalAlignment="Left" Margin="317,130,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="Mul_Click"/>        <Button Name ="Div" Content="/" HorizontalAlignment="Left" Margin="372,130,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="Div_Click"/>        <Button Name ="Modu" Content="%" HorizontalAlignment="Left" Margin="207,155,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="Modu_Click"/>        <Button Name ="SL" Content="SL" HorizontalAlignment="Left" Margin="262,155,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="SL_Click" Visibility="{Binding IsChecked,ElementName=ArOp,Converter={StaticResource b2v}}"/>        <!--Hier wird der SL Button nur aufgedeckt, wenn Arathmetische Operationen ausgewählt ist!-->        <Button Name ="SR" Content="SR" HorizontalAlignment="Left" Margin="317,155,0,0" VerticalAlignment="Top" Width="50" Height="20" Click="SR_Click" Visibility="{Binding IsChecked,ElementName=ArOp,Converter={StaticResource b2v}}"/>        <TextBox Name ="TBe1" HorizontalAlignment="Left" Height="23" Margin="207,60,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>        <TextBox Name ="TBe2" HorizontalAlignment="Left" Height="23" Margin="207,88,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>        <TextBox Name ="TBerg" HorizontalAlignment="Left" Height="51" Margin="357,60,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>        <RadioButton Name ="Dez" Content="Dezimal" HorizontalAlignment="Left" Margin="482,60,0,0" VerticalAlignment="Top" Checked="Dez_Checked" IsChecked="True" GroupName="Zahlensysteme"/>        <RadioButton Name ="Hex" Content="Hexadezimal" HorizontalAlignment="Left" Margin="482,80,0,0" VerticalAlignment="Top" Checked="Hex_Checked" GroupName="Zahlensysteme" />        <RadioButton Name ="Bin" Content="Binär" HorizontalAlignment="Left" Margin="482,100,0,0" VerticalAlignment="Top" Checked="Bin_Checked" GroupName="Zahlensysteme" />        <RadioButton Name="ArOp" Content="Arithmetische Operationen" HorizontalAlignment="Left" Margin="20,60,0,0" VerticalAlignment="Top" IsChecked="True" GroupName="Rechenart" Checked="ArOp_Checked"/>        <RadioButton Name="LogOp" Content="Logische Operationen" HorizontalAlignment="Left" Margin="20,80,0,0" VerticalAlignment="Top" GroupName="Rechenart" Checked="LoOp_Checked"/>        <RadioButton Name="US8B" Content="Unsigned 8Bit" HorizontalAlignment="Left" Margin="20,10,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="US8B_Checked"/>        <RadioButton Name="S8B" Content="Signed 8Bit" HorizontalAlignment="Left" Margin="20,30,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="S8B_Checked"/>        <RadioButton Name="US16B" Content="Unsigned 16Bit" HorizontalAlignment="Left" Margin="133,10,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="US16B_Checked"/>        <RadioButton Name="S16B" Content="Signed 16Bit" HorizontalAlignment="Left" Margin="133,30,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="S16B_Checked"/>        <RadioButton Name="US32Bit" Content="Unsigned 32Bit" HorizontalAlignment="Left" Margin="257,10,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="US32Bit_Checked"/>        <RadioButton Name="S32Bit" Content="Signed 32Bit" HorizontalAlignment="Left" Margin="257,30,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="S32Bit_Checked"/>        <RadioButton Name="US64Bit" Content="Unsigned 65Bit" HorizontalAlignment="Left" Margin="381,10,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" Checked="US64Bit_Checked"/>        <RadioButton Name="S64Bit" Content="Signed 64Bit" HorizontalAlignment="Left" Margin="381,30,0,0" VerticalAlignment="Top" GroupName="RegisterTypen" IsChecked="True" Checked="S64Bit_Checked"/>        <Label Name="Equal" Content="=" HorizontalAlignment="Left" Margin="332,72,0,0" VerticalAlignment="Top" Width="20"/>    </Grid></Window> 

 

 

Code Mainwindow.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace Calculator{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        calc calc = new calc();             public char RA = 'A';                                                               //charakter zum Unterscheiden zwischen den beiden Rechenarten, Arithmetische Operationen und Logische Operationen        public string Registry = "S64B";        public MainWindow()        {            InitializeComponent();            TBerg.Text = "";            TBe1.Text = "0";            TBe2.Text = "0";        }        //Einlesen der beiden Eingaben        void Input()        {            calc.Z1 = TBe1.Text;            calc.Z2 = TBe2.Text;        }        //Ausgabe des Ergebnisses        void Output()        {            TBerg.Text = calc.Z3;        }        //Funktion bei betätigen des Add/AND Buttons -ADD...Addition        private void Add_Click(object sender, RoutedEventArgs e)        {            if(RA == 'A')                                                                   //Abfrage der Rechenart, ob es Arithmetisch Operationen sind            {                Input();                                                                    //Einlsen der Eingaben durch die Unterfunktion Input()                calc.Add();                                                                 //Aufrufen der Add() Funktion von Klasse calc                Output();                                                                   //Ausgabe des Ergebnisses durch die Unterfunktion Output()            }            else if(RA == 'L')                                                              //Abfrage der Rechenart, ob es Logische Operationen sind            {                try                {                    Input();                    calc.AND();                                                                 //Aufrufen der AND() Funktion von Klasse calc                    Output();                }                catch                {                    MessageBox.Show("One of the inputs is no Bit-pattern!");                }            }            else            {                MessageBox.Show("You need to select one of the 2 RadioButtons!!");      //Ausgabe eines Fehlers falls es einen Fehler gab, und die Rechenart nicht ausgewählt ist            }        }        //Funktion bei betätigen des Subb/Or Buttons -Subb...Subtraktion        private void Subb_Click(object sender, RoutedEventArgs e)        {            if (RA == 'A')            {                Input();                calc.Subb();                                                                //Aufrufen der Subb() Funktion von Klasse calc                Output();            }            else if (RA == 'L')            {                Input();                calc.OR();                                                                  //Aufrufen der OR() Funktion von Klasse calc                Output();            }            else            {                MessageBox.Show("You need to select one of the 2 RadioButtons!!");            }        }        //Funktion bei betätigen des Mul/XOR Buttons -Mul...Multiplikation         private void Mul_Click(object sender, RoutedEventArgs e)        {            if (RA == 'A')            {                Input();                calc.Mul();                                                                 //Aufrufen der Mul() Funktion von Klasse calc                Output();            }            else if (RA == 'L')            {                Input();                calc.XOR();                                                                 //Aufrufen der XOR() Funktion von Klasse calc                Output();            }            else            {                MessageBox.Show("You need to select one of the 2 RadioButtons!!");            }        }        //Funktion bei betätigen des Div/NOT Buttons -Div...Division         private void Div_Click(object sender, RoutedEventArgs e)        {            if (RA == 'A')            {                Input();                calc.Div();                                                                 //Aufrufen der Div() Funktion von Klasse calc                Output();            }            else if (RA == 'L')            {                Input();                calc.NOT();                                                                 //Aufrufen der NOT() Funktion von Klasse calc                Output();            }            else            {                MessageBox.Show("You need to select one of the 2 RadioButtons!!");            }        }        //Funktion bei betätigen des Modu/NEG Buttons -Modu... Modulo -NEG...2er Komplement        private void Modu_Click(object sender, RoutedEventArgs e)        {            if (RA == 'A')            {                Input();                calc.Modu();                                                                //Aufrufen der Modu() Funktion von Klasse calc                Output();            }            else if (RA == 'L')            {                Input();                calc.NEG();                                                                 //Aufrufen der NEG() Funktion von Klasse calc                Output();            }            else            {                MessageBox.Show("You need to select one of the 2 RadioButtons!!");            }        }        //Funktion bei betätigen des SL Buttons -SL...Shift Left        private void SL_Click(object sender, RoutedEventArgs e)        {            calc.mSC = true;                                                                //mSC auf true setzen -mSC...member Shift Click            Input();            calc.SL();                                                                      //Aufrufen der SL() Funktion von Klasse calc            Output();            calc.mSC = false;                                                               //mSC auf false setzen        }        //Funktion bei betätigen des SR Buttons -SR...Shift Right        private void SR_Click(object sender, RoutedEventArgs e)        {            calc.mSC = true;            Input();            calc.SR();                                                                      //Aufrufen der SR() Funktion von Klasse calc            Output();            calc.mSC = false;        }        //Funktion, wenn Dezimal ausgewählt ist        private void Dez_Checked(object sender, RoutedEventArgs e)        {                calc.Base = 10;        }        //Funktion, wenn Hexadezimal ausgewählt ist        private void Hex_Checked(object sender, RoutedEventArgs e)        {            calc.Base = 16;        }        //Funktion, wenn Binär ausgewählt        private void Bin_Checked(object sender, RoutedEventArgs e)        {            calc.Base = 2;        }        //Funktion, wenn Arithmetische Operationen ausgewählt ist        private void ArOp_Checked(object sender, RoutedEventArgs e)        {            RA = 'A';            Dez.IsChecked = true;            Add.Content = "+";            Subb.Content = "-";            Mul.Content = "*";            Div.Content = "/";            Modu.Content = "%";        }        //Funktion, wenn Logische Operationen ausgewählt ist        private void LoOp_Checked(object sender, RoutedEventArgs e)        {            RA = 'L';            Dez.IsChecked = true;            Add.Content = "AND";            Subb.Content = "OR";            Mul.Content = "XOR";            Div.Content = "NOT";            Modu.Content = "NEG";        }        private void US8B_Checked(object sender, RoutedEventArgs e)        {            Registry = "US8B";        }        private void S8B_Checked(object sender, RoutedEventArgs e)        {            Registry = "S8B";        }        private void US16B_Checked(object sender, RoutedEventArgs e)        {            Registry = "US16B";        }        private void S16B_Checked(object sender, RoutedEventArgs e)        {            Registry = "S16B";        }        private void US32Bit_Checked(object sender, RoutedEventArgs e)        {            Registry = "US32B";        }        private void S32Bit_Checked(object sender, RoutedEventArgs e)        {            Registry = "S32B";        }        private void US64Bit_Checked(object sender, RoutedEventArgs e)        {            Registry = "US64B";        }        private void S64Bit_Checked(object sender, RoutedEventArgs e)        {            Registry = "S64B";        }    }} 

 

 

Code Class Calculator

//Sry guys if its in German but if you have any question feel free to ask them and I will answer them

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Calculator{    class calc    {        private int mZ1, mZ2, mZ3;                                      //interne Member variablen für Z1,Z2 und Z3        private int mBase = 10;                                         //interne Member variable für Base        public bool mSC = false;                                        //Wird verwendet um zu erkennen ob der Shift Button verwendet worden ist        public int Base        {            get            {                return mBase;            }            set            {                if(value == 2 || value == 10 || value == 16)            //Abfrage ob die Base variable einer diesen Werte hat                {                    mBase = value;                }                else                {                    throw new Exception("Invalid base value");                          }            }        }        public string Z1        {            get            {                return Convert.ToString(mZ1, mBase);                    //Rückgabe der Member Variable mit dem richtigen Zahlensystem(Base)            }            set            {                mZ1 = Convert.ToInt32(value, mBase);                    //Einlesen der Variable mit dem Zahlensystem(Base) und Umwandlung in Integer            }        }        public string Z2        {            get            {                return Convert.ToString(mZ2, mBase);            }            set            {                if(mSC == true)                {                    mZ2 = Convert.ToInt32(value, 10);                }                else                {                    mZ2 = Convert.ToInt32(value, mBase);                }                            }        }        public string Z3        {            get            {                return Convert.ToString(mZ3, mBase).ToUpper();  //Rückgabe des Ergebniss mit dem richtigen Zahlensystem(Base) und gröer, falls bei Hex eine Kombination aus A,B,C,D,E oder F mit den Zahlen 0-9 rauskommen soll            }        }        //Addition der 2 übergebenden Zahlen        public void Add()                                               {            mZ3 = mZ1 + mZ2;        }        //Subtraktion der 2 übergebenden Zahlen        public void Subb()                                              {            mZ3 = mZ1 - mZ2;        }        //Multiplikation der 2 übergebenden Zahlen        public void Mul()                                               {            mZ3 = mZ1 * mZ2;        }        //Division der 2 übergebenden Zahlen        public void Div()                                               {            mZ3 = mZ1 / mZ2;        }        //Modulo der 2 übergebenden Zahlen        public void Modu()                                              {            mZ3 = mZ1 % mZ2;        }        //Shift Left Zahl1 um die Anzahl von Zahl2        public void SL()                                                {            mZ3 = mZ1 << mZ2;        }        //Shift Reft Zahl1 um die Anzahl von Zahl2        public void SR()                                                {            mZ3 = mZ1 >> mZ2;        }        //Bitweise AND-Abfrage        public void AND()                                               {            mZ3 = mZ1 & mZ2;        }        //Bitweise OR-Abfrage        public void OR()                                                {            mZ3 = mZ1 | mZ2;        }        //Bitweise XOR-Abfrage        public void XOR()                                               {            mZ3 = mZ1 ^ mZ2;        }        //Bitweise Negation der Bits        public void NOT()                                               {            mZ3 = ~mZ1;        }        //Negation der ganzen Zahl        public void NEG()                                               {            mZ3 = -mZ1;                                                 }    }} 

Link to comment
https://linustechtips.com/topic/461978-need-help-with-a-c-programm/
Share on other sites

Link to post
Share on other sites

You should put some thought into improving your design. You are doing this in WPF, you should really be implementing it in accordance with the MVVM design pattern.

Honestly what you have right now is nasty.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Is there no other way because I will need to get something so I can still use my Base system

the example is what is also in my code;

 

mZ1 = Convert.ToInt32(value, 2);

 

I'm not sure what you're looking for. Convert.ToSByte, and all other Convert methods will throw an OverflowException. They are all internally using a cast like how I showed you. For example, here's how Convert.ToSByte is implemented.

[CLSCompliant(false)]public static sbyte ToSByte(long value) {    if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));     Contract.EndContractBlock();    return (sbyte)value; } 

As you can see, the method does (sbyte)value, which is what I showed you, but it also does the overflow check which you don't want.

Link to post
Share on other sites

You should put some thought into improving your design. You are doing this in WPF, you should really be implementing it in accordance with the MVVM design pattern.

Honestly what you have right now is nasty.

Thats bad to hear, but we need to do it in WPF :(

Link to post
Share on other sites

I'm not sure what you're looking for. Convert.ToSByte, and all other Convert methods will throw an OverflowException. They are all internally using a cast like how I showed you. For example, here's how Convert.ToSByte is implemented.

[CLSCompliant(false)]public static sbyte ToSByte(long value) {    if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));     Contract.EndContractBlock();    return (sbyte)value; } 
As you can see, the method does (sbyte)value, which is what I showed you, but it also does the overflow check which you don't want.

Ok I understand you now. The thing was before that I used the Convert.ToXXY() Function to easily work with Hex and Bin, because I don't only have Dez so yeah, but will do that manually. Thx for the help

Link to post
Share on other sites

Thats bad to hear, but we need to do it in WPF :(

 

I think you may have missed the point a little bit. MVVM is a design pattern that complements WPF. It's about the separation of the View (UI), Model (data model or computational model) and the VIewModel (the layer of business logic or whatever orchestrates things). No layer 'knows' anything about any other layer and they interact via various abstraction methodologies.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×