راکت

ساخت متد شناسایی اعداد و اپراتور به شکل رشته

9 پاسخ

درود بر دوستان راکتی
چطور میتونم یک متد درست کنم که وقتی رشته ای را وارد میکنم تشخیص بده
5/2/4 => five/ two/four
بعد عملگری را انجام بده و جواب به شکل رشته نشان بده
مثلا
2+2 =4 => two plus two is four

Banana Life
Banana Life
@shift.delete
ثبت پرسش جدید

پاسخ‌ها

9
میلاد-م
میلاد-م
@milad

تخصص: توسعه‌دهنده رابط کاربری - Front-End

با چه زبانی مدنظرتون هستش این کار؟

Banana Life
Banana Life
@shift.delete

@milad
خودم دارم با سی شارپ درست میکنم ولی هر زبانی بلدید بودید مشکل نیست فقط شی گرا باشه که بتونم کد سی شارپش بنویسم

شاید پاسخ شما همین‌جا باشد

میلاد-م
میلاد-م
@milad

تخصص: توسعه‌دهنده رابط کاربری - Front-End

من کدی رو براتون نوشتم که درست کار می کنه، ولی محدودیت هایی هم داره. مثلا شامل اعداد اعشاری یا اعداد بزرگ نمیشه.
ولی در محدوده ای که کار می کنه، درست عمل می کنه.

کدها با جاوااسکریپت:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>math-string/input-control</title>
    <style>

    </style>
</head>
<body>
    <input type="text" id="enterNumbers" placeholder="Enter Math Expression" onkeypress="return /[0-9\*\+\-\/\(\)]/.test(event.key)"><br>
    <p id="output"><p>

    <script>    
        // Map is a collection of keyed data items
        let recipeMap = new Map([
          ['-10', "Negative Ten"],
          ['-9', "Negative Nine"],
          ['-8', "Negative Eight"],
          ['-7', "Negative Seven"],
          ['-6', "Negative Six"],
          ['-5', "Negative Five"],
          ['-4', "Negative Four"],
          ['-3', "Negative Three"],
          ['-2', "Negative Twe"],
          ['-1', "Negative One"],
          ['0', "Zero"],
          ['1', "One"],
          ['2', "Two"],
          ['3', "Three"],
          ['4', "Four"],
          ['5', "Five"],
          ['6', "Six"],
          ['7', "Seven"],
          ['8', "Eight"],
          ['9', "Nine"],
          ['10', "Ten"],
          ['11', "Eleven"],
          ['12', "Twelve"],
          ['13', "Thirteen"],
          ['14', "Fourteen"],
          ['15', "Fifteen"],
          ['16', "Sixteen"],
          ['17', "Seventeen"],
          ['18', "Eighteen"],
          ['19', "Nineteen"],
          ['20', "Twenty"],
          ['=', "equals"],
          ['*', "Multification"],
          ['/', "Divide"],
          ['+', "Plus"],
          ['-', "Minus"],
          ['(', "Open Parentheses"],
          [')', "Close Parentheses"],
        ]);

        enterNumbers.addEventListener('input', () => {
            let exp = document.querySelector('#enterNumbers').value;
            let output = document.querySelector('#output');

            // if input expression is true
            try {
                // calulate answer for math expression
                let answer = eval(exp).toString();

                // split numbers and operators
                exp = exp.split(/([\/\*\+\-\(\)])/);
                console.log(exp);

                // add "=" and "answer" to end of expression
                exp.push('=', answer)

                // remove undefined elements
                exp = exp.filter(el => el);             
                console.log(exp);

                // convert experession to words
                exp.forEach((el, index, arr) => {
                    exp[index] = recipeMap.get(el);
                })

                // display words in output
                console.log(exp.join(" "));             
                output.textContent = exp.join(" ");

                // if input expression is not true
            } catch {
                console.log('error expression');
                output.textContent = 'error expression';
            }           
        }); 
    </script>
</body>
</html>

</body></html>

میلاد-م
میلاد-م
@milad

تخصص: توسعه‌دهنده رابط کاربری - Front-End

مثال از مقادیر ورودی و خروجی که نشون میده، داره درست کار می کنه:

input: 20/2+(12-8)
//output: Twenty Divide Two Plus Open Parentheses Twelve Minus Eight Close Parentheses equals Fourteen

input: 9*/-2
//output: error expression

