- Random Strings
using System;
namespace _022_Random_Strings
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.Write( "Enter a Seed: ");
int seed = int.Parse( Console.ReadLine());
Random r = new Random(seed);
Console.WriteLine("Ten Random Names starting with seed {0}: " ,seed);
for (int index = 0; index < 10; index++ )
{
string name = randomName(r) + " " + randomName(r);
Console.WriteLine(name);
}
}
private readonly static int A = Convert.ToInt32('A' );
private readonly static int Z = Convert.ToInt32('Z');
private static string randomName(Random r)
{
const int MAXLENGTH = 10;
int length = r.Next(1 ,MAXLENGTH+1);
string result = null;
for (int index = 0; index < length; index ++)
{
result += Convert.ToChar(r.Next(A,Z +1));
}
return result;
}
}
} - Random Unicode Strings
using System;
using System.Globalization;
using System.Text;
namespace _022_Random_Unicode_Strings
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Random r = new Random();
System.IO.StreamWriter sr = new System.IO.StreamWriter(" out.txt");
Console.SetOut(sr);
for (int i = 0; i < 10 ; i++)
{
Console.WriteLine(randomUnicodeString(r));
}
sr.Close();
}
private static bool isAcceptable(char c)
{
UnicodeCategory uc = char.GetUnicodeCategory(c);
if (char.IsControl(c)
|| uc == UnicodeCategory.OtherLetter
|| uc == UnicodeCategory.OtherNotAssigned
|| uc == UnicodeCategory.OtherNumber
|| uc == UnicodeCategory.OtherPunctuation
|| uc == UnicodeCategory.OtherSymbol
|| uc == UnicodeCategory.Surrogate
|| uc == UnicodeCategory.PrivateUse)
{
return false;
}
else
{
return true ;
}
}
private static string randomUnicodeString(Random r)
{
const int MAXLENGTH = 10;
int length = r.Next(1,MAXLENGTH+ 1);
string result = "";
while (result.Length < length)
{
char c = Convert.ToChar(r.Next(char.MinValue, char.MaxValue+1));
if (isAcceptable(c))
{
result += c;
}
}
return result;
}
}
} - Random Enum
using System;
namespace _023_Random_Enum
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
enum color {Red, Green, Blue, Agua, Crimson, Teal, Lime, Orange};
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main( string[] args)
{
Random r = new Random();
String randomColorName;
string [] colorNames = Enum.GetNames(typeof(color));
Console.WriteLine("10 Random colors:" );
for (int index = 0; index < 10; index++)
{
int randomColorIndex = r.Next( 0,colorNames.Length);
randomColorName = colorNames[randomColorIndex];
Console.WriteLine(randomColorName);
}
}
}
} - Random Weighted
using System;
namespace _024_Random_Weighted
{
enum color {Red=21, Green= 12, Blue=23, Agua =14, Crimson=15 , Teal=6, Lime= 17, Orange=18};
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string [] args)
{
Random r = new Random();
Console.WriteLine("10 Random Weighted Colors: " );
for (int index = 0; index < 10; index++)
{
Console.WriteLine (randomEnum(r,typeof(color),true));
}
Console.ReadKey();
}
private static string randomEnum(Random r, Type et, bool weighted)
{
string[] enumNames = Enum.GetNames (et);
String randomEnumName = enumNames[0 ];
if (weighted)
{
int [] eValues = (int[])Enum.GetValues(et);
float[] percentages = calculatePercentages(et);
int rand = r.Next( 1,101);
float accumulatedPercent = 0;
for (int index = 0; index < eValues.Length; index ++)
{
accumulatedPercent += percentages[index];
if (rand <= accumulatedPercent)
{
randomEnumName = enumNames[index];
break;
}
}
}
else
{
int randomEnumIndex = r.Next(0,enumNames.Length);
randomEnumName = enumNames[randomEnumIndex];
}
return (randomEnumName);
}
private static float[] calculatePercentages(Type et)
{
int[] eValues = (int[])Enum.GetValues(et);
float [] percentages = new float[eValues.Length];
int total = 0;
foreach (int eValue in eValues)
{
total += eValue;
}
for (int index = 0; index < percentages.Length; index ++)
{
percentages[index] = ( float)eValues[index] * 100 / total;
}
return percentages;
}
}
} - StringManipulation
using System;
namespace StringManipulation
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string gettysburg =
" Four score and seven years ago our fathers brought forth on this "
+ "continent a new nation, conceived in liberty and dedicated to the "
+ " proposition that all men are created equal. Now we are engaged in "
+ "a great civil war, testing whether that nation or any nation so "
+ " conceived and so dedicated can long endure. We are met on a great "
+ "battlefield of that war. We have come to dedicate a portion of "
+ " that field as a final resting-place for those who here gave their "
+ "lives that that nation might live. It is altogether fitting and "
+ " proper that we should do this. But in a larger sense, we cannot "
+ "dedicate, we cannot consecrate, we cannot hallow this ground. "
+ " The brave men, living and dead who struggled here have consecrated "
+ "it far above our poor power to add or detract. The world will "
+ " little note nor long remember what we say here, but it can never "
+ "forget what they did here. It is for us the living rather to be "
+ " dedicated here to the unfinished work which they who fought here "
+ "have thus far so nobly advanced. It is rather for us to be here "
+ " dedicated to the great task remaining before us--that from these "
+ "honored dead we take increased devotion to that cause for which "
+ " they gave the last full measure of devotion--that we here highly "
+ "resolve that these dead shall not have died in vain, that this "
+ " nation under God shall have a new birth of freedom, and that "
+ "government of the people, by the people, for the people shall "
+ " not perish from the earth.";
// Character Count
string president = "Abraham Lincoln ";
Console.WriteLine("There are {0} characters in {1} ",
president.Length,president);
president = president.Replace ("raham", "e");
Console.WriteLine(" There are {0} characters in {1}",
president.Length,president);
Console.WriteLine();
// Word Search
int pos = 0;
int toWordCount = 0;
string toWord = " to " ;
Console.Write("Locations of the word \" {0}\": ",toWord.Trim());
while ( (pos = gettysburg.IndexOf(toWord,pos,gettysburg.Length - pos)) != -1)
{
Console.Write ("{0} ",pos ++);
toWordCount++;
}
Console.WriteLine();
Console.WriteLine(
"The word \" {0}\" occurs {1} times in the Gettysburg address ",
toWord.Trim(),toWordCount);
Console.WriteLine
( "The last instance of the word \"{0 }\" appears at location {1}",
toWord.Trim(),
gettysburg.LastIndexOf(toWord));
// Formating
Console.WriteLine();
Console.WriteLine("{0}{1} ",president.PadRight(20)," 1861 - 1865");
Console.WriteLine(" {0,-20}{1}",president,"1861 - 1865 ");
string preformatted = string.Format(" {0,-20}{1}",president,"1861 - 1865 ");
Console.WriteLine(preformatted);
Console.WriteLine();
int fourScore = 4* 20;
Console.WriteLine("Four Score is ");
Console.WriteLine("{0} in decimal ".PadLeft(40,' .'),fourScore);
Console.WriteLine(" {0:x} in hex".PadLeft(40, '.'),fourScore);
Console.WriteLine( "{0:e} in scientific notation".PadLeft(40 ,'.'),fourScore);
// Modification
Console.WriteLine();
int locationL = president.IndexOf("L");
president = president.Remove(locationL,3);
Console.WriteLine (president);
president = president.Insert(locationL," Lin");
Console.WriteLine(president);
// Parsing
Console.WriteLine();
Console.WriteLine(" The first 10 words in the Gettysburg address are ");
Console.WriteLine(" _________________________________________________");
string whitespace = " \t";
string[] firstTen = gettysburg.Trim().Split(whitespace.ToCharArray(),11);
firstTen[ firstTen.Length-1] = null;
foreach ( string word in firstTen)
{
Console.WriteLine(word);
}
string[] individualWords = gettysburg.Trim ().
Split(whitespace.ToCharArray(),gettysburg.Length);
Console.WriteLine("There are {0} words in the Gettysburg address ",
individualWords.Length);
// Comparison
string Abe = "abe lincoln" ;
int linguisticalCompare = string.Compare(Abe,president);
Console.WriteLine();
Console.WriteLine(" Linguistically ");
if (linguisticalCompare < 0 )
{
Console.WriteLine("\" {0}\" is less than \ "{1}\"" ,Abe,president);
}
else if (linguisticalCompare > 0)
{
Console.WriteLine("\"{ 0}\" is greater than \" {1}\"",Abe,president);
}
else
{
Console.WriteLine(" \"{0}\ " is equal to \"{1 }\"",Abe,president);
}
int ordinalCompare = string.CompareOrdinal(Abe,president);
Console.WriteLine();
Console.WriteLine("Ordinally ");
if (ordinalCompare < 0)
{
Console.WriteLine ("\"{ 0}\" is less than \" {1}\"",Abe,president);
}
else if (ordinalCompare > 0)
{
Console.WriteLine( "\"{0 }\" is greater than \"{ 1}\"",Abe,president);
}
else
{
Console.WriteLine("\ "{0}\" is equal to \"{1}\ "",Abe,president);
}
Console.Write("press enter ");
Console.ReadLine();
}
}
}
--
Happy day, happy life!
No comments:
Post a Comment