โปรแกรมเพื่อเก็บข้อมูลของนักเรียนห้องหนึ่ง ประกอบด้วย รหัสนั

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น

Moderator:

User avatar
nokroo
Posts: 188
Joined: 01/01/1970 7:00 am
Location: วัดตะคร้ำเอน ต.ตะคร้ำเอน อ.ท่ามะกา จ.กาญจนบุรี

โปรแกรมเพื่อเก็บข้อมูลของนักเรียนห้องหนึ่ง ประกอบด้วย รหัสนั

Postby nokroo » 05/10/2013 8:07 am

#include <stdio.h>
#include <conio.h>
struct s {
char name[50];
char ID[50];
char Surname[50];
float Age;
float Height;
float Weight;
char Subjects1[50];
char Subjects2[50];
char Subjects3[50];
char Subjects4[50];
char Subjects5[50];
char Subjects6[50];
char Subjects7[50];
float GPA1[50];
float GPA2[50];
float GPA3[50];
float GPA4[50];
float GPA5[50];
float GPA6[50];
float GPA7[50];
};
int main(){
float avg;
struct s a[50],b[50];
FILE *fptr;
int i;
float sum=0;
fptr=fopen("file.txt","wb");
for(i=0;i<50;++i) {
fflush(stdin);

printf("\nEnter ID[%d]: ",i+1);
gets(a[i].ID);
printf("\nEnter name[%d]: ",i+1);
gets(a[i].name);
printf("\nEnter surname[%d]: ",i+1);
gets(a[i].Surname);
printf("\nEnter age[%d]: ",i+1);
scanf("%f",&a[i].Age);
printf("\nEnter height[%d]: ",i+1);
scanf("%f",&a[i].Height);
printf("\nEnter weight[%d]: ",i+1);
scanf("%f",&a[i].Weight);
printf("\nEnter Subject[%d]: ",1);
scanf("%s",&a[i].Subjects1);
printf("\nEnter Subjects[%d]: ",2);
scanf("%s",a[i].Subjects2);
printf("\nEnter Subjects[%d]: ",3);
scanf("%s",a[i].Subjects3);
printf("\nEnter Subjects[%d]: ",4);
scanf("%s",a[i].Subjects4);
printf("\nEnter Subjects[%d]: ",5);
scanf("%s",a[i].Subjects5);
printf("\nEnter Subjects[%d]: ",6);
scanf("%s",a[i].Subjects6);
printf("\nEnter Subjects[%d]: ",7);
scanf("%s",a[i].Subjects7);
printf("\nEnter GPA[%d]: ",1);
scanf("%f",&a[i].GPA1[i]);
printf("\nEnter GPA[%d]: ",2);
scanf("%f",&a[i].GPA2[i]);
printf("\nEnter GPA[%d]: ",3);
scanf("%f",&a[i].GPA3[i]);
printf("\nEnter GPA[%d]: ",4);
scanf("%f",&a[i].GPA4[i]);
printf("\nEnter GPA[%d]: ",5);
scanf("%f",&a[i].GPA5[i]);
printf("\nEnter GPA[%d]: ",6);
scanf("%f",&a[i].GPA6[i]);
printf("\nEnter GPA[%d]: ",7);
scanf("%f",&a[i].GPA7[i]);

sum += a[i].Age;

}


fwrite(a,sizeof(a),1,fptr);
fclose(fptr);


fptr=fopen("file.txt","rb");
fread(b,sizeof(b),1,fptr);

for(i=0;i<50;++i) {

printf("\nID[%d]: %s",i+1,b[i].ID);
printf("\nName[%d]: %s",i+1,b[i].name);
printf("\nSurname[%d]: %s",i+1,b[i].Surname);
printf("\nAge[%d]: %f",i+1,b[i].Age);
printf("\nHeight[%d]: %f",i+1,b[i].Height);
printf("\nWeight[%d]: %f",i+1,b[i].Weight);
printf("\nSubjects[%d]: %s",1,b[i].Subjects1);
printf("\nSubjects[%d]: %s",2,b[i].Subjects2);
printf("\nSubjects[%d]: %s",3,b[i].Subjects3);
printf("\nSubjects[%d]: %s",4,b[i].Subjects4);
printf("\nSubjects[%d]: %s",5,b[i].Subjects5);
printf("\nSubjects[%d]: %s",6,b[i].Subjects6);
printf("\nSubjects[%d]: %s",7,b[i].Subjects7);
printf("\nGPA[%d]: %f",1,b[i].GPA1[i]);
printf("\nGPA[%d]: %f",2,b[i].GPA2[i]);
printf("\nGPA[%d]: %f",3,b[i].GPA3[i]);
printf("\nGPA[%d]: %f",4,b[i].GPA4[i]);
printf("\nGPA[%d]: %f",5,b[i].GPA5[i]);
printf("\nGPA[%d]: %f",6,b[i].GPA6[i]);
printf("\nGPA[%d]: %f",7,b[i].GPA7[i]);
}

printf("\nSum Age = %f",sum);
fclose(fptr);
getch();
}
//www.programiz.com/c-programming/c-file-examples#sthash.1ee51FYv.dpuf

Return to “Programming - C/C<sup>++</sup>& java”

Who is online

Users browsing this forum: No registered users and 1 guest