public bool IsValid(string s)
    {
        Stack <Parenthese> stack = new Stack <Parenthese>();

        foreach (char symbol in s)
        {
            Parenthese parenthese = new Parenthese();
            parenthese.Init(symbol);

            if (stack.Count == 0)
            {
                stack.Push(parenthese);
            }
            else
            {
                Parenthese last_p = stack.Peek();
                if (last_p.type == parenthese.type && last_p.toward + parenthese.toward == 0)
                {
                    stack.Pop();
                }
                else
                {
                    stack.Push(parenthese);
                }
            }
        }
        return(stack.Count == 0);
    }
Exemple #2
0
        /// <summary>
        /// Форматирование слов
        /// </summary>
        private void ProcessData()
        {
            Process(0, words.Count(), -1);

            int index     = writeWords.FindIndex(l => l.word.StartsWith("esehtnerap"));
            int iterCount = 0;

            while (index >= 0)
            {
                WriteWord  tmpW = writeWords[index];
                Parenthese tmpP = parentheses.Where(p => p.repl_str.Equals(tmpW.word)).First();
                Process(tmpP.left, tmpP.right + 1, index);
                index      = writeWords.FindIndex(l => l.word.StartsWith("esehtnerap"));
                iterCount += 1;
                if (iterCount >= 549)
                {
                    break;
                }
            }

            foreach (var sq in singleQuotes)
            {
                List <WriteWord> tmpLineList = writeWords.Where(l => l.word.Equals(sq.Key, cmp)).ToList();
                int lineCount = tmpLineList.Count();

                for (int i = 0; i < lineCount; i++)
                {
                    WriteWord tmp = tmpLineList[i];
                    tmp.word = sq.Value;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Обработка слов
        /// </summary>
        /// <param name="startIndex">Индекс начала</param>
        /// <param name="endIndex">Индекс конца</param>
        /// <param name="WrWordIndex">Индекс слова для записи</param>
        private void Process(int startIndex, int endIndex, int WrWordIndex)
        {
            int        ids         = 0;                // размер отступа
            int        insertIndex = -1;               // индекс вставки текущего слова
            List <int> caseIds     = new List <int>(); // отступы для вложенных case
            int        curCaseIds  = -1;               // текуший отступ для case
            bool       isWith      = false;            // признак того, что запрос вынесен в with
            string     prevWordSt  = "";               // предыдущее от startIndex слово
            string     nextWordSt  = "";               // следующее от startIndex слово

            if (startIndex != 0)
            {
                prevWordSt = words[startIndex - 1];
            }
            if (startIndex != words.Count() - 1)
            {
                nextWordSt = words[startIndex + 1];
            }

            if (WrWordIndex >= 0)
            {
                WriteWord tmpW = writeWords[WrWordIndex];
                if (tmpW.isNewLine)
                {
                    ids = writeWords[WrWordIndex].intendSize;
                }
                else
                {
                    ids = CalcIntendSize(WrWordIndex) + 1; // Добавляем единицу из-за открывающей скобки
                }
                writeWords.RemoveAt(WrWordIndex);
                insertIndex = WrWordIndex - 1;
            }

            for (int i = startIndex; i < endIndex; i++)
            {
                if (WrWordIndex >= 0)
                {
                    insertIndex = insertIndex + 1;
                }
                string prevWord = "";
                string nextWord = "";
                string curWord  = words[i];

                if (i != 0)
                {
                    prevWord = words[i - 1];
                }
                if (i != words.Count() - 1)
                {
                    nextWord = words[i + 1];
                }


                if (curWord.Equals("(", cmp))
                {
                    if (WrWordIndex >= 0 && startIndex == i)
                    {
                        if (parNextLineWords.Where(x => x.Equals(prevWord, cmp)).Count() > 0)
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = ids, isNewLine = true
                            });
                            ids += 1;
                        }
                        else
                        {
                            if (parSpaceWords.Where(x => x.Equals(prevWord, cmp)).Count() > 0)
                            {
                                AddWord(insertIndex, new WriteWord {
                                    word = curWord, intendSize = 1, isNewLine = false
                                });
                            }
                            else
                            {
                                AddWord(insertIndex, new WriteWord {
                                    word = curWord, intendSize = 0, isNewLine = false
                                });
                            }
                        }
                    }
                    else
                    {
                        Parenthese par = parentheses.Where(p => p.left == i).First();
                        if (parNextLineWords.Where(x => x.Equals(prevWord, cmp)).Count() > 0)
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = par.repl_str, intendSize = ids, isNewLine = true
                            });
                        }
                        else
                        {
                            if (parSpaceWords.Where(x => x.Equals(prevWord, cmp)).Count() > 0)
                            {
                                AddWord(insertIndex, new WriteWord {
                                    word = par.repl_str, intendSize = 1, isNewLine = false
                                });
                            }
                            else
                            {
                                AddWord(insertIndex, new WriteWord {
                                    word = par.repl_str, intendSize = 0, isNewLine = false
                                });
                            }
                        }
                        i = par.right;
                    }

                    continue;
                }

                // комментарий. Пока пропускаем
                if (curWord.StartsWith("tnemmoc"))
                {
                    int t = 1 + 1;
                }

                if (curWord.Equals("union", cmp))
                {
                    ids -= 2;
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("with", cmp))
                {
                    if (prevWord.Equals("start"))
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 1, isNewLine = false
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids, isNewLine = true
                        });
                        ids   += 2;
                        isWith = true;
                    }

                    continue;
                }

                if (curWord.Equals("select", cmp))
                {
                    if (isWith)
                    {
                        ids   -= 2;
                        isWith = false;
                    }

                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids, isNewLine = true
                    });
                    ids += 2;
                    continue;
                }

                if (curWord.Equals("from", cmp))
                {
                    if (WrWordIndex >= 0 && fromFunctions.Where(x => x.Equals(prevWordSt, cmp)).Count() > 0)
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 1, isNewLine = false
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids - 2, isNewLine = true
                        });
                    }
                    continue;
                }

                if (joinTypes.Where(x => x.Equals(curWord, cmp)).Count() > 0 &&
                    joinTypes.Where(x => x.Equals(prevWord, cmp)).Count() == 0)
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids - 2, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("join", cmp) && joinTypes.Where(x => x.Equals(prevWord, cmp)).Count() == 0)
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids - 2, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("where", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids - 2, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("connect", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids - 2, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("start", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids - 2, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("case", cmp))
                {
                    if (WrWordIndex >= 0)
                    {
                        if (prevWord.Equals("("))
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = 0, isNewLine = false
                            });
                            curCaseIds = CalcIntendSize(insertIndex);
                        }
                        else
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = 1, isNewLine = false
                            });
                            curCaseIds = CalcIntendSize(insertIndex);
                        }
                    }
                    else
                    {
                        if (prevWord.Equals("("))
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = 0, isNewLine = false
                            });
                            curCaseIds = CalcIntendSize(writeWords.Count() - 1);
                        }
                        else
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = 1, isNewLine = false
                            });
                            curCaseIds = CalcIntendSize(writeWords.Count() - 1);
                        }
                    }

                    caseIds.Add(curCaseIds);
                    continue;
                }

                if (curWord.Equals("when", cmp) || curWord.Equals("else", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = curCaseIds + 2, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("then", cmp) || prevWord.Equals("else", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = curCaseIds + 4, isNewLine = true
                    });
                    continue;
                }

                if (curWord.Equals("end", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = curCaseIds, isNewLine = true
                    });
                    caseIds.RemoveAt(caseIds.Count() - 1);
                    if (caseIds.Count() > 0)
                    {
                        curCaseIds = caseIds.Last();
                    }
                    else
                    {
                        curCaseIds = -1;
                    }
                    continue;
                }

                if (curWord.Equals("group", cmp))
                {
                    if (prevWord.Equals("within", cmp))
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 1, isNewLine = false
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids - 2, isNewLine = true
                        });
                    }
                    continue;
                }

                if (curWord.Equals("order", cmp))
                {
                    if (WrWordIndex >= 0 && (prevWordSt.Equals("group", cmp)))
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 0, isNewLine = false
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids - 2, isNewLine = true
                        });
                    }
                    continue;
                }

                if (WrWordIndex >= 0 && fromFunctions.Where(x => x.Equals(prevWordSt, cmp)).Count() > 0)
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = 1, isNewLine = false
                    });
                    continue;
                }

                if (curWord.Equals("by", cmp) || curWord.Equals("sets", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = 1, isNewLine = false
                    });
                    continue;
                }

                if (curWord.Equals("and", cmp) || curWord.Equals("or", cmp))
                {
                    if (curCaseIds >= 0)
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = curCaseIds + 7, isNewLine = true
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids, isNewLine = true
                        });
                    }
                    continue;
                }

                if (curWord.Equals(","))
                {
                    if (WrWordIndex < 0)
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids, isNewLine = true
                        });
                        continue;
                    }

                    if (WrWordIndex > 0)
                    {
                        if (nextWordSt.Equals("select", cmp) || prevWordSt.Equals("by", cmp))
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = ids, isNewLine = true
                            });
                        }
                        else
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = 0, isNewLine = false
                            });
                        }
                        continue;
                    }
                }

                if (prevWord.Equals("on", cmp))
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = ids, isNewLine = true
                    });
                    continue;
                }

                if (prevWords.Where(x => x.Equals(prevWord, cmp)).Count() > 0)
                {
                    if (WrWordIndex >= 0 && fromFunctions.Where(x => x.Equals(prevWordSt, cmp)).Count() > 0)
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 1, isNewLine = false
                        });
                    }
                    else
                    {
                        if (prevWord.Equals("by", cmp) && parGroupWords.Where(x => x.Equals(curWord, cmp)).Count() > 0)
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = 1, isNewLine = false
                            });
                        }
                        else
                        {
                            AddWord(insertIndex, new WriteWord {
                                word = curWord, intendSize = ids, isNewLine = true
                            });
                        }
                    }
                    continue;
                }
                else if (WrWordIndex >= 0 && prevWord.Equals("("))
                {
                    if (nextWordSt.Equals("select", cmp) && prevWordSt.Equals("where", cmp))
                    {
                        ids += 1;
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = ids, isNewLine = true
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 0, isNewLine = false
                        });
                    }
                    continue;
                }
                else if (WrWordIndex >= 0 && curWord.Equals(")", cmp))
                {
                    if (prevWordSt.Equals("from", cmp) || prevWordSt.Equals("as", cmp) || prevWordSt.Equals("select", cmp))
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = writeWords[startIndex].intendSize, isNewLine = true
                        });
                    }
                    else if (nextWordSt.Equals("select", cmp) &&
                             !(prevWordSt.Equals("from", cmp) || prevWordSt.Equals("where", cmp) || prevWordSt.Equals("select", cmp)))
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = CalcIntendSize(startIndex), isNewLine = true
                        });
                    }
                    else
                    {
                        AddWord(insertIndex, new WriteWord {
                            word = curWord, intendSize = 0, isNewLine = false
                        });
                    }
                    continue;
                }
                else
                {
                    AddWord(insertIndex, new WriteWord {
                        word = curWord, intendSize = 1, isNewLine = false
                    });
                }
            }
        }
