Доброго времени суток, господа, прошу откликнуться и помочь! Написал парочку функций для перевода Int в Char и наоборот и написал программу для теста:
#include
#define INT_MIN -2147483648
#define INT_MAX 2147483647
int Char2Int(char string[]) {
bool negative = false;
int outputValue = 0;
int stringLength = 0;
while (string[stringLength] != '\0') { // count the number of characters in the string
stringLength++;
}
for (int i = 0; i < stringLength; i++) {
if (string == '-') {
negative = true; // remember that the input string is negative
} else if (string >= '0' && string INT_MAX) {
return ""; // return an empty string if the input is out of range
}
// Handle the special case where the input integer is 0
if (inputNumber == 0) {
resultString[0] = '0';
resultString[1] = '\0';
return resultString;
}
// Convert the integer to a string
int absoluteInputNumber;
if (inputNumber < 0) {
absoluteInputNumber = inputNumber * -1; // take the absolute value of inputNumber
resultString[0] = '-'; // add a minus sign to the beginning of the string
} else {
absoluteInputNumber = inputNumber;
}
int digit, reversedNumber = 0, resultIndex = 0;
while (absoluteInputNumber > 0) {
digit = absoluteInputNumber % 10;
reversedNumber = reversedNumber * 10 + digit;
absoluteInputNumber = absoluteInputNumber / 10;
}
while (reversedNumber > 0) {
resultString[resultIndex + 1] = (reversedNumber % 10) + '0'; // start at resultIndex + 1 to skip the minus sign
resultIndex++;
reversedNumber = reversedNumber / 10;
}
resultString[resultIndex + 1] = '\0'; // add a null terminator to the end of the string
return resultString; // return a pointer to the character array
}
int main() {
int inputNumber = 12345;
printf("Integer: %d\n", inputNumber);
printf("String: %s\n", Int2Char(inputNumber));
int outputNumber = Char2Int(Int2Char(inputNumber)); // convert the string back to an integer
printf("Output: %d\n", outputNumber);
return 0;
}
Ожидаю вывод:
Integer: 12345
String: 12345
Output: 12345
Получаю:
Integer: 12345
String:
Output: 0
Знатоки, в чем может быть проблема?
#include
#define INT_MIN -2147483648
#define INT_MAX 2147483647
int Char2Int(char string[]) {
bool negative = false;
int outputValue = 0;
int stringLength = 0;
while (string[stringLength] != '\0') { // count the number of characters in the string
stringLength++;
}
for (int i = 0; i < stringLength; i++) {
if (string == '-') {
negative = true; // remember that the input string is negative
} else if (string >= '0' && string INT_MAX) {
return ""; // return an empty string if the input is out of range
}
// Handle the special case where the input integer is 0
if (inputNumber == 0) {
resultString[0] = '0';
resultString[1] = '\0';
return resultString;
}
// Convert the integer to a string
int absoluteInputNumber;
if (inputNumber < 0) {
absoluteInputNumber = inputNumber * -1; // take the absolute value of inputNumber
resultString[0] = '-'; // add a minus sign to the beginning of the string
} else {
absoluteInputNumber = inputNumber;
}
int digit, reversedNumber = 0, resultIndex = 0;
while (absoluteInputNumber > 0) {
digit = absoluteInputNumber % 10;
reversedNumber = reversedNumber * 10 + digit;
absoluteInputNumber = absoluteInputNumber / 10;
}
while (reversedNumber > 0) {
resultString[resultIndex + 1] = (reversedNumber % 10) + '0'; // start at resultIndex + 1 to skip the minus sign
resultIndex++;
reversedNumber = reversedNumber / 10;
}
resultString[resultIndex + 1] = '\0'; // add a null terminator to the end of the string
return resultString; // return a pointer to the character array
}
int main() {
int inputNumber = 12345;
printf("Integer: %d\n", inputNumber);
printf("String: %s\n", Int2Char(inputNumber));
int outputNumber = Char2Int(Int2Char(inputNumber)); // convert the string back to an integer
printf("Output: %d\n", outputNumber);
return 0;
}
Ожидаю вывод:
Integer: 12345
String: 12345
Output: 12345
Получаю:
Integer: 12345
String:
Output: 0
Знатоки, в чем может быть проблема?