Lập trình chương trình máy tính bỏ túi - Phần 2

Bài tập lập trình

0.0 (0 đánh giá)
Tạo bởi Kteam Cập nhật lần cuối 2 năm trước 24.404 lượt xem 12 bình luận
Tác giả/Dịch giả: Kteam
Học nhanh

Danh sách bài học

Lập trình chương trình máy tính bỏ túi - Phần 2

 

Yêu cầu bài toán

Tiếp tục chương trình LẬP TRÌNH MÁY TÍNH BỎ TÚI. Nhưng tinh chỉnh tốt hơn đó là: Có thể tiếp tục cộng trừ nhân chia từ kết quả vừa tính.

Yêu cầu: Viết chương trình với mỗi trường hợp sử dụng IF – ELSESWITCH CASE.


Mục tiêu

Làm quen với Cấu trúc rẽ nhánh if – else Cấu trúc rẽ nhánh Switch case.


Hướng dẫn

Bài tập mang tính tham khảo, hỗ trợ các bạn làm quen và luyện tập với các bài toán lập trình từ cơ bản đến nâng cao trong C#. 

Để đảm bảo kiến thức về bài tập này, bạn nên tham khảo bài: 

Bài tập sẽ được hướng dẫn chi tiết qua các Live Stream tương tác hằng ngày tại Channel

Youtube.com/Kteam

Kteam khuyến khích các bạn tự phân tích đề bài > tự giải bài toán > debug để kiểm tra kết quả và fix lỗi trong quá trình giải. Sau đó, bạn có thể tham khảo source code mẫu hoặc video hướng dẫn để hoàn chỉnh bài tập. 

Để được hỗ trợ tốt nhất, bạn có thể đặt câu hỏi ở phần BÌNH LUẬN bên dưới bài viết hoặc ở mục HỎI & ĐÁP.

 

Kết luận

Bạn có thể củng cố kiến thức C# từ các khóa học tại LẬP TRÌNH C#.NET với rất nhiều khóa học từ cơ bản đến nâng cao, kèm theo các dự án thực tế như làm game, làm phần mềm quản lý.

Hoặc tìm hiểu thêm các bài tập khác trong khóa BÀI TẬP LẬP TRÌNH.

Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận hoặc góp ý của bạn để phát triển bài viết tốt hơn. Đừng quên: “ Luyện tập – Thử thách – Không ngại khó!


Tải xuống

Tài liệu

Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Lập trình chương trình máy tính bỏ túi - Phần 2 dưới dạng file PDF trong link bên dưới.

Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com

Đừng quên likeshare để ủng hộ Kteam và tác giả nhé!


Thảo luận

Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.

Tác giả/Dịch giả

Khóa học

Bài tập lập trình

Bài tập lập trình

Đánh giá

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
minhtan23 đã bình luận khoảng 1 năm trước

using System.Text;

