|
김미진 님이 쓰신 글 :
: #include <iostream.h>
: #include <stdlib.h>
:
: struct gujo //학생들의 자료를 입력할 구조체 선언
: {
: char name[8]; //이름[영문8자, 한글4자]
: int hak; //학번
: int sbj[3]; //과목점수[3과목 저장할 공간]
: int total; //합계
: float avg; //평균
: };
:
: void main(int argc, char *argv[])
: {
: int num, i, j; //int형 변수 선언 : 학생수 num, 반복문을 돌리기 위한 변수 i, j
: gujo *std, temp; //구조체 gujo형 변수 선언 : 포인터형 std변수, 평범한 temp변수
:
: num=atoi(argv[4]); //프로그램 시작할때 4번째로 입력한 학생수를 int형으로 바꿔서 num변수에 저장
:
: std = new gujo[num]; //gujo형 std포인터변수의 메모리 공간을 학생수(num)의 갯수만큼 공간확보
:
:
: //학생들의 자료입력
: for(i=0;i<num;++i) //0 ~ 학생수-1 까지 반복 ps.학생수가 3명이라도 번호는 0번부터 매기므로
: {
: cout << i+1 << "번 학생의 자료입력" << endl;
:
: cout << "이름 입력:";
: cin >> std[i].name; //gujo형 std포인터변수의 [i]번 배열의 name맴버변수에 이름값 입력
: cout << "학번 입력:";
: cin >> std[i].hak; //위의 설명과 같고 hak맴버변수에 입력
: cout << argv[1] << "과목의 점수 입력:";
: cin >> std[i].sbj[0]; //위와 동문
: cout << argv[2] << "과목의 점수 입력:";
: cin >> std[i].sbj[1]; // ''
: cout << argv[3] << "과목의 점수 입력:";
: cin >> std[i].sbj[2]; // ''
:
: std[i].total = std[i].sbj[0]+std[i].sbj[1]+std[i].sbj[2]; //total맴버 변수에 각 과목의 값을 모두 더해서 넣는다.
: std[i].avg = float(std[i].total)/3; //avg맴버 변수 = 실수형(총점totla)/3
:
: cout << "\n"; //보기 좋도록 한칸 띄움
: }
:
:
: //총점이 높은 순으로 다시 배열하는 반복문
: for(i=0;i<num-1;++i)
: {
: for(j=i+1;j<num;++j)
: {
: if(std[i].total < std[j].total) //두개의 총점을 비교해서...
: {
: temp = std[i]; //
: std[i] = std[j]; //값을 서로 바꾼다.
: std[j] = temp; //
: }
: }
: }
:
:
: //지금까지의 자료를 보기 좋게 나열하고 출력
: cout << " 학번 이름";
: cout.width(6);
: cout << argv[1];
: cout.width(6);
: cout << argv[2];
: cout.width(6);
: cout << argv[3];
: cout << " 총점 평균" << endl;
:
: for(i=0;i<num;++i)
: {
: cout.width(6);
: cout << std[i].hak;
: cout.width(8);
: cout << std[i].name;
: cout.width(6);
: cout << std[i].sbj[0];
: cout.width(6);
: cout << std[i].sbj[1];
: cout.width(6);
: cout << std[i].sbj[2];
: cout.width(6);
: cout << std[i].total;
: cout.width(6);
: cout.precision(3);
: cout << std[i].avg << endl;
: }
: }
:
:
|