结构体快排

转载自:https://blog.csdn.net/qingyuanluofeng/article/details/47159193

  1. /* 
  2. 题目: 
  3. 学生成绩排序:成绩从低到高,姓名字母从低到高(区分大小写),年龄从低到高排序 
  4. 输入: 
  5. abc 20 99 
  6. bcd 19 97 
  7. bed 20 97 
  8.  
  9. 输出: 
  10. bcd 19 97 
  11. bed 20 97 
  12. abc 20 99 
  13.  
  14. 易错点: 
  15. 1对于字符指针,scanf("%s",ps[i].sName)不需要取地址符 
  16. 2对于名字,要指定大小,否则内存访问非法,char sName[128];而不是定义char* sName; 
  17. 3int strcmp(const char* string1,const char* string2); <0:前<后 
  18.  
  19.  
  20. 得分:0 
  21. */  
  22.   
  23. #include <stdio.h>  
  24. #include <malloc.h>  
  25. #include <string.h>  
  26.   
  27. typedef struct Student  
  28. {  
  29.     //Student(char* name,int age,int grade):sName(name),iAge(age),iGrade(grade){}  
  30.     //char* sName;//就是这里出错了,没有具体制定多大  
  31.     //char* sName;  
  32.   
  33.     //法二,用重载操作符operator<的方法做  
  34.     bool operator < (const Student& stu) const  
  35.     {  
  36.         if(iGrade != stu.iGrade)  
  37.         {  
  38.             return iGrade < stu.iGrade;  
  39.         }  
  40.         else  
  41.         {  
  42.             if(sName != stu.sName)  
  43.             {  
  44.                 return strcmp(sName,stu.sName) < 0 ? true:false;  
  45.             }  
  46.             else  
  47.             {  
  48.                 return iAge < stu.iAge;  
  49.             }  
  50.         }  
  51.     }  
  52.     char sName[128];  
  53.     int iAge;  
  54.     int iGrade;  
  55. }Student;  
  56.   
  57. bool compare(struct Student stuA,struct Student stuB)  
  58. {  
  59.     if(stuA.iGrade < stuB.iGrade)  
  60.     {  
  61.         return true;  
  62.     }  
  63.     else if(stuA.iGrade == stuB.iGrade )  
  64.     {  
  65.         if(stuA.sName!=stuB.sName)  
  66.         {  
  67.             return strcmp(stuA.sName,stuB.sName) < 0 ? true:false ;//这里中断了  
  68.         }  
  69.         else  
  70.         {  
  71.             return stuA.iAge < stuB.iAge;  
  72.         }  
  73.     }  
  74.     else  
  75.     {  
  76.         return false;  
  77.     }  
  78. }  
  79.   
  80.   
  81. int partition(Student* ps,int low,int high)  
  82. {  
  83.     Student stu = ps[low];  
  84.     while(low < high)  
  85.     {  
  86.         //while(low < high && compare(stu,ps[high]))//法1  
  87.         while(low < high && stu < ps[high])  
  88.         {  
  89.             high--;  
  90.         }  
  91.         ps[low] = ps[high];  
  92.         //while(low < high && compare(ps[low],stu))//法1  
  93.         while(low < high && ps[low] < stu)  
  94.         {  
  95.             low++;  
  96.         }  
  97.         ps[high] = ps[low];  
  98.     }  
  99.     ps[low] = stu;  
  100.     return low;  
  101. }  
  102.   
  103. void quickSort(Student* ps,int low,int high)  
  104. {  
  105.     if(low < high)  
  106.     {  
  107.         int iPos = partition(ps,low,high);  
  108.         quickSort(ps,low,iPos-1);  
  109.         quickSort(ps,iPos+1,high);  
  110.     }  
  111. }  
  112.   
  113. void print(Student* ps,int iNum)  
  114. {  
  115.     for(int i = 0;i < iNum ;i++)  
  116.     {  
  117.         printf("%s %d %d\n",ps[i].sName,ps[i].iAge,ps[i].iGrade);  
  118.     }  
  119. }  
  120.   
  121.   
  122. int main(int argc,char* argv[])  
  123. {  
  124.     int iNum;  
  125.     while(EOF!=scanf("%d",&iNum))  
  126.     {  
  127.         //ptrStu ps = (ptrStu)malloc(iNum*sizeof(Student));  
  128.         Student* ps = (Student*)malloc(iNum*sizeof(Student));  
  129.         //Student ps[100];  
  130.         for(int i = 0; i < iNum; i++)  
  131.         {  
  132.             //char* sName;  
  133.             //int iAge,iGrade;  
  134.             //scanf("%s %d %d",&ps[i].sName,&ps[i].iAge,&ps[i].iGrade); //关键字符串不需要取地址,因为它本身就是一个地址,否则就是取地址的地址了  
  135.             scanf("%s %d %d",ps[i].sName,&ps[i].iAge,&ps[i].iGrade);  
  136.             //Student *stu = (Student*)malloc(1*sizeof(Student))(sName,iAge,iGrade);  
  137.             //ps[i] = stu;//难道是因为出了这里就不存在了  
  138.         }  
  139.         //printf("%s\n",ps[1].sName);  
  140.         //print(ps,iNum);  
  141.         quickSort(ps,0,iNum-1);  
  142.         print(ps,iNum);  
  143.         free(ps);  
  144.     }  
  145.     //getchar();  
  146.     return 0;  
  147. }  

