結構體儲存三個學生的成績
Ⅰ 用結構體編程三個學生的平均成績的輸出
#include<stdio.h>
struct student
{
char name[10]; //學生姓名
int score[4];//4個科目的分數
}stu[3]; //學生人數
int main()
{
int i,j,sum;
float avg;
sum=0;
printf("請輸入學生的姓名跟分數:\n\n");
for(i=0;i<3;i++)
{
printf("輸入stu[%d]的:\n",i);
scanf("%s",stu[i].name);
for(j=0;j<4;j++)
{
printf("請輸入score[%d]分數",j);
scanf("%d",&stu[i].score[j]);
}
printf("\n");
}
for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
{
sum=sum+stu[i].score[j];
avg=sum/3;
}
sum=0;
printf("score[%d]的平均分是:%5.2f\n",j,avg);
}
}
Ⅱ c語言:結構體:有3個學生,每個學生的數據包括學號、姓名、3門課的成績,要求輸出3門課總平均成績
定義了char number[3],所以只保存前三個字元,而你printf時卻要輸出6個,就連名字那前三個字元也輸出了。比如第一個信息,學號只存成了B13,後面寫的沒有存入,姓名存了SDF,因為name跟在number之後,所以你在輸出六個字元,就成了B13SDF
Ⅲ 用結構體數組存儲學生信息,每個學生有姓名、學號、年齡和成績。輸入N(10)名學生信息,按成績從高低
注意輸入數據時的形式。
#include<stdio.h>
struct person
{int xuehao;
char name[20];
char sex;
int year;
int grade;
}man[80];
main()
{int n,i,max,k=0;
printf("一共有多少學生;");
scanf("%d",&n);
for(i=0;i<n;i++)
{scanf("%d,%d,%d,",&man[i].xuehao,&man[i].year,&man[i].grade);
scanf("%c,%s",&man[i].sex,&man[i].name);}
max=man[0].grade;
for(i=0;i<n;i++)
if(max<man[i].grade)
{max=man[i].grade;
k=i;}
printf("name sex xuehao year grade\n");
printf("--------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%-12s%-4c%4d%8d%10d\n",man[i].name,man[i].sex,man[i].xuehao,man[i].year,man[i].grade);
printf("最高分的學生信息:\n");
printf("name sex xuehao year grade\n");
printf("--------------------------------------------------\n");
printf("%-12s%-4c%4d%8d%10d\n",man[k].name,man[k].sex,man[k].xuehao,man[k].year,man[k].grade);
}
Ⅳ 程序通過定義學生結構體數組,存儲了若干名學生的學號、姓名和3門課的成績。函數fun的功能是將
1. struct student
2. n - 1
3. a[i].name, a[j].name
4. for (i=0; i<3; i++)
5. printf("%6.2f ", s[j].score[i]);
Ⅳ c語言,設計一個學生結構體,輸入三個成績並求成績最大值
這就是最基本的結構體操回作。答
#include<stdio.h>
typedefstructs{
charname[32];
intscore;
}student;
intmain(void){
studenta,b,c;
intmax;
scanf("%s%d",a.name,&a.score);
scanf("%s%d",b.name,&b.score);
scanf("%s%d",c.name,&c.score);
max=a.score>b.score?a.score:b.score;
max=max>c.score?max:c.score;
printf("maxscoreis%d ",max);
return0;
}
Ⅵ (1)定義一個結構體數組,存放10個學生的學號,姓名,三門課的成績; 急求🙏
之前寫過一個記錄學生學號,年齡,體重,名字的數組結構,你可以稍作修改就可以了,請命名文件為HW1.java
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
importjava.util.Arrays;
importjava.util.Comparator;
importjava.util.Scanner;
<Student>,Serializable{
/**
*SerializableUID:ensuresserialize/de-.
*/
=-3515442620523776933L;
publicintgetNumber(){
returnnumber;
}
publicvoidsetNumber(intnumber){
this.number=number;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicdoublegetWeight(){
returnweight;
}
publicvoidsetWeight(doubleweight){
this.weight=weight;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
privateintnumber;
privateintage;
privatedoubleweight;
privateStringname;
publicStudent(intnumber,intage,doubleweight,Stringname){
this.number=number;
this.age=age;
this.weight=weight;
this.name=name;
}
@Override
publicintcompareTo(Studento){
if(this.age==o.age){
return(int)(this.weight-o.weight);
}
returnthis.age-o.age;
}
}
<Student>{
@Override
publicintcompare(Studento1,Studento2){
returno1.getAge()-o2.getAge();
}
}
<Student>{
@Override
publicintcompare(Studento1,Studento2){
return(int)(o1.getWeight()-o2.getWeight());
}
}
publicclassHW1{
//.
publicstaticvoidmain(String[]args){
System.out.println(" WelcometotheSystem,Chooseoptionsbelow:");
printPrompt();
Student[]students=null;
Scannerscanner=newScanner(System.in);
while(scanner.hasNextInt()){
switch(scanner.nextInt()){
case1:
System.out.println("PrintStudentInformation");
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
printStudents(students);
}
printPrompt();
break;
case2:
System.out.println(":");
intnumber=scanner.nextInt();
students=initilise(number,scanner);
printPrompt();
break;
case3:
System.out.println("Addanewstudent");
printPrompt();
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
intnewLength=students.length+1;
students=Arrays.Of(students,newLength);
students[newLength-1]=createStudent(scanner);
System.out.println("Newstudenthasbeenadded.");
printPrompt();
}
break;
case4:
System.out.println("SortingbyAge,WeightinAsceorder");
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
Student[]sortedStudents=students;
Arrays.sort(sortedStudents);
printStudents(sortedStudents);
}
break;
case5:
System.out.println("CalcaulteMin,Max,AveofAgeandWeight");
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
Student[]sortedAgeStudents=students;
Arrays.sort(sortedAgeStudents,newStudentSortByAge());
Student[]sortedWeightStudents=students;
Arrays.sort(sortedWeightStudents,newStudentSortByWeight());
intaverageAge=0;
doubleaverageWeight=0.0;
for(Studentstudent:sortedAgeStudents){
averageAge+=student.getAge();
averageWeight+=student.getWeight();
}
averageAge=averageAge/sortedAgeStudents.length;
averageWeight=averageWeight/sortedWeightStudents.length;
System.out.printf("MinAge:%d,MaxAge:%d,AveAge:%d ",sortedAgeStudents[0].getAge(),sortedAgeStudents[sortedAgeStudents.length-1].getAge(),averageAge);
System.out.printf("MinWeight:%f,MaxWeight:%f,AveWeight:%f ",sortedWeightStudents[0].getWeight(),sortedWeightStudents[sortedWeightStudents.length-1].getWeight(),averageWeight);
}
break;
case6:
System.out.println("WriteStudentdataintofile");
try(ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream(newFile("studentsData"),true))){
oos.writeObject(students);
printPrompt();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
break;
case7:
System.out.println("ReadstudentDatafromfile");
try(ObjectInputStreamois=newObjectInputStream(newFileInputStream(newFile("studentsData")))){
students=(Student[])(ois.readObject());
printPrompt();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
break;
default:
scanner.close();
System.out.println("Quit");
break;
}
}
}
publicstaticvoidprintPrompt(){
System.out.println("1:Displaycurrentstudents");
System.out.println("2:InitiliseNstudents");
System.out.println("3:Addnewstudent");
System.out.println("4:SortingbyAge,WeightinAsceorder");
System.out.println("5:CalcaulteMin,Max,AveofAgeandWeight");
System.out.println("6:WriteStudentdataintofile");
System.out.println("7:ReadstudentDatafromfile");
}
publicstaticStudent[]initilise(intn,Scannerscanner){
Student[]students=newStudent[n];
for(inti=0;i<n;i++){
students[i]=createStudent(scanner);
}
System.out.println(".");
returnstudents;
}
publicstaticvoidprintStudents(Student[]students){
for(Studentstudent:students){
System.out.printf("Student:%s,Number:%d,Age:%d,Weight:%f ",student.getName(),student.getNumber(),student.getAge(),student.getWeight());
}
}
(Student[]students){
for(Studentstudent:students){
System.out.printf("Age:%d,Weight:%f,Student:%s,Number:%d ",student.getAge(),student.getWeight(),student.getName(),student.getNumber());
}
}
(Scannerscanner){
intstudentNumber=0,studentAge=0;
doublestudentWeight=0.0;
StringstudentName=null;
System.out.println("EnterStudentNumber:");
while(scanner.hasNext()){
studentNumber=scanner.nextInt();
System.out.println("EnterStudentAge:");
studentAge=scanner.nextInt();
System.out.println("EnterStudentWeight:");
//nextDouble僅僅讀取double的值,在double值後的' '將會被nextLine()所讀取,所以讀取studentName時需要再讀取一次nextLine()
studentWeight=scanner.nextDouble();
System.out.println("EnterStudentName:");
scanner.nextLine();
studentName=scanner.nextLine();
break;
}
returnnewStudent(studentNumber,studentAge,studentWeight,studentName);
}
}
運行結果,
Ⅶ 10個學生,結構體變數輸出3門課總平均成績,和最高分的學生的數據(包學號、姓名、3門課成績、平均分
#include "stdio.h"
#include
#define SIZE 5
struct student{
char id[20];
char name[20];
int score[3];
} stud[SIZE];
float ave[SIZE];
void input() /* 輸入學生的信息 */
{
int i;
for(i=0;i<SIZE;i++)
{
printf("第%d個學生的信息:\n",i+1);
scanf("%s%s%d%d%d",stud[i].id,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
}
}
void average() /* 求每個學生的總平均分 */
{
int i;
for(i=0;i<SIZE;i++)
{
ave[i]=(stud[i].score[0]+stud[i].score[1]+stud[i].score[2])/3.0;
}
}
void max() /* 找出總分最高學生的數據 */
{
int i,j;
float ftemp;
struct student temp;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE-i-1;j++)
{
if(ave[j]<ave[j+1])
{
temp=stud[j];
stud[j]=stud[j+1];
stud[j+1]=temp;
ftemp=ave[j];
ave[j]=ave[j+1];
ave[j+1]=ftemp;
}
}
}
printf("\n%s %s %d %d %d %3.1f\n",stud[0].id,stud[0].name,stud[0].score[0],stud[0].score[1],stud[0].score[2],ave[0]);
}
void output() /* 輸出學生的信息 */
{
int i;
printf("\n");
for(i=0;i<SIZE;i++)
printf("%s %s %d %d %d %3.1f\n",stud[i].id,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],ave[i]);
}
void main()
{
input();
average();
output();
max();
}
Ⅷ 用c語言定義結構體,存儲學生學號和三門課成績及平均分,初始化成績如下
k=N[(int)s[0]-1].am+N[(int)s[0]-1].bm+N[(int)s[0]-1].cm;
改為
k=N[(int)(s[0]-'1')].am+N[(int)(s[0]-'1')].bm+N[(int)(s[0]-'1')].cm;
試試。
Ⅸ 學生有三門課程:A、B、C,3名學生的成績,要求用結構體輸出三個學生平均值等,下面代碼哪裡錯了
代碼的關鍵錯誤是下面兩個for語句後面都多加了一個分號,錯誤如下注釋:
for(i=0;i<=n-1;i++);//多加了回一個分號
s[i].aver=(s[i].a+s[i].b+s[i].c)%3;//平均值計答算應該是除以3即s[i].aver=(s[i].a+s[i].b+s[i].c)/3.0;除以3.0是為了將aver的結果轉換為float
for(i=0;i<=n-1;i++);//多加了一個分號
cout<<"姓名:"<<s[i].name<<","<<"A課程成績:"<<s[i].a<<","<<"B課程成績:"<<s[i].b<<","<<"C課程成績:"<<s[i].c<<","<<"平均分:"<<s[i].aver<<"."<<endl;
Ⅹ 定義結構體,包含三個成員學號,姓名,成績,輸入兩個學生的學號、姓名和成績,輸出成績較高學生的
scanf("%d,%s,%f",&student1.num,&student1.name,&student1.score); scanf("%d,%s,%f",&student2.num,&student2.name,&student2.score); 上面這2句不對,可以編譯成功,但是保存的值是錯誤的,因為它會把「逗號」當作字元送給了%s,所以最後面的%d沒有輸入數據。可以換成空格。 scanf() 開始讀取輸入以後,會在遇到的第一個空白字元空格(blank)、製表符(tab)或者換行符(newline)處停止讀取。 #include struct Student { int num; char name[20]; float score; }; void main() { struct Student student1,student2; scanf("%d %s %f",&student1.num,&student1.name,&student1.score); scanf("%d %s %f",&student2.num,&student2.name,&student2.score); if(student1.score>student2.score) printf("%s(%d):%6.2f\n",student1.name,student1.num,student1.score); else printf("%s(%d):%6.2f\n",student2.name,student2.num,student2.score); }