namespace HocCS
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region
            Console.OutputEncoding = Encoding.UTF8;

            bool checkNumberA, checkNumberB, checkPhepTinh;
            float numberA, numberB;
            int phepTinh;
            float Result = 0;
            bool checkExit = true;
            string[] arrayPhepTinh = { "Cộng", "Trừ", "Nhân", "Chia", "Chia Dư", "Chia Nguyên" };
            string[] arrayDau = { " + ", " - ", " x ", " / ", " % ", " / " };

            do
            {
                Console.WriteLine("----------------Máy Tính Bỏ Túi------------------");
                Console.WriteLine("\nChọn phép tính bằng các số đã định sẵn!");
                Console.WriteLine("Nếu muốn thoát vui lòng nhập số (7)");
                Console.WriteLine("Phép Cộng(1), Trừ(2), Nhân(3), Chia(4), Chia Dư(5), Chia Nguyên(6)");

                //Chọn phép tính 
                do
                {
                    Console.Write("Vui lòng nhập số tương ứng: ");
                    string strPhepTinh = Console.ReadLine();
                    checkPhepTinh = int.TryParse(strPhepTinh, out phepTinh);
                    
                    if (!checkPhepTinh || phepTinh > 7 || phepTinh < 1)
                    {
                        Console.WriteLine("Số vừa nhấn không đúng! Vui lòng nhấn đúng!\n");
                    }
                    if (phepTinh == 7)
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Cảm ơn bạn đã sử dụng chương trình!");
                        Console.WriteLine("Chương trình sẽ tự thoát sau 5s");
                        Thread.Sleep(5000); //Dừng màn hình trong 5000 mili giây = 5s 
                        Environment.Exit(0); //Lệnh thoát
                    }
                } while (!checkPhepTinh || phepTinh > 7 || phepTinh < 1);
                Console.WriteLine("\nPhép tính bạn chọn là: Phép {0}({1})", arrayPhepTinh[phepTinh - 1], phepTinh);

                //Nhập giá trị A,B
                do
                {
                    Console.Write("\nMời Bạn Nhập Giá Trị A: ");
                    string strNumberA = Console.ReadLine();
                    checkNumberA = float.TryParse(strNumberA, out numberA);
                    Console.Write("Mời Bạn Nhập Giá Trị B: ");
                    string strNumberB = Console.ReadLine();
                    checkNumberB = float.TryParse(strNumberB, out numberB);

                    if (!checkNumberA || !checkNumberB)
                    {
                        Console.WriteLine("Số vừa nhập không đúng! Vui lòng chỉ nhập số!");
                    }

                } while (!checkNumberA || !checkNumberB);


                // Phép Tính và kết quả phép tính 
                switch (phepTinh)
                {
                    case 1:
                        Result = numberA + numberB;
                        break;
                    case 2:
                        Result = numberA - numberB;
                        break;
                    case 3:
                        Result = numberA * numberB;
                        break;
                    case 4:
                        Result = numberA / numberB;
                        break;
                    case 5:
                        Result = numberA % numberB;
                        break;
                    case 6:
                        float Result2= (numberA / numberB)-(1/2);// Trừ 1/2 vì ép sang kiểu int làm tròn
                        Result = Convert.ToInt32(Result2);//Ép kiểu float sang int
                        break;
                }
                Console.WriteLine("\nKết quả của phép tính {0}:", arrayPhepTinh[phepTinh - 1]);
                Console.WriteLine(numberA + arrayDau[phepTinh - 1] + numberB + " = " + Result);

                //Tiếp tục hoặc thoát chương trình
                Console.WriteLine("\nBạn có muốn tính phép khác hay thoát chương trình!");
                bool checkStr;
                string strA;
                do
                {
                    Console.WriteLine("Tiếp tục nhập (Yes) hoặc (Y). Để thoát nhập (No) hoặc (N) ");
                    strA = Console.ReadLine();
                    //Viết thường,xóa khoảng trắng, tab ký tự vừa nhập
                    strA = strA.ToLower();
                    strA = strA.Replace(" ", "");
                    strA = strA.Replace("\t", "");

                    //Kiểm tra xem có nhập đúng hay không
                    if (strA == "yes" || strA == "y" || strA == "no" || strA == "n")
                    {
                        checkStr = false;
                    }
                    else checkStr = true;
                } while (checkStr);

                //Thoát chương trình
                if (strA == "no" || strA == "n")
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Cảm ơn bạn đã sử dụng chương trình!");
                    Console.WriteLine("Chương trình sẽ tự thoát sau 5s");
                    checkExit = false;
                    Thread.Sleep(5000); //Dừng màn hình trong 5000 mili giây = 5s 
                }
            } while (checkExit);
            //Lệnh thoát
            Environment.Exit(0);

            #endregion
        }
    }
}

Nhut Le đã bình luận 3 năm trước
using System;
using System.Collections.Generic;
using System.Linq;