转载于:https://www.cnblogs.com/cglongge/p/9069001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Tcl 中可以使用 `dict` 命令创建字典(或称为关联数组)。要创建一个结构体,可以将相关字段作为字典的键和值。 以下是一个示例,创建一个包含 `cell_name` 和 `num` 两个字段的结构体,并将其存储在一个字典中: ``` set my_struct [dict create cell_name "my_cell" num 42] ``` 要访问结构体中的字段,可以使用 `dict get` 命令: ``` set cell_name [dict get $my_struct cell_name] set num [dict get $my_struct num] ``` 要对包含结构体的数组进行排序,可以使用 `lsort` 命令。 `lsort` 可以按照指定的键来排序,所以我们需要提供一个排序函数,该函数将比较两个结构体中的 `num` 字段: ``` proc compare_num {a b} { set num_a [dict get $a num] set num_b [dict get $b num] if {$num_a < $num_b} { return -1 } elseif {$num_a > $num_b} { return 1 } else { return 0 } } ``` 然后可以使用 `lsort` 来对数组进行排序: ``` set my_array [list $my_struct1 $my_struct2 $my_struct3] set sorted_array [lsort -command compare_num $my_array] ``` 现在 `sorted_array` 中的结构体数组按照 `num` 字段的大小进行了排序。 ### 回答2: Tcl是一种解释型脚本语言,使用dict(字典)可以模拟结构体(Struct)的创建。在Tcl中,可以使用如下方式创建一个dict结构体,其中包括cell_name和num两个属性: ```tcl set struct [dict create cell_name "example" num 5] ``` 这样就创建了一个名字为struct的dict结构体,其中cell_name属性的值为"example",num属性的值为5。 要对结构体数组进行快速排序,可以使用Tcl内置的lsort命令结合自定义的排序函数。假设有一个结构体数组列表structList,其中包含多个dict结构体,可以使用如下代码对其进行排序: ```tcl proc compareNum {struct1 struct2} { set num1 [dict get $struct1 num] set num2 [dict get $struct2 num] return [expr {$num1 - $num2}] } set sortedList [lsort -command compareNum $structList] ``` 首先,使用proc定义了一个名为compareNum的排序函数,该函数接受两个参数struct1和struct2,分别代表结构体数组中的两个元素。函数通过dict get命令获取结构体中num属性的值,并进行比较。 接下来,使用lsort命令对structList进行排序,通过-option参数指定了compareNum函数作为自定义的排序规则。排序完成后,将排序结果存储在sortedList中。 使用以上方法,可以按照结构体数组中num属性的大小对其进行快速排序。 ### 回答3: 在TCL中,可以使用dict创建结构体,并在结构体中包含cell_name和num两个字段。具体实现步骤如下: 1. 首先,我们可以使用dict命令创建结构体。假设有一个结构体数组为structArr。通过以下命令可以将cell_name和num字段添加到结构体中: ``` set structArr [dict create cell_name "name" num 1] ``` 2. 然后,我们可以创建一个结构体数组,其中每个元素都是一个dict结构体。以包含两个元素的结构体数组为例: ``` set structArray [list [dict create cell_name "name1" num 5] [dict create cell_name "name2" num 2]] ``` 3. 接下来,我们可以使用快速排序算法对该结构体数组进行排序。可以编写一个快速排序的函数来实现此操作。以下是一种可能的实现方式: ``` proc quickSort {arr start end} { if {$start < $end} { set pivotIdx [partition $arr $start $end] quickSort $arr $start [expr {$pivotIdx - 1}] quickSort $arr [expr {$pivotIdx + 1}] $end } } proc partition {arr start end} { set pivotIdx $start set pivotVal [dict get [lindex $arr $end] num] for {set i $start} {$i < $end} {incr i} { if {[dict get [lindex $arr $i] num] < $pivotVal} { set temp [lindex $arr $i] set arr [lreplace $arr $i $i] set arr [linsert $arr $pivotIdx $temp] incr pivotIdx } } set temp [lindex $arr $pivotIdx] set arr [lreplace $arr $pivotIdx $pivotIdx] set arr [linsert $arr $end $temp] return $pivotIdx } quickSort $structArray 0 [expr {[llength $structArray] - 1}] ``` 通过以上步骤,我们可以通过dict在TCL中创建包含cell_name和num字段的结构体,并使用快速排序算法对结构体数组进行排序。最终,按照num字段的大小对结构体数组进行了快速排序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值