#include<stdio.h>
#include<string.h>
struct student
{ int num;
char name[20];
int sco[3];
} a[10];
main()
{ void px(struct student);
struct student a;
int i,j;
printf(" 请输入学号和姓名\n");
for(i=0;i<10;i++)
scanf("%d%s",&a[i].num,a[i].name);
printf(" 请输入3门课程的成绩\n");
for(i=0;i<10;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i].sco[j]);
px(a);
for(i=0;i<10;i++)
printf("\n %d,%s,%d,%d,%d",a[i].num,a[i].name,a[i].sco[0],a[i].sco[1],a[i].sco[2]);
}
void px(struct student a)
{ int i,j,k,n,m,h[10];
char temp[20];
for(i=0;i<10;i++)
{ m=0;
for(j=0;j<3;j++)
m+=a[i].sco[j];
h[i]=m;
}
for(i=0;i<9;i++)
{ for(j=i+1;j<10;j++)
if(h[i]<h[j])
{ k=h[i]; h[i]=h[j];h[j]=k;
k=a[i].num; a.num[i]=a[j].num; a.num[j]=k;
strcpy(temp,a[i].name);strcpy(a[i].name,a[j].name);strcpy(a[j].name,temp);
for(n=0;n<3;n++)
{ k=a[i].sco[n];
a[i].sco[n]=a[j].sco[n];
a[j].sco[n]=k;
}
}
}
}
{
int num;
char name[20];
int sco[3];
} a[10];
çä»·äºå£°æäºä¸ä¸ªstudentæ°ç»ï¼å¹¶ä¸struct student a[10];
è¿ä¸ªaæ°ç»æ¯å ¨å±åé
èä½ å¨ä¸»å½æ°éé¢è°ç¨äºstruct student a;è¿ä¸ªaæ¯å±é¨åéï¼è¿ä¸ªaæå ¨å±åéa[10]ç»è¦çæäºã追é®
å¯æ¯æ ä¸å¥½ç¼è¯ï¼æ»æ¯æä¸ä¸ª errorï¼æ¾ä¸åºæ¥ã
类似 è¿ç§ï¼
D:\Sylvia\Cç¼ç¨\a.c(39) : error C2198: 'strcpy' : too few actual parameters
D:\Sylvia\Cç¼ç¨\a.c(39) : error C2088: '[' : illegal for struct
D:\Sylvia\Cç¼ç¨\a.c(39) : error C2088: '[' : illegal for struct
è¦ä¸ï¼æå¸®ä½ æ¹ä¸ä¸å§ãä½ çè¡åï¼ä½ çç¨åºé®é¢å¾å¤ï¼æ ¢æ ¢è§£éï¼ææè¶è§£éä½ è¶ä¹±ãæå¸®ä½ åä¸ä¸ªæ¯è¾å·¥æ´çãç¨çä¸å
追é®æ©æ©é¢ é常æè°¢ä¹ã
追ç#include
#include
struct student
{
int num;
char name[20];
int sco[3];
} a[10];
// void px(struct student[]); // å½æ°å£°ææ¾å¨å½æ°å¤é¢
void sort(struct student[]); // æç²¾ç®äºä¸ä¸ä½ çç®æ³
void main() // è¿éæ好æç¡®çåä¸è¿åç±»åï¼å¦åæäºç¼è¯å¨ä¼æ¥é
{
//struct student a; // è¿å¥è¯æ²¡æç¨ï¼è·å
¨å±åééå
int i,j;
printf(" 请è¾å
¥å¦å·åå§å\n");
for(i=0;i<10;i++)
{// å æ¬å·æ¯ç¼ç è§èï¼æ¯ä¸ä¸ªè¯å¥½ä¹ æ¯
scanf("%d %s",&a[i].num,a[i].name);
}
// printf(" 请è¾å
¥3é¨è¯¾ç¨çæ绩\n");
for(i=0;i<10;i++)
{
printf("请è¾å
¥%sçä¸é¨æ绩\n",a[i].name); // è¿æ ·è¾å
¥çæ¶åç¥éèªå·±è¾å
¥çæ¯åªä¸ä¸ªå¦ç
for(j=0;j<3;j++)
{
scanf("%d",&a[i].sco[j]);
}
}
// px(a);
sort(a);
for(i=0;i<10;i++)
{
printf("\n %d,%s,%d,%d,%d",a[i].num,a[i].name,a[i].sco[0],a[i].sco[1],a[i].sco[2]);
}
}
/*
void px(struct student a[])// ä¼ è¿æ¥çæ¯ç»æä½æ°ç»ï¼æ以形åè¦è¿ä¹å
{
int i,j,k,n,m,h[10]; // åéåå°½ééæ©ææä¹çååï¼æå°±ä¸æ¹äº
char temp[20];
for(i=0;i<10;i++)
{
m=0;
for(j=0;j<3;j++)
{
m+=a[i].sco[j];
}
h[i]=m;
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
if(h[i]<h[j])
{
k=h[i];
h[i]=h[j];
h[j]=k;
k=a[i].num;
a.num[i]=a[j].num;
a.num[j]=k;
strcpy(temp,a[i].name); // ä¸è¦å°å¤è¡å½ä»¤æ¾å°ä¸è¡éï¼ä¸å©äºåæ¥è°è¯ï¼è¿ä¹æ¯ç¼ç è§è
strcpy(a[i].name,a[j].name);
strcpy(a[j].name,temp);
for(n=0;n<3;n++)
{
k=a[i].sco[n];
a[i].sco[n]=a[j].sco[n];
a[j].sco[n]=k;
}
}
}
}*/
void sort(struct student a[])
{
int i = 0, j = 0;
struct student temp;
// ä¸é¢æ¯ææä½ çå泡æåºç²¾ç®äºä¸ä¸çç®æ³
for (i = 0; i < 10; ++i)
{
for (j = i + 1; j < 10; ++j)
{
int totalI = a[i].sco[0] + a[i].sco[1] + a[i].sco[2];
int totalJ = a[j].sco[0] + a[j].sco[1] + a[j].sco[2];
if (totalI < totalJ)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
哦哦。。 我 是编译有一个错误。