Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 برنامه سازی با C# برنامه منو Instructor : Saeed Shiry.

Similar presentations


Presentation on theme: "1 برنامه سازی با C# برنامه منو Instructor : Saeed Shiry."— Presentation transcript:

1 1 برنامه سازی با C# برنامه منو Instructor : Saeed Shiry

2 2 کلاس های اصلی using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { class Calc class Menu

3 3 کلاس برنامه class Program { static void Main(string[] args) { Calc xcal = new Calc(); //برای تابع اصلی فقط یک کلاس ماشین حساب تعریف میکنیم ConsoleKeyInfo k; //برنامه را در یک حلقه بی نهایت بار تکرار میکنیم while (true) { // ماشین حساب را صدا میزنیم xcal.stratCalc(); Console.ForegroundColor = ConsoleColor.White; // از کاربر میخواهیم که در مورد اتمام یا ادامه برنامه تصمیم بگیرد Console.Write("\n Press any key to continue,or press E to exit: "); k = Console.ReadKey(); // با کلید مشخص شده از برنامه خارج میشویم if( k.KeyChar=='e' ||k.KeyChar=='E') break; } }}

4 4 کلاس ماشین حساب class Calc { char Operator; double opr1, opr2, answer; // ماشین حساب دارای سه داده اصلی است دو تا برای اعداد و یکی برای عملگر // تابع زیر ماشین حساب را نمایش داده و اجرا میکند public void stratCalc() {} // کار تابع زیر دریافت دو تا عدد از کاربر است void getOperand() {} } این تابع از نوع public است. ولی دیگری اینطور نیست

5 5 تابعی که ماشین حساب را راه اندازی میکند public void stratCalc() { Menu myMenu = new Menu(); // برای ماشین حساب یک شیئی از منو میسازیم myMenu.printMenu(); Operator = myMenu.getSelection(); getOperand(); // بر اساس ورودی کاربر عمل مربوطه را انجام میدهیم switch (Operator) { case '+': answer = opr1 + opr2; break; case '/': if (opr2 != 0) answer = opr1 / opr2; else answer = 0; break; case '*': answer = opr1 * opr2; break; case '-': answer = opr1 - opr2; break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\n {0} {1} {2} = {3}",opr1,Operator,opr2,answer); }

6 6 تابعی برای دریافت اعداد از کاربر void getOperand() { Console.ForegroundColor = ConsoleColor.Red; Console.Write("\nEnter first Operand:"); opr1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter Second Operand:"); opr2 = Convert.ToDouble(Console.ReadLine()); }

7 7 کلاس منو class Menu { // کلاس منو دارای دو تابع است یکی برای چاپ اطلاعات و دیگری برای خواندن انتخاب کاربر public void printMenu(){} public char getSelection(){} }

8 8 نمایش متن منو public void printMenu(){ Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.White; Console.Clear(); Console.WriteLine("Calculator Menu"); Console.WriteLine("Please enter Proper Character to select your Choice:"); Console.WriteLine("\n\n"); Console.WriteLine("For Multiply enter : *"); Console.WriteLine("For addition enter : +"); Console.WriteLine("For subtraction enter : -"); Console.WriteLine("For Division enter : /"); Console.Write("\nEnter your Choice----> "); }

9 9 بازگرداندن انتخاب کاربر public char getSelection() { ConsoleKeyInfo k; char ch='\0'; int sOK=0; while(sOK==0) { k = Console.ReadKey(); ch=k.KeyChar; switch (ch) { case '+': case '*': case '-': case '/': sOK = 1; break; default: Console.Write("\nUncorrect Selection. Enter your choice again:"); break; }

10 10 استفاده از help(کلید F1) DescriptionName Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window. Console.ReadKey () Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window. Console.ReadKey (Boolean)

11 11 Return Value A ConsoleKeyInfo object that describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more SHIFT, ALT, or CTRL modifier keys was pressed simultaneously with the console key.ConsoleKeyInfoConsoleKeyConsoleModifiers C# public static ConsoleKeyInfo ReadKey ()ConsoleKeyInfo

12 12 ConsoleKeyInfo k; تعریف متغیر

13 13

14 14

15 15


Download ppt "1 برنامه سازی با C# برنامه منو Instructor : Saeed Shiry."

Similar presentations


Ads by Google