2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,25 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace madLibs {
class Program {
static void Main(string[] args) {
string name, sex, addThis, thing;
bool isMale = false;
Console.Write("Enter a name: ");
name = Console.ReadLine();
while(isMale == false) {
Console.Write("Is that a male or female name? [m/f] ");
sex = Console.ReadLine().ToLower().ToCharArray()[0].ToString();
if(sex == "m") { isMale = true; } else if(sex == "f") { break; }
}
if (isMale){ addThis = "He "; }else{ addThis = "She "; }
Console.Write("Enter a thing: ");
thing = Console.ReadLine();
Console.WriteLine(Environment.NewLine + String.Format(("{0} went for a walk in the park. " + addThis +
"found a {1}. {0} decided to take it home."), name, thing));
Console.ReadKey();
}
}
using System.Text.RegularExpressions;
namespace MadLibs_RosettaCode
{
class Program
{
static void Main(string[] args)
{
string madLibs =
@"Write a program to create a Mad Libs like story.
The program should read an arbitrary multiline story from input.
The story will be terminated with a blank line.
Then, find each replacement to be made within the story,
ask the user for a word to replace it with, and make all the replacements.
Stop when there are none left and print the final story.
The input should be an arbitrary story in the form:
<name> went for a walk in the park. <he or she>
found a <noun>. <name> decided to take it home.
Given this example, it should then ask for a name,
a he or she and a noun (<name> gets replaced both times with the same value).";
StringBuilder sb = new StringBuilder();
Regex pattern = new Regex(@"\<(.*?)\>");
string storyLine;
string replacement;
Console.WriteLine(madLibs + Environment.NewLine + Environment.NewLine);
Console.WriteLine("Enter a story: ");
// Continue to get input while empty line hasn't been entered.
do
{
storyLine = Console.ReadLine();
sb.Append(storyLine + Environment.NewLine);
} while (!string.IsNullOrEmpty(storyLine) && !string.IsNullOrWhiteSpace(storyLine));
// Retrieve only the unique regex matches from the user entered story.
Match nameMatch = pattern.Matches(sb.ToString()).OfType<Match>().Where(x => x.Value.Equals("<name>")).Select(x => x.Value).Distinct() as Match;
if(nameMatch != null)
{
do
{
Console.WriteLine("Enter value for: " + nameMatch.Value);
replacement = Console.ReadLine();
} while (string.IsNullOrEmpty(replacement) || string.IsNullOrWhiteSpace(replacement));
sb.Replace(nameMatch.Value, replacement);
}
foreach (Match match in pattern.Matches(sb.ToString()))
{
replacement = string.Empty;
// Guarantee we get a non-whitespace value for the replacement
do
{
Console.WriteLine("Enter value for: " + match.Value);
replacement = Console.ReadLine();
} while (string.IsNullOrEmpty(replacement) || string.IsNullOrWhiteSpace(replacement));
int location = sb.ToString().IndexOf(match.Value);
sb.Remove(location, match.Value.Length).Insert(location, replacement);
}
Console.WriteLine(Environment.NewLine + Environment.NewLine + "--[ Here's your story! ]--");
Console.WriteLine(sb.ToString());
}
}
}