C-排序后,使2个多维阵列以正确的顺序对齐



我有2个多维数组。一个是给艺术家的,另一种是为歌曲。

进入4位艺术家和每个阵列进行排序后,歌曲保持相同的位置,但艺术家们却被散步。

我试图让歌曲都在整理后跟随艺术家。以下是输出和代码。

让我知道您是否需要查看更多我的代码并提前感谢。

我试图在Stackoverflow上发布我的代码,但它一直说代码未正确格式。这是我第一次使用stackoverflow

这是我到目前为止的代码

    sortArtists(artists, Artistsnum); 
    sortSongs(songsArtist1, numOfSongs);
    sortSongs(songsArtist2, numOfSongs);
    sortSongs(songsArtist3, numOfSongs);
    sortSongs(songsArtist4,numOfSongs);
 /*
 * Use here the function shuffleSongs to shuffle all the songs
 * Print the list of shuffled songs
 */
    // print the songs

printSongs(songsArtist1, artists[0]);
printSongs(songsArtist2, artists[1]);
printSongs(songsArtist3, artists[2]);
printSongs(songsArtist4, artists[3]);
  return 0;
}
// Guard for entering number of artist brought in frompointerconstantwo.c    
//  loop through entire string
//  for(; *sPtr != ''; ++sPtr)    // no initialisation
//  {
//      printf("%c",*sPtr);
//  }
int insertSongs(char songs [3][80])
{
    int i, length;
    // Insert the songs
    for(i=0;i<3;i++)
    {
        printf("Insert Song %d: ", i+1);
        fgets(songs[i],80,stdin);
        // If the return command is pressed it
        if(songs[i][0]=='n')
            break;
        // Remove the carriage return from the song
        length = strlen(songs[i]);
        songs[i][length - 1] = '';
    }
    if(i>0)
        return i;
    else
        return 0; 
}
    char newartist[4][80];

void printSongs(char songs[3][80], char artists[4][80])// added new  
{
    int i;
    for(i=0; i<4;i++)
    {
        printf("List of Artist %s",artists[i]);
        for(i=0;i<3; i++)
        {
            printf("nSong %d: %sn", i+1, songs[i]);
        }
    }
}
void sortArtists(char sortedArtists[][80] , int numOfArtist){
//    int numOfArtist = 4;
    size_t i = 0;
    size_t j = 0;
    size_t minIndex = 0;
    size_t minIndexChanged = 0;
    char artists[80];   // i added this myself not in note
    char swap [MAX_LENGTH];
    char newartist[4][80];
    printf("nThe number of Artist is %dn", numOfArtist);

     for(i=0; i < 3 ; i++){        // count_name = 4-1
     for(j=i+1; j< 4; j++)
    {
        if(strcmp(sortedArtists[i],sortedArtists[j]) > 0)
        {
            strcpy(swap,sortedArtists[i]);
            strcpy(sortedArtists[i],sortedArtists[j]);
            strcpy(sortedArtists[j],swap);
        }
    }
 }

}
/*
* This method sorts the songs of a specific artist alphabetically. It takes 
as input:
* - songsOfAnArtist: the array of the songs of an artist that was provided 
from the standard input
* - numOfArtists: the number of artists provided from the standard input
*/
void sortSongs(char songs[][80], int numOfSongs){
    size_t i = 0;
    size_t j = 0;
    size_t minIndex = 0;
    size_t minIndexChanged = 0;
    printf("Heren");
   // char songs[80];   // i added this myself not in note
    char swap [80];
    printf("nThe number of songs is %dn", numOfSongs);
    for(i=0; i < 2 ; i++){
        for(j=i+1; j< 3; j++)
        {
            if(strcmp(songs[i],songs[j]) > 0)
            {
               printf(songs[i]);  // new
               strcpy(swap,songs[i]);
               printf(songs[i]); // new
               strcpy(songs[i],songs[j]);
               strcpy(songs[j],swap);
            }
        }
    }
    for (i = 0; i < 3; i++){
        printf("%sn", songs[i]);
    }
}

可以将标识符添加到每一行以识别关联的艺术家和歌曲。如果艺术家的标识符匹配该歌曲的标识符,则按标识符和打印歌曲的值进行排序。

#include <stdio.h>
#include <string.h>
#define ARTISTS 4
#define SONGS 3
#define ID 3
#define LENGTH 80
int main ( void) {
    char artists[ARTISTS][LENGTH] = { { ""}};
    char songs[SONGS * ARTISTS][LENGTH] = { { ""}};
    char input[LENGTH - ID] = "";//shorter to allow for leading ID when concatenated
    int id = 0;
    for ( int each = 0; each < ARTISTS; each++) {
        id++;
        printf ( "enter artist name %d of %dn", each + 1, ARTISTS);
        fgets ( input, LENGTH - ID, stdin);
        input[strcspn ( input, "n")] = '';//remove trailing newline
        if ( '' == input[0]) {
            break;
        }
        sprintf ( artists[each], "%*d%s", ID, id, input);//print the id and name to artists[]
        for ( int loop = 0; loop < SONGS; loop++) {
            printf ( "tfor %s, enter song title %d of %dn", &artists[each][ID], loop + 1, SONGS);
            fgets ( input, LENGTH - ID, stdin);
            input[strcspn ( input, "n")] = '';//remove trailing newline
            if ( '' == input[0]) {
                break;
            }
            sprintf ( songs[loop + each * SONGS], "%*d%s", ID, id, input);//print the id and name to songs[]
        }
    }
    //sort artists
    for ( int each = 1; each < ARTISTS; each++) {
        int loop = each;
        while ( loop) {
            if ( 0 > strcmp ( &artists[loop][ID], &artists[loop - 1][ID])) {
                char swap[LENGTH] = "";
                strcpy ( swap, artists[loop]);
                strcpy ( artists[loop], artists[loop - 1]);
                strcpy ( artists[loop - 1], swap);
            }
            else {
                break;
            }
            loop--;
        }
    }
    //sort songs
    for ( int each = 1; each < ARTISTS * SONGS; each++) {
        int loop = each;
        while ( loop) {
            if ( 0 > strcmp ( &songs[loop][ID], &songs[loop - 1][ID])) {
                char swap[LENGTH] = "";
                strcpy ( swap, songs[loop]);
                strcpy ( songs[loop], songs[loop - 1]);
                strcpy ( songs[loop - 1], swap);
            }
            else {
                break;
            }
            loop--;
        }
    }
    //print artists and songs
    for ( int each = 0; each < ARTISTS; each++) {
        if ( artists[each][0]) {
            printf ( "artist %sn", &artists[each][ID]);//print name after id
            for ( int loop = 0; loop < ARTISTS * SONGS; loop++) {
                if ( 0 == strncmp ( artists[each], songs[loop], ID)) {//match the id
                    printf ( "tsong %sn", &songs[loop][ID]);//print the song after the id
                }
            }
        }
    }
    return 0;
}

最新更新