Wednesday, November 27, 2013

This is a simple CD application to help you keep track of your CD's

#!/bin/sh
echo
echo "*****************PLEASE READ THIS LICENSE AGREEMENT FIRST*************************"
echo
echo "Very simple example shell script for managing a CD collection."
echo "Copyright (C) 2013 University of Buea, Faculty of Engineering and Tech."
echo
echo "This program is free software; you can redistribute it and/or modify it."
echo "under the terms of the GNU General Public License as published by the"
echo "Free Software Foundation; either version 2 of the License, or (at your"
echo "option) any later version."
echo
echo "This program is distributed in the hopes that it will be useful, but"
echo "WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General"
echo "Public License for more details."
echo
echo "You should have received a copy of the GNU General Public License along"
echo "with this program; if not, write to the Free Software Foundation, Inc."
echo "675 Mass Ave, Cambridge, MA 02139, USA."


menu_choice=""
current_cd=""
title_file="title.cdb"
tracks_file="tracks.cdb"
temp_file=/tmp/cdb.$$
trap 'rm -f $temp_file' EXIT

get_return() {
    echo -e "Press return \c"
    read x
    return 0
}
get_confirm() {
    echo -e "Are you sure? \c"
    while true
    do
        read x
        case "$x" in
        y | yes | Y | Yes | YES )
            return 0;;
        n | no | N | No | NO )
        echo
        echo "Cancelled"
        return 1;;
        *) echo "Please enter yes or no" ;;
    esac
done
}

set_menu_choice() {
    #clear
    echo
    echo
    echo
    echo "Options :-"
    echo
    echo "    a) Add new CD"
    echo "    f) Find CD"
    echo "    c) Count the CDs and tracks in the catalog"
    if [ "$cdcatnum” != " ]; then
        echo "    l) List tracks on $cdtitle"
        echo "    r) Remove $cdtitle"
        echo "    u) Update track information for $cdtitle"
    fi
    echo "    q) Quit"
    echo
    echo -e "Please enter choice then press return \c"
    read menu_choice
    return
}

insert_title() {
    echo $* >> $title_file
    return
}
insert_track() {
    echo $* >> $tracks_file
    return
}
add_record_tracks() {
    echo "Enter track information for this CD"
    echo "When no more tracks enter q"
    cdtrack=1
    cdttitle=""
    while [ "$cdttitle" != "q" ]
    do
        echo -e “Track $cdtrack, track title? \c”
        read tmp
        cdttitle=${tmp%%,*}
        if [ "$tmp" != "$cdttitle" ]; then
            echo "Sorry, no commas allowed"
            continue
        fi
        if [ -n “$cdttitle” ] ; then
            if [ “$cdttitle” != “q” ]; then
                insert_track $cdcatnum,$cdtrack,$cdttitle
            fi
        else
            cdtrack=$((cdtrack-1))
        fi
    cdtrack=$((cdtrack+1))
    done
}

add_records() {
    # Prompt for the initial information
   
    echo -e "Enter catalog name \c"
    read tmp
    cdcatnum=${tmp%%,*}
   
    echo -e "Enter title \c"
    read tmp
    cdtitle=${tmp%%,*}
   
    echo -e "Enter type \c"
    read tmp
    cdtype=${tmp%%,*}
   
    echo -e "Enter artist/composer \c"
    read tmp
    cdac=${tmp%%,*}
   
    # Check that they want to enter the information
   
    echo About to add new entry
    echo "$cdcatnum $cdtitle $cdtype $cdac"
   
    # If confirmed then append it to the titles file
   
    if get_confirm ; then
        insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
        add_record_tracks
    else
        remove_records
    fi
    return
}

find_cd() {
    if [ “$1” = "n" ]; then
        asklist=n
    else
        asklist=y
    fi
    cdcatnum=""
    echo -e "Enter a string to search for in the CD titles \c"
        read searchstr
    if [ “$searchstr” = “” ]; then
        return 0
    fi
        grep "$searchstr" $title_file > $temp_file
        set $(wc -l $temp_file)
        linesfound=$l
        case “$linesfound” in
        0)    echo "Sorry, nothing found"
            get_return
            return 0
            ;;
        1)    ;;
        2)    echo "Sorry, not unique."
            echo "Found the following"
            cat $temp_file
            get_return
            return 0
        esac
       
        IFS=","
        read cdcatnum cdtitle cdtype cdac < $temp_file
        IFS=" "
       
        if [ -z "$cdcatnum" ]; then
            echo "Sorry, could not extract catalog field from $temp_file"
            get_return
            return 0
        fi
       
        echo
        echo Catalog number: $cdcatnum
        echo Title: $cdtitle
        echo Type: $cdtype
        echo Artist/Composer: $cdac
        echo
        get_return
        if [ "$asklist" = "y" ]; then
            echo -e "View tracks for this CD? \c"
                read x
            if [ “$x” = “y” ]; then
                echo
                list_tracks
                echo
            fi
        fi
        return 1
}

