استفاده از کلاس ها در سی شارپ

بازدید9kپست ها3آخرین فعالیت10 سال پیش
0
0

سلام میخواستم بدونم اگه من یک کدی داشته باشم که نخوام توی فرم یک باشه ، یعنی یک کلاس جدا براش بسازم چیکار باید انجام بدم ؟
کد :
[CSHARP]
private string gt;
private string j;
[/CSHARP]

[CSHARP]
gt = textBox1.Text.ToLower().Substring(textBox1.Text.ToLower().IndexOf("enter the right answer to start chatting.") + 42);
gt = gt.Replace(" ", "");
gt = gt.Replace("=...", "");
gt = gt.Replace("thousand", "");
gt = gt.Replace("hundred", "");
gt = gt.Replace("and", ",");
gt = gt.Replace("ten", "10");
gt = gt.Replace("eleven", "11");
gt = gt.Replace("twelve", "12");
gt = gt.Replace("thirteen", "13");
gt = gt.Replace("fourteen", "14");
gt = gt.Replace("fifteen", "15");
gt = gt.Replace("sixteen", "16");
gt = gt.Replace("seventeen", "17");
gt = gt.Replace("eighteen", "18");
gt = gt.Replace("nineteen", "19");
gt = gt.Replace("twenty-", "2");
gt = gt.Replace("thirty-", "3");
gt = gt.Replace("forty-", "4");
gt = gt.Replace("fifty-", "5");
gt = gt.Replace("sixty-", "6");
gt = gt.Replace("seventy-", "7");
gt = gt.Replace("eighty-", "8");
gt = gt.Replace("ninety-", "9");
gt = gt.Replace("twenty", "20");
gt = gt.Replace("thirty", "30");
gt = gt.Replace("forty", "40");
gt = gt.Replace("fifty", "50");
gt = gt.Replace("sixty", "60");
gt = gt.Replace("seventy", "70");
gt = gt.Replace("eighty", "80");
gt = gt.Replace("ninety", "90");
gt = gt.Replace("one", "1");
gt = gt.Replace("two", "2");
gt = gt.Replace("three", "3");
gt = gt.Replace("four", "4");
gt = gt.Replace("five", "5");
gt = gt.Replace("six", "6");
gt = gt.Replace("seven", "7");
gt = gt.Replace("eight", "8");
gt = gt.Replace("nine", "9");
if (gt.ToLower().Contains("-"))
{
int y = Convert.ToInt32(gt.Substring(0, +1));
int x = Convert.ToInt32(gt.Substring(2, +1));
j = (y - x).ToString();
}
else if (gt.ToLower().Contains("+"))
{
int y = Convert.ToInt32(gt.Substring(0, +1));
int x = Convert.ToInt32(gt.Substring(2, +1));
j = (y + x).ToString();
}
else if (gt.ToLower().Contains("*"))
{
int y = Convert.ToInt32(gt.Substring(0, +1));
int x = Convert.ToInt32(gt.Substring(2, +1));
j = (y * x).ToString();
}
else if (gt.ToLower().Contains("whatisthe1stdigitof"))
{
j = gt.Substring(gt.IndexOf("of") + 2);
j = j.Substring(0, j.IndexOf("?") - 4);

                                }
                                else if (gt.ToLower().Contains("whatisthe2nddigitof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 3);
                                    j = j.Substring(0, j.IndexOf("?") - 3);

                                }
                                else if (gt.ToLower().Contains("whatisthe3rddigitof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 4);
                                    j = j.Substring(0, j.IndexOf("?") - 2);

                                }
                                else if (gt.ToLower().Contains("whatisthe4thdigitof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 5);
                                    j = j.Substring(0, j.IndexOf("?") - 1);

                                }
                                else if (gt.ToLower().Contains("whatisthe5thdigitof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 6);
                                    j = j.Substring(0, j.IndexOf("?") - 0);

                                }
                                else if (gt.ToLower().Contains("asdigits?"))
                                {
                                    j = gt.Substring(gt.IndexOf("Whatis") + 6);
                                    j = j.Substring(0, j.IndexOf("as") - 0);

                                }
                                else if (gt.ToLower().Contains("whichisthesmallest?"))
                                {
                                    j = gt.Substring(gt.IndexOf("bers") + 4);
                                    j = j.Substring(0, j.IndexOf(",w") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    long lng = 0;
                                    lng = Convert.ToInt64(s[s.GetLowerBound(0)]);
                                    for (var i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++)
                                    {
                                        if (lng > Convert.ToInt64(s[i]))
                                        {
                                            lng = Convert.ToInt64(s[i]);
                                        }
                                    }
                                    j = lng.ToString();

                                }
                                else if (gt.ToLower().Contains("whichisthebiggest?"))
                                {
                                    j = gt.Substring(gt.IndexOf("bers") + 4);
                                    j = j.Substring(0, j.IndexOf(",w") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    long lng = 0;
                                    lng = Convert.ToInt64(s[s.GetUpperBound(0)]);
                                    for (var i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++)
                                    {
                                        if (lng < Convert.ToInt64(s[i]))
                                        {
                                            lng = Convert.ToInt64(s[i]);
                                        }
                                    }
                                    j = lng.ToString();

                                }
                                else if (gt.ToLower().Contains("the1stnumberof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 2);
                                    j = j.Substring(0, j.IndexOf("is?") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    j = s[0].ToString();

                                }
                                else if (gt.ToLower().Contains("the2ndnumberof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 2);
                                    j = j.Substring(0, j.IndexOf("is?") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    j = s[1].ToString();

                                }
                                else if (gt.ToLower().Contains("the3rdnumberof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 2);
                                    j = j.Substring(0, j.IndexOf("is?") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    j = s[2].ToString();

                                }
                                else if (gt.ToLower().Contains("the4thnumberof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 2);
                                    j = j.Substring(0, j.IndexOf("is?") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    j = s[3].ToString();

                                }
                                else if (gt.ToLower().Contains("the5thnumberof"))
                                {
                                    j = gt.Substring(gt.IndexOf("of") + 2);
                                    j = j.Substring(0, j.IndexOf("is?") - 0);
                                    string[] s = null;
                                    s = j.Split(',');
                                    j = s[4].ToString();
                                }
                                MessageBox.Show("Javabe sahih " + j + " Mibashad");

[/CSHARP]

خب اگه من بخوام اینو توی کلاس تعریف کنم ، کدش چطوری میشه ؟
و اگه بخوام وقتی مثلا سوال پرسیده شد ، برنامه به این کلاس رجوع کنه و جواب رو بدست بیاره و جواب رو به کاربر نشون بده ، چیکار باید انجام بدم ؟

یک سوال دیگه ، آیا این کدها به نظر شما که کدنویسیتون قوی تره درست هستند؟ اگه نه ، پس کد رو به چه شکلی تغییر میدید ؟

ممنون

0

سلام یک ماشین حساب با استفاده از یک تکست باکس!
مثلا :

enter the right answer to start chatting.
What is sixty thousand sixty-six as digitis ?

یا

enter the right answer to start chatting.
6 - 9 = ?

یا استفاده از Switch چطوری میشه ؟؟
ممنون

0

سلام
دوست من دونستن این نوع دستور ها پیش نیازهر زبان برنامه نویسیه چطور میخوای ماشین حساب (یا هر برنامه دیگه ای) بنویسی در حالی که switch رو بلد نیستی؟ اول باید پیشنیاز ها رو بلد باشی

C# Switch Statement

مثلا این کد در عنوان برنامه روز هفته رو چاپ میکنه:
[CSHARP]
string res = "";
switch (System.DateTime.Now.DayOfWeek.ToString())
{
case "Saturday":
res = "شنبه";
break;
case "Sunday":
res = "یک شنبه";
break;
case "Monday":
res = "دوشنبه";
break;
case "Tuesday":
res = "سه شنبه";
break;
case "Wednesday":
res = "چهار شنبه";
break;
case "Thursday":
res = "پنج شنبه";
break;
case "Friday":
res = "جمعه";
break;
}
this.Text = res;
[/CSHARP]

سوال برنامه نویسی دارید؟

ندونستن عیب نیست، نپرسیدن چرا!

خوش آمدید

برای طرح سوال، ایجاد بحث و فعالیت در سایت نیاز است ابتدا وارد حساب کاربری خود شوید. در صورتی که هنوز عضو سایت نیستید میتوانید در عرض تنها چند ثانیه ثبت نام کنید.