c語言的成績排序
Ⅰ c語言 怎麼對成績排序
#include <stdlib.h>
int cmp(const void *a, const void *b)
{
student *x=(student*)a;
student *y=(student*)b;
if(x->score[0] > y->score[0])//升序排序(從小到大) 如果>改成<,就是降序排序(從大到小)
return 1;
return 0;
}
//上面函數放在struct student下面,需要該結構
//排序方式可以靈活調整,上面我只是用第一門分數進行比較
//你也可以用其它分數進行比較,多寫2個函數就行,如int cmp2(const void*a,const void*b)等等。
//排序函數
qsort(stu,STU_NUM,sizeof(stu[0]),cmp);
//stu是數組名,STU_NUM是數組元素個數,cmp就排序方式(可以替換為cmp2等等)
Ⅱ C語言成績排序
#include<bits/stdc++.h>
usingnamespacestd;
structdata{
charst1[233],st2[233];
doublea,b,c;
}a[233];
intmycomp(constdata&a,constdata&b){
return((a.a+a.b+a.c)<(b.a+b.b+b.c));
}
intmain(){
intn=1;
while(scanf("%s%s%lf%lf%lf",&a[n].st1,&a[n].st2,&a[n].a,&a[n].b,&a[n].c)!=EOF)
n++;
n--;
sort(a+1,a+n+1,mycomp);
for(inti=1;i<=n;i++)
printf("%s%s%.2lf%.2lf%.2lf ",a[i].st1,a[i].st2,a[i].a,a[i].b,a[i].c);
}
Ⅲ C語言怎麼進行成績的排序
#include<stdio.h>
int main()
{
int a[10],i,j,t,k;
printf("請以次輸入10個學生的成績:");
for(i=0;i<10;i++)scanf("%d",&a[i]);
for(i=0;i<9;i++)
{
k=i;
for(j=i;j<10;j++)
if(a[j]>a[k])k=j;
if(i!=k)
{
t=a[i];
a[i]=a[k];
a[k]=t;
}
}
printf("由高到低排序輸出成績:\n");
for(i=0;i<10;i++)
printf("%d\n",a[i]);
return 0;
}
Ⅳ 用C語言編程:從鍵盤輸入10個學生的成績,由高到低排序輸出成績
你好,我們這里需要用到數組鑲套使用for函數以及冒泡演算法,具體的代碼如下。
#include <iostream>
using namespace std;
int main()
{
int s [10], i, j, t;
cout << "輸入10個數:";
for (i = 0; i < 10; i++)
{
cout << "請輸入第" << i+1 << "名學生的成績;" << endl;
cin >> s[i];
}
for (i = 0; i<10; i++)
{
for (j = 0; j<9 - i; j++)
if (s[j]<s[j + 1])
{
t = s[j + 1];
s[j + 1] = s[j];
s[j] = t;
}
}
cout << "成績由高到低為;";
for (i = 0; i<10; i++)
cout << s[i]<<" " ;
}
以下為效果圖
Ⅳ c語言用指針做成績表高到低排序…怎麼做呀
給你講個思路 學過太久了 不翻自己以前的模板就不會了
不知道你們要求的指針是什麼意思,如果嚴格規定是鏈表就很難了
先說簡單的
第一張
voidsort(int*score,intn)//score是保存成績的數組n是人數
{
inti,j;
for(i=0;i<n-1;i++)//這里要跑n-1次循環每次選出剩下元素中最大的放到前面
{
intmaxx=i;//那麼最後只剩兩個元素時只要選一次就好
for(j=i+1;j<n;j++)//掃第i個元素之後的元素
{
if(score[maxx]<score[j])
maxx=j;
}
inttemp=score[i];
score[i]=score[maxx];
score[maxx]=temp;
}
return;
}//排序完畢
第二種
首先你肯定建好了一個鏈表 (這個鏈表不會建你這題真很難)
然後把你的鏈表切開 (有沒有初始頭結點? 就是不保存數據 但是依舊是一個節點防止鏈表為空的節點 沒有的話把鏈表從第一個節點後面切開 有的話切第二個節點之後的)
切開後就變成了兩條鏈表 比如 (-1)--1--4--6--8--9--7--5--1 切開就是 (-1)--1
4--6--8--9--7--5--1
兩條鏈表的頭節點都要保存(不然你就找不到了)
從第二條鏈表 (長的那條)中拿出第一個節點 斷開 然後試著往第一條鏈表中插入
比第一個節點的值大? 插到它之前 否則往後走 直到遇到一個比他大的節點 插到它之後
循環插入之後,這條鏈表就是有序的了
(其實鏈表構建的時候你就可以構建一條有序的鏈表 )
Ⅵ C語言按成績排序問題
tc調試通過 #include<stdio.h> #define NUM 50 main() { int i,j; float n; struct student{ char name[50];/*學生姓名*/ float point;/*成績*/ }stu[NUM],temp; printf("請依次輸入學生姓名及對應成績\n"); for(i=0;i<NUM;i++) { scanf("%s",stu[i].name); scanf("%f",&n); n=stu[i].point; } for(i=0;i<NUM;i++) for(j=i+1;j<NUM;j++) if(stu[i].point<stu[j].point) { temp=stu[i]; stu[i]=stu[j]; stu[j]=temp; } printf("下面為排好序的學生姓名及對應的成績"); for(i=0;i<NUM;i++) printf("%s:%f\n",stu[i].name,stu[i].point); }
Ⅶ c語言程序設計 成績排序
BiThrTree bt = NULL;
CreateBTree(bt);//生成一棵二杈排序樹(輸入單個字元,以#結束)
InOrderPrint_1(bt); //中序遍歷輸出結點(遞歸)
cout << endl;
BiThrTree BT = InOrderThreading(bt);//中序遍歷二杈樹,並將其中序線索化
InOrderTraverse_Thr(BT);//中序遍歷線索二杈樹的非遞歸演算法, T 指向頭結點
system("PAUSE");
return EXIT_SUCCESS;
}
void InOrderTraverse_Thr(BiThrTree T)//中序遍歷線索二杈樹的非遞歸演算法, T 指向頭結點
{
BiThrTree p = T->lchild; //p指向根結點
while (p != T) //空樹或遍歷結束時,p == T
{
Ⅷ C語言的學生成績排序問題
#include <stdio.h>
#include <stdlib.h>
int main() {
struct student {
int num;
float scores;
};
student *stu = new student;
float insert = 0, temp = 0;
int i = 0;
for (; insert != -1; i++) {
printf("請輸入學生成績(結束輸入-1):");
scanf("%f", &insert);
stu[i].num = i + 1;
stu[i].scores = insert;
}
for (int m = 0; m < i - 2; m++) {
for (int n = 0; n < i - 2; n++) {
temp = stu[n].scores;
if (temp < stu[n + 1].scores) {
stu[n].scores = stu[n + 1].scores;
stu[n + 1].scores = temp;
temp = stu[n].num;
stu[n].num = stu[n+1].num;
stu[n+1].num = (int)temp;
}
}
}
for (int j = 0 ; j < i - 1 ; j++){
printf("%s%d%s\t%s%d\t%s%.2f\n","第",j+1,"名:","號數:",stu[j].num,"成績:",stu[j].scores);
}
system("PAUSE");
return 0;
}
Ⅸ C語言學生成績排序程序
#include "stdio.h"
void main()
{
int i,j;
float A[11],t;
for(i=0;i<10;i++)
{
printf("第%d個學生成績:\n",i+1);
scanf("%f",&A[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(A[j]<A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
printf("插入學生成績:\n");
scanf("%f",&t);
if(t<A[9]) A[10]=t;
else if(t>A[0])
{
for(i=9;i>=0;i--)
A[i+1]=A[i];
A[0]=t;
}
else
{
for(i=9;i>=1;i--)
{
A[i+1]=A[i];
if(A[i]<=t && A[i-1]>=t)
{
A[i]=t;
break;
}
}
}
for(i=0;i<11;i++)
printf("%g ",A[i]);
}
Ⅹ C語言 成績排序
do{
scanf("%s %d",st[i].name,&st[i].score);
if(st[i].score>=0&&st[i].score<=100)
break;
}while(1);
使用這個語句輸入。
第二個問題:
現在實行的C語言標准一般都C89標准,C89標準是不支持變長數組的,只有C99標准才支持。(變長數組就是長度是一個變數)
如果真想在C89下使用變長數組,那就只能用malloc和free開辟空間來實現
int n = 0;
scanf("%d",&n);
struct student *st = (struct student *)malloc(n * sizeof(struct student));
這樣,st可以直接當一個數組來用,st[4]類似的加下標都沒問題(數組=指針=字元串,我說原則,char數組=字元串)
不要忘了最後用free(st);釋放獲得的空間,不然會造成內存浪費的……