namespace _20
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            void WrongNumberFormat() { Console.WriteLine("Số nhập vào phải là một số nguyên hoặc số thực."); }
            void ShowResult(string operationChoice, float aNum, float bNum, float result, string operation) { Console.Write("Phép toán đã lựa chọn: {0}. Bấm để cộng tiếp hoặc bỏ trống và enter để trở về.\nKết quả: {1} {4} {2} = {3}", operationChoice, aNum, bNum, result, operation); }
            float aNum = new();
            float bNum = new();
            (float, string) Operator(float a, float b, string op)
            {
                float result = 0;
                if (op == "+")
                    result = a + b;
                else if (op == "-")
                    result = a - b;
                else if (op == "*")
                    result = a * b;
                else if (op == "/")
                    result = a / b;
                else if (op == "%")
                    result = a % b;
                return (result, op);
            }
            float resultStep1 = 0;
            while (true)
            {
                string[] optionList = new string[7] { "Cộng 2 số", "Trừ 2 số", "Nhân 2 số", "Chia nguyên 2 sô", "Chia dư 2 số", "Chia 2 số", "Thoát" };
                string[] operationChoice = new string[6] { "+", "-", "*", "/", "%", "/" };
                int i = 0;
                Console.Clear();
                foreach (string optionValue in optionList)
                { Console.Write("[{0}]" + optionValue.ToString() + "    ", i + 1); i++; }
                Console.Write("\nLựa chọn =>>>");
                bool getChoiceSuccess = int.TryParse(Console.ReadLine(), out int choice);
                if (!getChoiceSuccess || choice <= 0 || choice > 7)
                {
                    Console.WriteLine("Lựa chọn của bạn không hợp lệ. Chỉ chấp nhận số nguyên dương từ 1-7\nNhấn phím bất kì để thử lại.");
                    Console.ReadKey();
                    continue;
                }
                if (choice == 7) break;
                while (true)
                {
                    Console.Write("Nhập vào số a\n=>>>");
                    bool getASuccess = float.TryParse(Console.ReadLine(), out aNum);
                    if (!getASuccess)
                    {
                        WrongNumberFormat();
                        continue;
                    }
                getB:
                    Console.Write("Nhập vào số b\n=>>>");
                    bool getBsuccess = float.TryParse(Console.ReadLine(), out bNum);
                    if (!getBsuccess)
                    {
                        WrongNumberFormat();
                        goto getB;
                    }
                    break;
                }
                Console.Clear();
                float result = new();

                for (int j = 1; j < 7; j++)
                {
                    if (choice == j)
                    {
                        if (choice == 4)
                        {
                            (resultStep1, string operation) = Operator(aNum, bNum, operationChoice[j - 1]);
                            ShowResult(optionList[choice - 1], aNum, bNum, (int)(resultStep1), operation);
                        }
                        else
                        {
                            (resultStep1, string operation) = Operator(aNum, bNum, operationChoice[j - 1]);
                            ShowResult(optionList[choice - 1], aNum, bNum, resultStep1, operation);
                        }
                    }
                }
                while (true)
                {
                    string keepCalculate = Console.ReadLine();
                    if (keepCalculate == null || keepCalculate.Length < 2)
                        break;
                    string operatorValue = keepCalculate.Substring(0, 1);
                    if (operatorValue != "+" && operatorValue != "-" && operatorValue != "*" && operatorValue != "/")
                        Console.Write("Toán tử bạn vừa nhập không chính xác, vui lòng nhập lại.");
                    bool addValueSuccess = float.TryParse(keepCalculate.Substring(1, keepCalculate.Length - 1), out float addValue);
                    if (addValueSuccess)
                    {
                        (float result2,string operator2) = Operator(resultStep1, addValue, operatorValue);
                        Console.Write("={3}", resultStep1,operatorValue,addValue,result2);
                        resultStep1 = result2;
                    }
                    else
                        WrongNumberFormat();
                }
            }

        }
    }
}

Mình thấy có bạn ở dưới viết cái bài này mà cả 400 dòng code, sợ thật :')

