'동적메모리'에 해당되는 글 1건

  1. 2009.03.31 Time, 동적 메모리
C/C++2009. 3. 31. 20:23
/*
#include <stdio.h>
#include <time.h>
int main(){
 time_t t;
 time(&t);
 printf("숫 자 : %d \n",t);
 printf("문 자 : %s \n",ctime(&t));
 
 return 0;
}
*/
/*
#include <stdio.h>
#include <time.h>
int main(){
 char date[10];
 char time[10];
 _strdate(date);
 _strtime(time);
 printf("날 짜 : %s \n",date);
 printf("시 간 : %s \n\n",time);
 printf("시: %c%c \n",time[0],time[1]);
 printf("분: %c%c \n",time[3],time[4]);
 printf("초: %c%c \n\n",time[6],time[7]);
 printf("월: %c%c \n",date[0],date[1]);
 printf("일: %c%c \n",date[3],date[4]);
 printf("년: %c%c \n",date[6],date[7]);
 
 return 0;
}
*/
/*
#include <turboc.h>  //디지털 시계구현
int main(){
 time_t t;
 struct tm *pt;
 while(1){
  time(&t);
  pt=localtime(&t);    //현재시간
  printf("%d년 %d월 %d일 %d시 %d분 %d초 \r",
   1900+pt->tm_year,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);
  Sleep(1000);
  }
 return 0;
}
*/
/*
#include <turboc.h>  //세부적인 시간 구하기
#include <sys/timeb.h>
int main(){
 struct tm t;
 struct _timeb tb;
 while(1){  
  if(kbhit()){
  _ftime(&tb);
  t=*localtime(&tb.time);    //현재시간  
  printf("%d년 %d월 %d일 %d시 %d분 %d.%d \r",
   1900+t.tm_year,t.tm_mon+1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec,tb.millitm);  
  Sleep(10);    
  getch();
   printf("\n");
 }
 } 
 
 return 0;
}
*/
/*
#include <turboc.h>   //D-day 프로그램
#define DAYSEC (24*60*60)
 void input(struct tm * in_time){
 printf("년도 : ");
 scanf("%d",&(in_time->tm_year));
 in_time->tm_year-=1900;
 printf("월 : ");
 scanf("%d",&(in_time->tm_mon));
 in_time->tm_mon-=1;
 printf("일 : ");
 scanf("%d",&(in_time->tm_mday));
}
int main(){
 time_t n1,n2;
 struct tm t1,t2 = {0,0,0,1,0,100}; //2000년 1월 1일     {1,0,100} 일 월 1900년
 int tmp;
 input(&t2);

 n1 = time(NULL);
 t1 = *localtime(&n1);
 n1 = mktime(&t1);
 n2 = mktime(&t2);
 n1 = n1 - n2;
 tmp = (int)(n1/DAYSEC);
 printf("%d년 %d월 %d일부터 지금까지\n",
  t2.tm_year+1900,t2.tm_mon+1,t2.tm_mday);
 if(mktime(&t1) > mktime(&t2)){
  printf("%d일 지났습니다.\n",tmp);
 }else{
  printf("%d일 남았습니다.\n",abs(tmp));
 }
 return 0;
}
*/


/*
#include <stdio.h>   //동적 할당예제
#include <stdlib.h>  //malloc함수 및 free 함수
int main(){
 int* int_p;        //↓sizeof(int) 해야
 int_p = (int *)malloc(4);  //heap영역에 4byte를 할당
 if(int_p==NULL){ //메모리 할당 실패 시 종료
  printf("메모리 할당 실패 \n");
  return 1;
 }
 *int_p = 100;
 printf("*int_p의 값은 : %d \n", *int_p);
 free(int_p);   //메모리 해제
 printf("*int_p의 값은(메모리 해제후) : %d \n", *int_p);
 
 return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>   //malloc함수 및 free 함수
int main(){
 int* int_ar;
 int i;
 int sel;
 printf("메모리를 할당할 공간 : ");
 scanf("%d",&sel);
 int_ar = (int*)malloc(sel*sizeof(int));
 for(i=0;i<sel;i++){
  int_ar[i] = (i+1)*2;
  printf("ar[%d] = %d \n", i, int_ar[i]);
 }
 free(int_ar);  //메모리 해제
 return 0;
}
*/
#include <stdio.h>
struct node {
 int Data;
 char Name[10];
 int Kor;
 int Eng;
 int Math;
};
int main(){
 struct node * tmp;
 int i;
 tmp = (struct node *)malloc( 5 * sizeof(struct node));
 for(i=0;i<5;i++){
  tmp[i].Data = 1001 + i;
  printf("ID : %d \n",tmp[i].Data);
  printf("이름 : ");
  fflush(stdin);
  scanf("%s",&tmp[i].Name);
  printf("국어 : ");  
  scanf("%s",&tmp[i].Kor);
 }
 

 for(i=0;i<5;i++){
  printf("%5d | %10s | 5d \n",tmp[i].Data,tmp[i].Name,tmp[i].Kor);
 }
 return 0;
}
Posted by 샤키