C program for TCC

7 04 2009

It has been a while since I last blogged and I figured I should put something up. I wrote a C program for training purposes for my last job. It’s just a simple command-line calculator. Enjoy!
// c_training.c
// Written by Patrick Proctor
// on February 16, 2009
// patproct@iupui.edu
//
// Last modified: March 2, 2009
//
// This file demonstrates a small portion
// of my mad programming SKILLZ.

#include <stdio.h>
#include "c_training.h"
int main () {
int menuChoice;
mainMenu ();
printf( "Menu choice: " );
scanf( "%d", &menuChoice );
if ( ( menuChoice < 1 || menuChoice > 6 ) && menuChoice != 42  ){
printf("Program has exited.\n");
} // eliminates any selection that is not 1-5
if ( menuChoice == 1 ) {
add();
}
if ( menuChoice == 2 ) {
subtract();
}
if ( menuChoice == 3 ) {
multiply();
}
if ( menuChoice == 4 ) {
divide();
}
if ( menuChoice == 5 ) {
modulus();
}
if ( menuChoice == 6 ) {
about();
}
if ( menuChoice == 42 ) {
blownMind();
}
return 0;
}
int mainMenu () {
printf( "|      Calculator      |\n" );
printf( "|======================|\n" );
printf( "| 1. Add               |\n" );
printf( "| 2. Subtract          |\n" );
printf( "| 3. Multiply          |\n" );
printf( "| 4. Divide            |\n" );
printf( "| 5. Modulus           |\n" );
printf( "|----------------------|\n" );
printf( "| 6. About             |\n" );
printf( "| 7. Exit              |\n" );
printf( "|======================|\n" );
return 0;
}
int add () {
float alpha;
float beta;
float gamma;
printf( "Enter two numbers to add, separated\n by a space: " );
scanf( "%f %f", &alpha, &beta );
gamma = alpha + beta;
printf( "Sum of %f and %f is %f.\n", alpha, beta, gamma);
return 0;
}
int subtract () {
float alpha;
float beta;
float gamma;
printf( "Enter two numbers to subtract, separated\n by a space: ");
scanf( "%f %f", &alpha, &beta );
gamma = alpha - beta;
printf( "Difference of %f and %f is %f.\n", alpha, beta, gamma );
return 0;
}
int multiply () {
float alpha;
float beta;
float gamma;
printf( "Enter two numbers to multiply, separated\n by a space: " );
scanf( "%f %f", &alpha, &beta );
gamma = alpha * beta;
printf( "Product of %f and %f is %f.\n", alpha, beta, gamma );
return 0;
}
int divide () {
float alpha;
float beta;
float gamma;
printf( "Enter two numbers to divide, separated\n by a space: " );
scanf( "%f %f", &alpha, &beta );
gamma = alpha / beta;
printf( "Quotient of %f and %f is %f.\n", alpha, beta, gamma );
return 0;
}
int modulus () {
int alpha;
int beta;
int gamma;
printf( "Enter two integers to find a modulus, separated\n by a space: " );
scanf( "%d %d", &alpha, &beta );
gamma = alpha % beta;
printf( "Modulus of %d and %d is %d.\n", alpha, beta, gamma );
return 0;
}
int about() {
printf( "|=============================================|\n" );
printf( "|                    About                    |\n" );
printf( "|=============================================|\n" );
printf( "| This program was written by Patrick Proctor |\n" );
printf( "| out of boredom on Monday, February 16, 2009 |\n" );
printf( "| in NU342. Enjoy!                            |\n" );
printf( "|                                             |\n" );
printf( "|---------------------------------------------|\n" );
printf( "|                                             |\n" );
printf( "| patrick42h@gmail.com                        |\n" );
printf( "| https://patrick42h.wordpress.com/            |\n" );
printf( "|=============================================|\n\n" );
return 0;
}
Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: