0
 #include <stdio.h>

typedef struct ComplexNumber_S
{
    float realNumber;
    float img;
} ComplexNumber;

/**
* @brief gets a number
* @return the input number
*/
float GetNumber()
{
    float num;
    while (scanf_s("%f", 9, &num) != 1)
    {
        while (getchar() != '\n');
        printf("Try again: ");
    }
    return num;
}

/**
*  Getting a char from user
*  @return a char
*/
char GettingOperator()
{
    printf("Enter operator: +, -, *, /, any other chars \n");
    char operatorr;
    operatorr = getchar();
    return operatorr;
}

/**
*   Getting a string, asks for real or complex 
*   @return string 
*/
char * GettingType()
{
    char type[8];
    printf("Enter number type: put: real or complex!\n");
    fgets(type, sizeof(type), stdin);
    return type;

}

/**
*   Getting a complex or real number
*   @param - type: real number or complex
*/
ComplexNumber GettingComplexNumber(char *type)
{
    ComplexNumber result;
    result.realNumber = 0.0;
    result.img = 0.0;
    if (type == "real")
    {
        printf("Enter a number\n");
        result.realNumber = GetNumber();
        result.img = 0.0;
    }

    if (type == "complex")
    {
        printf("Enter real number");
        result.realNumber = GetNumber();
        printf("Enter image field");
        result.img = GetNumber();

    }
    return result;
}

/**
*   @param num1 -  a complex number a + bi
*   @param num2 - a commplex number c + di
*
*   @return num1 / num2
*   formula: (ac + bd) / (c^2 + d^2) + i((bc - ad) / (c^2 + d^2))
*/
ComplexNumber Devision(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    float denominator = num2.realNumber * num2.realNumber + num2.img * num2.img;
    result.realNumber = (num1.realNumber * num2.realNumber + num1.img * num2.img) / 
        denominator;
    result.img = (num1.img * num2.realNumber - num1.realNumber * num2.img) /
        denominator;
    return result;

}

/**
*  @param num1 - a + bi
*  @param num2 - c + di
*  @return a complex number 
*  formula:  ac - bd + i(ad + bc)
*/
ComplexNumber Multiplicate(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    result.realNumber = num1.realNumber * num2.realNumber - num1.img * num2.img;
    result.img = num1.realNumber * num2.img + num1.img * num2.realNumber;
    return result;

}

/**
*  
*  @param num1 - a complex number
*  @param num2 - a complex number
*
*  @return num1 - num2 in a complex number
*/
ComplexNumber OperatorMinus(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    result.realNumber = num1.realNumber - num2.realNumber;
    result.img = num1.img - num2.img;
    return result;

}

/**
*  adding two complex numbers.
*  @param num1 - a complex nnumber
*  @param num2 - a complex number
*
*/
ComplexNumber OperatorPlus(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    result.realNumber = num1.realNumber + num2.realNumber;
    result.img = num1.img + num2.img;
    return result;

}

void PrintComplexNumber(ComplexNumber num)
{
    if (num.img > 0.0)
    {
        printf("%.2f + %.2fi", num.realNumber, num.img);
    }
    else
    {
        printf("%.2f  %.2fi", num.realNumber, num.img);
    }
}

/**
*         calculator of complex and real numbers
* @return 0 - when terminates
*/
int main()
{
    char *type;
    type = GettingType();
    ComplexNumber num1 = GettingComplexNumber(type);
    
    char operatorr;
    int ext = 0;
    ComplexNumber num2;
    
    while (ext == 0)
    {
        operatorr = GettingOperator();
        type = GettingType();
        num2 = GettingComplexNumber(type);
        switch (operatorr)
        {
        case '+':
            num1 = OperatorPlus(num1, num2);
            break;
        case '*':
            num1 = Multiplicate(num1, num2);
            break;
        case '/':
            num1 = Devision(num1, num2);
            break;
        case '-':
            num1 = OperatorMinus(num1, num2);
            break;
        default:
            ext = 1;
            break;

        }
    }
    
    PrintComplexNumber(num1);
    return 0;
}

