Thursday, September 8, 2011

9.8.11

Dim quote As String = "The important thnig is not to " & "stop questioning. --Albert Einstein"

'---left(quote, 3)
MsgBox(quote.Substring(0, 3))

'-----mid(quote,5,9)
MsgBox(quote.Substring(4, 9))

'-----right(quote, 8)
MsgBox(quote.Substring(quote.Length - 8))


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oldString As String = "The important thing is not to stop questioning. --Albert Einstein"
Dim newString As String = ""
Dim displayString As String = ""
Dim counter As Integer = 1


newString = oldString.ToUpper
addToString(newString, displayString, counter)

newString = UCase(oldString)

'to lower case
newString = oldString.ToLower()
addToString(newString, displayString, counter)
newString = LCase(oldString)

newString = StrConv(oldString, VbStrConv.ProperCase)

addToString(newString, displayString, counter)

newString = MixedCase(oldString)

addToString(newString, displayString, counter)

'---display results
MsgBox(displayString)

End Sub

Public Function MixedCase(ByVal origText As String) As String
'---convert a string to "proper" case
Dim counter As Integer
Dim textParts() As String = Split(origText, " ")

For counter = 0 To textParts.Length - 1
If (textParts(counter).Length > 0) Then
textParts(counter) = UCase(Microsoft.VisualBasic.Left(textParts(counter), 1)) & LCase(Mid(textParts(counter), 2))
End If
Next

Return Join(textParts, " ")
End Function

Private Sub addToString(ByVal addElem, ByRef addToElem, ByRef counter)
addToElem &= "[" & counter & "] " & addElem & vbCrLf & vbCrLf
counter = counter + 1
End Sub
End Class


#include
#define RATE1 0.12589
#define RATE2 0.17901
#define RATE3 0.20971
#define BREAK1 360.0
#define BREAK2 680.0
#define BASE1 (RATE1 * BREAK1)
#define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1)))

int main(void)
{
double kwh;
double bill;

printf("Please enter the kwh used.\n");
scanf("%lf", &kwh);
if (kwh <= BREAK1)
bill = RATE1 * kwh;
else if (kwh <= BREAK2)
bill = BASE1 + (RATE2 * (kwh - BREAK1));
else
bill = BASE2 + (RATE3 * (kwh - BREAK2));
printf("The charge for %.1f is $%1.2f.\n", kwh, bill);
return 0;

}


#include
#include

int main(void)
{
unsigned long num;
unsigned long div;
bool isPrime;

printf("Please enter an integer for analysis;");
printf("Enter q to quit.\n");
while (scanf("%lu", &num) == 1)
{
for (div = 2, isPrime = true; (div * div) <= num; div++)
{
if (num % div == 0)
{
if ((div * div) != num)
{
printf("%lu is divisible by %lu and %lu.\n", num, div, num / div);
} else {
printf("%lu is divisible by %lu.\n", num, div);
}

isPrime = false; //number is not prime
}
}
if (isPrime)
{
printf("%lu is prime.\n", num);
}
printf("Please enter another # for analysis;");
printf("Enter q to quit.\n");
}
printf("Bye.\n");

return 0;
}


#include
#include
#include
#define STOP '|'

int main(void)
{
char c; //read in character
char prev;//previous character read
long n_chars = 0L; //number of characters
int n_lines = 0; //number of lines
int n_words = 0; //number of words
int p_lines = 0; //number of partial lines
bool inword = false; // true if c is in a word

printf("Enter text to be analyzed (|to terminate):\n");
prev = '\n'; //used to identify complete lines
while ((c = getchar()) != STOP)
{
n_chars++;
if (c == '\n')
{
n_lines++; //count lines
}
if (!isspace(c) && !inword)
{
inword = true;
n_words++;
}
if (isspace(c) && inword)
{
inword = false; //reached end of word
}
prev = c;
}

if (prev != '\n')
{
p_lines = 1;
}
//%ld used for long
printf("characters = %ld, words = %d, lines =%d,", n_chars, n_words, n_lines);
printf("partial lines = %d\n", p_lines);

return 0;

}


#include
#define COVERAGE 200
//because the program is using type int, the division is truncated. that is, 215/200 becomes 1
int main(void)
{
int sq_feet;
int cans;

printf("Enter number of square feet to be painted:\n");
while (scanf("%d", &sq_feet) == 1)
{
cans = sq_feet / COVERAGE;
cans += ((sq_feet % COVERAGE == 0)) ? 0 : 1;
printf("You need %d %s of paint.\n", cans, cans == 1 ? "can" : "cans");
printf("Enter next value (q to quit):\n");
}

return 0;
}

No comments: