Linux_ Small projects in phase C

Posted by Pointybeard on Fri, 31 Dec 2021 06:02:28 +0100

Random roll call device based on C command line

Project description

Using C language, make a roll call program running on the command line. When the program runs, it can specify the roster file of the class. After running, the process of randomly selecting names is displayed on the interface. The speed is from fast to slow, and gradually freeze to a "lucky man", and the program ends.

Functional requirements

  1. Specify a common text file (*. txt) as the list to read student names in behavioral units
  2. The random extraction process is dynamically displayed at the terminal, with a slow - Fast - slow gradient effect
  3. After extracting names from the list, remove the corresponding names from the list
  4. You can select several students at a time
  5. Finally, when the students on the list are not enough to draw once, you should be able to choose to restore the list (overwrite)

Idea:

  1. Because the read name needs to be deleted from the file, it will be troublesome to operate directly on the file; So I define a two-dimensional array, read all the names in the file into the array, perform relevant operations in the array, and finally write the array back to the file

    /*Define a Get_txt() function reads data from the file. The input parameters of this function are two-dimensional array and the path + name of the read file
     The return value is the number of read files*/
    int Get_txt(char ch[][SIZE_NAME],char* txt);
    
    /*Because in Windows system, text files are wrapped with ` \ r\n 'by default (this has been verified in the last note); We don't need line breaks when displaying names, so we define a deal_ Use the line() function to process data in a two-dimensional array \ r\n*/
    void Deal_line(char ch[][SIZE_NAME],int count);
    
    //Define Del_name() function to delete the extracted student's name
    void Del_name(char ch[][SIZE_NAME],int mem[],int* count,int number);
    
    /*Define a write_ The file() function rewrites the contents of the two-dimensional array back to the file*/
    void Write_file(char ch[][SIZE_NAME],char* txt,int count);
    
  2. The function of randomly extracting student names is realized by using random numbers

    /*Define pitch_ The on() function randomly extracts several students from the array, and the speed of the extraction process presents a slow fast slow gradient effect*/
    void Pitch_on(char ch[][SIZE_NAME],int mem[],int count,int number)
    {
        ...
        for(i = 0;i < TIME_PITCH;i++)
        {
            //system("cls");// Clear screen (use this function to get better visual effect)  
            for(j = 0;j < number;j++)
            {   
                flat1 = 1;
                mem[j] = 0;
                //Since the delay function is not placed in this loop, if rand() is directly used to generate random numbers,
                //There is a case where the same random number exists several times in succession
                srand(time(NULL)+i+j);
                num = (rand()) % count;
                //Processing of random numbers that produce the same
                for(n = 0;n < j;n++)
                {
                    if(num == mem[n])
                    {
                        j --;
                        flat1 = 0;
                    }      
                }
                if(flat1 == 1)
                {
                    mem[j] = num;
                    printf("%s\t",ch[num]);
                }
            }
            ...
    }
    
  3. When extracting several names, the number of names in the list may be less than the number of people, so it is necessary to ask whether to overwrite the list again; Then there must be a list of names that have not been drawn before they can be covered

    //Define Set_backup_name() sets the backup file name by the file name
    void Set_backup_name(char* txt,char* backup);
    
    //Define BackUp() to set up backup files
    void BackUp(char ch[][SIZE_NAME],char* backup,int count);
    
    //Define Over_txt() to overwrite the file, that is, to overwrite the contents of the backup file into the file
    

4. Main function

int main(int argc,char** argv)
{
    int i = 0;
    int j = 0;
    int num = 0;
    int number = 1;//Indicates how many students are drawn at one time
    int mem[PITCH_NUMBER] = {0};//Store the numbers of multiple students extracted at one time
    char ch[COUNT_NAME][SIZE_NAME] = {0};//Members in the storage list
    char *txt = argv[1];//Address + name of storage list
    char *backup = calloc(1,strlen(txt)+2);//Store backup file name

    //To judge whether to select one or more students, enter 9 at most
    if(argv[2] != NULL)
        number = (int)(argv[2][0]-'0');
    int count = Get_txt(ch,txt);
    Deal_line(ch,count);
    Set_backup_name(txt,backup);
    BackUp(ch,backup,count);
    //The number of remaining students is less than the number of samples     
    if(count < number)
    {
        Over_txt(ch,mem,txt,backup,count);
        return 0;
    }      
    Pitch_on(ch,mem,count,number);
    Del_name(ch,mem,&count,number);
    //Displays the number of people remaining in the list
    printf("\nnumber:%d\n",count);
    Write_file(ch,txt,count);
    //Free heap space
    free(backup);
    backup = NULL;
    return 0;
}

Result display:

This is my first small C project. The thinking logic may not be very standard. Don't spray if you don't like it.

>>Program source code file
>> Text encoding in windows and Linux

Topics: C