링크드리스트

카테고리 없음 2017. 6. 19. 22:18

#include<stdio.h>

#include<stdlib.h>

typedef struct nood

{

int data;

struct nood *nood_link; //포인터

}*pdata,data;

int main()

{

pdata h,n1,n2;


h=n1=(pdata)malloc(sizeof(data));

n1->data=10;

n1->nood_link=NULL;


n2=(pdata)malloc(sizeof(data));

n2->data=10;

n2->nood_link=NULL;


n1->nood_link=n2;


}

동적

카테고리 없음 2017. 6. 19. 21:58

#include<stdio.h>

#include<stdlib.h>


int main()

{

int num=1;

int **arr;

int h=6,w=10;

arr=(int**)malloc(sizeof(int*)*h);

arr[0]=(int*)malloc(sizeof(int)*h*w);


for(int i=1;i<h;++i)

{

arr[i]=arr[i-1]+w;

}


for(int i=0;i<h;++i)

{

for(int j=0;j<w;j++)

{

arr[i][j]=num++;

printf("%d ",arr[i][j]);

}

printf("\n");

}


free(arr[0]);

free(arr);

}



출처 : http://codeng.tistory.com/8

괄호

카테고리 없음 2017. 6. 19. 21:26

//동절할당 링크드리스트 

//달력 바이너리 야구 알고리즘 그리기


#include<stdio.h>

#include<string.h>


int main()

{

char arr[100];

int a=0,b=0;

int size;


printf("괄호를 입력하세요 : ");

gets(arr);



size=strlen(arr);


//괄호 체크

for(int i=0;i<size;++i)

{

if(arr[i]=='(')

a++;

else if(arr[i]==')')

b++;


}


if(a!=b)

{

printf("괄호 갯수 안맞아");

return 0;;

}

//앞뒤 체크

if(arr[0]!='(' && arr[size-1]!=')')

{

printf("앞뒤 괄호 잘못댐");

return 0;

}

for(int i=0;i<size;++i)

{

if(arr[i]=='(')

a--;

else if(arr[i]==')')

b--;


if(a>b)

{

printf("괄호 아다리 안맞다");

return 0;

}

}

// (  뒤에  ) 많으면 안댐 나중에 (() 남는다는거



printf("맞당");

}

야구

카테고리 없음 2017. 6. 19. 18:11

#include<stdio.h>

#include<stdlib.h>

#include<time.h>


int main()

{

int com[3];

int game[3];

char yes_no;

int strike,ball,count;



srand(time(NULL));

printf("야구게임 시작\n");


while(1)

{

//3개 랜덤입력

com[0]=rand()%10;

com[1]=rand()%10;

com[2]=rand()%10;


//중복 확인

if(com[0]==com[1] || com[0]==com[2] || com[1]==com[2])

continue;

count=1;

while(1)

{

ball=0,strike=0;

//사용자 입력

printf("0~9까지 수 입력 : ");

scanf("%d %d %d",&game[0],&game[1],&game[2]);


if(com[0]==game[0])strike++;

else if(com[0]==game[1] || com[0]==game[2])ball++;


if(com[1]==game[1])strike++;

else if(com[1]==game[0] || com[1]==game[2])ball++;


if(com[2]==game[2])strike++;

else if(com[2]==game[1] || com[2]==game[0])ball++;


printf("%d회 %d번 스트라이크 %d번 볼\n",count++,strike,ball);


if(strike==3)

break;


}

ffulsh(stdin)

printf("게임을 계속 하시겠습니까 y/n ?");

scanf("%c",&yes_no);

if(yes_no=='n' || yes_no=='N')

break;

}


}

야구

카테고리 없음 2017. 6. 19. 17:22

#include<stdio.h>

#include<stdlib.h>


#include<time.h>


int main()

{

int com[3]={0,}; //랜덤으로 3자리 저장

int gamer[3]={0,}; //사용자 입력수;


int count; //1회,2회...

int i;

int strike,ball;

char yesno;


srand(time(NULL));

count=1;


printf("야구게임을 시작합니다");

while(1)

{

com[0]=rand()%10;

com[1]=rand()%10;

com[2]=rand()%10;




if(com[0]==com[1] || com[0]==com[2] || com[1]==com[2])

{

continue;

}

printf("0~9 사이의 숫자를 3개 입력하시오\n");





while(1)

{

strike=0;ball=0;


printf("\n3개의 숫자[0~9]를 입력하세요:");


scanf("%d %d %d",&gamer[0],&gamer[1],&gamer[2]);

 

if(com[0]==gamer[0])

{

strike++;

}

else if(com[0]==gamer[1] || com[0]==gamer[2])

{

ball++;

}

if(com[1]==gamer[1])strike++;

else if(com[1]==gamer[0] || com[1]==gamer[2])ball++;


if(com[2]==gamer[2])strike++;

else if(com[2]==gamer[0] || com[2]==gamer[1])ball++;


if(gamer[0]>9 || gamer[1]>9 || gamer[2]>9)

{

printf("입력한 숫자가 너무 큽니다. 0~9를 입력하세요");

continue;

}


printf("\n%d회 [%d개 스트라이크] [%d개 볼]\n",count,strike,ball);

if(strike==3)

break;

count++;

}


fflush(stdin);

printf("계속 하시겠습니까?y/n?");

scanf("%c",&yesno);

if(yesno=='n' || yesno=='N')

break;


}


}

fflush 함수

☆C언어 2017. 6. 19. 16:28

* int fflush(FILE * stream);

stream에 stdout이 들어가는 경우 :

