Lab 13 – 2018

Silumine [ EST ][ ENG ]

Info ülesande kohta:

  1. Esimene funktsioon peaks lugema faili sisu ja selle ekraanile kuvama
  2. Teine funktsioon peaks sorteerima väärtused tähestikulises järjekorras ja kuvama ekraanile
  3. Kolmas funktsioon peaks sisestama nime massiivi õigesse kohta, olenemata kas nimi algab suure või väikse tähega

Kood:

#include <stdio.h>
#include <string.h>
#define DOCLENGTH 9
#define NAMEMAX 12
int insert_into_arrays(char names[DOCLENGTH][NAMEMAX], char emails[DOCLENGTH][NAMEMAX], char new_name[NAMEMAX], char new_email[NAMEMAX]);
void sort_arrays(char names[DOCLENGTH][NAMEMAX], char emails[DOCLENGTH][NAMEMAX]);
void fill_arrays(char names[DOCLENGTH][NAMEMAX], char emails[DOCLENGTH][NAMEMAX], char file_name[50]);
int main(int argc, char* argv[]){
    if(argc < 2){
        printf("I need an argument.\n open cmd line or terminal, navigate to the directory where this file is stored, and run this program as:\
        \n'./thisprogram file_to_open'\nif on linux \nor\
        \n'thisprogram.exe file_to_open'\nif on windows");
        return 1;
    }
    char file_name[50];
    //copy our first argument into the file_name string
    strcpy(file_name, argv[1]);
    
    // we allocate two arrays, one for names, one for emails 
    char names [DOCLENGTH][NAMEMAX];
    char emails [DOCLENGTH][NAMEMAX];
    
    //fill_arrays reads the file and fills the arrays with this data
    fill_arrays(names, emails, file_name);
    
    //print out the current state of the array
    for(int i=0; i < DOCLENGTH; i++){
        printf("%s %s\n", names[i], emails[i]);
    }
    
    
    // sort the data alphabetically by name
    sort_arrays(names, emails);
    puts("\n\n");
    // print the current state of the arrays
    for(int i = 0; i < DOCLENGTH; i++){
        printf("%s %s\n", names[i], emails[i]);
    }
    
    //allocate a space for new name and email
    char new_name[NAMEMAX];
    char new_email[NAMEMAX];
    
    //get user input
    printf("enter a name to insert");
    scanf("%s", new_name);
    
    printf("enter that person's email");
    scanf("%s", new_email);
    //insert this new entry in the right place in the arrays
    int slot = insert_into_arrays(names, emails, new_name, new_email);
    
    puts("\n\n");
    //print the current state of the arrays
    for(int i = 0; i < DOCLENGTH; i++){
        if(i == slot){
            printf("this is the one added  ");
        }
        printf("%s %s\n", names[i], emails[i]);
    }
    return 0;
}
void fill_arrays(char names[DOCLENGTH][NAMEMAX], char emails[DOCLENGTH][NAMEMAX], char file_name[50]){
    //open the file for reading
    FILE* data = fopen("file_name", "r");
    // one at a time, insert entries into the arrays
    for( int i = 0; i < DOCLENGTH; i++){
        fscanf(data, "%s %s", names[i], emails[i]);
    }
    //always close the file!
    fclose(data);
}
void sort_arrays(char names[DOCLENGTH][NAMEMAX], char emails[DOCLENGTH][NAMEMAX]){
    //allocate a temporary string for shuffling
    char temp[NAMEMAX];
    
    //the brk variable tells us when we should break out of the loop
    int brk;
    
    //i tells us the number of times we've gone through the dataset
    int i;
    
    //break == 0 is the flag to tell us that no changes were made, and the set is sorted
    while(brk == 0){
        brk = 0;
        //j tells us which member of the array we're inspecting
        for(int j = 0; j < DOCLENGTH - i; j++){
            //if a pair needs to be swithed
            if( names[j][0] > names[j+1][0]){
                //record that a switch was made
                brk = 1;
                //perform a switch of both emails and names
                strcpy(temp, names[j]);
                strcpy(names[j], names[j+1]);
                strcpy(names[j+1], temp);
                
                strcpy(temp, emails[j+1]);
                strcpy(emails[j+1], emails[j]);
                strcpy(emails[j], temp);
            }
            i++;
        }
    }
}
int insert_into_arrays(char names[DOCLENGTH][NAMEMAX], char emails[DOCLENGTH][NAMEMAX], char new_name[NAMEMAX], char new_email[NAMEMAX]){
    //initialize outside of the loop, so that we will still have it for returning
    int i=1;
    
    // i tells us which member of the array we're inspecting
    for(; i<DOCLENGTH; i++){
        
        //if the first letter of this name belongs before the entry we're looking at
        if(new_name[0] < names[i][0]){
            //(DOCLENGTH-i)*NAMEMAX is the size of the block we're moving
            int move_size = (DOCLENGTH)*NAMEMAX;
            //copy the rest of the name array one slot forward
            memcpy(&names[i+1][0], &names[i][0], move_size);
            
            //copy the new name and email into the now opened slot
            strcpy(names[i], new_name);
            strcpy(emails[i], new_email);
            break;
        }
        
    }
    //return the index of the slot that we placed it in
    return i;
}