update_cd() {
    if [ -z "cdcatnum" ]; then
        echo "You must select a CD first"
        find_cd n
    fi
    if [ -n “$cdcatnum” ]; then
    echo “Current tracks are :-”
        list_tracks
    echo
    echo "This will re-enter the tracks for $cdtitle"
        get_confirm && {
        grep -v "^${cdcatnum}," $tracks_file > $temp_file
        mv $temp_file $tracks_file
    echo
        add_record_tracks
    }
    fi
    return
}

count_cds() {
    set $(wc -l $title_file)
    num_titles=$l
    set $(wc -l $tracks_file)
    num_tracks=$l
    echo found $num_titles CDs, with a total of $num_tracks tracks
    get_return
    return
}

remove_records() {
    if [ -z “$cdcatnum” ]; then
        echo You must select a CD first
        find_cd n
    fi
    if [ -n “$cdcatnum” ]; then
        echo "You are about to delete $cdtitle"
        get_confirm && {
        grep -v "^${cdcatnum}," $title_file > $temp_file
        mv $temp_file $title_file
        grep -v "^${cdcatnum}," $tracks_file > $temp_file
        mv $temp_file $tracks_file
        cdcatnum=""
        echo Entry removed
        }
        get_return
    fi
    return
}

list_tracks() {
    if [ "$cdcatnum" = "" ]; then
        echo no CD selected yet
        return
    else
        grep "^${cdcatnum}," $tracks_file > $temp_file
        num_tracks=$(wc -l $temp_file)
    if [ "$num_tracks" = "0" ]; then
        echo "no tracks found for $cdtitle"
    else {
        echo
        echo "$cdtitle :-"
        echo
        cut -f 2- -d , $temp_file
        echo
    } | ${PAGER:-more}
        fi
    fi
    get_return
    return
}

    rm -f $temp_file
    if [ ! -f $title_file ]; then
        touch $title_file
    fi
    if [ ! -f $tracks_file ]; then
        touch $tracks_file
    fi
   
    # Now the application proper

    clear
    echo
    echo "This Is A Mini CD Manager"
    sleep 1
    quit=n
    while [ "$quit" != "y" ];
    do
        set_menu_choice
    case "$menu_choice"  in
        a) add_records;;
        r) remove_records;;
        f) find_cd y;;
        u) update_cd;;
        c) count_cds;;
        l) list_tracks;;
        b)
            echo
            echo
            more $title_file
            echo
            get_return;;
        q | Q ) quit=y;;
        *) echo “Sorry, choice not recognized”;;
    esac
done

    #Tidy up and leave

    rm -f $temp_file
    echo "Finished"
exit 0

love is a cycle

Love is a cycle: When you love, you get hurt. When you get hurt,
you hate. When you hate, you try to forget. When you try to
forget, you start missing. And when you start missing, you'll
eventually fall in love again

Sunday, November 17, 2013

Elite programmers: This is a c programme to count the frequency of ea...


Elite programmers: This is a c programme to count the frequency of ea...:

