Imports System
Namespace StringSearch
Class Tester
Public Sub Run()
'create some strings to work with
Dim s1 As String = "One Two Three Four"
Dim index As Integer
'get the index of the last space
index = s1.LastIndexOf(" ")
'get the last word
Dim s2 As String = s1.Substring((index + 1))
'set s1 to the substring starting at 0
'and ending at index (the start of the last word)
'thus s1 has One Two Three
s1 = s1.Substring(0, index)
'find the last space in s1 (after "two")
index = s1.LastIndexOf(" ")
'set s3 to the substring starting at index
'the space after "two" plus one more
'thus s3 = "three"
Dim s3 As String = s1.Substring((index + 1))
'reset s1 to the substring starting at 0
'and ending at index, thus the string "one Two"
s1 = s1.Substring(0, index)
'reset index to the space betwee "One" and "Two"
index = s1.LastIndexOf(" ")
'set s4 to the substring starting one space after index, thus the substring "two"
Dim s4 As String = s1.Substring((index + 1))
'reset s1 to teh substring starting at 0
' and ending at index, thus "one"
s1 = s1.Substring(0, index)
'set index to the last space, but there is none so index now = -1
index = s1.LastIndexOf(" ")
'set s4 to the substring at one past the last space, there was no last space
'so this sets s5 to the substring starting at zero
Dim s5 As String = s1.Substring((index + 1))
Console.WriteLine("s1: {0}", s1)
Console.WriteLine("s2: {0}", s2)
Console.WriteLine("s3: {0}", s3)
Console.WriteLine("s4: {0}", s4)
Console.WriteLine("s5: {0}", s5)
End Sub 'run
Public Shared Sub main()
Dim t As New Tester()
t.Run()
End Sub 'main
End Class 'tester
End Namespace 'StringSearch
Public Sub Run()
'create some strings to work with
Dim s1 As String = "One, Two, Three Liberty Associates, Inc."
'constants for the space and comma characters
Const Space As Char = " "c
Const Comma As Char = ","c
'array of delimiters to split the sentence with
Dim delimiters() As Char = {Space, Comma}
Dim output As String = ""
Dim ctr As Integer = 0
'split the string and then iterate over the
'resulting array of string
Dim resultArray As String() = s1.Split(delimiters)
Dim substring As String
For Each substring In resultArray
ctr = ctr + 1
output &= ctr.ToString()
output &= ": "
output &= substring
output &= Environment.NewLine
Next
Console.WriteLine(output)
End Sub 'run
namespace SimpleProject2
{
class Program
{
static int Main(string[] args)
{
ShowEnvironmentDetails();
Console.ReadLine();
return -1;
}
static void ShowEnvironmentDetails()
{
//print out drives no this machine, and other interesting details
foreach (string drive in Environment.GetLogicalDrives())
Console.WriteLine("Drive: {0}", drive);
Console.WriteLine("OS: {0}", Environment.OSVersion);
Console.WriteLine("Number of processors: {0}", Environment.ProcessorCount);
Console.WriteLine(".NET Version: {0}", Environment.Version);
}
}
}
static int Main(string[] args)
{
Console.WriteLine("***** Basic Console I/O *****");
GetUserData();
Console.ReadLine();
return -1;
}
static void GetUserData()
{
//get name and age
Console.Write("Please enter your name: ");
string userName = Console.ReadLine();
Console.Write("Please enter your age: ");
string userAge = Console.ReadLine();
//change echo color, just for fun
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
//echo the console
Console.WriteLine("Hello {0}! You are {1} years old.", userName, userAge);
//restore previous color;
Console.ForegroundColor = prevColor;
}
static int Main(string[] args)
{
Console.WriteLine("***** Format Numerical Data *****");
FormatNumericalData();
Console.ReadLine();
return -1;
}
static void FormatNumericalData()
{
Console.WriteLine("The value 99999 in various formats:");
Console.WriteLine("c format: {0:c}", 99999);
Console.WriteLine("d9 format: {0:d9}", 99999);
Console.WriteLine("f3 format: {0:f3}", 99999);
Console.WriteLine("n format: {0:n}", 99999);
//notice that upper or lowercasing for hex
//determines if letters are upper or lowercase
Console.WriteLine("E format: {0:E}", 99999);
Console.WriteLine("e format: {0:e}", 99999);
Console.WriteLine("X format: {0:X}", 99999);
}
#import
int main (int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Programming is fun!");
NSLog(@"Programming in Objective-C is even more fun!");
NSLog(@"Testing...\n...1\n...2\n...3");
int sum;
sum = 50 + 25;
NSLog(@"The sum of 50 and 25 is %i", sum);
int value1, value2;
value1 = 50;
value2 = 25;
sum = value1 + value2;
NSLog(@"\nThe sum of %i and %i is %i", value1, value2, sum);
}
return 0;
}
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"In Objective-C, lowercase letters are significant");
NSLog(@"\n main is where program execution begins.");
NSLog(@"\nOpen and closed braces enclose program statements in a routine.");
int difference;
difference = 87 - 15;
NSLog(@"\n The difference of 87 and 15 is %i", difference);
int answer, result;
answer = 100;
result = answer - 10;
NSLog(@"The result is %i\n", result + 5);
}
return 0;
}
#import
@interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
@end
// ----- program section -------
int main (int argc, const char * argv[])
{
@autoreleasepool {
Fraction *myFraction;
//create an instance of a fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];
//set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
//display the fraction using the print method
NSLog(@"The value of myFraction is:");
[myFraction print];
}
return 0;
}
//----- interface section -----
@interface Fraction: NSObject
-(void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
@end
//---implementation section -----
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
@end
// ----- program section -------
int main (int argc, const char * argv[])
{
@autoreleasepool {
Fraction *frac1 = [[Fraction alloc] init];
Fraction *frac2 = [[Fraction alloc] init];
//set first fraction to 2/3
[frac1 setNumerator: 2];
[frac1 setDenominator: 3];
//set second fraction to 3/7
[frac2 setNumerator: 3];
[frac2 setDenominator: 7];
//display the fractions
NSLog(@"First fraction is:");
[frac1 print];
NSLog(@"Second fraction is:");
[frac2 print];
}
return 0;
}
@interface Fraction: NSObject
- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
- (int) numerator;
- (int) denominator;
@end
//--- @implementation section ----
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
- (void) setDenominator: (int) d
{
denominator = d;
}
- (int) numerator
{
return numerator;
}
- (int) denominator
{
return denominator;
}
@end
// ----- program section -------
int main (int argc, const char * argv[])
{
@autoreleasepool {
Fraction *myFraction = [[Fraction alloc] init];
//set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
//display the fraction using our two new methods
NSLog(@"The value of myFraction is: %i/%i", [myFraction numerator], [myFraction denominator]);
}
return 0;
}
int main (int argc, const char * argv[])
{
{
@autoreleasepool {
int integerVar = 100;
float floatingVar = 331.79;
double doubleVar = 8.44e+11;
char charVar = 'W';
NSLog(@"integerVar = %i", integerVar);
NSLog(@"floatingVar = %f", floatingVar);
NSLog(@"doubleVar = %g", doubleVar);
NSLog(@"charVar = %c", charVar);
}
}
return 0;
}
int main (int argc, const char * argv[])
{
{
@autoreleasepool {
int a = 100;
int b = 2;
int c = 25;
int d = 4;
int result;
result = a - b;
NSLog(@"a - b = %i", result);
result = b * c;
NSLog(@"b * c = %i", result);
result = a / c;
NSLog(@"a / c = %i", result);
result = a + b * c;
NSLog(@"a + b * c = %i", result);
NSLog(@"a * b + c * d = %i", a * b + c * d);
}
}
return 0;
}
@autoreleasepool {
int a = 25;
int b = 2;
float c = 25.0;
float d = 2.0;
NSLog(@"6 + a / 5 * b = %i", 6 + a / 5 * b);
NSLog(@"a / b * b = %i", a / b * b);
NSLog(@"c / d * d = %f", c / d * d);
NSLog(@"-a = %i", -a);
}
No comments:
Post a Comment