본문 바로가기

Web Site Note

C언어프로그래밍

728x90

C언어 성적관리 - 배열과 구조

예시 1

#include <stdio.h>
struct student      //학생정보   
{
    char name[50];  //이름
    int roll;       //번호
 int exam;       //시험
 int attendance; //출석
 int etc;        //기타
    float marks;    //등급
 float mean;     //평균,,,
} s[10];            //10명으로 변수 s인 배열로 선언

int main()
{
    int i;

    printf("Enter information of students:\n");

    // storing information
 // 정보를 저장
    for(i=0; i<10; ++i)
    {
        s[i].roll = i+1;

  //학번을 입력받고
        printf("\nFor roll number%d,\n",s[i].roll);
  //이름을 입력받고
        printf("Enter name: ");
        scanf("%s",s[i].name);
  //등급을 입력받고
        printf("Enter marks: ");
        scanf("%f",&s[i].marks);
  //시험점수 입력받고
        printf("Enter Exam: ");
        scanf("%d",&s[i].exam);

  
  printf("\n");
    }
 //학생정보 출력
    printf("Displaying Information:\n\n");
    // displaying information
    for(i=0; i<10; ++i)
    {
  //학번
        printf("\nRoll number: %d\n",i+1);
  //이름
        printf("Name: ");
        puts(s[i].name);
  //등급
        printf("Marks: %.1f",s[i].marks);
        printf("\n");
    }
    return 0;
}

 

예시2.

#include <stdio.h>
#include<stdlib.h>

struct course
{
   int marks;          //점수
   char subject[30];   //과목
};

int main()
{
   struct course *ptr;
   int i, noOfRecords;
   //등록할 학생 수
   printf("Enter number of records: ");
   scanf("%d", &noOfRecords);

   // Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.
   ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));

   for(i = 0; i < noOfRecords; ++i)
   {
       printf("Enter name of the subject and marks respectively:\n");
       scanf("%s %d", &(ptr+i)->subject, &(ptr+i)->marks);
   }

   printf("Displaying Information:\n");

   for(i = 0; i < noOfRecords ; ++i)
       printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks);

   return 0;
}

 

728x90

'Web Site Note' 카테고리의 다른 글

javascript basic  (0) 2017.12.13
C언어 포인트  (0) 2017.05.08
비주얼베이직프로그램  (0) 2017.05.04
비주얼베이직 6.0 기초  (0) 2017.05.04
연속하는 수의 합 1+1/2+1/3+...1/n  (0) 2017.04.25