當前位置:首頁 » 編程語言 » c語言結構體的排序

c語言結構體的排序

發布時間: 2022-05-24 12:08:36

⑴ 一個c語言問題:對結構體的排序問題

typedef struct /*定義結構體數組*/
{
char num[10]; /*學號*/
char name[20]; /*姓名*/
int age; /*年齡*/
char sex[20]; /*性別*/
char birthday[20]; /*出生年月*/
char address[50]; /*地址*/
int tel; /*電話*/
char e_mail[50]; /*郵箱*/
}Student;
Student stu[80]; /*結構體數組變數*/

void Sort_by_num(Student stud[],int n) /*按學號排序*/
{ int i,j;
Student t;
bool key;
for(i=0;i<n-1;i++) /*冒泡法排序*/
{
key=1;
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{
key=0;
strcpy(t.num,stud[j+1].num);
strcpy(t.name,stud[j+1].name);
strcpy(t.sex,stud[j+1].sex);
strcpy(t.birthday,stud[j+1].birthday);
strcpy(t.address,stud[j+1].address);
strcpy(t.e_mail,stud[j+1].e_mail);
t.age=stud[j+1].age; //int 型這樣賦值就行了
t.tel=stud[j+1].tel;

strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j+1].name,stud[j].name);
stud[j+1].age=stud[j].age;
strcpy(stud[j+1].sex,stud[j].sex);
strcpy(stud[j+1].birthday,stud[j].birthday);
strcpy(stud[j+1].address,stud[j].address);
stud[j+1].tel=stud[j].tel;
strcpy(stud[j+1].e_mail,stud[j].e_mail);

strcpy(stud[j].num,t.num);
strcpy(stud[j+1].name,t.name);
stud[j+1].age=t.age;
strcpy(stud[j+1].sex,t.sex);
strcpy(stud[j+1].birthday,t.birthday);
strcpy(stud[j+1].address,t.address);
stud[j+1].tel=t.tel;
strcpy(stud[j+1].e_mail,t.e_mail);
}
if(key==1) //用於判斷是否已經排好序了,如果已經排好序,則退出循環
break;
}
}

⑵ C語言結構體數組排序

多定義一個可以比較的數據段(比如整型),然後排序,冒泡排序可以解決問題。注意交換值的時候最好用引用類型&定義一個函數,用函數實現兩個結構體的數值互換。

⑶ C語言結構體排序

#include<stdio.h>

struct
{
long long id;
char name[32];
int s;
}t,stus[200];

int main()
{
int n,i,swap=1,sorted=0;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lld %s %d",&stus[i].id,stus[i].name,&stus[i].s);
printf("\n");
while(swap)
{
swap=0;
for(i=0;i<n-1-sorted;i++)
{
if(stus[i+1].s>stus[i].s)
{
swap=1;
t=stus[i];
stus[i]=stus[i+1];
stus[i+1]=t;
}
}
sorted++;
}
for(i=0;i<n;i++)
printf("%lld %s %d\n",stus[i].id,stus[i].name,stus[i].s);
return 0;
}

⑷ 關於C語言結構體數據排序的問題

