结构体数组做参数

发布网友 发布时间:2022-04-20 01:19

我来回答

2个回答

热心网友 时间:2023-07-01 06:57

这样既可:把结构体定义放到main外面

#include<stdio.h>/*结构体变量stu,内含学生学号,姓名,三门课成绩,要求在main中赋初值,在函数print中打印输出*/

void print(struct stu *s, int i);

struct cj/*三门课的成绩*/
{
int yuwen;
int shuxue;
int yingyu;
}cj[3]={ {80,90,100},{70,71,72},{60,61,62} };

struct stu/*学生*/
{
int num;
char name[20];
struct cj score;
}stu1[3]={
{1,"one",cj[0].yuwen,cj[0].shuxue,cj[0].yingyu},
{2,"two",cj[1].yuwen,cj[1].shuxue,cj[1].yingyu},
{3,"three",cj[2].yuwen,cj[2].shuxue,cj[2].yingyu}
};

int main()
{
int i;

printf("输入一个学生号码\n");
scanf("%d",&i);
print(&stu1[i - 1], i);
getchar() ;
}
void print(struct stu *s, int i)
{
int n;
printf("%d,%s,%d,%d,%d\n",s->num, s->name,cj[i - 1].yuwen,cj[i - 1].shuxue,cj[i - 1].yingyu);
}

热心网友 时间:2023-07-01 06:58

记得当年我班实验课的时候我看着题就恶心吐了,我班12个女生让我帮写,还都要不一样的,差点没整疯了我,不想再敲了,给你发段C++代码你自己看吧,或者手动改成C,C和C++差别不大,很容易的,这种题教科书般的C++代码。
#include<string.h>
#include<iostream.h>
#define N 10
class std
{
int No; //学号
char name[10]; //姓名
float Eng; //英语成绩
float C; //C成绩
float Mat; //数学成绩
float total; //总成绩
public:
void setname(char na[]){strcpy(name,na);};
void setNo(int n){No=n;};
void setEng(float e){Eng=e;};
void setC(float c){C=c;};
void setMat(float m){Mat=m;};
void settotal(float t){total=t;};
char* getname(){return name;};
int getNo(){return No;};
float getEng(){return Eng;};
float getC(){return C;};
float getMat(){return Mat;};
float gettotal(){ return total;};
};

class compute
{
int ns;
std na[N];
static float sumEng;
static float sumC;
static float sumMat;
public:
void getdata();
void sort();
void disp();
float aveEng()
{
return sumEng/ns;
}
float aveC()
{
return sumC/ns;
}
float aveMat()
{
return sumMat/ns;
}
};

float compute::sumEng=0.0;
float compute::sumC=0.0;
float compute::sumMat=0.0;
void compute::getdata()
{
int i,sno;
float En,Ch,Ma,Tol;
char tname[10];
cout<<"Please input the amounts of students!";
cin>>ns;
for(i=0;i<ns;i++)
{
cout<<"Input the student's NO.:";
cin>>sno;
na[i].setNo(sno);
cout<<"Input the student's name.:";
cin>>tname;
na[i].setname(tname);
cout<<"Input the student's English score:";
cin>>En;
sumEng=sumEng+En;
na[i].setEng(En);
cout<<"Input the student's C score:";
cin>>Ch;
sumC=sumC+Ch;
na[i].setC(Ch);
cout<<"Input the student's Maths score:";
cin>>Ma;
sumMat=sumMat+Ma;
na[i].setMat(Ma);
Tol=En+Ch+Ma;
na[i].settotal(Tol);
}
}
void compute::sort() //直接选择排序
{
int i,j,pick;
std temp;
for(i=0;i<ns-1;i++)
{
pick=i;
for(j=i+1;j<ns;j++)
{
if(na[j].gettotal()>na[pick].gettotal())
pick=j;
}
temp=na[i];
na[i]=na[pick];
na[pick]=temp;
}
}
void compute::disp()
{
cout<<"List"<<" NO"<<" Name"<<" English"<<" Chinese"<<" Maths"<<" Total "<<endl;
for(int i=0;i<ns;i++)
{
cout<<i+1<<" "<<na[i].getNo()<<" "<<na[i].getname()<<" "<<na[i].getEng()<<" "
<<na[i].getC()<<" "<<na[i].getMat()<<" "<<na[i].gettotal()<<endl;
}
}

void main()
{
compute obj;
obj.getdata();
obj.sort();
obj.disp();
cout<<"The average score of English is:"<<obj.aveEng()<<endl;
cout<<"The average score of Chinese is:"<<obj.aveC()<<endl;
cout<<"The average score of Maths is:"<<obj.aveMat()<<endl;
}
夜安,分给别人吧,我去觉觉了。
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com