xxxts2txxx đã bình luận 4 năm trước
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bai21
{
    class Program
    {
        static void Main(string[] args)
        {
            // lập trình máy tính cầm tay
            Console.InputEncoding = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;
            // Done là hoàn thành, nếu false thì vào vòng lặp. true thì có nghĩa không vào vòng lặp hoặc hoàn thành vòng lặp vô tận
            bool BoolLuaChonSoLan1 = true;
            bool BoolLuaChonSoLan2 = false;
            bool DoneGame = false;
            string PhepTinh = "";
            double SoThuNhat=0;
            double SoThuHai=0;
            string BieuThuc = "";
            double kq =0;

            while (!DoneGame)
            {
                PhepTinh = LuaChonPhepTinh();
                Console.WriteLine(string.Format("Bạn chọn phép tính {0}", PhepTinh));
                // Đây là trường hợp chạy lần đầu
                if (BoolLuaChonSoLan1)
                {
                    List<double> KQLan1 = LuaChonSoLan1();
                    SoThuNhat = KQLan1[0];
                    SoThuHai = KQLan1[1];
                }


                // Đây là trường hợp chạy lần 2
                if (BoolLuaChonSoLan2)
                {
                    List<double> KQLan2 = LuaChonSoLan2(kq);
                    SoThuNhat = KQLan2[0];
                    SoThuHai = KQLan2[1];
                }

               kq = KetQuaPhepTinh(SoThuNhat, PhepTinh, SoThuHai);
                    Console.Write("Chọn 1 nếu muôn tính tiếp, còn không muốn tính nữa thì bấm gì cũng được: ");
                    string ChoiTiep = Console.ReadLine();
                    if (ChoiTiep == "1")
                    {
                        DoneGame = false;
                        BoolLuaChonSoLan1 = false;
                        BoolLuaChonSoLan2 = true;
                    }
                    else
                    {
                        DoneGame = true;
                    }
                
            }
            Console.ReadKey();
        }
        static string LuaChonPhepTinh()
        {
            string PhepTinh="";
            bool DoneVongLap = false;
            while (!DoneVongLap)
            {
                Console.Write("Nhập lựa chọn của bạn (+, -, *, /): ");
                PhepTinh = Console.ReadLine();
                if (PhepTinh == "+" || PhepTinh == "-" || PhepTinh == "*" || PhepTinh == "/" +
                    "")
                {
                    DoneVongLap = true;
                }
            }
            return PhepTinh;
        }
        static List<double> LuaChonSoLan1()
        {
            double SoThuNhat = 0;
            double SoThuHai = 0;
            bool DoneVongLap = false;
            while (!DoneVongLap)
            {
                try
                {
                    Console.Write("Nhập số thứ nhất: ");
                    SoThuNhat = double.Parse(Console.ReadLine());
                    Console.Write("Nhập số thứ hai: ");
                    SoThuHai = double.Parse(Console.ReadLine());
                    DoneVongLap = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Nhập lại số");
                }
            }

            return new List<double>() { SoThuNhat, SoThuHai };
        }
        static List<double> LuaChonSoLan2(double kq)
        {
            double SoThuNhat = 0;
            double SoThuHai = 0;
            bool DoneVongLap = false;
            while (!DoneVongLap)
            {
                try
                {
                    SoThuNhat = kq;
                    Console.Write("Nhập số: ");
                    SoThuHai = Int32.Parse(Console.ReadLine());
                    DoneVongLap = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Nhập lại số");
                }
            }

            return new List<double>() { SoThuNhat, SoThuHai };
        }
        static double KetQuaPhepTinh(double SoThuNhat, string PhepTinh, double SoThuHai)
        {
            DataTable dt = new DataTable();
            string BieuThuc = SoThuNhat.ToString() + PhepTinh + SoThuHai.ToString();
            // Ép kết quả từ object trang string để dễ ép sang double hơn.
            string ketqua = dt.Compute(BieuThuc, "").ToString();
            double kq;
            double.TryParse(ketqua, out kq);
            Console.WriteLine("Kết quả của phép tính {0} là: {1} ", BieuThuc, kq);
            return kq;
        }
    }
}

 

