当前位置:网站首页>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
边栏推荐
- torch. Finfo function
- String Basics
- 跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
- Cv2.lut() (populates the output array with values from the lookup table)
- New product release Junda intelligent integrated environmental monitoring terminal
- A blog written clearly by vit
- 初步了解认识正则表达式(Regex)
- nn. PReLU(planes)
- Let Google browser fofa plug-in come alive
- 字符串基础知识
猜你喜欢

同时做测试,别人已经年薪20w起,为什么你还在为达到月薪10k而努力?

Algorinote_ 2_ Main theorem and Akra bazzi theorem

Shell language

It has been engaged in the functional test of 10K to the test development of 40W annual salary for 5 years, and spent 7 days sorting out the super comprehensive learning route

Lintcode:127. Topology sorting

#113 Path Sum II

leetcode:210. 课程表 II

Introduction to scala basic grammar (III) various operators in Scala

Data visualization - broken line area chart

Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
随机推荐
MySQL field truncation principle and source code analysis
Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
在同花顺开户安全么,买股票怎么网上开户
最简单ALV模板
String Basics
Can tonghuashun open an account? Is it safe to open an account in tonghuashun
二分查找
Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
China hydraulic cylinder linear position sensor market trend report, technical dynamic innovation and market forecast
Can flush open an account? Can you directly open the security of securities companies on the app? How to open an account online when buying stocks
跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
How do testers plan for their future? To achieve 25K in 2 years?
JSP中的监听器
How to determine the sample size of an inspection lot in SAP QM's initial sampling strategy?
Lua pattern matching
[tutorial] Firefox send: deployment method of Firefox open source temporary file sharing service platform
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Data visualization diagram microblog forwarding diagram
Let Google browser fofa plug-in come alive
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?