如何将字符串传递给函数,然后检查它是否等于字符串
How to pass a string to function and then check if it is equal to a string
#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
而不是我之前传递的字符串。
比如我给的输入是real
。当我调试时,我注意到 type = '/0'
而不是我希望它是“真实的”,因此,条件 type == "real"
是 false
我该如何解决这个问题?
不确定为什么你得到“null”,(编辑:参见 )一定是 GettingType
而不是 GettingComplexNumber
的问题。我无法测试,因为您使用的是 scanf_s
,它似乎是 Windows 特定的。
但是除此之外,你不能将字符串与==
进行比较,这只是比较指针地址。相反,您需要使用标准 strcmp
函数进行字符串比较。
在您的 GettingType
函数中,您声明了 char type[8];
。这会将 type
保存在堆栈中,因此,当函数结束时,内存将丢失。相反,您需要使用 malloc
将 type
保存在堆中,即使在函数结束后也会保存数据。考虑这段代码:
char * GettingType()
{
char *type = (char*)malloc(sizeof(char)*8);
printf("Enter number type: put: real or complex!\n");
fgets(type, 8, stdin);
return type;
}
你的代码有几个问题:
您正在 return 引用 GettingType
中的局部变量。我建议使用常量值,这样魔术数字就不会出现并动态分配。别忘了 #include <stdlib.h>
您不能将字符串与 ==
进行比较。与 ==
比较检查存储字符串的内存地址,而不是存储在它们上的值。您可以改用 string.h
中的函数 strcmp
。 strcmp
将按字母顺序进行比较,如果相等则 return 0。
您正在使用 fgets 阅读。 '\n'
保存在您的数组中。所以即使你可以用 ==
检查它也不会相等。您必须与 real\n
和 complex\n
.
进行比较
您在调用 GettingOperator()
之前没有清空缓冲区,因此它正在读取读取浮点数后留下的 '\n'
。
读完operator
你又读了一篇type
。所以你应该为 \n
.
添加一个额外的 getchar()
您scanf_s
中的参数有误。那个9不应该在那里。您可以通过将其放在 %
.
之后来限制它
您正在向用户询问信息,忘记在您的消息后添加 space。
您的消息不清楚,要退出程序您应该输入无效的运算符。我不得不通过查看您的开关中的 default
来计算。
即使我提供的运算符无效,您的代码也会要求输入第二个数字。它不应该,因为运算符无效。
我已经完成了建议的大部分更改。但是,您仍然需要验证用户输入。例如:如果用户输入的内容既不是 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 上的快捷方式),看看发生了什么,并与您认为应该发生的事情进行比较。这可能会为您节省很多时间!
#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
而不是我之前传递的字符串。
比如我给的输入是real
。当我调试时,我注意到 type = '/0'
而不是我希望它是“真实的”,因此,条件 type == "real"
是 false
我该如何解决这个问题?
不确定为什么你得到“null”,(编辑:参见 GettingType
而不是 GettingComplexNumber
的问题。我无法测试,因为您使用的是 scanf_s
,它似乎是 Windows 特定的。
但是除此之外,你不能将字符串与==
进行比较,这只是比较指针地址。相反,您需要使用标准 strcmp
函数进行字符串比较。
在您的 GettingType
函数中,您声明了 char type[8];
。这会将 type
保存在堆栈中,因此,当函数结束时,内存将丢失。相反,您需要使用 malloc
将 type
保存在堆中,即使在函数结束后也会保存数据。考虑这段代码:
char * GettingType()
{
char *type = (char*)malloc(sizeof(char)*8);
printf("Enter number type: put: real or complex!\n");
fgets(type, 8, stdin);
return type;
}
你的代码有几个问题:
您正在 return 引用
GettingType
中的局部变量。我建议使用常量值,这样魔术数字就不会出现并动态分配。别忘了#include <stdlib.h>
您不能将字符串与
==
进行比较。与==
比较检查存储字符串的内存地址,而不是存储在它们上的值。您可以改用string.h
中的函数strcmp
。strcmp
将按字母顺序进行比较,如果相等则 return 0。您正在使用 fgets 阅读。
进行比较'\n'
保存在您的数组中。所以即使你可以用==
检查它也不会相等。您必须与real\n
和complex\n
.您在调用
GettingOperator()
之前没有清空缓冲区,因此它正在读取读取浮点数后留下的 '\n'
。读完
添加一个额外的operator
你又读了一篇type
。所以你应该为\n
.getchar()
您
之后来限制它scanf_s
中的参数有误。那个9不应该在那里。您可以通过将其放在%
.您正在向用户询问信息,忘记在您的消息后添加 space。
您的消息不清楚,要退出程序您应该输入无效的运算符。我不得不通过查看您的开关中的
default
来计算。即使我提供的运算符无效,您的代码也会要求输入第二个数字。它不应该,因为运算符无效。
我已经完成了建议的大部分更改。但是,您仍然需要验证用户输入。例如:如果用户输入的内容既不是 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 上的快捷方式),看看发生了什么,并与您认为应该发生的事情进行比较。这可能会为您节省很多时间!