Tuesday, January 10, 2012

Tuesday 1-10-12

@autoreleasepool {
int number_to_test, remainder;

NSLog(@"Enter your number to be tested: ");
scanf("%i", &number_to_test);

remainder = number_to_test % 2;

if (remainder == 0)
{
NSLog(@"The number is even.");
}
if (remainder != 0)
{
NSLog(@"The number is odd.");
}
}


@autoreleasepool {
int number_to_test, remainder;

NSLog(@"Enter your number to be tested:");
scanf("%i", &number_to_test);

remainder = number_to_test % 2;

if(remainder == 0)
NSLog(@"The number is even.");
else
NSLog(@"The number is odd.");
}

@autoreleasepool {
int year, rem_4, rem_100, rem_400;
NSLog(@"Enter the year to be tested: ");
scanf("%i", &year);

rem_4 = year % 4;
rem_100 = year % 100;
rem_400 = year % 400;

if ((rem_4 == 0 && rem_100 != 0) || rem_400 == 0)
NSLog(@"It's a leap year.");
else
NSLog(@"Nope, it's not a leap year.");
}

@autoreleasepool {
int number, sign;

NSLog(@"Please type in a number: ");
scanf("%i", &number);

if(number < 0)
sign = -1;
else if (number == 0)
sign = 0;
else //must be positive
sign = 1;
NSLog(@"Sign = %i", sign);

}

@autoreleasepool {
char c;
NSLog(@"Enter a single character:");
scanf(" %c", &c);

if((c>='a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
NSLog(@"It's an alphabetic characer.");
else if (c >= '0' && c <= '9')
NSLog(@"It's a digit");
else
NSLog(@"It's a special character.");

}


#import

@interface Calculator: NSObject
//accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

//arithmetic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end

@implementation Calculator
{
double accumulator;
}
-(void) setAccumulator:(double) value
{
accumulator = value;
}

-(void) clear
{
accumulator = 0;
}

-(double) accumulator
{
return accumulator;
}

-(void) add: (double) value
{
accumulator += value;
}

-(void) subtract:(double) value
{
accumulator -= value;
}

-(void) multiply:(double) value
{
accumulator *= value;
}

-(void) divide:(double) value
{
accumulator /= value;
}
@end


int main (int argc, const char * argv[])
{

@autoreleasepool {
double value1, value2;
char operator;
Calculator *deskCalc = [[Calculator alloc] init];
NSLog(@"Type in your expression.");
scanf("%lf %c %lf", &value1, &operator, &value2);
[deskCalc setAccumulator: value1];
if (operator=='+')
[deskCalc add:value2];
else if (operator == '-')
[deskCalc subtract: value2];
else if (operator == '*')
[deskCalc multiply: value2];
else if (operator == '/')
[deskCalc divide: value2];

NSLog(@"%.2f", [deskCalc accumulator]);

}
return 0;
}


#include
int main(void)
{
int num;
num = 1;
printf("I am a simple ");
printf("computer.\n");
printf("My favorite number is %d because it is first.\n", num);

return 0;
}


class Program
{
static void Main(string[] args)
{
LocalVarDeclarations();
NewingDataTypes();
ObjectFunctionality();
DataTypeFunctionality();
BooleanFunctionality();
CharFunctionality();
}

static void LocalVarDeclarations()
{
Console.WriteLine("=> Data Declarations:");
//local variables are delcared and initialized as follows:
//dataType varName = initialValue;
int myInt = 0;

//You can also declare and assign on two lines
string myString;
myString = "This is my character data";
Console.WriteLine("{0}", myInt);
Console.WriteLine("{0}", myString);
Console.WriteLine();
}

static void NewingDataTypes()
{
Console.WriteLine("=> Using new to create variables:");
bool b = new bool();
int i = new int();
double d = new double();
DateTime dt = new DateTime();
Console.WriteLine("{0}, {1}, {2}, {3}", b, i, d, dt);
Console.WriteLine();
}

static void ObjectFunctionality()
{
Console.WriteLine("=> System.Object functionality:");
//a C# int is really a shorthand for System.Int32
//which inherits the following members from System.Object
Console.WriteLine("12.GetHashCode() = {0}", 12.GetHashCode());
Console.WriteLine("12.Equals(23) = {0}", 12.Equals(23));
Console.WriteLine("12.ToString() = {0}", 12.ToString());
Console.WriteLine("12.GetType() = {0}", 12.GetType());
Console.WriteLine();
}

static void DataTypeFunctionality()
{
Console.WriteLine("=> Data type Functionality: ");
Console.WriteLine("Max of int: {0}", int.MaxValue);
Console.WriteLine("Min of int: {0}", int.MinValue);
Console.WriteLine("Max of double: {0}", double.MaxValue);
Console.WriteLine("Min of double: {0}", double.MinValue);
Console.WriteLine("double.Epsilon: {0}", double.Epsilon);
Console.WriteLine("double.PositiveInfinity: {0}", double.PositiveInfinity);
Console.WriteLine("double.NegativeInfinity: {0}", double.NegativeInfinity);
Console.WriteLine();

}

static void BooleanFunctionality()
{
Console.WriteLine("bool.FalseString: {0}", bool.FalseString);
Console.WriteLine("bool.TrueString: {0}", bool.TrueString);
}

static void CharFunctionality()
{
Console.WriteLine("=> char type Functionality:");
char myChar = 'a';
Console.WriteLine("char.IsDigit('a'): {0}", char.IsDigit(myChar));
Console.WriteLine("char.IsLetter('a'): {0}", char.IsLetter(myChar));
Console.WriteLine("char.IsWhiteSpace('Hello There', 5): {0}", char.IsWhiteSpace("Hello There", 5));
Console.WriteLine("char.IsWhiteSpace('Hello There', 6): {0}", char.IsWhiteSpace("Hello There", 6));
Console.WriteLine("char.IsWhiteSpace('A Person', 0): {0}", char.IsWhiteSpace("A Person", 0));
Console.WriteLine("char.IsWhiteSpace('A Person'), 1): {0}", char.IsWhiteSpace("A Person", 1));
Console.WriteLine("char.IsPunctuation('?'): {0}", char.IsPunctuation('?'));
Console.WriteLine();
}
}

No comments:

Post a Comment