PTA exercise 7-8 converting strings into decimal integers (15 points) - it's easy to have ideas, but it's not easy to get it right~

overview:

The overall idea of this problem is easy to think of, but it is particularly easy to make mistakes in specific implementation. This blog summarizes this problem from two aspects: easy to make mistakes and problem-solving ideas.

catalogue

overview:

Title:

Input format:

Output format:

Input example:

Output example:

1, Problem solving ideas:

2, Code:

3, Error prone sharing:

Error prone point 1:

Error prone point 2:

Summary:

Title:

Enter a string that # ends with. This question requires that all non hexadecimal characters (regardless of case) be filtered out to form a new string representing hexadecimal numbers, and then convert it into decimal numbers for output. If the character "-" exists before the first hexadecimal character, it means that the number is negative.

Input format:

Input gives a non empty string that # ends in one line.

Output format:

Output the converted decimal number in one line. The problem is to ensure that the output is within the long integer range.

Input example:

+-P-xf4+-1!#

Output example:

 -3905

1, Problem solving ideas:

I personally like to follow the steps of PTA questions. If I don't have a clear idea, I first start from the output examples given by PTA, first solve the input and output of special cases, and then carry out the input and output of universal examples. It will be easier to do the questions step by step.  

It is easy to think of the problem of converting string into decimal integer. It is roughly divided into the following steps:

1. Input a string of characters, '#' indicates the end. First, you need to define a character array arr [] to store the input string, and output the getchar() and while() loop judgment I use. Getchar() is very error prone if it is used as a while() loop condition.

2. The title requires the character "-" before the first hexadecimal character, and the last output hexadecimal number is a negative number. The second step is to find the subscript of the first hexadecimal number in arr [] and store it in the tmp variable, and then traverse from position 0 to tmp position. If '-' exists, the flag will be changed to 1, which is used to judge the positive and negative at last.

3. The third step is to find the hexadecimal characters 0 ~ 9 and 'a'~'f 'and' a'~'f' in the ARR [] array and store them in a new array arr2 [], and record several qualified characters.  

4. Because the title is not case sensitive, in order to facilitate subsequent operations, convert all lowercase characters in the array into corresponding uppercase characters. This step is very simple. As long as you know the ASCII table, it's OK.

5. This step is a very important step. The problem is how to convert these characters in arr2 [] into decimal numbers. This is also divided into several small steps. First, you need to know the number corresponding to this character. For the characters' 0 '~' 9 ', you only need to subtract the value of' 0 'from the corresponding number. For' A'~'F', you need to subtract 55. There is no explanation here, Also look at the ASCII table.

The above five steps are my ideas for doing questions. Although I feel very wordy and not concise, the overall idea is very gradual and easy to understand. Hey hey.

 

2, Code:

#include <stdio.h>
#include <string.h>

int mypow(int x,int y)
{
	//y power of x
	int i = 0;
	if (y == 0)
	{
		return 1;
	}
	else if (y == 1)
	{
		return x;
	}
	else
	{
		int p = x;
		for (i = 0; i < y-1; i++)
		{
			p = p * x;
		}
		return p;
	}

}
int main()
{
	char arr[30];
	memset(arr, '\0', sizeof(arr));
	int i = 0;
	int tmp = 0;//Store the first hexadecimal character subscript
	do //Error prone point 1: enter the string correctly
	{
		arr[i] = getchar();
		i++;
	} while (arr[i-1] != '#');

	char arr2[20];
	int count = 0;
	memset(arr2, '\0', sizeof(arr2));
	for (int j = 0; j < i; j++)
	{
		if ((arr[j] >= '0' && arr[j] <= 'F') || (arr[j] >= 'a' && arr[j] <= 'f'))
		{
			tmp = j;
			break;
		}
	}
	int flag = 0;//Hexadecimal front - 0, 1
	for (int j = 0; j < tmp; j++)
	{
		if (arr[j] == '-')
		{
			flag = 1;
			break;
		}
	}

	for (int j = 0; j < i; j++)
	{
		if ((arr[j] >= '0' && arr[j] <= '9') || (arr[j] >= 'a' && arr[j] <= 'f') || (arr[j] >= 'A' && arr[j] <= 'F'))
		{
			arr2[count++] = arr[j];//Store hexadecimal characters
		}
	}
	for (int j = 0; j < count; j++)
	{
		if (arr2[j] >= 'a' && arr2[j] <= 'f')
		{
			arr2[j] = arr2[j] - 32;
		}
	}
	int sum = 0;
	for (int j = 0; j < count; j++)
	{
		if (arr2[j] <= '9' && arr2[j] >= '0')
		{
			sum = sum + (arr2[j] - '0')*mypow(16, count - j - 1);
		}
		else
		{
			sum = sum + (arr2[j] - 55)*mypow(16, count - j - 1);
		}
	}
	if (flag)
	{
		printf("%d", -1 * sum);
	}
	else
	{
		printf("%d", sum);
	}
	return 0;
}

3, Error prone sharing:

Error prone point 1:

The first point is the use of getchar(). It must be noted that this function is read from the standard input stream, that is, from the keyboard. You must read something once you use it.

Error prone point 2:

The second point is between character 0 and character F. there are several other characters that are not coherent.

Summary:

Before doing the problem, don't directly type the code. You should first clarify the general idea, and then you need to know a little about the ASCII table, and then there's nothing. It's a regular problem.

Keywords: C

Added by pnoeric on Mon, 27 Dec 2021 06:53:02 +0200