你好!

我试图创建一个复数计算器,但问题是我将初始化char *传递给函数并将其视为null.

我不明白为什么该函数GettingComplexNumber(char *type) 将变量type视为null我之前传递的字符串。

例如,我给出了 input real。当我调试时,我注意到 type = '/0'不是我希望它是“真实的”,所以条件type == "real"是假的

我怎么解决这个问题?

4

3 回答 3

3

在您的GettingType功能上,您声明char type[8];. 这保存type在堆栈中,因此,当函数结束时,内存会丢失。相反,您需要使用 保存type在堆中malloc,即使在函数结束后也会保存数据。考虑这段代码:

char * GettingType()
{
    char *type = (char*)malloc(sizeof(char)*8);
    printf("Enter number type: put: real or complex!\n");
    fgets(type, 8, stdin);
    return type;
}
于 2021-01-09T19:02:34.540 回答
1

不知道为什么你得到“null”,(编辑:请参阅@Birkagal's answer了解你为什么得到 null)一定是GettingType而不是GettingComplexNumber. 我无法测试,因为您正在使用scanf_s它似乎是特定于 Windows 的。

但除此之外,您不能将字符串与进行比较==,这只是比较指针地址。相反,您需要使用标准strcmp函数进行字符串比较。

于 2021-01-09T19:01:24.027 回答
1

您的代码中存在一些问题:

  1. 您正在返回对GettingType. 我建议使用一个常量值,这样幻数就不会出现并动态分配。别忘了做#include <stdlib.h>

  2. 您不能将字符串与==. 比较==检查存储字符串的内存地址,没有存储在它们上的值。您可以改用strcmpfrom的功能string.hstrcmp将按字母顺序进行比较,如果相等则返回 0。

  3. 您正在使用 fgets 阅读。'\n'保存在您的阵列中。因此,即使您可以检查==它也不会相等。您必须与real\n和进行比较complex\n

  4. 您在调用之前没有清空缓冲区,GettingOperator()因此它正在读取\n'读取浮点数后留下的 ' 。

  5. 读完operatortype又读了一遍。所以你应该getchar()\n.

  6. 你的参数scanf_s是错误的。那个9不应该在那里。您可以通过将其放在%.

  7. 您正在向用户询问信息并且在您发送消息后忘记放置空格。

  8. 您的消息不清楚,要离开程序,您应该输入无效的运算符。我必须通过查看default您的开关中的来确定这一点。

  9. 即使我提供了无效的运算符,您的代码也会要求提供第二个数字。不应该,因为运算符无效。

我已经做出了建议的大部分更改。但是,您仍然需要验证用户输入。例如:如果用户输入的内容既不是real也不是,或者complex类似的东西会发生什么。这是更改后的结果:

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define STRING_SIZE 15
typedef struct ComplexNumber_S
{
    float realNumber;
    float img;
} ComplexNumber;

/**
* @brief gets a number
* @return the input number
*/
float GetNumber()
{
    float num;
    while (scanf_s("%f", &num) != 1)
    {
        while (getchar() != '\n');
        printf("Try again: ");
    }
    return num;
}

/**
*  Getting a char from user
*  @return a char
*/
char GettingOperator()
{
    printf("Enter operator: +, -, *, /, (any other chars to leave the program)\nYour choice: ");
    char operatorr;
    operatorr = getchar();
    getchar(); // You need to get the \n so it won't cause trouble when reading another char or string
    return operatorr;
}

/**
*   Getting a string, asks for real or complex
*   @return string
*/
char* GettingType()
{
    char* type = (char*)malloc(sizeof(*type) * STRING_SIZE);;
    printf("Enter number type: put: real or complex!\nYour choice: ");
    fgets(type, STRING_SIZE, stdin);
    return type;

}

/**
*   Getting a complex or real number
*   @param - type: real number or complex
*/
ComplexNumber GettingComplexNumber(char* type)
{
    ComplexNumber result;
    result.realNumber = 0.0;
    result.img = 0.0;
    if (!strcmp(type, "real\n"))
    {
        printf("Enter a real number: \n");
        result.realNumber = GetNumber();
        result.img = 0.0;
    }

    if (!strcmp(type, "complex\n"))
    {
        printf("Enter a real number for the real part: ");
        result.realNumber = GetNumber();
        printf("Enter a real number for the imaginary part: ");
        result.img = GetNumber();

    }
    getchar(); // since you are reading a char as operator you need to catch the '\n' here also
    return result;
}

/**
*   @param num1 -  a complex number a + bi
*   @param num2 - a commplex number c + di
*
*   @return num1 / num2
*   formula: (ac + bd) / (c^2 + d^2) + i((bc - ad) / (c^2 + d^2))
*/
ComplexNumber Devision(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    float denominator = num2.realNumber * num2.realNumber + num2.img * num2.img;
    result.realNumber = (num1.realNumber * num2.realNumber + num1.img * num2.img) /
        denominator;
    result.img = (num1.img * num2.realNumber - num1.realNumber * num2.img) /
        denominator;
    return result;

}

/**
*  @param num1 - a + bi
*  @param num2 - c + di
*  @return a complex number
*  formula:  ac - bd + i(ad + bc)
*/
ComplexNumber Multiplicate(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    result.realNumber = num1.realNumber * num2.realNumber - num1.img * num2.img;
    result.img = num1.realNumber * num2.img + num1.img * num2.realNumber;
    return result;

}

/**
*
*  @param num1 - a complex number
*  @param num2 - a complex number
*
*  @return num1 - num2 in a complex number
*/
ComplexNumber OperatorMinus(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    result.realNumber = num1.realNumber - num2.realNumber;
    result.img = num1.img - num2.img;
    return result;

}

/**
*  adding two complex numbers.
*  @param num1 - a complex nnumber
*  @param num2 - a complex number
*
*/
ComplexNumber OperatorPlus(ComplexNumber num1, ComplexNumber num2)
{
    ComplexNumber result;
    result.realNumber = num1.realNumber + num2.realNumber;
    result.img = num1.img + num2.img;
    return result;

}

void PrintComplexNumber(ComplexNumber num)
{
    if (num.img > 0.0)
    {
        printf("%.2f + %.2fi\n", num.realNumber, num.img);
    }
    else
    {
        printf("%.2f  %.2fi\n", num.realNumber, num.img);
    }
}

/**
*         calculator of complex and real numbers
* @return 0 - when terminates
*/
int main()
{
    char* type;
    type = GettingType();
    ComplexNumber num1 = GettingComplexNumber(type);

    char operatorr;
    int ext = 0;
    ComplexNumber num2;

    while (ext == 0)
    {
        operatorr = GettingOperator();

        //If the user didn't type one of the operators below then break the loop
        if (!strchr("+-/*", operatorr))
            break;
        type = GettingType();
        num2 = GettingComplexNumber(type);
        switch (operatorr)
        {
        case '+':
            num1 = OperatorPlus(num1, num2);
            break;
        case '*':
            num1 = Multiplicate(num1, num2);
            break;
        case '/':
            num1 = Devision(num1, num2);
            break;
        case '-':
            num1 = OperatorMinus(num1, num2);
            break;
        }
    }

    PrintComplexNumber(num1);
    return 0;
}

我想你正在使用 Visual Studio。如果是这样,您可以在调试模式下测试您的代码,并查看每个变量中保存的内容,一次执行一条语句。只需点击继续点击F11(这是 Visual Studio 2019 上的快捷方式),查看正在发生的事情并与您认为应该发生的事情进行比较。这可能会为您节省很多时间!

于 2021-01-09T19:43:34.693 回答