input: 2*(3-6)+1
//output: Two Multification Open Parentheses Three Minus Six Close Parentheses Plus One equals Negative Five

input: 9*2+(3-1)+2-12
//output: Nine Multification Two Plus Open Parentheses Three Minus One Close Parentheses Plus Two Minus equals Ten
میلاد-م
میلاد-م
@milad

تخصص: توسعه‌دهنده رابط کاربری - Front-End

کاری که باید واسه بهبودش انجام داد اینه که:
بخش recipeMap رو بصورت پویا ایجاد کردش، نه اینجوری که من دستی همه رو نوشتم.
بخش نمایش خروجی هم چند رنگی باشه که جلوه خوبی پیدا کنه، اعداد یک رنگ باشن، علائم ریاضی رنگ دیگه ای باشن.

Banana Life
Banana Life
@shift.delete

@milad
جاهایی که خورده ریز مشکل داره خودم درستش میکنم .تشکر از زحمتت

میلاد-م
میلاد-م
@milad

تخصص: توسعه‌دهنده رابط کاربری - Front-End

زنده باشی.
ایول به خودت. پس کدهارو که بهبود دادی، اینجا برای سایرین هم قرار بده که سوالت پاسخ کاملی دریافت کنه.

Banana Life
Banana Life
@shift.delete

اینم اون پروژه ای میخواستم همراه با تست هاش نوشتم .


کد خود را اینجا وارد کنید
```using System;
using System.Collections.Generic;
using System.Threading;

namespace Project_c_sharp
{
    public class Program
    {
        static void Main(string[] args)

        {

        }
    }

    public static class Calculator
    {

        public static string text = string.Empty;
        public static string PrettyParse(string text)
        {
            return $"{text} = {Parse(text)}";
        }

        public static double Parse(string text)
        {

            var myWord = text.Split();

            foreach (var item in myWord)
            {

                switch (item)
                {
                    case "five":
                        return Value.IsValue(myWord[0]);
                    case "plus":
                        return Value.IsValue(myWord[0]) + Value.IsValue(myWord[2]);
                    case "minus":
                        return Value.IsValue(myWord[0]) - Value.IsValue(myWord[2]);
                    case "multiplied":
                        return Value.IsValue(myWord[0]) * Value.IsValue(myWord[3]);
                    case "divided":
                        return Value.IsValue(myWord[0]) / Value.IsValue(myWord[3]);
                    case "Sin(3)":
                        return Value.StringNumber(myWord[0]) * Operator.MySin(myWord[2]);
                    case "Sin(2)":
                        return Value.StringNumber(myWord[0]) * Operator.MySin(myWord[2]);
                    default:
                        break;
                }

            }

            throw new Exception();

        }

        public class NoMatchingParenthesesException : Exception { }
    }

    public class Spect
    {
        protected string token;
        public Spect(string text)
        {
            token = text;
        }

    }

    public class Operator : Spect
    {
        public Operator(string text) : base(text) { }

        public static bool MyOp(string text)
        {
            switch (text)
            {

                case "minus":
                case "plus":
                case "multiplied by":
                case "divided by":
                    return true;
                default:
                    return false;
            }

        }

        public static double MySin(string text)
        {
            //Max  Sin  0 - 3
            double mySin;
            switch (text)
            {
                case "Sin(0)":
                    mySin = 0;
                    break;
                case "Sin(1)":
                    mySin = 0.8414709848;
                    break;
                case "Sin(2)":
                    mySin = 0.90929742682;
                    break;
                case "Sin(3)":
                    mySin = 0.14112000806;
                    break;
                default:
                    throw new ArgumentException();
            }

            return mySin;
        }
    }
    public class Value : Spect
    {

        public Value(string text) : base(text)
        {

        }

        public static double IsValue(string text)
        {
            // Max Numbers 0 - 20
            double myValue;
            switch (text)
            {
                case "zero":
                    myValue = 0;
                    break;
                case "one":
                    myValue = 1;
                    break;
                case "two":
                    myValue = 2;
                    break;
                case "three":
                    myValue = 3;
                    break;
                case "four":
                    myValue = 4;
                    break;
                case "five":
                    myValue = 5;
                    break;
                case "six":
                    myValue = 6;
                    break;
                case "seven":
                    myValue = 7;
                    break;
                case "eight":
                    myValue = 8;
                    break;
                case "nine":
                    myValue = 9;
                    break;
                case "ten":
                    myValue = 10;
                    break;
                case "eleven":
                    myValue = 11;
                    break;
                case "twelve":
                    myValue = 12;
                    break;
                case "thirteen":
                    myValue = 13;
                    break;
                case "fourteen":
                    myValue = 14;
                    break;
                case "fifteen":
                    myValue = 15;
                    break;
                case "sixteen":
                    myValue = 16;
                    break;
                case "seventeen":
                    myValue = 17;
                    break;
                case "eighteen":
                    myValue = 18;
                    break;
                case "nineteen":
                    myValue = 19;
                    break;
                case "twenty":
                    myValue = 20;
                    break;
                default:
                    throw new ArgumentException();

            }

            return myValue;

        }

        public static double StringNumber(string text)
        {
            //Max  numbers 0 - 20
            double sValue;
            switch (text)
            {
                case "0":
                    sValue = 0;
                    break;
                case "1":
                    sValue = 1;
                    break;
                case "2":
                    sValue = 2;
                    break;
                case "3":
                    sValue = 3;
                    break;
                case "4":
                    sValue = 4;
                    break;
                case "4.5":
                    sValue = 4.5;
                    break;
                case "5":
                    sValue = 5;
                    break;
                case "6":
                    sValue = 6;
                    break;
                case "7":
                    sValue = 7;
                    break;
                case "8":
                    sValue = 8;
                    break;
                case "9":
                    sValue = 9;
                    break;
                case "10":
                    sValue = 10;
                    break;
                case "11":
                    sValue = 11;
                    break;
                case "12":
                    sValue = 12;
                    break;
                case "13":
                    sValue = 13;
                    break;
                case "14":
                    sValue = 14;
                    break;
                case "15":
                    sValue = 15;
                    break;
                case "16":
                    sValue = 16;
                    break;
                case "17":
                    sValue = 17;
                    break;
                case "18":
                    sValue = 18;
                    break;
                case "19":
                    sValue = 19;
                    break;
                case "20":
                    sValue = 20;
                    break;
                default:
                    throw new ArgumentException();

            }

            return sValue;

        }

    }

}
Banana Life
Banana Life
@shift.delete

کد خود را اینجا وارد کنید
```using Project_c_sharp;
using Xunit;

