
Contact
C
C++
Visual Basic
Java
JavaScript
DHTML
Style Sheets
About
Normalization
Active X
TDC Binding
PHP
Perl and CGI
Flash
XML
SQL
Chat
MCSE
Linux
Cabling
|
|
C Code
Projects - 2004 C. Germany |
|
|
C is
the predecessor of C++, and as such we
owe much of C++'s power and finesse to
this efficient and beautiful language! |
Currency Converter 3.0
Download: CurrencyConverter.exe
// Curreny Calculator 3.0 - 2004 C. Germany - Team A Software
// Flow Chart At Bottom
#include <stdio.h>
// This function encapsulates the error checking process.
// If we take input as a float or integer, when the user enters
// a character/string, it will throw the program into an infinite
// loop and crash it. If, on the other hand, we take input as
// a string, we can parse the string for ASCII values that relate
// to numbers and then we can handle both kinds of input.
// The first part parses numbers to the left of the decimal,
// then the second part parses numbers to the right.
// If we get a non-number ASCII value, an error message is displayed
// and the the return value is set to 0.0.
//For the "Miracle C" Compiler, these had to be globally declared outside the function.
int x;
float THEamount = 0.0;
float factor; //Factor for decimal places
//---------------------------------------------------------------------------------------------------
float CheckErrors(char * amountString)
{
x = 0;
THEamount = 0.0;
printf("String value passed is %s.", amountString);
while( (amountString[x] - 48) < 10 && (amountString[x] - 48) > 0 )
{
//Multiply by 10 to move the decimal place to the left.
THEamount = 10 * THEamount + (amountString[x] - 48);
x++; //proceed to next character in the array
}
//If last character is a decimal point, we need to move numbers to
//the right instead of the left.
if(amountString[x] == '.')
{
printf("\nI see a decimal point!\n");
x++; //Need to increment x to the next character FIRST
factor = 1.0; //factor needs to be reinitialized each time!
while( (amountString[x] - 48) < 10 && (amountString[x] - 48) > 0 )
{
factor = factor * 0.1; //Decrease by factor of 10
//Add a decimal place to the right
THEamount = THEamount + (amountString[x] - 48) * factor;
x++;
}
//If they type a non-number ASCII value after the decimal flag
//it as an error and return 0.0 as the value.
if(!(amountString[x] - 48) < 10 && (amountString[x] - 48) > 0)
{
printf("\n\nSorry, but that was not a whole number.\n");
printf("The number you have entered will be set to 0 (NULL).\n");
THEamount = 0.0;
return THEamount;
}
} //close block that executes if char was a ‘.’
else
{
//If they type a non-number ASCII value before the decimal flag
//it as an error and return 0.0 as the value.
if(!(amountString[x] - 48) < 10 && (amountString[x] - 48) > 0)
{
printf("\n\nSorry, but that was not a normal number!\n");
printf("The strange string you have entered will be set to 0 NULL.\n");
THEamount = 0.0;
return THEamount;
}
}
return THEamount;
} //close function
//---------------------------------------------------------------------------------------------------
void ConvertCurrency()
{
//Note: "to" = units to 1 US Dollar
// "from" = 1 US Dollar to units
int selection = 100;
float amount = 0.0, result = 0.0;
char amt[10];
//Variable for converting to and from the US dollar. Self explanatory.
float EUROSto, RUBLESto, PESOSto, RUPEESto, YENto, POUNDSto;
float EUROSfrom, RUBLESfrom, PESOSfrom, RUPEESfrom, YENfrom, POUNDSfrom;
EUROSto = .8040;
RUBLESto = 24.00884;
PESOSto = 11.4155;
RUPEESto = 45.9137;
YENto = 108.778;
POUNDSto = .5343;
EUROSfrom = 1.2438;
RUBLESfrom = .0344;
PESOSfrom = .0876;
RUPEESfrom = .0218;
YENfrom = .0092;
POUNDSfrom = 1.8715;
//Print a menu of conversion choices
printf("\n\t**************** Main Menu ***************");
printf("\n\t* *");
printf("\n\t* Choose an option below: *");
printf("\n\t* *");
printf("\n\t* 1. US Dollars to EUROS. *");
printf("\n\t* 2. US Dollars to RUBLES. *");
printf("\n\t* 3. US Dollars to PESOS. *");
printf("\n\t* 4. US Dollars to RUPEES. *");
printf("\n\t* 5. US Dollars to YEN. *");
printf("\n\t* 6. US Dollars to POUNDS. *");
printf("\n\t* 7. EUROS to US Dollars. *");
printf("\n\t* 8. RUBLES to US Dollars. *");
printf("\n\t* 9. PESOS to US Dollars. *");
printf("\n\t* 10. RUPEES to US Dollars. *");
printf("\n\t* 11. YEN to US Dollars. *");
printf("\n\t* 12. POUNDS to US Dollars. *");
printf("\n\t* *");
printf("\n\t******************************************\n\n");
scanf("%d", &selection); // Get their choice
printf("\n\nNow enter the amount to convert (as whole or decimal value): ");
scanf("%s", amt); // Take the input as a string (char array).
amount = CheckErrors(amt); // Pass to error-checking function.
printf("\n\nThe string converted to a float number is: %f .\n\n", amount);
//Perform the calculations based on user's choice.
switch(selection)
{
case 1 : result = amount * EUROSto;
printf("\n%f US dollars is %f EUROS.", amount, result);
break;
case 2 : result = amount * RUBLESto;
printf("\n%f US dollars is %f RUBLES.", amount, result);
break;
case 3 : result = amount * PESOSto;
printf("\n%f US dollars is %f PESOS.", amount, result);
break;
case 4 : result = amount * RUPEESto;
printf("\n%f US dollars is %f RUPEES.", amount, result);
break;
case 5 : result = amount * YENto;
printf("\n%f US dollars is %f YEN.", amount, result);
break;
case 6 : result = amount * POUNDSto;
printf("\n%f US dollars is %f POUNDS.", amount, result);
break;
case 7 : result = amount * EUROSfrom;
printf("\n%f EUROS is %f US dollars.", amount, result);
break;
case 8 : result = amount * RUBLESfrom;
printf("\n%f RUBLES is %f US dollars.", amount, result);
break;
case 9 : result = amount * PESOSfrom;
printf("\n%f PESOS is %f US dollars.", amount, result);
break;
case 10 : result = amount * RUPEESfrom;
printf("\n%f RUPEES is %f US dollars.", amount, result);
break;
case 11 : result = amount * YENfrom;
printf("\n%f YEN is %f US dollars.", amount, result);
break;
case 12 : result = amount * POUNDSfrom;
printf("\n%f POUNDS is %f US dollars.", amount, result);
break;
default : printf("Invalid choice.");
break;
} //close switch
} //close function
//---------------------------------------------------------------------------------------------------
void DisplayChart()
{
//Just display a simple chart with today's exchange rates.
printf("\n\t**************** Currency Rates *************");
printf("\n\t* *");
printf("\n\t* 1. 1 US Dollar = .8040 EUROS. *");
printf("\n\t* 2. 1 US Dollar = 24.00884 RUBLES. *");
printf("\n\t* 3. 1 US Dollar = 11.4155 PESOS. *");
printf("\n\t* 4. 1 US Dollar = 45.9137 RUPEES. *");
printf("\n\t* 5. 1 US Dollar = 108.778 YEN. *");
printf("\n\t* 6. 1 US Dollar = .5343 POUNDS. *");
printf("\n\t* *");
printf("\n\t* 7. EUROS = 1.2438 US Dollars. *");
printf("\n\t* 8. RUBLES = .0344 US Dollars. *");
printf("\n\t* 9. PESOS = .0876 US Dollars. *");
printf("\n\t* 10. RUPEES = .0218 US Dollars. *");
printf("\n\t* 11. YEN to = .0092 Dollars. *");
printf("\n\t* 12. POUNDS = 1.8715 US Dollars. *");
printf("\n\t* *");
printf("\n\t*********************************************\n\n");
}
//---------------------------------------------------------------------------------------------------
void main()
{
int RunProgram = 100;
char choice = ‘z’;
printf("\nCurrency Converter v. 3.0 - 2004 C. Germany - Team A Software!!");
//Lock program into a loop so it will keep running until user selects quit.
while(RunProgram != 0)
{
printf("\n\nMENU - Select an option\n\n");
printf("\n\tQ = Quit");
printf("\n\tC = Convert Curency");
printf("\n\tD = Display Chart");
printf("\n\n\tEnter your choice: ");
scanf("%s", &choice);
//Main menu choices
switch(tolower(choice))
{
case ‘q’ : RunProgram = 0;
break;
case ‘c’ : ConvertCurrency(); //Call conversion menu
break;
case ‘d’ : DisplayChart();
break;
default : printf("Sorry, invalid choice.");
break;
} //close switch statement
} //close while true loop on choice
//User has chosen to quit - display exit message.
printf("\nYou have choosen to quit.\nEnding calculator program. Exiting...\n\n");
} // close main
|
Number
Guessing Game
Download: NumGame.exe
#include <stdio.h>
//--------------------------------------------------------------------------------------------------------
int RollEmBaby()
{
//Yeah, I know it's pseudo random, but good enough for our purposes ...
int HighNumber;
int LowNumber;
int RandomResult;
HighNumber = 12;
LowNumber = 2;
srand(time(NULL));
RandomResult = (rand()%HighNumber) + LowNumber;
return RandomResult;
}
//--------------------------------------------------------------------------------------------------------
void main(void)
{
int ComputersNumber;
int UsersChoice;
ComputersNumber = RollEmBaby();
printf("There are two ways to win, and both depend\n");
printf("on whether or not lady luck shines.\n\n");
printf("1. If you guess the number, you win. For this,\n");
printf("you have a snow ball\'s chance in Hell! :) \n\n");
printf("2. At slightly better odds, your second chance\n");
printf("is if you roll snake eyes or double sixes!\n\n");
printf("Now that\'s a pretty fair deal, right?");
printf("\n\n What number do you guess? ");
scanf("%d", &UsersChoice);
printf("\n\nThe number rolled was: %d .", ComputersNumber);
if(UsersChoice == ComputersNumber)
{
printf("\n\nYou did it! Unbelieveable luck! You win!\n\n");
}
if(ComputersNumber == 2)
{ printf("\n\nYou win! Snake Eyes! Yeah!"); }
if(ComputersNumber >2 && ComputersNumber < 12)
{
if(ComputersNumber != UsersChoice)
{
printf("\n\nSorry . . . You loose.\n\n");
}
}
if(ComputersNumber == 12)
{ printf("\n\nYou win! Double sixes!!!"); }
} // close main()
//Version 2.0
#include <stdio.h>
void Greet()
{
char TheirName[20];
printf("Hi! What\'s your name?\t");
scanf("%s", TheirName);
printf("\n\nHello, %s, I hope you are having a nice day!\n", TheirName);
}
int LuckyNumber(int low, int high)
{
int PlayerGuess;
int Lucky;
srand(time(NULL));
Lucky = (rand()%low) + high;
return Lucky;
}
void main(void)
{
int MagicNumber;
int PlayerGuess;
Greet();
MagicNumber = LuckyNumber(1, 6);
printf("\nWhat\'s your best guess? ");
scanf("%i", &PlayerGuess);
printf("\n\nReceived %d from player.\n\n");
if(PlayerGuess == MagicNumber)
{
printf("\n\nYou did it! You guessed my lucky number!\n\n");
}
else
{
printf("\n\nSorry, you did not guess it.\n\n");
printf("The number was %d", MagicNumber);
}
}
|
Multidimensional Arrays of Type
Char and Command Line Arguments
Download: CommandLine.exe
#include <stdio.h>
#include <ctype.h>
int main(int argc, char * argv[])
{
//2004 - C. Germany. The string arguments passed to the command line are
//actually multi-dimensional arrays, so we need nested for loops.
char * tempstring;
printf("I received %d arguments!\n", argc);
for(int i=0; i<argc; i++)
{
printf("\nArgument %d:", i);
tempstring = argv[i];
printf("You typed: \"");
printf(tempstring);
printf("\". In capital letters that\'s \"");
for(int z = 0; tempstring[z]; z++)
putchar(toupper(tempstring[z]));
printf("\".");
}
printf("\n\n");
char * t1;
t1 = "the end";
for(int x=0; t1[x]; x++)
putchar(toupper(t1[x]));
printf("\n\n\n");
return 0;
}
|
Multidimensional Arrays to Save
Unnecessary Lines of Code
| Download: MultiArray.exe
//2004 - C.
Germany // Parsing a string for integer errors without isdigit() and
cctype
#include <iostream>
void main(void)
{
//Take three numbers as a multi-dimensional array of strings
char FavNumber[3][10];
int Num[3] = {0, 0, 0}; //initialize to NULL
int x = 0;
int count;
printf("Enter your three favorite numbers: \n\n");
for(count = 0; count<3; count++)
{
//scanf("%d%d%d", &a, &b, &c);
printf("Enter favorite number %d: ", count+1); //add 1 for fence post
scanf("%s", FavNumber[count]);
//Need to convert the ASCII value character array to integer values
x = 0; //reset x to 0 for each loop
while( (FavNumber[count][x] - 48) < 10 && (FavNumber[count][x] -
48) > 0 )
{
Num[count] = 10 * Num[count] + (FavNumber[count][x]
- 48);
x++;
}
if(!(FavNumber[count][x] - 48) < 10 && (FavNumber[count][x] - 48) >
0)
{ //offset for fencepost - x was incremented 1 past
printf("\n\nHey, not fair! That was not a plain old number!\n\n");
printf("I will set this garbledygunk number to 0 (NULL)!\n\n");
Num[count] = 0;
}
} //close the for loop
for(count = 0; count<3; count++)
{
printf("\nFavorite number %d as a string values is: %s .",
(count+1),FavNumber[count]);
printf("\nFavorite number %d as an integer value is: %d \n",
(count+1), Num[count]);
} //close for loop
} //close main()
//2004 C. Germany - Accounting for User Error
#include <iostream>
#include <cctype>
using namespace std;
void main(void)
{
//Take three numbers as a multi-dimensional array of stirngs
char FavNumber[3][10];
int Num[3] = {0, 0, 0}; //initialize to NULL
int x = 0;
int count;
printf("Enter your three favorite numbers: \n\n");
for(count = 0; count<3; count++)
{
//scanf("%d%d%d", &a, &b, &c);
printf("Enter favorite number %d: ", count+1); //add 1 for fence post
scanf("%s", FavNumber[count]);
//Need to convert the ASCII value character array to integer values
x = 0; //reset x to 0 for each loop
while(isdigit(FavNumber[count][x]))
{
Num[count] = 10 * Num[count] + (FavNumber[count][x] - 48);
x++;
}
if(!isdigit(FavNumber[count][x-1]))
{ //offset for fencepost - x was incremented 1 past
printf("\n\nHey, not fair! That was not a plain old
number!\n\n");
printf("I will set this garbledygunk number to 0 (NULL)!");
Num[count] = 0;
}
} //close the for loop
for(count = 0; count<3; count++)
{
printf("\nFavorite number %d as a string values is: %s .",
(count+1),FavNumber[count]);
printf("\nFavorite number %d as an integer value is: %d \n",
(count+1), Num[count]);
} //close for loop
} //close main() |
Arrays and Loops
Download: Alphabet.exe
// C Program Example - C. Germany
// Implement the following program using a while loop instead of a for loop
#include <stdio.h>
void main(void)
{
int counter;
char Alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
//Alphabet forwards
printf("This is for my son Jacob who can now get to A-B-C-D-E!\n\n");
printf("Here\'s the phonetic alphabet forwards, Jacob:\n\n ");
counter = 0;
while(counter <= 26)
{
printf("%c", Alphabet[counter]);
counter++;
}
printf("\n\n");
//Alphabet backwards
printf("\n\nHere\'s the phonetic alphabet backwards:\n\n ");
counter = 26;
while(counter >= 0)
{
printf("%c", Alphabet[counter]);
counter--;
}
}
// Same behavior but using for loops instead of while true
#include <stdio.h>
void main(void)
{
int counter;
char Alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
//Alphabet forwards
printf("This is for my son Jacob who can now get to A-B-C-D-E!\n\n");
printf("Here\'s the phonetic alphabet forwards, Jacob:\n\n ");
for (counter = 0; counter <= 26; counter++)
printf("%c", Alphabet[counter]);
printf("\n\n");
//Alphabet backwards
printf("\n\nHere\'s the phonetic alphabet backwards:\n\n ");
for (counter = 26; counter >= 0; counter--)
printf("%c", Alphabet[counter]);
}
|
Reversing For Loops
Download: ReverseLoop.exe
#include <stdio.h>
void main(void)
{
int counter;
for (counter = 1; counter <= 5; counter++)
printf("%d ", counter);
printf("\nStarting second loop\n");
for (counter = 1; counter <= 10; counter++)
printf("%d ", counter);
printf("\nStarting third loop\n");
for (counter = 0; counter <= 5; counter++)
printf("%d ", counter);
}
|
Pointers
Download: Pointers.exe
#include <stdio.h>
void main(void)
{
int NumOne = 1;
int NumTwo = 2;
int * IAmAPointerToAnInteger;
// Assign pointer to an address
IAmAPointerToAnInteger = &NumOne;
// Change the value pointed to by IAmAPointerToAnInteger to 5
*IAmAPointerToAnInteger = 5;
// Display the value
printf("The value pointed to by IAmAPointerToAnInteger is %d . The variable NumOne is %d\n",
*IAmAPointerToAnInteger, NumOne);
}
|
Temperature Conversion
| Download: Temperature.exe
//2004 C. Germany -
Convert Temperatures
#include <stdio.h>
void ConvertTemp()
{
//Note: "to" = units to 1 US Dollar
// "from" = 1
US Dollar to units
int selection;
float temperature, result;
printf("\nChoose:");
printf("\n1 = Convert Farenheit to Celcius.");
printf("\n2 = Convert Celcius to Farenheit.");
printf("\n\nYour choice: ");
scanf("%d", &selection);
printf("\n\nNow enter the temperature to convert: ");
scanf("%f", &temperature);
switch(selection)
{
//Convert
Farenheit to Celcius
case 1 :
result = ( (temperature - 32) * 5 ) / 9;
printf("\n %f degrees farenheit is %f degrees celcius.",
temperature, result);
break;
//Convert Celcius to Farenheit
case 2 :
result = (temperature * 9 / 5) + 32;
printf("\n %f degrees celcius is %f degrees farenheit.",
temperature, result);
break;
default : printf("Invalid choice.");
break;
} //close switch
} //close function
//-------------------------------------------------------------------------------------------------------------------------------
void main()
{
int RunProgram = 100;
int choice = 100;
printf("\nTempertature Converter - 2004 C. Germany -
Team A Software!!");
while(RunProgram != 0)
{
printf("\n\nMENU
- Select an option\n\n");
printf("\n0 =
Quit");
printf("\n1 =
Convert Temperature");
;
printf("\n\nEnter
your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0 : RunProgram = 0;
break;
case 1 : ConvertTemp();
break;
default : printf("Sorry, invalid choice.");
break;
} //close switch statement
} //close while true loop on choice
printf("\nYou have choosen to quit.\nEnding temperature
conversion program. Exiting...\n\n");
} // close main
|
How Many Lines?
| Download: Lines.exe
#include <stdio.h>
void main(void)
{
int NumTimes;
int x;
printf("How many lines woud you like? ");
scanf("%d", &NumTimes);
//NumTimes = 4;
printf("Received the number %d .", NumTimes);
printf("\n\n");
if(NumTimes < 500 && NumTimes >0)
{
for(x = 0; x < NumTimes; x++)
{
printf("This is line number ");
//Need to adjust the offset for the fencepost error
printf("%d", x+1);
printf(".\n");
}
}
else
{
if(NumTimes <= 0)
{
printf("Printing 0 or less lines is pretty pointless.");
}
else
{
printf("More than 500?!? That is too many lines! We don\'t have all day!");
}
}
printf ("\n\n");
printf ("The End");
printf ("\n\n");
} // close main()
Download: Lines2.exe
#include <stdio.h>
void main()
{
int NumTimes;
int x;
int StopIt;
StopIt = 0; //false
while(StopIt != 1)
{
printf("How many lines woud you like? ");
scanf("%d", &NumTimes);
//NumTimes = 4;
printf("Received the number %d .", NumTimes);
printf("\n\n");
switch(NumTimes)
{
case 0 : printf("Too few lines to print!\n");
StopIt = 1; //true
break;
case 1 : printf("A line!\n");
StopIt = 1; //true
break;
case 2 : printf("A line!\nA line!\n");
StopIt = 1; //true
break;
case 3 : printf("A line!\nA line!\nA line!\n");
StopIt = 1; //true
break;
default : printf("That is not an option!\n");
break;
} // close switch
} // close while true loop
printf("\n\n");
printf("The End");
printf("\n \n");
} // close main() function
|
Array of Strings
Download: StringArray.exe
#include <stdio.h>
char * GetName()
{
char pName[20];
printf("Please tell me your name: ");
scanf("%s", pName);
printf("Received the string: ");
printf(pName);
printf(" while inside the GetName function.");
printf("\nNow exiting the GetName() function.");
return pName;
}
void main(void)
{
int x;
char * Messages[3] = {"C++ ", "is very close ", "to C.\n\n"};
char * TheName;
TheName = GetName();
printf("\n\n");
printf(TheName);
printf(" , the 3 messages are: ");
for(x = 0; x <3; x++)
{
printf(Messages[x]);
}
printf("\n\nThe End");
}
|
Detecting Numbers With
IsDigit()
#include <stdio.h>
#include <cctype.h>
void main(void)
{
char FavNumber[10];
int Num, x;
int a, b, c;
x = 0;
printf("Enter your three favorite numbers: ");
//scanf("%d%d%d", &a, &b, &c);
scanf("%s", FavNumber);
printf("Your three favorite numbers as string values are: %s \n\n", FavNumber);
//Need to convert the ASCII value character array to integer values
while(isdigit(FavNumber[x]))
{
if(!isdigit(FavNumber[x]))
{
printf("\n\nHey! That's not a number! Invalid.\n\n");
break;
} //close if
else
{
Num = 10 * Num + (FavNumber[x] - 48);
x++;
} //close else
} //close while true loop
printf("Your favorite numbers as integers are: %d \n", Num);
}
|
Even Numbers
include <stdio.h>
void main(void)
{
int remainder;
int result;
int x;
printf("Counting even numbers from 0 -
100:\n\n");
for(x=0; x<=100; x++)
{
result = x % 2;
if(result == 0)
{
printf("\t");
printf("%d", x);
}
}
} |
©2004 C. Germany

|
|