홈 > IT > IT정보
IT정보

함수포인터를 이용한 간단한 사칙연산 계산기

3 realdragonhead 3 223 0 0
#include <stdio.h>

typedef int (*whatido)(int, int);    // 함수 포인터 선언

int plusfunc(int target1, int target2) {
    return target1 + target2;
}

int minusfunc(int target1, int target2) {
    return target1 + target2;
}

int multifunc(int target1, int target2) {
    return target1 + target2;
}

int divifunc(int target1, int target2) {
    return target1 + target2;
}

int plztodothis(int target1, int target2, whatido todo) {
    todo(target1, target2);
}

int main() {
    whatido func1 = NULL;
    int first = 0;
    int second = 0;
    char ex=0;
    puts("plz input operation(ex]3*2)");
    scanf("%d %s %d",first, ex, second);

    switch(ex) {
        case '+':
            func1 = plusfunc;
            break;
        case '-':
            func1 = minusfunc;
            break;
        case '*':
            func1 = multifunc;
            break;
        case '/':
            func1 = divifunc;
            break;
        default :
            return 0;
    }
   
    result = plztodothis(first, second, func1);
    printf("result : %d\n", result);
    return 0;
}

3 Comments
3 Selovic 11.03 06:34  
#include <stdio.h>
#pragma warning(disable: 4996)

typedef int(*whatido)(int, int);    // 함수 포인터 선언

int plusfunc(int target1, int target2) {
	return target1 + target2;
}

int minusfunc(int target1, int target2) {
	return target1 + target2;
}

int multifunc(int target1, int target2) {
	return target1 + target2;
}

int divifunc(int target1, int target2) {
	return target1 + target2;
}

int plztodothis(int target1, int target2, whatido todo) {
	todo(target1, target2);
}

int main() {
	whatido func1 = NULL;
	int first = 0;
	int second = 0;
	int result;
	char ex = 0;
	puts("plz input operation(ex]3*2)");
	scanf("%d %s %d", &first, &ex, &second);

	switch (ex) {
	case '+':
		func1 = plusfunc;
		break;
	case '-':
		func1 = minusfunc;
		break;
	case '*':
		func1 = multifunc;
		break;
	case '/':
		func1 = divifunc;
		break;
	default:
		return 0;
	}

	result = plztodothis(first, second, func1);
	printf("result : %dn", result);
	return 0;
}

이런식으로 수정해야 돌아가네요!
3 Selovic 11.03 06:38  
#include <stdio.h>
#pragma warning(disable: 4996)

typedef int(*whatido)(int, int);    // 함수 포인터 선언

int plusfunc(int target1, int target2) {
	return target1 + target2;
}

int minusfunc(int target1, int target2) {
	return target1 + target2;
}

int multifunc(int target1, int target2) {
	return target1 + target2;
}

int divifunc(int target1, int target2) {
	return target1 + target2;
}

int plztodothis(int target1, int target2, whatido todo) {
	todo(target1, target2);
}

int main() {
	whatido func1 = NULL;
	int first = 0;
	int second = 0;
	int result;
	int ex;
	puts("plz input operation(ex]3*2)");
	scanf("%d %s %d", &first, &ex, &second);

	switch (ex) {
	case '+':
		func1 = plusfunc;
		break;
	case '-':
		func1 = minusfunc;
		break;
	case '*':
		func1 = multifunc;
		break;
	case '/':
		func1 = divifunc;
		break;
	default:
		return 0;
	}

	result = plztodothis(first, second, func1);
	printf("result : %dn", result);
	return 0;
}


ex 변수 선언할때 char 로 선언하셨는데 * 부호의경우 char에 안담기는경우가 있어서
4byte 공간이 필요합니다...
3 realdragonhead 11.07 10:05  
아 먼가 급하게 짜느라 오류가 많은거 같네요 한번 오류난거 수정하고 올려볼겠습니다! ㅠㅠㅠ
좀더 기능 추가해서 올려보겠습니다
Category
Facebook Twitter GooglePlus KakaoStory KakaoTalk NaverBand
PlusFriend Chat