All pastes #792546 Raw Edit

roundyz

public c v1 · immutable
·published 2007-11-21 14:40 UTC
rendered paste body
#include "stdafx.h"											/*This is for visual studio or windows, not mentioned in K&R*/#include <stdio.h>#include <string.h>#include <ctype.h>/*Function prototypesTODO - NB stick these in a .h file later*/char* convertCharToMorseCode(char *input);						/*converts a char  to its morse equvilent*/void inputFromKeyboard();										/*gets input from the keyboard*/void inputFromFile(char *filename);									/*gets input from a file*/void printMenu()	;											/*prints out the menu*/      #define USERINPUTLENGTH 15									/* the max length a user has to work with */#define ALPHALENGTH 26										/* ammount of letters in the alphabet */char morseData[]=" .- -...-.-.-.. .   ..-.--. ......  .----.- .-..--  -.  --- .--.--.-.-. ... -   ..- ...-.-- -..--.----..";	    /*holds the alphabet in morse*/char alpha[]="abcdefghijklmnopqrstuvwxyz";						/*Holds the alphabet*/char empty = ' ';												/* An empty char, used to null the pointers */char *p_to_morse;											/*allways should point to current morse code*///int tmp = 12;	//											/*for program loop*/char fileName[] = "C:/filetoopen.txt";									/*file to load array from*/int ch;/*done do not mess with this now*/int _tmain(int argc, _TCHAR* argv[]) {	ch = 9;	printMenu();		while  ((scanf("%d",&ch))!= 0){	    printMenu();	    	    switch (ch){		    case 1:{				fflush(stdin);				inputFromKeyboard();                fflush(stdin);				break;		    }		    case 2:{				fflush(stdin);				inputFromFile(fileName);				fflush(stdin);				break;		    } 			default:{				printf("Not a menu option, Try again.\n\n");						exit(1);				break;			}	    }	}        return 0;}void inputFromFile(char *inFileName){ FILE *fp;int a; if ((fp = fopen(inFileName,"r")) == NULL){	printf("Sorry file %s, can't be found",inFileName); } else {	 while (fp != '\0'){		//convert to char        int a = fgetc(fp);			char b = (char)a;			char bp = b;		p_to_morse = convertCharToMorseCode(&bp);		printf ("%.4s", p_to_morse);		fp++;	 } }  }/* doe do not mess with this now" */void inputFromKeyboard() {	char user_input[USERINPUTLENGTH];                           //string to hold user input	char *p_to_user_input = &user_input[0];	p_to_morse = &morseData[0];							    							    //for program loop		printf("Type in a char to convert to morse code...\n(0=exit)\n\n");   		int i = 0;		scanf("%s",&user_input);		while ( user_input[i] != '\0'){			p_to_morse= convertCharToMorseCode(&user_input[i]);			printf ("%.4s", p_to_morse);			i++;		}			ch = 9;			printf("press 9 to continue\n\n");}/*looks in the alphabet array for the char and returns a pointer to the matching char in the morse aphabet array*/char* convertCharToMorseCode(char* input){	char *pToMorse;	pToMorse = &alpha[1];	for (int j = 0; j<ALPHALENGTH; j++){		if (*input == alpha[j]){		    pToMorse = &morseData[j*4];		} else if (*input == '\n') {		    break; 		}	}	return pToMorse;}void printMenu(){	printf("\t*********************\n\t******Main Menu******\n\t**1 = from keyboard**\n\t**2 = from file    **\n\t**0 = exit         **\n\t*********************\n\t*********************\n\n");}