Le Rachitique đã bình luận 4 năm trước
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bai_tap_lap_trinh_4
{
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Unicode;
            
        

            while (true)
            {
                Console.Clear();
                Start();
                var select = Console.ReadKey();
                if (select.Key == ConsoleKey.D1)
                {
                    Console.WriteLine();
                    Tong();
                }
                else if (select.Key == ConsoleKey.D2)
                {
                    Console.WriteLine();
                    Hieu();
                }
                else if (select.Key == ConsoleKey.D3)
                {
                    Console.WriteLine();
                    Tongz();
                }
                else if (select.Key == ConsoleKey.Escape)
                {
                    break;
                }
            }

            //Console.ReadKey();
        }

        static void Tong()
        {
            Start:
            Console.Clear();
            Console.WriteLine("Chương trình tính tổng 2 số");
            Console.WriteLine("Nhấn A để lấy kết quả vừa tính");
            Console.WriteLine("Nhấn ESC để trở lại");

            ConsoleKey[] arrKey = { ConsoleKey.D0, ConsoleKey.D1, ConsoleKey.D2, ConsoleKey.D3, ConsoleKey.D4, 
                                      ConsoleKey.D5, ConsoleKey.D6, ConsoleKey.D7, ConsoleKey.D8, ConsoleKey.D9 };
            string a = "0";
            string b = "0";
            double Ans = 0;
            while (true)
            {
                var typeA = Console.ReadKey(true);
                if (Array.IndexOf(arrKey, typeA.Key) > -1)
                {
                     Console.Write(typeA.KeyChar);
                     a = a + typeA.KeyChar;
                }
                else if (typeA.Key == ConsoleKey.Subtract && a == "0")
                {
                    Console.Write(typeA.KeyChar);
                    a = typeA.KeyChar + a;                    
                }
                else if (typeA.Key == ConsoleKey.Add)
                {
                    Console.Write(" " + typeA.KeyChar + " ");
                    while (true)
                    {
                        var typeB = Console.ReadKey(true);
                        if (Array.IndexOf(arrKey, typeB.Key) > -1)
                        {
                            Console.Write(typeB.KeyChar);
                            b = b + typeB.KeyChar;
                        }
                        else if (typeB.Key == ConsoleKey.Subtract && b == "0")
                        {
                            Console.Write(typeB.KeyChar);
                            b = typeB.KeyChar + b;
                        }
                        else if ((typeB.Key == ConsoleKey.OemPlus))
                        {
                            Console.Write(" " + typeB.KeyChar + " ");
                            Ans = double.Parse(a) + double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        else if (typeB.Key == ConsoleKey.A && b == "0")
                        {
                            Console.Write("Ans<{0}> = ", Ans);
                            b = Ans.ToString();
                            Ans = double.Parse(a) + double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        if (typeB.Key == ConsoleKey.Escape)
                        {
                            goto Start;
                        }
                    }                    
                }
                else if (typeA.Key == ConsoleKey.A && a == "0")
                {
                    Console.Write("Ans<{0}> + ",Ans);
                    a = Ans.ToString();
                    while (true)
                    {
                        var typeB = Console.ReadKey(true);
                        if (Array.IndexOf(arrKey, typeB.Key) > -1)
                        {
                            Console.Write(typeB.KeyChar);
                            b = b + typeB.KeyChar;
                        }
                        else if (typeB.Key == ConsoleKey.Subtract && b == "0")
                        {
                            Console.Write(typeB.KeyChar);
                            b = typeB.KeyChar + b;
                        }
                        else if ((typeB.Key == ConsoleKey.OemPlus))
                        {
                            Console.Write(" " + typeB.KeyChar + " ");
                            Ans = double.Parse(a) + double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        else if (typeB.Key == ConsoleKey.A && b == "0")
                        {
                            Console.Write("Ans<{0}> = ", Ans);
                            b = Ans.ToString();
                            Ans = double.Parse(a) + double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        if (typeB.Key == ConsoleKey.Escape)
                        {
                            goto Start;
                        }
                    }
                    
                }

                if (typeA.Key==ConsoleKey.Escape)
                {
                    break;
                }
                
            }


        }

        static void Hieu()
        {
        Start:
            Console.Clear();
            Console.WriteLine("Chương trình tính hiệu 2 số");
            Console.WriteLine("Nhấn A để lấy kết quả vừa tính");
            Console.WriteLine("Nhấn ESC để trở lại");

            ConsoleKey[] arrKey = { ConsoleKey.D0, ConsoleKey.D1, ConsoleKey.D2, ConsoleKey.D3, ConsoleKey.D4, 
                                      ConsoleKey.D5, ConsoleKey.D6, ConsoleKey.D7, ConsoleKey.D8, ConsoleKey.D9 };
            string a = "0";
            string b = "0";
            double Ans = 0;
            while (true)
            {
                var typeA = Console.ReadKey(true);
                if (Array.IndexOf(arrKey, typeA.Key) > -1)
                {
                    Console.Write(typeA.KeyChar);
                    a = a + typeA.KeyChar;
                }
                else if (typeA.Key == ConsoleKey.Subtract && a == "0")
                {
                    Console.Write(typeA.KeyChar);
                    a = typeA.KeyChar + a;
                }
                else if (typeA.Key == ConsoleKey.Subtract)
                {
                    Console.Write(" " + typeA.KeyChar + " ");
                    while (true)
                    {
                        var typeB = Console.ReadKey(true);
                        if (Array.IndexOf(arrKey, typeB.Key) > -1)
                        {
                            Console.Write(typeB.KeyChar);
                            b = b + typeB.KeyChar;
                        }
                        else if (typeB.Key == ConsoleKey.Subtract && b == "0")
                        {
                            Console.Write(typeB.KeyChar);
                            b = typeB.KeyChar + b;
                        }
                        else if ((typeB.Key == ConsoleKey.OemPlus))
                        {
                            Console.Write(" " + typeB.KeyChar + " ");
                            Ans = double.Parse(a) - double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        else if (typeB.Key == ConsoleKey.A && b == "0")
                        {
                            Console.Write("Ans<{0}> = ", Ans);
                            b = Ans.ToString();
                            Ans = double.Parse(a) - double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        if (typeB.Key == ConsoleKey.Escape)
                        {
                            goto Start;
                        }
                    }
                }
                else if (typeA.Key == ConsoleKey.A && a == "0")
                {
                    Console.Write("Ans<{0}> - ", Ans);
                    a = Ans.ToString();
                    while (true)
                    {
                        var typeB = Console.ReadKey(true);
                        if (Array.IndexOf(arrKey, typeB.Key) > -1)
                        {
                            Console.Write(typeB.KeyChar);
                            b = b + typeB.KeyChar;
                        }
                        else if (typeB.Key == ConsoleKey.Subtract && b == "0")
                        {
                            Console.Write(typeB.KeyChar);
                            b = typeB.KeyChar + b;
                        }
                        else if ((typeB.Key == ConsoleKey.OemPlus))
                        {
                            Console.Write(" " + typeB.KeyChar + " ");
                            Ans = double.Parse(a) - double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        else if (typeB.Key == ConsoleKey.A && b == "0")
                        {
                            Console.Write("Ans<{0}> = ", Ans);
                            b = Ans.ToString();
                            Ans = double.Parse(a) - double.Parse(b);
                            Console.WriteLine(Ans);
                            a = "0";
                            b = "0";
                            break;
                        }
                        if (typeB.Key == ConsoleKey.Escape)
                        {
                            goto Start;
                        }
                    }

                }

                if (typeA.Key == ConsoleKey.Escape)
                {
                    break;
                }

            }


        }

        static void Tongz()
        {

            Console.Clear();
            Console.WriteLine("Chương trình tính tổng và hiệu n số");
            Console.WriteLine("Nhấn A để lấy kết quả vừa tính");
            Console.WriteLine("Nhấn ESC để trở lại");

            ConsoleKey[] arrKey = { ConsoleKey.D0, ConsoleKey.D1, ConsoleKey.D2, ConsoleKey.D3, ConsoleKey.D4, 
                                      ConsoleKey.D5, ConsoleKey.D6, ConsoleKey.D7, ConsoleKey.D8, ConsoleKey.D9 };
            string a = "0";
            double Ans = 0;
            ArrayList arr = new ArrayList();
            char opt = '+';

            while (true)
            {
                var typeA = Console.ReadKey(true);
                if (Array.IndexOf(arrKey, typeA.Key) > -1)
                {
                    Console.Write(typeA.KeyChar);
                    a = a + typeA.KeyChar;
                }
                else if (typeA.Key == ConsoleKey.Subtract && a == "0")
                {
                    Console.Write(typeA.KeyChar);
                    a = typeA.KeyChar + a;
                }
                else if (typeA.Key == ConsoleKey.A && a == "0")
                {
                    Console.Write("Ans<{0}>",Ans);
                    a = a + Ans.ToString();
                }
                else if (typeA.Key == ConsoleKey.Add)
                {
                    Console.Write(" " + typeA.KeyChar + " ");
                    if (opt == '+')
                    {
                        arr.Add(double.Parse(a));
                    }
                    else
                    {
                        arr.Add(0 - double.Parse(a));
                    }
                    opt = '+';
                    a = "0";
                }
                else if (typeA.Key == ConsoleKey.Subtract)
                {
                    Console.Write(" " + typeA.KeyChar + " ");
                    if (opt == '+')
                    {
                        arr.Add(double.Parse(a));
                    }
                    else
                    {
                        arr.Add(0 - double.Parse(a));
                    }
                    opt = '-';
                    a = "0";
                }
                else if ((typeA.Key == ConsoleKey.OemPlus))
                {
                    Console.Write(" " + typeA.KeyChar + " ");

                    if (opt == '+')
                    {
                        arr.Add(double.Parse(a));
                    }
                    else
                    {
                        arr.Add(0 - double.Parse(a));
                    }
                    opt = '+';
                    a = "0";

                    double result = 0;
                    foreach (double item in arr)
                    {
                        //Console.Write(" " + item + " ");
                        result = result + item;
                    }
                    Console.WriteLine(result);
                    Ans = result;
                    arr.Clear();
                  
                }
                else if (typeA.Key == ConsoleKey.Escape)
                {
                    break;
                }
            }


        }

        static void Start()
        {
            Console.WriteLine("---------------------------");
            Console.WriteLine("|    Máy tính bỏ túi      |");
            Console.WriteLine("---------------------------");
            Console.WriteLine("1.Tính tổng a + b");
            Console.WriteLine("2.Tính hiệu a - b");
            Console.WriteLine("3.Tính tổng a + b - c ...");
            Console.WriteLine("ESC.Thoát chương trình");
        }
    }
}

 

nguynthnh22 đã bình luận 5 năm trước

using System;
using System.Text;

namespace CSharp_Bai3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            int cont;
            double A, B;

            while (true)
            {

                Console.Write("Xin nhập số đầu tiên ");
                A = Convert.ToDouble(Console.ReadLine());
            lmao:
                Console.Write("Xin nhập số thứ hai ");
                B = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Nhập 1 để cộng \nNhập 2 để trừ \nNhập 3 để chia \nNhập 4 để chia lấy dư \nNhập 5 để nhân \nNhập 6 để chia lấy nguyên \nNhập 7 để Thoát");
                int method = Convert.ToInt32(Console.ReadLine());
                switch (method)
                {
                    case 1:
                        {
                            Console.WriteLine("Bạn chọn phép cộng");
                            Console.WriteLine("Kết quả {0}", A + B);
                            Console.WriteLine("Ấn 1 nếu muốn tiếp tục thực hiện tính toán với kết quả vừa ra, ấn số bất kỳ nếu muốn thực hiện phép tính mới");
                            cont = Convert.ToInt32(Console.ReadLine());
                            switch (cont)
                            {
                                case 1:
                                    {
                                        A += B;
                                        goto lmao;

                                    }
                                default:
                                    break;
                            }


                            break;
                        }

                    case 2:
                        {
                            Console.WriteLine("Bạn chọn phép trừ");
                            Console.WriteLine("Kết quả {0}", A - B);
                            Console.WriteLine("Ấn 1 nếu muốn tiếp tục thực hiện tính toán với kết quả vừa ra, ấn số bất kỳ nếu muốn thực hiện phép tính mới");
                            cont = Convert.ToInt32(Console.ReadLine());
                            switch (cont)
                            {
                                case 1:
                                    {
                                        A -= B;
                                        goto lmao;
                                    }
                                default:
                                    break;
                            }
                            break;
                        }
                    case 3:
                        {

                            Console.WriteLine("Bạn chọn phép chia");
                            Console.WriteLine("Kết quả {0}", A / B);
                            Console.WriteLine("Ấn 1 nếu muốn tiếp tục thực hiện tính toán với kết quả vừa ra, ấn số bất kỳ nếu muốn thực hiện phép tính mới");
                            cont = Convert.ToInt32(Console.ReadLine());
                            switch (cont)
                            {
                                case 1:
                                    {
                                        A /= B;
                                        goto lmao;
                                    }
                                default:
                                    break;
                            }
                            break;
                        }
                    case 4:
                        {
                            Console.WriteLine("Bạn chọn phép chia lấy dư");
                            Console.WriteLine("Kết quả {0}", A % B);
                            Console.WriteLine("Ấn 1 nếu muốn tiếp tục thực hiện tính toán với kết quả vừa ra, ấn số bất kỳ nếu muốn thực hiện phép tính mới");
                            cont = Convert.ToInt32(Console.ReadLine());
                            switch (cont)
                            {
                                case 1:
                                    {
                                        A %= B;
                                        goto lmao;
                                    }
                                default:
                                    break;
                            }
                            break;
                        }
                    case 5:
                        {
                            Console.WriteLine("Bạn chọn phép nhân");
                            Console.WriteLine("Kết quả {0}", A * B);
                            Console.WriteLine("Ấn 1 nếu muốn tiếp tục thực hiện tính toán với kết quả vừa ra, ấn số bất kỳ nếu muốn thực hiện phép tính mới");
                            cont = Convert.ToInt32(Console.ReadLine());
                            switch (cont)
                            {
                                case 1:
                                    {
                                        A *= B;
                                        goto lmao;
                                    }
                                default:
                                    break;
                            }
                            break;
                        }
                    case 6:
                        {

                            Console.WriteLine("Bạn chọn phép chia lấy nguyên");
                            Console.WriteLine("Kết quả {0}", (A - A % B) / B);
                            Console.WriteLine("Ấn 1 nếu muốn tiếp tục thực hiện tính toán với kết quả vừa ra, ấn số bất kỳ nếu muốn thực hiện phép tính mới");
                            cont = Convert.ToInt32(Console.ReadLine());
                            switch (cont)
                            {
                                case 1:
                                    {
                                        A = (A - A % B) / B;

                                        goto lmao;
                                    }
                                default:
                                    break;
                            }
                            break;
                        }


                    case 7:
                        Console.WriteLine("Tạm biệt!");
                        goto yolo;

                }
            }
        yolo:
            Console.ReadKey();
        }


    }
}

Không có video.