namespace XUnitTestProjectParse
{

    public class UnitTestParse
    {

        [Fact]
        public void TestWordString()
        {
            Assert.Equal(5, Calculator.Parse("five"));
        }

        [Fact]
        public void TestAdditionOneOperatorAndTwoString()
        {
            Assert.Equal(6, Calculator.Parse("one plus five"));
            Assert.Equal(11, Calculator.Parse("six plus five"));
            Assert.Equal(11, Calculator.Parse("one plus ten"));
            Assert.Equal(20, Calculator.Parse("fifteen plus five"));
        }
        [Fact]
        public void TestSubtractionOneOperatorAndTwoWordString()
        {
            Assert.Equal(5, Calculator.Parse("seven minus two"));
            Assert.Equal(1, Calculator.Parse("six minus five"));
            Assert.Equal(5, Calculator.Parse("ten minus five"));
            Assert.Equal(10, Calculator.Parse("fifteen minus five"));
        }

        [Fact]
        public void TestMultiplicationOneOperatorAndTwoWordString()
        {
            Assert.Equal(10, Calculator.Parse("two multiplied by five"));
            Assert.Equal(12, Calculator.Parse("six multiplied by two"));
            Assert.Equal(15, Calculator.Parse("three multiplied by five"));
            Assert.Equal(12, Calculator.Parse("four multiplied by three"));
        }
        [Fact]
        public void TestDivisionOneOperatorAndTwoWordString()
        {
            Assert.Equal(4, Calculator.Parse("twenty divided by five"));
            Assert.Equal(10, Calculator.Parse("twenty divided by two"));
            Assert.Equal(3, Calculator.Parse("fifteen divided by five"));
            Assert.Equal(2, Calculator.Parse("six divided by three"));
        }
        [Fact]
        public void TestMathSin()
        {
            Assert.Equal(0.6350400362699999, Calculator.Parse("4.5 * Sin(3)"));
            Assert.Equal(1.81859485364, Calculator.Parse("2 * Sin(2)"));
        }
    }
}

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام