根據(jù)題目中信息所示:僅輸入一科目成績(jī);人數(shù)不作為參數(shù)輸入;
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)與策劃設(shè)計(jì),遂寧網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:遂寧等地區(qū)。遂寧做網(wǎng)站價(jià)格咨詢:028-86922220
現(xiàn)在假設(shè):及格線為60分(題目未明確給出)
#includestdio.h
//#include cstring
//#includealgorithm
typedef struct {
int grade;
}student;
//選擇排序
void selectSort(student stu[10],int n){
for(int i=0;i=n;i++)
{int k=i;
for(int j=k+1;j=n;j++){
if(stu[j].gradestu[k].grade)
{k=j;
}}
int temp=stu[i].grade;
stu[i].grade=stu[k].grade;
stu[k].grade=temp;}}
//數(shù)據(jù)輸出
void display(student stu[10],int n)
{
for(int j=0;jn;++j)
printf("%d ",stu[j].grade);
? printf("\n");}
//數(shù)據(jù)最大值與最小值
void poll_values(student stu[2],int n){
printf("%d %d",stu[0].grade,stu[n].grade);
printf("\n");
}
int main(){
student stu[10];
student fail[10];
student poll[2];
int n=0;//總?cè)藬?shù)
int m=0;//不及格人數(shù)
while(n10){
scanf("%d",stu[n].grade);
if(stu[n].grade0){
break;
}
++n;}
selectSort(stu,n);
//--------------------------------------
for(int i=0;in;i++)
if(stu[i].grade60){
fail[m]=stu[i];
++m;}
selectSort(fail,m);
display(stu,n);
display(fail,m);
poll[0]=stu[0];
poll[1]=stu[n-1];
display(poll,2);
//poll_values(stu,n)
return 0;
}
運(yùn)行結(jié)果如下
//這里我在網(wǎng)上找來運(yùn)行著練手的,具體原作者我忘了保存,抱歉。十分感謝他(她)的汗水。
//可以自己微調(diào)一下
//成績(jī)排序方面,可以用平均成績(jī)排序輸出模塊,按相應(yīng)科目更改一下數(shù)組序號(hào)則可
//或者,再寫一個(gè)科目選擇,按選擇中的科目引用相應(yīng)的數(shù)組內(nèi)容則可以實(shí)現(xiàn)按科目排序
#includestring.h??
#includestdio.h
/*定義學(xué)生結(jié)構(gòu)體*/??
struct?Student??
{??
char?ID[20];??
char?Name[20];??
float?Mark1;??
float?Mark2;
float?Mark3;??
float?Average;??
};??
/*聲明學(xué)生數(shù)組及學(xué)生數(shù)量*/??
struct?Student?students[1000];??
int?num=0;??
/*求平均值*/??
float?Avg(struct?Student?stu)??
{??
return?(stu.Mark1+stu.Mark2+stu.Mark3)/3;??
}??
/*通過學(xué)號(hào)返回?cái)?shù)組下標(biāo)*/??
int?Student_SearchByIndex(char?id[])??
{??
int?i;??
for?(i=0;inum;i++)??
{??
if?(strcmp(students[i].ID,id)==0)??
{??
return?i;??
}??
}??
return?-1;??
}??
/*通過姓名返回?cái)?shù)組下標(biāo)*/??
int?Student_SearchByName(char?name[])??
{??
int?i;??
for?(i=0;inum;i++)??
{??
if?(strcmp(students[i].Name,name)==0)??
{??
return?i;??
}??
}??
return?-1;??
}??
/*顯示單條學(xué)生記錄*/??
void?Student_DisplaySingle(int?index)??
{??
printf("%10s%10s%8s%8s%8s%10s\n","學(xué)號(hào)","姓名","成績(jī)","成績(jī)","成績(jī)","平均成績(jī)");??
printf("-------------------------------------------------------------\n");??
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,??
students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);??
}??
/*插入學(xué)生信息*/??
void?Student_Insert()??
{??
while(1)??
{??
printf("請(qǐng)輸入學(xué)號(hào):");??
scanf("%s",students[num].ID);??
getchar();??
printf("請(qǐng)輸入姓名:");??
scanf("%s",students[num].Name);??
getchar();??
printf("請(qǐng)輸入成績(jī):");??
scanf("%f",students[num].Mark1);??
getchar();??
printf("請(qǐng)輸入成績(jī):");??
scanf("%f",students[num].Mark2);??
getchar();??
printf("請(qǐng)輸入成績(jī):");??
scanf("%f",students[num].Mark3);??
getchar();??
students[num].Average=Avg(students[num]);??
num++;??
printf("是否繼續(xù)?(y/n)");??
if?(getchar()=='n')??
{??
break;??
}??
}??
}??
/*修改學(xué)生信息*/??
void?Student_Modify()??
{??
//float?mark1,mark2,mark3;??
while(1)??
{??
char?id[20];??
int?index;??
printf("請(qǐng)輸入要修改的學(xué)生的學(xué)號(hào):");??
scanf("%s",id);??
getchar();??
index=Student_SearchByIndex(id);??
if?(index==-1)??
{??
printf("學(xué)生不存在!\n");??
}??
else??
{??
printf("你要修改的學(xué)生信息為:\n");??
Student_DisplaySingle(index);??
printf("--?請(qǐng)輸入新值--\n");??
printf("請(qǐng)輸入學(xué)號(hào):");??
scanf("%s",students[index].ID);??
getchar();??
printf("請(qǐng)輸入姓名:");??
scanf("%s",students[index].Name);??
getchar();??
printf("請(qǐng)輸入成績(jī):");??
scanf("%f",students[index].Mark1);??
getchar();??
printf("請(qǐng)輸入成績(jī):");??
scanf("%f",students[index].Mark2);??
getchar();??
printf("請(qǐng)輸入成績(jī):");??
scanf("%f",students[index].Mark3);??
getchar();??
students[index].Average=Avg(students[index]);??
}??
printf("是否繼續(xù)?(y/n)");??
if?(getchar()=='n')??
{??
break;??
}??
}??
}??
/*刪除學(xué)生信息*/??
void?Student_Delete()??
{??
int?i;??
while(1)??
{??
char?id[20];??
int?index;??
printf("請(qǐng)輸入要?jiǎng)h除的學(xué)生的學(xué)號(hào):");??
scanf("%s",id);??
getchar();??
index=Student_SearchByIndex(id);??
if?(index==-1)??
{??
printf("學(xué)生不存在!\n");??
}??
else??
{??
printf("你要?jiǎng)h除的學(xué)生信息為:\n");??
Student_DisplaySingle(index);??
printf("是否真的要?jiǎng)h除?(y/n)");??
if?(getchar()=='y')??
{??
for?(i=index;inum-1;i++)??
{??
students[i]=students[i+1];//把后邊的對(duì)象都向前移動(dòng)??
}??
num--;??
}??
getchar();??
}??
printf("是否繼續(xù)?(y/n)");??
if?(getchar()=='n')??
{??
break;??
}??
}??
}??
/*按姓名查詢*/??
void?Student_Select()??
{??
while(1)??
{??
char?name[20];??
int?index;??
printf("請(qǐng)輸入要查詢的學(xué)生的姓名:");??
scanf("%s",name);??
getchar();??
index=Student_SearchByName(name);??
if?(index==-1)??
{??
printf("學(xué)生不存在!\n");??
}??
else??
{??
printf("你要查詢的學(xué)生信息為:\n");??
Student_DisplaySingle(index);??
}??
printf("是否繼續(xù)?(y/n)");??
if?(getchar()=='n')??
{??
break;??
}??
}??
}??
/*按平均值排序*/??
void?Student_SortByAverage()??
{??
int?i,j;??
struct?Student?tmp;??
for?(i=0;inum;i++)??
{??
for?(j=1;jnum-i;j++)??
{??
if?(students[j-1].Averagestudents[j].Average)??
{??
tmp=students[j-1];??
students[j-1]=students[j];??
students[j]=tmp;??
}??
}??
}??
}??
/*顯示學(xué)生信息*/??
void?Student_Display()??
{??
int?i;??
printf("%10s%10s%8s%8s%8s%10s\n","學(xué)號(hào)","姓名","成績(jī)","成績(jī)","成績(jī)","平均成績(jī)");??
printf("-------------------------------------------------------------\n");??
for?(i=0;inum;i++)??
{??
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,??
students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);??
}??
}??
/*將學(xué)生信息從文件讀出*/??
void?IO_ReadInfo()??
{??
FILE?*fp;??
int?i;??
if?((fp=fopen("Database.txt","rb"))==NULL)??
{??
printf("不能打開文件!\n");??
return;??
}??
if?(fread(num,sizeof(int),1,fp)!=1)??
{??
num=-1;??
}??
else??
{??
for(i=0;inum;i++)??
{??
fread(students[i],sizeof(struct?Student),1,fp);??
}??
}??
fclose(fp);??
}??
/*將學(xué)生信息寫入文件*/??
void?IO_WriteInfo()??
{??
FILE?*fp;??
int?i;??
if?((fp=fopen("Database.txt","wb"))==NULL)??
{??
printf("不能打開文件!\n");??
return;??
}??
if?(fwrite(num,sizeof(int),1,fp)!=1)??
{??
printf("寫入文件錯(cuò)誤!\n");??
}??
for?(i=0;inum;i++)??
{??
if?(fwrite(students[i],sizeof(struct?Student),1,fp)!=1)??
{??
printf("寫入文件錯(cuò)誤!\n");??
}??
}??????
fclose(fp);??
}??
/*主程序*/??
void?main()??
{??
int?choice;??
IO_ReadInfo();??
while(1)??
{??
/*主菜單*/??
printf("\n------?學(xué)生成績(jī)管理系統(tǒng)------\n");??
printf("1.?增加學(xué)生記錄\n");??
printf("2.?修改學(xué)生記錄\n");??
printf("3.?刪除學(xué)生記錄\n");??
printf("4.?按姓名查詢學(xué)生記錄\n");??
printf("5.?按平均成績(jī)排序\n");??
printf("6.?退出\n");??
printf("請(qǐng)選擇(1-6):");??
scanf("%d",choice);??
getchar();??
switch(choice)??
{??
case?1:??
Student_Insert();??
break;??
case?2:??
Student_Modify();??
break;??
case?3:??
Student_Delete();??
break;??
case?4:??
Student_Select();??
break;??
case?5:??
Student_SortByAverage();??
Student_Display();??
break;??
case?6:??
exit(0);??
break;??
}??
IO_WriteInfo();??
}??
}
#include stdio.h
int main()
{
int i,j,temp,a[100]={0},ranking=0,n;
printf("請(qǐng)輸入學(xué)生數(shù):\n");
scanf("%d",n);
printf("請(qǐng)輸入學(xué)生成績(jī):\n");
for (i=0;in;i++)
{
scanf("%d",a[i]);
}
//對(duì)成績(jī)排序
for (i=0;in;i++)
{
for (j=i+1;jn;j++)
{
if (a[i]a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("成績(jī)由高到低順序排序:\n");
for (i=0;in;i++)
{
++ranking;
if ((a[i]==a[i-1]))
{
ranking=ranking-1;
}
printf("%d:%d\n",a[i],ranking);
}
printf("\n");
return 0;
}
你好,我們這里需要用到數(shù)組鑲套使用for函數(shù)以及冒泡算法,具體的代碼如下。
#include iostream
using namespace std;
int main()
{
int s [10], i, j, t;
cout "輸入10個(gè)數(shù):";
for (i = 0; i 10; i++)
{
cout "請(qǐng)輸入第" i+1 "名學(xué)生的成績(jī);" endl;
cin s[i];
}
for (i = 0; i10; i++)
{
for (j = 0; j9 - i; j++)
if (s[j]s[j + 1])
{
t = s[j + 1];
s[j + 1] = s[j];
s[j] = t;
}
}
cout "成績(jī)由高到低為;";
for (i = 0; i10; i++)
cout s[i]"\t" ;
}
以下為效果圖
#includestdio.h
int main()
{
int a[10],i,j,t,k;
printf("請(qǐng)以次輸入10個(gè)學(xué)生的成績(jī):");
for(i=0;i10;i++)scanf("%d",a[i]);
for(i=0;i9;i++)
{
k=i;
for(j=i;j10;j++)
if(a[j]a[k])k=j;
if(i!=k)
{
t=a[i];
a[i]=a[k];
a[k]=t;
}
}
printf("由高到低排序輸出成績(jī):\n");
for(i=0;i10;i++)
printf("%d\n",a[i]);
return 0;
}