gpt4 book ai didi

c - 排序数字(下划线)

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:02 26 4
gpt4 key购买 nike

我正在尝试解决我的最后一个问题,但我仍然不知道如何解决它。我的任务是编写一个对数字进行排序的程序,但是:我们的导师给了我们一些处理数字的程序的额外要点,例如:000054667(实际上是 54667)和 345_845(实际上是 345845)。第一个问题已经解决了,但我不知道如何处理第二个问题。因此,我的问题是:你有什么提示/线索可以帮助我吗?我也发送我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define NUMBER_CHUNK 13


char* getNumber(FILE* fp)
{
int length, c;
int current=0;
char *number;

number=(char*)malloc(sizeof(char)*NUMBER_CHUNK);
if(!number)
{
printf("Error while alocating memory!\n");
return NULL;
}

length=NUMBER_CHUNK;

while(!isspace(c=fgetc(fp)) && !feof(fp))
{
if(isdigit(c))
{
number[current]=c;
current++;
if(current>=length)
{
length+=NUMBER_CHUNK;

if((number=((char*)realloc(number,sizeof(char*)*length)))==NULL)
{
free(number);
return NULL;
}
}
}

else
{
return NULL;
}
}

number[current]='\0';
return number;
}

int compare( const void *str1, const void *str2)
{
int value;
char* curr1;
char* curr2;

curr1=*(char**)str1;
curr2=*(char**)str2;

while(*curr1=='0') curr1++;
while(*curr2=='0') curr2++;

if(strlen(curr1) < strlen(curr2)) return -1;
if(strlen(curr1) > strlen(curr2)) return 1;

value=strcmp(curr1, curr2);

return value;
}

int main(int argc, char** argv)
{
FILE* fp;
char** tab;
int i=0;
int lines=0;
int length=10;

if(argc!=2)
{
printf("Incorrent syntax! Use ./name_of_program input_file\n");
return 1;
}

if(!(fp=fopen(argv[1],"r")))
{
printf("Could not open the file! Please try again!\n");
return 2;
}

tab = (char**)malloc(length*(sizeof(char*)));

if(!tab)
{
printf("Could not allocate memory! Terminating...\n");
free(tab);
return 3;
}

while(!feof(fp))
{
tab[i]=getNumber(fp);



if(i>=length)
{
length+=10;

if((tab=((char**)realloc(tab,sizeof(char*)*length)))==NULL)
{
free(tab);
return 5;
}
}
if(tab[i]==NULL)
{
printf("Incorrect character in the infile! Terminating\n");
free(tab);
return 4;
}
if(*tab[i]=='\0')
{
free(tab[i]);
i--;
}

i++;
lines++;
lines=i;
}
printf("\nBEFORE\n");


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

qsort(tab, lines, sizeof(char*), &compare);

printf("\nAFTER\n");


for(i=0;i<lines;i++)
{
printf("%s\n",tab[i]);
free(tab[i]);
}

free(tab);
fclose(fp);
return 0;
}

感谢您的帮助;)

最佳答案

更改字符串累积和比较例程。

字符串积累:

if(isdigit(c) || (c == '_'))

比较。合并忽略 _ 的值(value)评级有点冗长。但它不限于适合任何 int 范围的数字。

 int compare(const void *str1, const void *str2) {
const char* curr1 = *(const char**) str1;
const char* curr2 = *(const char**) str2;
// Remove leading zeros
while ((*curr1 == '0') || (*curr1 == '_'))
curr1++;
while ((*curr2 == '0') || (*curr2 == '_'))
curr2++;
int value = 0;
size_t len1 = 0;
size_t len2 = 0;
while (*curr1 || *curr2) {
while (*curr1 == '_')
curr1++;
while (*curr2 == '_')
curr2++;
// If a difference has not been found yet ...
if (value == 0) {
value = *curr1 - *curr2;
}
if (*curr1) {
curr1++;
len1++;
}
if (*curr2) {
curr2++;
len2++;
}
}
// If significant digits in string1 more than string2 ...
if (len1 != len2) {
return (len1 > len2) ? 1 : -1;
}
return value;
}

关于c - 排序数字(下划线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429472/

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