輸入學生成績按平均分排名。我平均分求出來了。
但是對結構體數組的排序一直沒搞懂。
#include<stdio.h>
#include<string.h>
struct
liebiao
{
int
hao;
char
name[20];
float
fen1;
float
fen2;
float
fen3;
float
pingjun;
}data[50],*p=data;
int
pingjun();
void
paiming();
main(void)
{
int
j;
j=pingjun(p);
paiming(data[50]);
}
int
pingjun(struct
liebiao
*q)
{
int
i,j=0;
char
panan;
for(i=0;i<50;i++,q++,j++)
{
printf("輸入學號:");
scanf("%d",&q->hao);
printf("輸入姓名:");
fflush(stdin);
gets(q->name);
printf("輸入第1科成績:");
scanf("%f",&q->fen1);
printf("輸入第2科成績:");
scanf("%f",&q->fen2);
printf("輸入第3科成績:");
scanf("%f",&q->fen3);
q->pingjun=(q->fen1+q->fen2+q->fen3)/3;
printf("是否要繼續?<y
or
n>");
fflush(stdin);
scanf("%c",&panan);
if(panan=='n')
{
printf("排序前的學員信息如下:\n");
printf("學號\t姓名\t平均成績\n");
for
(i=0;i<=j;i++)
printf("%d\t%s\t%0.2f\n",data[i].hao,data[i].name,data[i].pingjun);
return
j;
break;
/*這語句有啥用?能執行到這里嗎?*/
}
}
}//從這開始是排名。我頭都大了。我寫的一定不對了。
請高手幫我改改。
void
paiming(struct
liebiao
(*q1)[50])
{
int
n,u,i;
struct
liebiao
temp,*q2;
printf("排名後的學員信息如下:\n");
for(n=0;n<=y;n++,q1++)
/*y橫空出世?*/
{
for
(u=1;u<=y;u++,q2++)
{
if
(data[n].pingjun<data[u].pingjun)
temp=q1->data;
/*有這樣用的嗎?*/
q1->data=q2->data;
q2->data=temp;
}
}
for
(i=0;i<5;i++)
printf("%d\t%s\t%0.2f\n",data[i].hao,data[i].name,data[i].pingjun);
}
還有個問題我感覺結構體不能在子函數中轉換呢。

⑸ C語言 結構體如何排序

由於你只是交換字元串,所以修改如下,請檢驗。
定義一個字元串數組 char sn[10];
交換時進行如下修改
strcpy(sn,library.book[k].number);
strcpy(library.book[k].number,library.book[i].number);
strcpy(library.book[i].number,sn);

用strcpy(s1,s2)進行復制字元串,不能直接s1=s2。
#include<stdio.h>
#include <time.h>
typedef struct
{
char number[10];//書號
char name[50];//書名
char author[20];//作者
char publish[50];//出版社
char time[20];//出版時間
float price;//單價
}mbook;
typedef struct
{
int count;//書的本數
mbook book[10];//最大可有書的數量
}mlibrary;
mlibrary library,t;

main()
{ void sort_by_price(void);
sort_by_price();
}
void sort_by_price(void) /*定義按價格排序函數*/
{
int i,j,k;
char sn[10];
for(i=0;i<library.count-1;i++)
{
k=i;
for(j=i+1;j<library.count;j++)
if(library.book[j].price<library.book[k].price)
k=j;
if(k!=i)
{
strcpy(sn,library.book[k].number);
strcpy(library.book[k].number,library.book[i].number);
strcpy(library.book[i].number,sn);
}
}
} ;

⑹ C語言 結構體排序問題

由於你只是交換字元串,所以修改如下,請檢驗。
用strcpy(s1,s2)進行復制字元串,不能直接s1=s2。
#include<stdio.h>
#include <time.h>
typedef struct
{
char number[10];//書號
char name[50];//書名
char author[20];//作者
char publish[50];//出版社
char time[20];//出版時間
float price;//單價
}mbook;
typedef struct
{
int count;//書的本數
mbook book[10];//最大可有書的數量
}mlibrary;
mlibrary library,t;

main()
{ void sort_by_price(void);
sort_by_price();
}
void sort_by_price(void) /*定義按價格排序函數*/
{
int i,j,k;
char sn[10];
for(i=0;i<library.count-1;i++)
{
k=i;
for(j=i+1;j<library.count;j++)
if(library.book[j].price<library.book[k].price)
k=j;
if(k!=i)
{
strcpy(sn,library.book[k].number);
strcpy(library.book[k].number,library.book[i].number);
strcpy(library.book[i].number,sn);
}
}
}

⑺ C語言中結構體數據排序

設結構體名為AAA,結構體數組聲明為struct AAA a[N];(N為宏定義常量),身份證成員名為id,則排序函數可如下寫——

#include"stdio.h"
#include<string.h>
#defineN3
structAAA{
charid[20];
intage;
};
voidmysort(structAAA*p){//排序函數
structAAAt;
inti,j,k;
for(i=0;i<N;i++){
for(k=i,j=k+1;j<N;j++)
if(strcmp((p+j)->id,(p+k)->id)<0)
k=j;
if(i!=k)
t=*(p+k),*(p+k)=*(p+i),*(p+i)=t;
}
}
intmain(intargc,char*argv[]){//測試主函數
structAAAa[N]={{"650104194812109907",77},{"333018201801015555",1},{"650104194812109903",80}};
mysort(a);
printf("%s %d ",a[0].id,a[0].age);
printf("%s %d ",a[1].id,a[1].age);
printf("%s %d ",a[2].id,a[2].age);
return0;
}

運行結果:

⑻ 求用c語言怎麼對結構體進行排序

和int類型數組排序一樣的啊,
把結構體類型看成int類型

⑼ 關於C語言的結構體中元素的篩選,排列順序。

錯了兩個地方,第一個錯誤是q的使用,因為q=per,所以每次*q=*(p+1);會導致把數組第一個改掉。第二個錯誤是冒泡只冒了一遍,應該用兩層循環。
只要改一點點就好了,寫注釋的地方就是改動的地方。
#include "stdafx.h"
#include "string"
void main()
{
struct person
{
char name[20];
char sex;
int age;
float height;
}per[7]={{"Li Ping",'M',22,178.2},{"Gao Ming",'F',23,171.5},{"Zhao Er",'F',32,168.6},{"Wang Ying",'F',20,158.9},{"Zhang Xiao",'M',25,181.0},{"Deng Tao",'M',27,177.6},{"Hao San",'F',22,175.7},};
struct person *p;
struct person q; //這行,去了*
for (int i = 0; i < 7; i++) { //這行,新加的
for(p = per; p < per+7; p++) //這行,加了p=per
{
if(strcmp(p->name,(p+1)->name)>0)
{
q=*(p+1); //這行,去了*
*(p+1)=*p;
*p=q; //這行,去了*
}
}
}

//下面這幾行,最後循環一次輸出排序的結果
for(p = per; p < per+7; p++) {
printf("name=%-18s,sex=%3c,age=%4d,height=%7.2f\n",p->name,p->sex,p->age,p->height);
}
}
}