Exemple #4
0
        public static string CalCulateGlobalResult(string operations)
        {
            operations = operations.Replace("|", "");
            string        numberNow             = "";
            Parenthese    parenthese            = null;
            List <string> numbersOperationsBase = new List <string> {
            };

            TakeCareNoneOperatorBeforeParenthese(ref operations);

            foreach (var numberOperator in operations)
            {
                if (numberOperator == '(')
                {
                    if (parenthese == null)
                    {
                        parenthese = new Parenthese();
                    }
                    else
                    {
                        parenthese = parenthese.OpenParenthese();
                    }
                }
                else if (numberOperator == ')')
                {
                    parenthese.NumbersOperators.Add(numberNow);
                    numberNow = "";
                    (Parenthese parentheseAncestor, string resultParenthese) = parenthese.CloseParenthese();
                    if (parentheseAncestor == null)
                    {
                        parenthese = null;
                        numbersOperationsBase.Add(resultParenthese);
                    }
                    else
                    {
                        parenthese = parentheseAncestor;
                        parenthese.NumbersOperators.Add(resultParenthese);
                    }
                }
                else
                {
                    if (operators.Contains(numberOperator))
                    {
                        if (parenthese == null)
                        {
                            numbersOperationsBase.Add(numberNow);
                            numbersOperationsBase.Add(numberOperator.ToString());
                            numberNow = "";
                        }
                        else
                        {
                            parenthese.NumbersOperators.Add(numberNow);
                            parenthese.NumbersOperators.Add(numberOperator.ToString());
                            numberNow = "";
                        }
                    }
                    else
                    {
                        numberNow += numberOperator;
                    }
                }
            }

            numbersOperationsBase.Add(numberNow);

            return(numbersOperationsBase.ResultOperationsOfThisBlock());
        }