当前位置:网站首页>ATOI super resolution
ATOI super resolution
2022-06-12 21:05:00 【Bald and weak】
Function description :
The library has its own functions , A reference header file is required #include<stdlib.h>
The string 123 Convert to integer number 123( one hundred and twenty-three )
characteristic :
- Input 123 Output ->123 // Direct conversion
- Input 123.5 Output ->123 //int Type conversion ( It can also be understood as directly putting . Treat as character )
- Input -123 Output ->-123 // Characters to be recognized ‘-’, convert to -( Minus sign )
- Input +123 Output ->123
- Input ( Space )123 Output ->123 // If there are spaces , Move forward directly to cover the space
- Input 12 ( Space )3 Output ->12 // If there is a space after it, it has stopped , Unable to continue to identify
- Input -+123 Output ->0 // For two or more +- The symbol does not recognize
- Input --123 Output ->0
- Input 123abcd123 Output ->123
- Input 123--++123 Output ->123
thought ( Distributed solution ):
1, Input 123 Output ->123
1, Get the number of characters and the number of digits corresponding to a single character converted to a number
The relationship between the number of characters and the number of digits :1234 -4 Characters -4 It's a thousand 10 Of 3 Power
How to get the number of characters : The judgment character is in ’0’,’9’ Between , Definition count Add up and
The number of digits in a number is 10 Of count-1 Power
2, Realize the conversion of single character :
adopt ASCCI code ‘1’-‘0’=1;
eg: 0:ASCCI 48 1:ASCCI 49
Code implementation :
int GetBite(char* arr, int length)
{
int count = 0;
int len;
for (int i = 0; i < len; i++)
{
if (arr[i] >= '0' && arr[i] <= '9')
{
count++;
}
}
return count;
}
int my_atoi(char* arr, int len)
{
assert(arr != NULL);
int result = 0;
int bite = GetBite(arr, len);
for (int i = 0; i < len; i++)
{
result = (arr[i] - '0') * pow(10, bite - 1); // Code interpretation ①
}
return result;
}
①result=(arr[i]-'0')*pow(10,bite-1);
pow(10,bite-1): pow The function is to implement 10 Of bite-1 Power
arr[i]-'0': Convert characters to numbers
2, Input 123.5 Output ->123 //int Type conversion ( It can also be understood as directly putting . Treat as character )
Modify the code :
result = (arr[i] - '0') * pow(10, bite - 1);// Code interpretation ②
②(int): Type cast ,result by int type ,pow by double type ,int*double=double, A strong turn is needed
3, Input -123 Output ->-123 // Characters to be recognized ‘-’, convert to -( Minus sign )
Modify the code :
int flag = 1;// Store the sign
for (int i = 0; i < len; i++) // Change an integer to a positive or negative number
{
if (arr[i] == '-')
flag = -1;
else // Add else Purpose : If not else,-'-'-'0' It's not a character , Perform error
result = (int)(result + (arr[i] - '0') * poe(10, bite - 1));
}
4, Input +123 Output ->123
Modify the code :
int flag = 1;// Store the sign
for (int i = 0; i < len; i++) // Change an integer to a positive or negative number
{
if (arr[i] == '-')
flag = -1;
else if (str[i] == '+') {
continue;// End the current cycle , Start a new cycle
else // Add else Purpose : If not else,-'-'-'0' It's not a character , Perform error
result = (int)(result + (arr[i] - '0') * poe(10, bite - 1));
}
5, Input ( Space )123 Output ->123 // If there are spaces , Move forward directly to cover the space
Modify the code :
while (arr[i] == ' ')//(*arr)
arr[i] = arr[i++];//arr++;
6, Input 12 ( Space )3 Output ->12 // If there is a space after it, it has stopped , Unable to continue to identify
Modify the code :
GetBite Within the function :
for (int i = 0; i < len; i++)
{
if (arr[i] >= '0' && arr[i] <= '9')
{
count++;
}
else
break; // When a space is encountered, it will be returned directly
}
7, Input - +123 Output ->0// For two or more +- The symbol does not recognize
8, Input --123 Output ->0
//(7)(8) Empathy , The code is as follows :
GetBite Within the function :
// use num To control +- Number
int num = 0;
for (int i = 0; i < len; i++)
{
if (arr[i] >= '0' && arr[i] <= '9')
{
count++;
if (num >= 2)// If there are two or more +- The result is 0
return 0;
}
else if (arr[i] == '+' || arr[i] == '-') // obtain +- The number of
num++;
else
break; // When a space is encountered, it will be returned directly
}
9, Input 123abcd123 Output ->123
10, Input 123--++123 Output ->123
// And (6) Empathy
The comprehensive code is as follows :
#define _CRT_SECURE_NO_WARNINGS //VS The compiler needs
#include<assert.h> // Assertion
#include<stdio.h>
#include<string.h>
#include<stdlib.h> // atoi itoa
#include<math.h> //pow function
int GetBite(char* arr, int length)
{
int count = 0, num = 0;
int len = strlen(str);
// Delete the blank space
for (int i = 0; i < len; i++)
{
if (arr[i] >= '0' && arr[i] <= '9')
{
count++;
if (num >= 2)// If there are two or more +- The result is 0
return 0;
}
else if (arr[i] == '+' || arr[i] == '-') // obtain +- The number of
num++;
else
break;
}
return count;
}
int my_atoi(char* arr, int len)
{
assert(arr != NULL);
int result = 0;
int flag = 1;// Store the sign
int i = 0;
while (arr[i] == ' ')//(*arr)
arr[i] = arr[i++];//arr++;
int bite = GetBite(arr, len);
int len = strlen(arr);// Recalculate length
for (int i = 0; i < len; i++) // Change an integer to a positive or negative number
{
if (arr[i] == '-')
flag = -1;
else if (arr[i] == '+')
continue;// Ends the current cycle
else // Add else Purpose : negative -'0' It's not a character , Perform error
result = (int)(result + (arr[i] - '0') * pow(10, bite - 1));
}
return result * flag;
}
int main()
{
char arr[] = "123";
int length = sizeof(arr) / sizeof(arr[0]);
printf("%d", my_atoi(arr, length));
return 0;
}
Please let me know in the comments section if there are any mistakes thank you
边栏推荐
- Solution of multi machine room dynamic loop status network touch screen monitoring
- atoi超强解析
- Nexus3搭建本地仓库
- Mxnet record IO details
- Library cache lock brought by add trandata
- 没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?
- #141 Linked List Cycle
- 大小端转换
- Scope and scope chain
- MySQL field truncation principle and source code analysis
猜你喜欢

Library cache lock brought by add trandata

torch. Finfo function

多机房动环状态网络触摸屏监控解决方案

一级指针&二级指针知识点梳理

#113 Path Sum II

At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?

Data visualization - broken line area chart

Solve the cvxpy error the solver GLPK_ MI is not installed

Data visualization - biaxial comparison effect

How do testers plan for their future? To achieve 25K in 2 years?
随机推荐
居家办公期间如何提升沟通效率|社区征文
做自媒体视频,友好的新媒体运营必备app分享
ASCII 码对照表
UVa11991 Easy Problem from Rujia Liu
Draw according to weight
test
Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer
remote: Support for password authentication was removed on August 13, 2021
Access control system based on RFID
Listener in JSP
Teambition 协作应用心得分享|社区征文
在同花顺开户安全么,买股票怎么网上开户
Mxnet record IO details
Understanding of functions
Data visualization diagram microblog forwarding diagram
Social metauniverse: start from redefining yourself
UVa11991 Easy Problem from Rujia Liu
递归调用知识点-包含例题求解二分查找、青蛙跳台阶、逆序输出、阶乘、斐波那契、汉诺塔。
USB机械键盘改蓝牙键盘
lintcode:127 · 拓扑排序