Создание меню и разбиение программы - C#
Формулировка задачи:
Есть программа, которая высчитывает самое длинное слово, сколько раз оно встречалось в тексте. как правильно сделать, что бы высвечивалось меню, где при нажатии на клавишу 1 мы вводим текст, при нажатии 2 выводим наш результат?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Console.Write("Введите путь к файлу: ");
Stack<string> stack = new Stack<string>();
string line = Console.ReadLine();
File.WriteAllText(@"c:\temp\file.txt", line, Encoding.Default);
string[] words = line.Split(new string[] {" ", ".", ",", "\r\n", "\t"}, StringSplitOptions.RemoveEmptyEntries);
int maxlength = 0;
for (int i = 0; i < words.Length; i++)
if (maxlength < words[i].Length) maxlength = words[i].Length;
for (int i = 0; i < words.Length; i++)
{
if (words[i].Length == maxlength && !stack.Contains(words[i])) stack.Push(words[i]);
}
string[] maxwords = stack.ToArray();
int count;
for (int i = 0; i < maxwords.Length; i++)
{
count = 0;
foreach (string x in words)
{
if (x == maxwords[i]) ++count;
}
Console.WriteLine("{2}. Самое длинное слово: {0}\n Число вхождений: {1}", maxwords[i], count, i);
}
Console.ReadKey(true);
}
}
}Решение задачи: «Создание меню и разбиение программы»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
while (true)
{
uint choice;
Console.WriteLine("Нажмите\n 1 Ввод текста\n 2 Вывод результата");
while ((!uint.TryParse(Console.ReadLine(), out choice)) || (choice > 2))
Console.WriteLine("Повторите ввод");
if (choice == 1)
{
Console.Write("Введите текст: ");
Stack<string> stack = new Stack<string>();
string line = Console.ReadLine();
File.WriteAllText(@"c:\temp\file.txt", line, Encoding.Default);
}
else if (choice == 2)
{
Stack<string> stack = new Stack<string>();
string path = @"c:\temp\file.txt";
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
string[] words = readText.Split(new string[] { "...", "!", " ", ".", ",", "\r\n", "\t" }, StringSplitOptions.RemoveEmptyEntries);
int maxlength = 0;
for (int i = 0; i < words.Length; i++)
if (maxlength < words[i].Length) maxlength = words[i].Length;
for (int i = 0; i < words.Length; i++)
{
if (words[i].Length == maxlength && !stack.Contains(words[i])) stack.Push(words[i]);
}
string[] maxwords = stack.ToArray();
int count;
for (int i = 0; i < maxwords.Length; i++)
{
count = 0;
foreach (string x in words)
{
if (x == maxwords[i]) ++count;
}
}
{
Console.WriteLine("{2}. Самое длинное слово: {0}\n Число вхождений: {1}", maxwords[i], count, i);
}
}
Console.ReadKey(true);
}
}
}
}