C计算器在函数函数中有很多函数

时间:2014-08-20 08:46:22

标签: c function pointers function-pointers calculator

我先编程了一个简单的计算器。现在我想将各个程序组件外包给函数。问题是开关部分。程序总是给我默认消息:坏操作符。请看看并告诉我一些tipps.指针和双指针有问题吗?

这是我的代码:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

void newcalc(char*,double*,char*,double*);
double switchfunk(double**, char**, double**);
double readcalc(double**,char**,double**);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);

int main()
{
    double a=0, b=0;
    char op='R', restart = 'Y';
    newcalc(&restart,&a,&op,&b);
    fflush(stdin);
    getchar();
    return 0;
}

double switchfunk(double** x, char** opp, double** y)
{
    printf("\n ---CALCULATOR--- \n\n\n");
    switch (**opp)
    {
        case '+':printf("%lf + %lf = %lf\n", **x, **y, addition(**x, **y)); break;
        case '-':printf("%lf - %lf = %lf\n", **x, **y, subtraction(**x, **y)); break;
        case '*':printf("%lf * %lf = %lf\n", **x, **y, multiplication(**x, **y)); break;
        case '/':printf("%lf / %lf = %lf\n", **x, **y, division(**x, **y)); break;
        default:printf("bad operator!");
    }
    return 0;
}

void newcalc(char* restart,double* x, char* opp, double* y)
{
    while (restart != (char*)'N')
    {
        readcalc(&x, &opp, &y);
        switchfunk(&x, &opp, &y);
        printf("New Calc? (Y,N) \n");
        fflush(stdin);
        scanf("%c", &restart);
        if (restart != (char*)'Y'&&restart != (char*)'N')
        {
            printf("Bad input!");
        }
    }
}

double readcalc(double** x,char** opp,double** y)
{
    printf("\n Type your calculation!(z.B.4+7)\n");
    scanf("%lf%c%lf",x,opp,y);
    return 0;
}

double addition(double a,double b)
{
    double c = 0;
    c = a + b;
    return c;
}

double subtraction(double a, double b)
{
    double c = 0;
    c = a - b;
    return c;
}

double multiplication(double a, double b)
{
    double c = 0;
    c = a*b;
    return c;
}

double division(double a, double b)
{
    double c = 0;
    c = a / b;
    return c;
}

祝你好运!

2 个答案:

答案 0 :(得分:2)

您的代码中存在很多问题,其中大部分都与您使用指针有关。这是一个问题:

newcalc函数中,循环中包含以下条件:

restart != (char*)'N'

这将按预期工作,实际上总是是真的,并给你一个无限循环。

原因是restart是一个指针,指向restart函数中本地main变量的位置。 永远不会(char *) 'N'(指向地址78的指针)相同。

如何解决这个具体问题?首先,不要将它作为参数,将其声明为本地(非指针!)变量:

char restart = 'y';

然后在循环条件下正常使用

while (restart == 'y' || restart == 'Y') { ... }

要提示更多指针问题,请记住scanf想要一个指向变量存储值的指针吗?

但是在例如readcalc函数变量xoppy是指向数据应存储位置的指针,但是你将这些指针传递给{ {1}}:

scanf

在这里,你应该使用指针作为scanf("%lf%c%lf",x,opp,y); 函数的参数,而不是指针指针。

换句话说,它应该声明为

readcalc

你很可能在你的(不必要的)使用指针和指针指针时遇到很多其他问题,但这些问题确实很突出。

答案 1 :(得分:0)

我认为你滥用指针并使用地址而不是值本身。此外,您正在使用指针指针,而您不需要这样做。看看下面的代码。

#include <stdio.h>
#include <stdlib.h>
void newcalc (char*,double*,char*,double*);
double switchfunk(double*, char*, double*);
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);

int main()
 {
    double a=0, b=0;
    char op='R', restart = 'Y';
    newcalc(&restart,&a,&op,&b);
    fflush(stdin);
    getchar();
return 0;
}
double switchfunk (double* x, char* opp, double* y)
{
    printf("\n ---CALCULATOR--- \n\n\n");
    switch ( *opp )
    {
        case '+':
                    printf("%lf + %lf = %lf\n", *x, *y, addition(*x, *y)); 
                    break;
        case '-':
                    printf("%lf - %lf = %lf\n", *x, *y, subtraction(*x, *y)); 
                    break;
        case '*':
                    printf("%lf * %lf = %lf\n", *x, *y, multiplication(*x, *y)); 
                    break;
        case '/':
                    printf("%lf / %lf = %lf\n", *x, *y, division(*x, *y));
                    break;
        default:printf("bad operator!");
    }

    return 0;
}
void newcalc(char* restart,double* x, char* opp, double* y)
{

    while ( *restart == 'Y' || *restart == 'y')
    {
        readcalc(x, opp, y);
        switchfunk(x, opp, y);
        printf("New Calc? (Y,N) \n");
        fflush(stdin);
        scanf("%c", restart);
        // if (restart != (char*)'Y'&&restart != (char*)'N')
        // {
            // printf("Bad input!");
        // }
    }
}

double readcalc(double* x,char* opp,double* y)
{
    printf("\n Type your calculation!(z.B.4+7)\n");
    scanf("%lf%c%lf",x,opp,y);
    return 0;
}

double addition(double a,double b)
{
    return a + b;
}
double subtraction(double a, double b)
{
    return a - b;
}
double multiplication(double a, double b)
{
    return a*b;
}
double division(double a, double b)
{
    return a / b;
}