프로그래머의 삶 Programmer's Life/C & C++

단순....입력받아 출력...구조체 연습중..

Oliver's World 2008. 11. 5. 11:34
728x90






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

typedef struct{
 char *name;
 char *sn;
 int escore;
 int mscore;
}student;

student *record;
int n=0;


void inputdata(student *ptr)
{
 if(n >= 3)
 {
  printf("Memory Full~\n");
 }
 else
 {
 (ptr+n)->name=(char *)malloc(sizeof(char));
 (ptr+n)->sn=(char *)malloc(sizeof(char));
 printf("Input data\n");
 printf("Name : ");
 scanf("%s",(ptr+n)->name);
 printf("School Number : ");
 scanf("%s",(ptr+n)->sn);
 printf("English Score : ");
 scanf("%d",&(ptr+n)->escore);
 printf("Math Score :");
 scanf("%d",&(ptr+n)->mscore);
 n++;
 }
}

void Outdata(student *ptr)
{
 int a;
 char *c;
 c=(char *)malloc(sizeof(char));
 if(n == 0)
 {
  printf("Empty\n");
 }
 else
 {
 printf("Who's data you want?Input School Number\n");
 scanf("%s",c);
 for(a=0;a<n;a++)
 {
  if(strcmp(((ptr+a)->sn),c)==0)
  {
   printf("%s\n",(ptr+a)->name);
   printf("%s\n",(ptr+a)->sn);
   printf("%d\n",(ptr+a)->escore);
   printf("%d\n",(ptr+a)->mscore);
  }
 }
 }
}

void main()
{
 int a;
 record=(student *)malloc(sizeof(student)*3);
 printf("input number\n");
 while(1)
 {
  printf("1.Input \t 2.Out \t 3.Exit\n");
  scanf("%d",&a);
  switch(a)
  {
   case 1:
    {
     inputdata(record);
     break;
    }
   case 2:
   {
     Outdata(record);   
     break;
   }
   case 3:
   {
     free(record);
     exit(0);
   }
  }
 }
}







728x90