⑽ C語言結構體排序問題

就是排序問題嘛,跟結構體有啥關系?

比較結構體中的shu成員啊

冒泡排序的核心代碼如下:

struct student ss[20],t;

for(i=0;i<20-1;i++)
{
for(j=20-1;j>i;j--)
{
if(ss[j].shu < ss[j-1].shu)
{
t=ss[j];
ss[j]=ss[j-1];
ss[j-1]=t;
}
}
}

補充:
只需要在交換的時候,按整個結構體換,就行了啊
下面交換代碼中的t和ss都是結構體啊

struct student ss[20],t;

t=ss[j];
ss[j]=ss[j-1];
ss[j-1]=t;

熱點內容
酷狗音樂試聽緩存刪了會怎樣 發布:2025-02-13 11:02:12 瀏覽:267
python游戲服務端 發布:2025-02-13 11:00:19 瀏覽:927
雲原生伺服器 發布:2025-02-13 10:55:34 瀏覽:827
linuxip命令查看ip 發布:2025-02-13 10:49:45 瀏覽:421
java基礎應用 發布:2025-02-13 10:44:53 瀏覽:711
linux內核搶占 發布:2025-02-13 10:36:32 瀏覽:890
家裝公司源碼 發布:2025-02-13 10:35:35 瀏覽:48
aspnet更新資料庫 發布:2025-02-13 10:35:34 瀏覽:384
海爾壓縮機不工作 發布:2025-02-13 10:15:32 瀏覽:223
才兒坊編程 發布:2025-02-13 10:09:58 瀏覽:730