gpt4 book ai didi

c - C 中的名称排序

转载 作者:行者123 更新时间:2023-11-30 20:29:05 25 4
gpt4 key购买 nike

下面是我制作的一个像手机一样对名称进行排序的程序,即按第一个字母按字母顺序对列表进行排序,然后在该排序列表中再次按第二个字母按字母顺序对列表进行排序,依此类推。如果两个名字具有相同的字母,并且一个名字中添加了一个或多个额外的字母,那么该名字将出现在另一个名字的下方。但我找不到这段代码有什么问题。请帮忙。我已尽力使其尽可能具有可读性。

#include<stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int check_counter=0;
int col_no=0;

int in_check(int *check, int element_no, int total_element)
{
int i;

for(i=0; i<total_element; i++)
{
if(check[i]==element_no)
return 1;
}

return 0;
}


int bubble_sort_chars(char **arr, int len_of_arr, int *check)
{
int main_counter,sub_counter1,sub_counter2;
char char1, char2;
char *temp;

for(main_counter=len_of_arr-1,check_counter=0; main_counter>0; main_counter--)//Decreasing main counter untill it indicates the position of the second element
{
for(sub_counter1=0,sub_counter2=1; sub_counter1<main_counter; sub_counter1++,sub_counter2++)//Increasing the sub counter for a specific value of main counter untill it indicates the immidiate previous value of main counter
{
//Check if the sub_counter 1 and 2 belongs to check[] or it's a space or not
if( in_check(check,sub_counter1,check_counter+1)==1 || *(*(arr+sub_counter1)+col_no)==' ' )
sub_counter1++;
else if( in_check(check,sub_counter2,check_counter+1)==1 || *(*(arr+sub_counter2)+col_no)==' ' )
sub_counter2++;

//If the sub_counter element is null then putting the position the the string in the check()
if(*(*(arr+sub_counter2)+col_no)==0)
{
check[check_counter]=sub_counter2;
check_counter++;
sub_counter2++;
continue;
}

char1 = *(*(arr+sub_counter1)+col_no); char2= *(*(arr+sub_counter2)+col_no);
//Making the first character element of each string to upper case if it's lower case
if( char1>=97 && char1<=122 ) char1 -= 32;
if( char2>=97 && char2<=122 ) char2 -= 32;

if(char1>char2)
{
temp = *(arr+sub_counter2);
*(arr+sub_counter2) = *(arr+sub_counter1);
*(arr+sub_counter1) = temp;
}
}
}

return check_counter;
}


void alphabet_sort(char **arr, int len_of_arr)
{
int main_counter,start_pos,ch;
char char1,char2;
int check[len_of_arr];

ch=bubble_sort_chars(arr,len_of_arr,check);
if( ch == len_of_arr-1 || ch == len_of_arr )
return;

for(main_counter=0; main_counter<len_of_arr-1; main_counter++)
{
start_pos=main_counter;
char1 = *(*(arr+main_counter)+col_no);
if( char1>=97 && char1<=122 ) char2 -= 32;//Making the first character element of the string to upper case if it's lower case and putting it in char2

while(*(*(arr+main_counter)+col_no)==char1 || *(*(arr+main_counter)+col_no)==char2)//Counting where the alphabet ends (char1 or char2) in the sorted list of first element characters
main_counter++;
main_counter--;

col_no++;
alphabet_sort(arr+start_pos, main_counter-start_pos );
}

return;
}

int main()
{
char *name[]=
{
"A",
"AB",
"Al",
"ABc",
"abk",
"Zap",
"abce",
"Abv",
"abcp",
"zop",
"zzz",
"P",
"Zap",
"Abcd",
"Zoo",
"A",
"c"
};


alphabet_sort(name,17);

int i;
for(i=0; i<17; i++)
{
printf("%s\n", name[i]);
}

getch();
}

最佳答案

关于:

for( main_counter=0; main_counter<len_of_arr-1; main_counter++ )
{
start_pos=main_counter;
char1 = *(*(arr+main_counter)+col_no);
if( char1>=97 && char1<=122 )
char2 -= 32;//Making the first character element of the string to upper case if it's lower case and putting it in char2

while( *(*( arr+main_counter ) + col_no ) == char1 || *(*( arr+main_counter ) + col_no ) == char2 )//Counting where the alphabet ends (char1 or char2) in the sorted list of first element characters
main_counter++;

main_counter--;

col_no++;
alphabet_sort(arr+start_pos, main_counter-start_pos );
}

main_counter-start_pos 的结果是 0 或 -1

关于c - C 中的名称排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com