'프로그래밍/C, C++'에 해당되는 글 12건
- 2007/06/14 C언어 - 070614 / 동적 배열
- 2007/06/11 C언어 - 070607 / 파일 입출력 응용문제
- 2007/06/11 C언어 - 070531 / 구조체 실습 예제
- 2007/04/26 C언어 - (Report) 070426 / 난수응용 야구게임
- 2007/04/26 C언어 - 070426 / 난수발생 함수
- 2007/04/12 C언어 - 070412 / 문자열 함수
- 2007/04/08 C언어 연산자
- 2007/04/08 C언어 기본 데이터형
- 2007/04/05 C언어 - 070405 / 포인터 응용
- 2007/03/22 C언어 - 070322 / 함수응용
- 2007/03/15 C언어 - 070314 / 제어문, 반복문
- 2007/03/15 EditPlus, Borland C++ 5.5 설정
#include <stdio.h>
#include <stdlib.h>
//동적 배열
typedef unsigned int UNINT;
void sum(UNINT n);
void main()
{
UNINT max;
puts("배열 사이즈 입력 :");
scanf("%d",&max);
sum(max);
}
void sum(UNINT n)
{
UNINT i;
UNINT *ptd;
ptd = (UNINT *) malloc(n * sizeof (UNINT));
if (ptd == NULL)
{
puts("메모리 확보 실패");
exit(0);
}
puts("\n출력 :");
for (i=0; i < n; i++)
{
*ptd=i+1;
printf("값=%d\n", *ptd++);
}
free(ptd);
}
#include <stdio.h>
#include <process.h>
FILE *ifp, *ofp;
void main()
{
int i;
float n[4];
float sum,ave,max,min;
ifp = fopen("ex_in.txt","r");
ofp = fopen("ex_out.txt","w");
if (ifp==NULL)
{
printf("파일 열기 실패 입니다.\n");
exit(0);
}
fprintf(ofp,"합계\t평균\t최고값\t최소값\n");
while (!feof(ifp))
{
fscanf(ifp,"%f %f %f %f",&n[0],&n[1],&n[2],&n[3]);
sum = n[0] + n[1] + n[2] + n[3];
ave = sum / 4.0;
max = n[0];
min = n[0];
for (i=0;i<4;i++)
{
if (max < n[i])
{
max = n[i];
}
if (min > n[i])
{
min = n[i];
}
}
fprintf(ofp,"%.1f\t%.1f\t%.1f\t%.1f\n",sum,ave,max,min);
}
fclose(ifp), fclose(ofp);
}
/*
구조체 실습 예제 2번
*/
#include <stdio.h>
struct student{
char name[10];
int c,cpp,vc,rank;
int tot;
};
void sum(struct student *pst, int n);
void sort(struct student *pst, int n);
void show(struct student *pst, int n);
void main()
{
struct student st[4]={
{"홍길동",90,90,90,1,0},
{"지흔이",80,89,70,1,0},
{"김진수",70,73,80,1,0},
{"박순이",60,87,80,1,0},
};
sum(st,4);
sort(st,4);
show(st,4);
}
void sum(struct student *pst, int n)
{
for(int i=0;i<n;i++)
{
pst[i].tot = pst[i].c + pst[i].cpp + pst[i].vc;
}
}
void sort(struct student *pst, int n)
{
struct student temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(pst[i].tot < pst[j].tot)
{
pst[i].rank++;
}
if(pst[i].rank < pst[j].rank)
{
temp=pst[i];
pst[i]=pst[j];
pst[j]=temp;
}
}
}
}
void show(struct student *pst, int n)
{
printf("이 름 | C | C++| VC | 총점| 석차\n");
printf("---------------------------------\n");
for(int i=0;i<n;i++)
{
printf("%s | %d | %d | %d | %d | %d\n",pst[i].name,pst[i].c,pst[i].cpp,pst[i].vc,pst[i].tot,pst[i].rank);
}
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 3 //배열 크기
void numCheck(int *number, int *in);
void numRandom();
int in[SIZE]; //입력받을 값 전역변수로 선언
int number[SIZE]; //난수받을 값 전역변수로 선언
int strike; //스트라이크 갯수 저장할 카운트 전역변수로 선언
int ball; //볼 갯수 저장할 카운트 전역변수로 선언
void main()
{
int i;
numRandom(); //numRandom 호출
printf("숫자 야구 게임! 3자리 숫자를 맞춰보세요.(0~9)\n");
printf("ex) 1 2 3\n");
while(1)
{
if (strike == 3) //정답이면 출력
{
printf("-----------\n");
printf("All Strike!\n");
printf("-----------\n");
for (i=0; i<SIZE; i++)
{
printf("%d ",number[i]);
}
printf("\n-----------\n");
break;
}else{ //숫자 입력받는 부분
scanf("%d %d %d",&in[0],&in[1],&in[2]);
numCheck(number,in);
printf(" : %d Strike, %d Ball 입니다.\n",strike,ball);
}
}
}
//중복되지 않는 난수 발생시켜서 저장하는 함수
void numRandom()
{
int i,j,random;
srand((unsigned) time(NULL));
for (i=0; i<SIZE; )
{
random = rand()%10;
//난수를 발생시켜 random변수에 저장
for (j=0; j<i; j++)
{
if (number[j] == random) break;
//number배열 첫번째부터 받은 난수와 비교해서 같으면 빠져나오기
}
if (i==j) //i와 j가 같으면
{
number[i++] = random;
//number배열에 난수값 저장하고 i를 증가
}
}
}
//입력받은 숫자와 난수의 일치여부 체크하는 함수
void numCheck(int *number, int *in)
{
int i,j;
strike=0; // trike 0으로 초기화
ball=0; //ball 0으로 초기화
for (i=0; i<SIZE; i++)
{
for (j=0; j<SIZE; j++)
{
if (number[i] == in[j] && i==j)
//숫자와 자릿수가 일치하면 strike 값 증가
{
strike++;
}else if (number[i] == in[j] && i!=j )
//숫자가 일치하고 자릿수가 틀리면 ball 값 증가
{
ball++;
}
}
}
}
//난수 발생 함수 만들기
#include <stdio.h>
#include <time.h>
void srand1(unsigned int seed);
int rand1();
static unsigned long int next;
void main()
{
int count;
unsigned seed;
srand1((unsigned) time(NULL));
for (count=0; count<5; count++)
{
printf("%d\n",rand1());
}
}
int rand1()
{
next = next * 1103515245 + 12345;
return (unsigned int) (next/65536) % 10;
}
void srand1(unsigned int seed)
{
next = seed;
}
//stdlib.h 헤더 파일, 내장 함수로 난수 만들기
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int i;
srand((unsigned) time(NULL));
for (i=0; i<5; i++)
{
printf("%d\n",rand()%10);
}
}
// 문자열 길이
#include <stdio.h>
int u_strlen(char *c);
void main()
{
char *c;
int count;
printf("문자를 입력하세요.");
gets(c);
count=u_strlen(c);
printf("문자의 길이 : %d",count);
}
int u_strlen(char *c)
{
int k=0;
while (*c++ != '\0')
{
k++;
}
return k;
}
// 문자열 비교
#include <stdio.h>
int is_ch(char *c, char *s);
int u_strlen(char *c);
void main()
{
char *c, *s;
int is;
puts("문자열을 입력하세요.");
gets(c);
puts("찾을 문자를 입력하세요.");
gets(s);
is = is_ch(c,s);
if (is==1)
{
puts("문자를 찾았습니다.");
}else{
puts("해당 문자가 없습니다.");
}
}
int is_ch(char *c, char *s)
{
int i,max;
int result=0;
max = u_strlen(c);
for (i=0; i<=max; i++)
{
if (*c == *s)
result = 1;
else
*c++;
}
return result;
}
int u_strlen(char *c)
{
int k=0;
while (*c++ != '\0')
{
k++;
}
return k;
}
| 구 분 (우선순위) | 연 산 자 | 연산방향 | |
| 괄호, 구조체, 공용체 | () [] . -> | 좌 -> 우 | |
| 단항연산자 | ! ~ ++ -- -부호 +부호 * & sizeof | 우 -> 좌 | |
| 이 항 연 산 자 |
승제 | * / % | 좌 -> 우 |
| 가감 | + - | ||
| SHIFT | << >> | ||
| 비교 | < <= > >= | ||
| 등가 | == != | ||
| bit AND | & | ||
| bit XOR | ^ | ||
| bit OR | | | ||
| 논리 AND | && | ||
| 논리 OR | || | ||
| 조건연산자 | ? : | 우 -> 우 | |
| 대입연산자 | = += -= *= /= %= >>= <<= &= ^= |= | 우 -> 우 | |
| 나열연산자 | , | 좌 -> 우 | |
산술연산자
| 연산자 | 의미 |
| * | 곱셈 |
| / | 나눗셈 |
| % | 나머지 연산자 |
| + | 덧셈 |
| - | 뺄셈 |
증감연산자
| 연산자 | 의미 |
| ++ | ++a 또는 a++ (a=a+1) |
| -- | --b 또는 b-- (b=b-1) |
관계연산자
| 연산자 | 의미 |
| < | ~보다 작다 |
| <= | ~보다 작거나 같다 |
| > | ~보다 크다 |
| >= | ~보다 크거나 같다 |
| == | ~와 같다 |
| != | ~와 같지 않다. |
논리연산자
| 연산자 | 의미 |
| ! | 두 값이 참이면 거짓,거짓이면 참으로 바꿈 |
| && | 두 값이 모두 참일때만 결과가 참 |
| || | 두 값이 모두 거짓일 때만 결과가 거짓 |
|
A |
B |
A && B |
A || B |
!A |
!B |
|
0 |
0 |
0 |
0 |
1 |
1 |
|
0 |
1 |
0 |
1 |
1 |
0 |
|
1 |
0 |
0 |
1 |
0 |
1 |
|
1 |
1 |
1 |
1 |
0 |
0 |
비트 논리연산자
| 연산자 | 의미 |
| ~ | 해당 비트를 반전시킨다.(1 -> 0, 0->1) |
| & | 두 비트 모두 참이면 결과가 참 |
| ^ | 두 비트가 서로 값이 다르면 참 |
| | | 두 비트가 모두 거짓이어야 거짓 |
쉬프트 연산자
| 연산자 | 의미 |
| << | 비트만큼 좌측으로 이동 (x << n는 x * 2n의 결과와 같다.) |
| >> | 비트만큼 우측으로 이동 (x >> n는 x / 2n의 결과와 같다.) |
(1) 정수형(Integer type)
unsigned(부호없는 정수) : 0 이상의 양수만 표현하도록 해준다.
|
정수형 |
크기(byte) |
부호 |
범위 |
| int |
2 |
O |
-32768 ~ 32767 |
| unsigned |
2 |
X |
0 ~ 65535 |
| long |
4 |
O |
-2147483648 ~ 2147483647 |
| unsigned long |
4 |
X |
0 ~ 4294967295 |
(2) 문자형 (Character type)
|
문자형 |
바이트 |
부호 |
사용빈도 |
| char |
1 |
O |
-128 ~ 127 |
| unsigned char |
1 |
X |
0 ~ 255 |
(3) 부동형 (Floating point type)
|
구분 |
최대지수 |
최소지수 |
최대정밀도 |
유효정밀도 |
출력정밀도 |
| float |
+38 |
-38 |
8자리 |
7자리 |
7자리 |
| double |
+308 |
-308 |
18자리 |
16자리 |
15~16자리 |
C언어Ⅱ - Report 070405 / 포인터 응용문제
#include <stdio.h>
//포인터 응용문제1
void gugudan(int *pdan);
void main()
{
int dan;
while(1)
{
printf("단수를 입력하시오.");
scanf("%d",&dan);
{
int dan;
while(1)
{
printf("단수를 입력하시오.");
scanf("%d",&dan);
if (dan==0)
{
break;
}
{
break;
}
gugudan(&dan);
}
}
}
}
void gugudan(int *pdan)
{
int i,j;
{
int i,j;
i = *pdan;
for(j=1;j<=9;j++)
{
printf("%d * %d = %d \n",i,j,i*j);
}
}
{
printf("%d * %d = %d \n",i,j,i*j);
}
}
#include <stdio.h>
//포인터 응용문제2
long gisu(long *pa, long *pb);
void main()
{
long a,b;
while(1)
{
printf("두 수를 입력하시오.(계수 지수)");
scanf("%ld %ld",&a,&b);
{
long a,b;
while(1)
{
printf("두 수를 입력하시오.(계수 지수)");
scanf("%ld %ld",&a,&b);
if (a==0 && b==0)
{
break;
}
printf("%ld의 %ld승은 =%ld\n",a,b,gisu(&a,&b));
}
}
{
break;
}
printf("%ld의 %ld승은 =%ld\n",a,b,gisu(&a,&b));
}
}
long gisu(long *pa, long *pb)
{
long val = 1;
long i;
{
long val = 1;
long i;
for (i=1;i<=*pb;i++)
{
val = val * *pa;
}
{
val = val * *pa;
}
return val;
}
}
#include <stdio.h>
#define SIZE 5
#define SIZE 5
// 포인터 응용문제3
void cba(int *pa, int n);
// cba함수원형 생성
void main()
{
int a[SIZE]={3,1,5,2,7};
void main()
{
int a[SIZE]={3,1,5,2,7};
cba(a,SIZE);
}
}
// cba함수 코딩
void cba(int *pa, int n)
{
int i;
void cba(int *pa, int n)
{
int i;
for (i=0;i<n;i++)
{
*(pa+i) = *(pa+i) * 2;
}
{
*(pa+i) = *(pa+i) * 2;
}
for (i=(n-1);i>=0;i--)
{
printf("%d",*(pa+i));
}
}
{
printf("%d",*(pa+i));
}
}
#include <stdio.h>
//함수 응용문제3
void input(int s_su[], int n);
int cha(int s_su[], int n);
int cha(int s_su[], int n);
void main()
{
int su[6];
input(su,6);
printf("최대값 최소값의 차이:%d",cha(su,6));
}
{
int su[6];
input(su,6);
printf("최대값 최소값의 차이:%d",cha(su,6));
}
void input(int s_su[], int n)
{
int i;
{
int i;
printf("정수를 입력하시오.");
for(i=0;i<n;i++)
scanf("%d",&s_su[i]);
}
for(i=0;i<n;i++)
scanf("%d",&s_su[i]);
}
int cha(int s_su[],int n)
{
int i,max,min;
{
int i,max,min;
max = min = s_su[0];
for(i=0;i<n;i++)
{
if(max < s_su[i])
max=s_su[i];
if(min > s_su[i])
min=s_su[i];
}
{
if(max < s_su[i])
max=s_su[i];
if(min > s_su[i])
min=s_su[i];
}
return max-min;
}
}
#include <stdio.h>
//함수 응용문제4
void samgak(int a[][3]);
void main()
{
int a[3][3],i,j;
samgak(a);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i>=j)
printf("1\t");
else
printf("0\t");
}
printf("\n");
}
}
{
int a[3][3],i,j;
samgak(a);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i>=j)
printf("1\t");
else
printf("0\t");
}
printf("\n");
}
}
void samgak(int a[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i>=j)
a[i][j] = 1;
else
a[i][j] = 0;
}
}
}
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i>=j)
a[i][j] = 1;
else
a[i][j] = 0;
}
}
}
실행결과 :
#include <stdio.h>
//문제1 생년,띠,나이
void main()
{
void main()
{
int birth, year;
int age;
int age;
printf( "당신이 태어난 연도는? " );
scanf( "%d", &birth);
scanf( "%d", &birth);
printf( "현재 연도는? " );
scanf( "%d", &year);
scanf( "%d", &year);
age = year - birth + 1;
if(year%12==0)
printf("당신은 %d년생, 원숭이띠이고, %d살이다.\n", birth, age);
else if(year%12==1)
printf("당신은 %d년생, 닭띠이고, %d살이다.\n", birth, age);
else if(year%12==2)
printf("당신은 %d년생, 개띠이고, %d살이다.\n", birth, age);
else if(year%12==3)
printf("당신은 %d년생, 돼지띠이고, %d살이다.\n", birth, age);
else if(year%12==4)
printf("당신은 %d년생, 쥐띠이고, %d살이다.\n", birth, age);
else if(year%12==5)
printf("당신은 %d년생, 소띠이고, %d살이다.\n", birth, age);
else if(year%12==6)
printf("당신은 %d년생, 범띠이고, %d살이다.\n", birth, age);
else if(year%12==7)
printf("당신은 %d년생, 토끼띠이고, %d살이다.\n", birth, age);
else if(year%12==8)
printf("당신은 %d년생, 용띠이고, %d살이다.\n", birth, age);
else if(year%12==9)
printf("당신은 %d년생, 뱀띠이고, %d살이다.\n", birth, age);
else if(year%12==10)
printf("당신은 %d년생, 말띠이고, %d살이다.\n", birth, age);
else if(year%12==11)
printf("당신은 %d년생, 양띠이고, %d살이다.\n", birth, age);
else
printf(" ");
}
printf("당신은 %d년생, 원숭이띠이고, %d살이다.\n", birth, age);
else if(year%12==1)
printf("당신은 %d년생, 닭띠이고, %d살이다.\n", birth, age);
else if(year%12==2)
printf("당신은 %d년생, 개띠이고, %d살이다.\n", birth, age);
else if(year%12==3)
printf("당신은 %d년생, 돼지띠이고, %d살이다.\n", birth, age);
else if(year%12==4)
printf("당신은 %d년생, 쥐띠이고, %d살이다.\n", birth, age);
else if(year%12==5)
printf("당신은 %d년생, 소띠이고, %d살이다.\n", birth, age);
else if(year%12==6)
printf("당신은 %d년생, 범띠이고, %d살이다.\n", birth, age);
else if(year%12==7)
printf("당신은 %d년생, 토끼띠이고, %d살이다.\n", birth, age);
else if(year%12==8)
printf("당신은 %d년생, 용띠이고, %d살이다.\n", birth, age);
else if(year%12==9)
printf("당신은 %d년생, 뱀띠이고, %d살이다.\n", birth, age);
else if(year%12==10)
printf("당신은 %d년생, 말띠이고, %d살이다.\n", birth, age);
else if(year%12==11)
printf("당신은 %d년생, 양띠이고, %d살이다.\n", birth, age);
else
printf(" ");
}
#include <stdio.h>
//문제2 구구단
void main()
{
void main()
{
int a;
int i,j;
printf("홀수단은 0, 짝수단은 1을 입력하세요. ");
scanf("%d",&a);
int i,j;
printf("홀수단은 0, 짝수단은 1을 입력하세요. ");
scanf("%d",&a);
if(a<0)
{
printf("홀수단은 0, 짝수단은 1을 입력하세요. ");
scanf("%d",&a);
}
{
printf("홀수단은 0, 짝수단은 1을 입력하세요. ");
scanf("%d",&a);
}
if(a==0)
for(i=2;i<=9;i++) {
if(i%2!=0)
for(j=1;j<=9;j++) {
printf("%d*%d=%d\n",i,j,i*j);
}
if(i%2!=0)
for(j=1;j<=9;j++) {
printf("%d*%d=%d\n",i,j,i*j);
}
printf("\n");
}
else if(a==1)
for(i=2;i<=9;i++) {
if(i%2==0)
for(j=1;j<=9;j++) {
printf("%d*%d=%d\n",i,j,i*j);
}
printf("%d*%d=%d\n",i,j,i*j);
}
printf("\n");
}
else
printf("");
}
printf("");
}
#include <stdio.h>
//문제3 최대값 최소값
void main()
{
void main()
{
int a, b;
while(1)
{
printf("숫자 두개를 입력하세요. ");
scanf("%d %d", &a, &b);
{
printf("숫자 두개를 입력하세요. ");
scanf("%d %d", &a, &b);
if(a==0 && b==0) {
break;
}
else {
if(a > b) {
printf("최대값:%d\n", a);
printf("최소값:%d\n", b);
}
else {
printf("최대값:%d\n", b);
printf("최소값:%d\n", a);
}
}
}
break;
}
else {
if(a > b) {
printf("최대값:%d\n", a);
printf("최소값:%d\n", b);
}
else {
printf("최대값:%d\n", b);
printf("최소값:%d\n", a);
}
}
}
}
EditPlus Borland C++ 5.5 설정
프로그램 다운로드 :
Borland Compiler
명령 : C:\Borland\bcc55\Bin\bcc32.exe
인수 : -v -IC:\Borland\bcc55\include -v -LC:\Borland\bcc55\lib -n$(FileDir) $(FilePath)
디렉토리 : C:\Borland\bcc55\bin
출력 내용 캡쳐 : 체크
Borland Run
명령 : C:\WINDOWS\system32\cmd.exe
인수 : /c $(FileNameNoExt)
디렉토리 : $(FileDir)



ex01.cpp
ex_in.txt
Report_C_070322.zip


Recent Comment