/*Author : FE12A061 - FEMENCHA AZOMBO FABRICE
  Title  : A program to count the occurence of each word in a file
  Date   : 10 November, 2013 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct words
{
    char *word;        //struct words is a binary tree that stores a node and a pointer to the left and right siblings
    int count;
    struct words *left;
    struct words *right;
};
struct words *bintree(struct words **head,char *word);  //bintree is a function of type struct words and the 1st arg is a pointer to the bin tree that stores the words
void print_tree(struct words *head);    //print_tree is a function that prints a binary tree
char *readfile(char *filename);         //readfile is a function that reads a text file and stores it in a string
int total;                             //an interger to store the total number of words in the file
int
main (int argc, char *argv[])    //argc means main can take some argumments
{
    char *word=NULL,*str=NULL;
    total = 0;
    struct words *head=NULL;
   
    if((str = readfile(argv[1])) != NULL)// the 1st argument is the name of the file to be read
    {
       
        const char* delim = " ,\n\r\t.;:_-()~`|!?><%^&*$#@{}[]0123456789\'\"/";//delim is a list of all punctuation marks that seperates two words form each other
        word = strtok(str,delim);    //strtok is takes a string and seperates it into words using the delimities given above
        head = bintree(&head,word);
-+          while((word = strtok(NULL,delim)) != NULL){
            head = bintree(&head,word);
        }
        printf("Total = %d\n",total);
        printf("|%-20s | %-5s |\n%25s\n","WORDS","freq","|=============================");
        print_tree(head);
    }
    return 0;
}


void print_tree(struct words *head){
    if(head == NULL)
        return ;
    print_tree(head->right); //note that print_tree uses the post order traversal to print the tree.
    printf("|%-20s | %-5d |\n",head->word,head->count);//that is, print the word at the right subtree, the node and then left subtree
    print_tree(head->left);
    return ;   
}
char *readfile(char *filename){
    FILE *fp = fopen(filename,"r");
    char *str=NULL;
    if(fp !=NULL){
        fseek(fp,0,SEEK_END);    //moves the pointer 2 the end of the file
        long int size = ftell(fp);   //ftell determines the size of the file and store the answer in a variable call size after fseek had move the pointer to the end
        rewind(fp);  //rewind brings the pointer back to te beginning
        str = malloc(size + 1); //malloc allocate space to store the words in string called str
        fread(str,size,1,fp);  //fread read the the entire file and stores it in str.the 1 signifies it is reading the entire file as one block
        fclose(fp); //close the file after processing
    }
    else {
        printf("Cannot open file.Please make sure the file is in the current directory and try again\n") ;
        return 0 ;
    }
    return str;
}

struct words *bintree(struct words **head,char *word){
    if(*head == NULL){    //checks if the tree is empty
        *head = malloc(sizeof(struct words *));    //allocate space for the words.I.E initialise the head
        (*head)->word = malloc(sizeof(char) * strlen(word) + 1); //compute the size of the word
        strcpy((*head)->word,word); //copy the word into the word part of the tree. I.E word
        (*head)->count = 1;  //initialise the count for that word
        (*head)->left = (*head)->right = NULL; //declare new nodes to store the next words.I.E left and right subtrees
        total++; //total is just a variable that keep track of the number of words in the file
    }
    else if(strcasecmp((*head)->word,word) < 0){   //strcasecmp compares incoming word with the existing word without considering the cases
        bintree(&(*head)->left,word); //if the incomming word is less than the word already found in the tree, store it in the left subtree
    }
    else if(strcasecmp((*head)->word,word) > 0){
        bintree(&(*head)->right,word); //else store it in the right subtree
    }
    else{
        (*head)->count++; //if the words are the some then increment the count part of the tree(the word)
        total++;
    }
   
    return *head; //return the head of the tree(the parent) when all the words are in the tree
}


/*How to run the program*/

/*compile the programe, run and give the name of the file as argument of main*/




Wednesday, November 13, 2013

This is a c programme to count the frequency of each word in a file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct words
{
    char *word;        //struct words is a binary tree that stores a node and a pointer to the left and right siblings
    int count;
    struct words *left;
    struct words *right;
};
struct words *bintree(struct words **head,char *word);//bintree is a function of type struct words and the 1st arg is a pointer to the bin tree that stores the words
void print_tree(struct words *head);//print_tree is a function that prints a binary tree
char *readfile(char *filename);//readfile is a function that reads a text file and stores it in a string
int total;//an interger to store the total number of words in the file
int
main (int argc, char *argv[])
{
    char *word=NULL,*str=NULL;
    total = 0;
    struct words *head=NULL;
   
    if((str = readfile(argv[1])) != NULL)//
    {
       
        const char* delim = " ,\n\r\t.;:_-()~`|!?><%^&*$#@{}[]0123456789\'\"/";//delim is list of all chars that seperates two words form each other
        word = strtok(str,delim);//strtok is takes a string and seperates it into words using the delimities
        head = bintree(&head,word);
        while((word = strtok(NULL,delim)) != NULL){
            head = bintree(&head
            ,word);
        }
        printf("Total = %d\n",total);
        printf("|%-20s | %-5s |\n%25s\n","WORDS","freq","|============================");
        print_tree(head);
    }
    return 0;
}


void print_tree(struct words *head){
    if(head == NULL)
        return ;
    print_tree(head->right);//note that print_tree uses the post order traversal to print the tree
    printf("|%-20s | %-5d |\n",head->word,head->count);
    print_tree(head->left);
    return ;   
}
char *readfile(char *filename){
    FILE *fp = fopen(filename,"r");
    char *str=NULL;
    if(fp !=NULL){
        fseek(fp,0,SEEK_END);//moves the pointer 2 the end of the file
        long int size = ftell(fp);//ftel determine the size of the file
        rewind(fp);//rewind brings the pointer back to te beginning
        str = malloc(size + 1);
        fread(str,size,1,fp);//fread read the the entire file and stores it in a file.the 1 signifies it is reading the entire file as one block
        fclose(fp);
    }
    return str;
}

struct words *bintree(struct words **head,char *word){
    if(*head == NULL){
        *head = malloc(sizeof(struct words *));//allocate space for the words
        (*head)->word = malloc(sizeof(char) * strlen(word) + 1);
        strcpy((*head)->word,word);
        (*head)->count = 1;
        (*head)->left = (*head)->right = NULL;
        total++;
    }
    else if(strcasecmp((*head)->word,word) < 0){//strcasecmp compares without considering the cases
        bintree(&(*head)->left,word);
    }
    else if(strcasecmp((*head)->word,word) > 0){
        bintree(&(*head)->right,word);
    }
    else{
        (*head)->count++;
        total++;
    }
   
    return *head;
}