Kalkulator systemowy

0

Witam. Mam zrobic projekt z C#, którego temat brzmi: Kalkulator systemowy. Zamiana binarnej na dziesiętną, dziesiętnej na binarną, dziesiętną na szesnastkową i odwrotnie. Mam zamianę dziesiętnej na binarną w C++ ale nie wiem jak to zrobić by działało w C#. Oto kod z C++:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   int x, i, j, ok;
   int tab[256];
    
   printf("Wprowadz liczbe dziesietna: ");
   scanf("%d", &x);
    
   for(i=0; i<=255; i++)
   {
      tab[i] = 0;
   }
   
   i = 0;
   while(x > 0)
   {
      if(x%2 == 0) tab[i] = 0;
      if(x%2 == 1) tab[i] = 1;
      x = x/2;
      i++;
   }

   ok = 0;
   printf("Postac binarna liczby: ");
   for (j=255; j>=0; j--)
   {
       if(tab[j] == 1) ok = 1;
       if(ok == 1) printf("%d", tab[j]);
   }
   printf("\n\n");

   system("PAUSE");
   return 0; 
}

a ja próbowałem czegoś takiego w C#:

 public partial class Form1 : Form
    {
        int x, i, j, ok,y;
        int[] tab = new int[256]; 
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x = int.Parse(textBox1.Text);

            for (i = 0; i <= 255; i++)
            {
                tab[i] = 0;
            }

            i = 0;
            while (x > 0)
            {
                if (x % 2 == 0) tab[i] = 0;
                if (x % 2 == 1) tab[i] = 1;
                x = x / 2;
                i++;
            }

            ok = 0;
            //"Postac binarna liczby: ");
            for (j = 255; j >= 0; j--)
            {
                if (tab[j] == 1) ok = 1;

                 (ok == 1) //printf("%d", tab[j]);

                    richTextBox1.AppendText(" " + Convert.ToString(tab[y]) + "\n");
            } 

co robię źle?

0

1.Ta tablica jest tutaj niepotrzebna.
2.Masz błąd składni przy (ok == 1) //printf("%d", tab[j]); (wcięło if).

0

Faktycznie wcięło if, które przed momentem dołożyłem i wygląda to teraz tak że konwertuje bez problemu. Jedno z głowy....teraz muszę zrobić żeby działało to też w drugą stronę i z Hex'em

1

String z wartością hehsadecymalną na integer:

int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

Integer na string z reprezentacją binarną:

Convert.ToString(5, 2); // base in {2, 8, 10, 16}
0

Wrrr...wiem, że to pewnie błahe ale jak zrobić by mi z textBoxa pobierało ciąg znaków/string (w moim wypadku kod szesnastkowy) i zapisywało do jakiejś zmiennej?

1

jakaśZmienna = textBox.Text;

1

String s=textBox1.Text;

0

Dzięki wielkie :)

0

Dobra projekt prawie skończony. Zamiana dec/bin bin/dec dec/hex hex/dec już zrobiona. Ale mam w zadaniach do projektu jeszcze coś czego nie kumam zupełnie, mianowicie:

(...)
3. przycisk 3 ma zamienić liczbę dziesiętną na liczbę o kodzie (od 3 do 9)
(...)
6. przycisk opcji CheckBox (dla dziesiętnych) pozwala wybrać system konwersji

O co może chodzić?

Z tym CheckBox'em podejrzewam że mam wybrać poprzez zaznaczenie czy chcę konwersję dec/bin bin/dec.Ale po co by to było skoro już mam to zrobione i przypisane do każdego buttona z osobna?I dlaczego dla dziesiętnych?

0

