如何优先排序字符? [英] How to priority sort characters?

查看:144
本文介绍了如何优先排序字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编辑我的问题我写了我的功能但输出有一些错误,我该如何解决?

  void enqueue(char type,int p){
if(que == NULL){
que = malloc(sizeof(struct node));
que-> nextPtr = NULL;
que-> Page = p;
que-> userType = type;
return;
}
q = prev = que;

while((q-> nextPtr!= NULL)&& charcmp(q-> userType,type)< 0){
prev = q;
q = q-> nextPtr;
}
if(charcmp(q-> userType,type)== 0){
while(q-> nextPtr!= NULL&& q-> pages< ; p){
prev = q;
q = q-> nextPtr;
}
}

n = malloc(sizeof(struct node));
n-> pages = p;
n-> userType = type;

if((charcmp(q-> userType,type)< 0 || charcmp(q-> userType,type)== 0)&& q-> ; p){
n-> nextPtr = q-> nextPtr;
q-> nextPtr = n;
}
if((q-> pages> = p)){
n-> nextPtr = q;
if(prev-> nextPtr == q){
prev-> nextPtr = n;
}
else if(que == q)
{
que = n;
}
}

}



这是我的charcmp函数。

  int charcmp(char a,char b){
while(a == b){
return 0;
}

while(a!= b){

if(a =='C'&&(b =='G' b =='A')){
return -1;
}

if(a =='G'&((b =='A')){
return -1;
}

if(a =='A'&&(b =='C'|| b =='G')){
return 1;
}
if(a =='G'&& b =='C'){
return 1;
}
}
}

我的输出有一些错误。我不能修复它。例如它的返回C,C,A,C,G

解决方案

所以,排序一系列自定义对象,如下所示:

  typedef struct {
char c; //一个字符
int i; //一个数字
} pair;

然后,您可以应用标准函数 qsort() 使用自定义比较功能,如下所示:

  int compare(const void * a,const void * b){
pair * p1 =(pair *)a;
pair * p2 =(pair *)b;
if(p1> c == p2-> c)return p1> i - p2-> i;
if(p1> c =='C'|| p1> c =='G'&& p2-> c =='A')return -1;
return 1;
}

所以测试数据的测试代码将打印 C,1 C,8 G,2 G,5 A,4 A,7 A,10

  int main(){
pair input [] = {{'A',10},{'A',7},{'G',5},
{'C' ,8},{'A',4},{'C',1},{'G',2}};
qsort(input,7,sizeof(pair),compare);
pair * cur = input; (int i = 0; i< 7; ++ i,++ cur){
printf(%c,%d,cur-> c,cur-> i) ;
}
printf(\\\
);
}

如何将任意输入转换为数组结构,是一个练习。 p>

I edit my question. I wrote my functions. But there is some mistakes in output .How can I fix it ?

void enqueue(char type,int p){
 if (que == NULL){
    que =malloc(sizeof(struct node));
    que->nextPtr = NULL;
    que->pages=p;
    que->userType=type;
    return;
}
q = prev = que;

while((q->nextPtr != NULL) && charcmp(q->userType,type)<0){
    prev = q;
    q = q->nextPtr;
}
if(charcmp(q->userType,type)==0){
    while(q->nextPtr !=NULL && q->pages < p){
         prev = q;
         q = q->nextPtr;    
    } 
}

n = malloc(sizeof(struct node));
n->pages=p;
n->userType=type;

if ( (charcmp(q->userType,type) < 0 || charcmp(q->userType,type)==0 ) && q->pages < p){
    n->nextPtr = q->nextPtr;
    q->nextPtr = n;
}
if((q->pages >= p )){
    n->nextPtr = q;
    if (prev->nextPtr == q){
        prev->nextPtr = n;
    }
    else if (que == q)
    {
        que = n;
    }
}   

}

and this is my charcmp function .

int charcmp(char a,char b){
while(a==b){
    return 0;
}

while(a!=b){

if(a=='C' && (b=='G'|| b=='A')){
    return -1;
}

if(a=='G' && (b=='A')){
    return -1;
}

if(a=='A' && (b=='C' || b=='G')){
    return 1;
}
if(a=='G' && b=='C'){
    return 1;
}
}
 }

My output has some mistakes. I can't fix it.For example its return C,C,A,C,G

解决方案

So, you want to sort an array of custom objects, like this:

typedef struct {
    char c;  // a character
    int i;   // a number
} pair;

Then you can apply a standard function qsort() with a custom compare function like this:

int compare(const void *a, const void *b) {
    pair *p1 = (pair*) a;
    pair *p2 = (pair*) b;
    if (p1->c == p2->c) return p1->i - p2->i;
    if (p1->c == 'C' || p1->c == 'G' && p2->c == 'A') return -1;
    return 1;
}

So that this testing code with your test data will print C,1 C,8 G,2 G,5 A,4 A,7 A,10

int main() {
    pair input[] = {{'A', 10}, {'A', 7}, {'G', 5},
                    {'C', 8}, {'A', 4}, {'C', 1}, {'G', 2}};
    qsort(input, 7, sizeof(pair), compare);
    pair *cur = input;
    for (int i=0; i<7; ++i, ++cur) {
        printf("%c,%d ", cur->c, cur->i);
    }
    printf("\n");
}

How to transform arbitrary input into array of structures, is an exercise.

这篇关于如何优先排序字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