Code C#: Nhập, xuất với mảng 1 chiều (Console)

Người đăng: chisenhungsuutam on Thứ Năm, 28 tháng 2, 2013


using System;
static void Main(string[] args){
    int[] a = new int[10];
    //nhap gia tri cho mang 1 chieu
    for (int i = 0; i < 10; i++){
        Console.Write("Nhap a[{0}]=", i);
        a[i] = int.Parse(Console.ReadLine());
    }
    //xuat mang 1 chieu
    for (int i = 0; i < 10; i++){
        Console.WriteLine("{0} ", a[i]);
    }
}
More about

Code C#: Tổng 2 số nguyên a, b nhập từ bàn phím (Console)

Người đăng: chisenhungsuutam


using System;
namespace Tong2so{
    class Program {
        static void Main(string[] args) {
            int a, b, kq;
            Console.WriteLine(" Nhập gia tri a: ");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine(" Nhập gia tri b: ");
            b = int.Parse(Console.ReadLine());
            kq = a + b;
            Console.WriteLine("kq=" + kq.ToString());
        }
    }
}
More about

Code C#: Giải phương trình bậc nhất (Console)

Người đăng: chisenhungsuutam


using System; 
namespace PhuongTrinhBacNhat{
    class Program {
        static void Main(string[] args) {
            Console.Write("Nhap he so a :");
            int a = int.Parse(Console.ReadLine());
            Console.Write("Nhap he so b :");
            int b = int.Parse(Console.ReadLine());
            if (a == 0)
                if (b == 0)
                    Console.WriteLine("Phuong trinh co vo so nghiem");
                else
                    Console.WriteLine("Phuong trinh vo nghiem ");
            else{
                int ketqua = -b / a;
                Console.WriteLine("Phuong trinh co nghiem la x = {0}",
                ketqua.ToString());
            }
            Console.ReadKey();
        }
    }
}
More about

Code C#: Code Examples sample source codes

Người đăng: chisenhungsuutam on Thứ Tư, 27 tháng 2, 2013


Center form window

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

  public class Form1 : System.Windows.Forms.Form {
    private Button myButton; 

    public Form1()   {
      this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
      this.ClientSize = new System.Drawing.Size(320, 260);
        
            this.StartPosition=FormStartPosition.CenterScreen;
      // CenterToScreen();

      myButton = new Button();
      myButton.Text = "Superman";
      myButton.Location = new System.Drawing.Point(69, 36);
      myButton.Size = new System.Drawing.Size(140, 45);

      Controls.Add(myButton);
    }
    static void Main() {
      Application.Run(new Form1());
    }
  }

More about

Code C#: Code Examples sample source codes

Người đăng: chisenhungsuutam


Get selected checkbox list items

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

public class Form1 : System.Windows.Forms.Form {
   private System.Windows.Forms.CheckedListBox chkListPossibleValues;
   private System.Windows.Forms.ListBox lstSelected;
   private System.Windows.Forms.Button btnMove;
   private System.ComponentModel.Container components=null;

   public Form1() {
      InitializeComponent();
      this.chkListPossibleValues.Items.Add("Ten");
   }

   private void InitializeComponent() {
      this.lstSelected = new System.Windows.Forms.ListBox();
      this.btnMove = new System.Windows.Forms.Button();
      this.chkListPossibleValues = new System.Windows.Forms.CheckedListBox();
      this.SuspendLayout();
  
      this.lstSelected.Location = new System.Drawing.Point(240, 12);
      this.lstSelected.Name = "lstSelected";
      this.lstSelected.Size = new System.Drawing.Size(150, 180);
      this.lstSelected.TabIndex = 1;

      this.btnMove.Location = new System.Drawing.Point(160, 70);
      this.btnMove.Name = "btnMove";
      this.btnMove.TabIndex = 3;
      this.btnMove.Text = "Move";
      this.btnMove.Click += new System.EventHandler(this.btnMove_Click);

      this.chkListPossibleValues.CheckOnClick = true;
      this.chkListPossibleValues.Items.AddRange(new object[] {"One", "Two", "Three",
                  "Four", "Five","Six","Seven", "Eight", "Nine"});
      this.chkListPossibleValues.Location = new System.Drawing.Point(8, 8);
      this.chkListPossibleValues.Name = "chkListPossibleValues";
      this.chkListPossibleValues.Size = new System.Drawing.Size(136, 184);
      this.chkListPossibleValues.TabIndex = 0;

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(380, 200);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnMove,
                                  this.lstSelected,  this.chkListPossibleValues});
      this.Name = "Form1";
      this.Text = "List Boxes";
      this.ResumeLayout(false);

      }

      static void Main() {
         Application.Run(new Form1());
      }

      private void btnMove_Click(object sender, System.EventArgs e) {
         if (this.chkListPossibleValues.CheckedItems.Count > 0) {
            this.lstSelected.Items.Clear();
            foreach (string item in this.chkListPossibleValues.CheckedItems) {
                this.lstSelected.Items.Add(item.ToString());
            }
            for (int i = 0; i < this.chkListPossibleValues.Items.Count; i++){
                 this.chkListPossibleValues.SetItemChecked(i, false);
            }     
         }
      }
}

More about

Code C#: Giải phương trình bậc 2 (Console)

Người đăng: chisenhungsuutam


/* Giải và biện luận phương trình bậc 2 (sử dụng ứng dụng Console) */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GiaiPT_bac2{
    class Program{
        static void Main(string[] args){
            float a, b, c;
            string xau;
            // Nhap a,b,c
            Console.WriteLine("\n Nhap he so: ");
            Console.Write("\n a= ");
            xau = Console.ReadLine();
            a = float.Parse(xau);
            Console.Write("\n b= ");
            xau = Console.ReadLine();
            b = float.Parse(xau);
            Console.Write("\n c= ");
            xau = Console.ReadLine();
            c = float.Parse(xau);
            // Giai va bien luan
            if (a == 0)
                if (b == 0)
                    if (c == 0)
                        Console.WriteLine("\n Phuong trinh vo so nghiem");
                    else
                        Console.WriteLine("\n Phuong trinh vo nghiem");
                else
                    Console.WriteLine("\n Phuong trinh co 1 nghiem x= {0}", c / b);
            else {
                float delta=b*b-4*a*c;
                if(delta<0)
                    Console.WriteLine("\n Phuong trinh vo nghiem");
                if(delta==0)
                    Console.WriteLine("\n Phuong trinh co nghiem kep x1=x2= {0}",-b/(2*a));
                if (delta > 0){
                    float x1, x2;
                    x1 = (-b - (float)Math.Sqrt(delta)) / (2 * a);
                    x2 = (-b + (float)Math.Sqrt(delta)) / (2 * a);
                    Console.WriteLine("\n Phuong trinh co 2 nghiem x1= {0}; x2= {1}", x1, x2);
                }
            }
        }
    }
}

More about