Po kiego tak sobie życie komplikować? Jak już masz więcej niż dwa przeliczenia to zrób to sobie uniwersalnie:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SysConv
{
    public partial class SysConv:Form
    {
        public SysConv()
        {
            InitializeComponent();
        }

        private void InpBox_TextChanged(object sender,EventArgs e)
        {
            StringBuilder r=new StringBuilder();
            String s=InpBox.Text,dig="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int f=(int)InpSys.Value,t=(int)OutSys.Value;
            for(int i=0;i<s.Length;++i)
            {
                int p=dig.IndexOf(char.ToUpper(s[i]));
                if ((p < 0) || (p >= f))
                {
                    OutBox.Text = "Error";
                    return;
                }
                for(int k=r.Length-1;k>=0;--k)
                {
                    p+=dig.IndexOf(r[k])*f;
                    int n=p/t;
                    r[k]=dig[p-n*t];
                    p=n;
                }
                while(p>0)
                {
                    int n=p/t;
                    r.Insert(0,dig[p-n*t]);
                    p=n;
                }
            }
         OutBox.Text=r.ToString();
        }
    }
}
namespace SysConv
    {
    partial class SysConv
        {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components=null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
            {
        if(disposing&&(components!=null))
            {
        components.Dispose();
            }
        base.Dispose(disposing);
            }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
            {
                this.InpBox = new System.Windows.Forms.TextBox();
                this.InpSys = new System.Windows.Forms.NumericUpDown();
                this.OutSys = new System.Windows.Forms.NumericUpDown();
                this.OutBox = new System.Windows.Forms.TextBox();
                ((System.ComponentModel.ISupportInitialize)(this.InpSys)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.OutSys)).BeginInit();
                this.SuspendLayout();
                // 
                // InpBox
                // 
                this.InpBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.InpBox.Location = new System.Drawing.Point(12, 12);
                this.InpBox.Name = "InpBox";
                this.InpBox.Size = new System.Drawing.Size(790, 20);
                this.InpBox.TabIndex = 0;
                this.InpBox.Text = "255";
                this.InpBox.TextChanged += new System.EventHandler(this.InpBox_TextChanged);
                // 
                // InpSys
                // 
                this.InpSys.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
                this.InpSys.Location = new System.Drawing.Point(813, 12);
                this.InpSys.Maximum = new decimal(new int[] {
            36,
            0,
            0,
            0});
                this.InpSys.Minimum = new decimal(new int[] {
            2,
            0,
            0,
            0});
                this.InpSys.Name = "InpSys";
                this.InpSys.Size = new System.Drawing.Size(43, 20);
                this.InpSys.TabIndex = 1;
                this.InpSys.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
                this.InpSys.Value = new decimal(new int[] {
            10,
            0,
            0,
            0});
                this.InpSys.ValueChanged += new System.EventHandler(this.InpBox_TextChanged);
                // 
                // OutSys
                // 
                this.OutSys.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
                this.OutSys.Location = new System.Drawing.Point(813, 38);
                this.OutSys.Maximum = new decimal(new int[] {
            36,
            0,
            0,
            0});
                this.OutSys.Minimum = new decimal(new int[] {
            2,
            0,
            0,
            0});
                this.OutSys.Name = "OutSys";
                this.OutSys.Size = new System.Drawing.Size(43, 20);
                this.OutSys.TabIndex = 3;
                this.OutSys.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
                this.OutSys.Value = new decimal(new int[] {
            2,
            0,
            0,
            0});
                this.OutSys.ValueChanged += new System.EventHandler(this.InpBox_TextChanged);
                // 
                // OutBox
                // 
                this.OutBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.OutBox.Location = new System.Drawing.Point(12, 38);
                this.OutBox.Name = "OutBox";
                this.OutBox.ReadOnly = true;
                this.OutBox.Size = new System.Drawing.Size(790, 20);
                this.OutBox.TabIndex = 2;
                // 
                // SysConv
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(868, 70);
                this.Controls.Add(this.OutSys);
                this.Controls.Add(this.OutBox);
                this.Controls.Add(this.InpSys);
                this.Controls.Add(this.InpBox);
                this.Name = "SysConv";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.InpSys)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.OutSys)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();

            }

        #endregion

        private System.Windows.Forms.TextBox InpBox;
        private System.Windows.Forms.NumericUpDown InpSys;
        private System.Windows.Forms.NumericUpDown OutSys;
        private System.Windows.Forms.TextBox OutBox;
        }
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1