출력 버퍼를 비운다. stdout의 경우 비운다는게 버리는게 아니라 '목적지(모니터에 출력)로 보내라.' 뜻이다. 그래서 이 경우는 '즉시 출력'을 의미한다. 


stream에 stdin이 들어가는 경우 :

입력 버퍼를 비운다. stdin의 경우 비운다는게 말 그대로 버린다는 뜻이다. '목적지에 도달시키지 말고 버려라.'



'☆C언어' 카테고리의 다른 글

개미 문제(틀림)  (0) 2017.11.14
달력 1~12월 다 찍어내기  (0) 2017.06.17
c언어 꿀팁  (0) 2014.10.14
조건부 컴파일 #if #elif #else #endif  (0) 2013.10.09
2차원 배열 포인터  (0) 2012.09.18

야구

카테고리 없음 2017. 6. 19. 16:22

#include<stdio.h>

#include<stdlib.h>


#include<time.h>

#include<memory.h>

int main()

{

int com[3]={0,}; //랜덤으로 3자리 저장

int gamer[3]={0,}; //사용자 입력수;

int guess[10]={0,}; //회마다 0~9까지 입력한수 체크

int count; //1회,2회...

int i;

int strike,ball;

char yesno;


srand(time(NULL));

count=1;


printf("야구게임을 시작합니다");

while(1)

{

com[0]=rand()%10;

com[1]=rand()%10;

com[2]=rand()%10;




if(com[0]==com[1] || com[0]==com[2] || com[1]==com[2])

{

continue;

}

printf("0~9 사이의 숫자를 3개 입력하시오\n");


memset(guess,0,sizeof(guess)); //비우기




while(1)

{

strike=0;ball=0;

for(i=0;i<10;++i)

{

printf("%d ",guess[i]); //입력한애들 초기값 0체크 1

}


printf("\n3개의 숫자[0~9]를 입력하세요:");


scanf("%d %d %d",&gamer[0],&gamer[1],&gamer[2]);

 

if(com[0]==gamer[0])

{

strike++;

}

else if(com[0]==gamer[1] || com[0]==gamer[2])

{

ball++;

}

if(com[1]==gamer[1])strike++;

else if(com[1]==gamer[0] || com[1]==gamer[2])ball++;


if(com[2]==gamer[2])strike++;

else if(com[2]==gamer[0] || com[2]==gamer[1])ball++;


if(gamer[0]>9 || gamer[1]>9 || gamer[2]>9)

{

printf("입력한 숫자가 너무 큽니다. 0~9를 입력하세요");

continue;

}


guess[gamer[0]]=1;

guess[gamer[1]]=1;

guess[gamer[2]]=1;

printf("\n%d회 [%d개 스트라이크] [%d개 볼]\n",count,strike,ball);

if(strike==3)

break;

count++;

}


fflush(stdin);        //출력버퍼 비운다

printf("계속 하시겠습니까?y/n?");

scanf("%c",&yesno);

if(yesno=='n' || yesno=='N')

break;


}


}

바이너리 ch

카테고리 없음 2017. 6. 19. 12:39

#include<stdio.h>

#include<stdlib.h>

#include<string.h>


int main()

{

FILE *in,*out;

char name[100]={'0',};

char new_name[100]={'0',};

char buf[100];

int size;

int num=0;

char *data;

char ch;


printf("Input file name : ");

gets(name);

printf("file size : ");

scanf("%d",&size);


data=(char*)malloc(sizeof(char)*size);



in=fopen(name,"rb");


strncpy(new_name,name,5);




while(!feof(in))

{

sprintf_s(buf,sizeof(buf),"%s%d",new_name,num++);

out=fopen(buf,"wb");

for(int i=0;i<size;i++)

{

ch=getc(in);

if(ch!=EOF)

{

fprintf(out,"%c",ch);

}

else

break;

}

}



fclose(in);

fclose(out);


}

바이너리 ch

카테고리 없음 2017. 6. 19. 12:29

#include<stdio.h>

#include<stdlib.h>

#include<string.h>


int main()

{

FILE *in,*out;

char name[100]={'0',};

char new_name[100]={'0',};

char buf[100];

int size;

int num=0;

char *data;

char ch;


printf("Input file name : ");

gets(name);

printf("file size : ");

scanf("%d",&size);


data=(char*)malloc(sizeof(char)*size);



in=fopen(name,"rb");


strncpy(new_name,name,5);


while(!feof(in))

{

sprintf_s(buf,sizeof(buf),"%s%d",new_name,num++);

out=fopen(buf,"wb");

for(int i=0;i<size;++i)

{

ch=getc(in);

if(ch!=EOF)

fprintf(out,"%c",ch);

else

break;

}

}


fclose(in);

fclose(out);


}

바이너리

카테고리 없음 2017. 6. 19. 03:04

#include<stdio.h>

#include<stdlib.h>

#include<string.h>


int main()

{

FILE *in,*out;

char new_file[100]={'0'}; //파일 이름들 초기화

char buf[100];

char name[100]={'0'};

char *data;

int size;

int num=0;

printf("input file name : ");

gets(name);

printf("size : ");

scanf("%d",&size);

data=(char*)malloc(sizeof(data)*size);


strncpy(new_file,name,5);


in=fopen(name,"rb");


while(!feof(in))

{

sprintf_s(buf,sizeof(buf),"%s%d",new_file,num++);

out=fopen(buf,"wb");

if(!feof(in))

{

fread(data,size,1,in);

fwrite(data,size,1,out);

}


}


fclose(in);

fclose(out);

}