atoi

LeetCode-8-字符串转换整数 (atoi)

こ雲淡風輕ζ 提交于 2020-01-23 21:17:09
题意描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2^31, 2^31 − 1]。如果数值超过这个范围,请返回 INT_MAX (2^31 − 1) 或 INT_MIN (−2^31) 。 示例: 示例一: 输入 : "42" 输出 : 42 示例二: 输入 : " -42" 输出 : - 42 解释 : 第一个非空白字符为 '-' , 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 - 42 。 示例三: 输入 : "4193 with words" 输出 : 4193 解释 : 转换截止于数字 '3'

like atoi but to float

☆樱花仙子☆ 提交于 2020-01-22 13:31:56
问题 Is there a function similar to atoi which converts a string to float instead of to integer? 回答1: atof() (or std::atof() talking C++ - thanks jons34yp) 回答2: boost::lexical_cast<float>(str); This template function is included in the popular Boost collection of libraries, which you'll want learn about if you're serious about C++. 回答3: Convert a string to any type (that's default-constructible and streamable): template< typename T > T convert_from_string(const std::string& str) { std:

Convert String of ASCII digits to int in MIPS/Assembler

允我心安 提交于 2020-01-21 09:45:07
问题 Im writing some MIPS code to take a string of ASCII digits and convert the string into an integer. The string is entered by the user and can be at most 10 digits in length. My code works fine and uses the obvious method of performing looped addition after multiplying the Least Significant number in the string by a power of ten determined by the index of the array, starting from the last digit entered (10^0) to the first digit entered (10^n, n=number of digits in the array). I was wondering if

Convert String of ASCII digits to int in MIPS/Assembler

冷暖自知 提交于 2020-01-21 09:44:08
问题 Im writing some MIPS code to take a string of ASCII digits and convert the string into an integer. The string is entered by the user and can be at most 10 digits in length. My code works fine and uses the obvious method of performing looped addition after multiplying the Least Significant number in the string by a power of ten determined by the index of the array, starting from the last digit entered (10^0) to the first digit entered (10^n, n=number of digits in the array). I was wondering if

字符串转换整数 (atoi)

南笙酒味 提交于 2020-01-21 03:09:48
题目: 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 思路: 这道题,就从左往右开始遍历。寻找数据的起始值。情况如下: 开始遇到空格则跳过。 非空格情况 1)符号’-‘或者’+'则记录为正还是负,用flag标记,然后开始处理数据转换,将当前i++之后开始处理数据。 2)符号不是数字则返回0.是数字则开始记录当前i,开始处理数字。 寻找到起始值之后开始向右遍历转换数字。数字获取可以通过sum=sum*10+str.charAt(i)-‘0’,将sum定义为long型,只要sum!=(int)sum说明已经超过int最大值,则通过判断flag返回int最大、最小值。 class Solution { public int

LeetCode-8.字符串转换整数(atoi)

…衆ロ難τιáo~ 提交于 2020-01-18 06:04:44
地址: https://leetcode-cn.com/problems/string-to-integer-atoi/ 思路:按照题目要求判断即可 Code: #include<iostream> using namespace std; typedef long long LL; class Solution { public: int myAtoi(string str) { long long res=0; int l=0,len=str.length(); while(l<len&&str[l]==' '){ ++l; } int p=1,Min=1<<31,Max=-(Min+1); if(l!=len){ if(str[l]=='+') p=1,++l; else if(str[l]=='-') p=-1,++l; while(l<len&&str[l]>='0'&&str[l]<='9'){ res=res*10+str[l++]-'0'; if(p*res<Min){ res=Min; break; }else if(p*res>Max){ res=Max; break; } } res*=p; } return res; } }; int main() { ios::sync_with_stdio(false); string str; Solution So;

C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数

寵の児 提交于 2020-01-16 06:23:04
在编程中经常需要用到数字与字符串的转换,下面就总结一下。 1.atoi()    C/C++标准库函数 ,用于字符串到整数的转换。   函数原型:int atoi (const char * str); 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main () 4 { 5 char *numchars="1234"; 6 int num=atoi(numchars); 7 printf("%d\n",num); 8 return 0; 9 }   另外C/C++还提供的标准库函数有:   (1)long int atol ( const char * str );     (2)double atof (const char* str); 2.itoa()    不是 C/C++标准库函数, 用于 整数到字符串的转换。   函数原型: char *itoa( int value, char *string, int radix); 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main () 4 { 5 int num=1234; 6 int radix=8; 7 char res[20]; 8 itoa(num,res,radix); 9 printf("%d(10)=%s

将数字字符串转化成整型数据

ぃ、小莉子 提交于 2020-01-16 06:11:40
将数字字符串转化成整型数据我们将用到两个函数。 1. c_str 它的作用是将string对象转化成char*,为什么要这样 做呢,这就要说起另外一个函数了。 2.atoi (它是array to integer的缩写) 它的作用是将数字字符串转化成整型数据。但是要注意 atoi(const char * s), 这个是它的标准用法,如果是 atoi(string s)这个就不行了。 具体的用法是。 View Code string str="-1234"; int num = atoi(str.c_str()); string str = "1234"; int Num = atoi(str.c_str()); 此时的Num应该就是1234了。 但是要注意: str中只能包含0~9这10个数字和+、-这两种符号。 其它的任何符号都不能有,否则不能正确得到结果。 来源: https://www.cnblogs.com/o8le/archive/2012/03/31/2426444.html

Convert each digit from number saved in string to array of int

泄露秘密 提交于 2020-01-11 14:04:08
问题 I'm writing this project on DFA and i want to save and covert each digit of an integer saved as a string to an int array.This is the code from the function responsible for that: int l=final_states.size(); int* temp_final; temp_final=new int [l]; for(int i=0;i<l;i++) { temp_final[i]=atoi(final_states.at(i).c_str()); } This gives me the following mistake : request for member 'c_str' in '((DFA*)this)->DFA::final_states.std::basic_string<_CharT, _Traits, _Alloc>::at<char, std::char_traits<char>,

C programming: print only int from fgets

元气小坏坏 提交于 2020-01-11 02:08:48
问题 See this main : int main(void) { int i; int ch; char str[512]; fgets(str, sizeof str, stdin); for (i = 0; i <= (strlen(str)); i++) { if (str[i] != '\0' && str[i] != '\n') { int num = atoi(&str[i]); printf("%d\n", num); } } return 0; } I want to get line with numbers from user and get all the numbers without any spaces or tabs . For example: The input 1 2 3 . But in this case this the output: 1 2 2 3 3 So why i received 2 and 3 twice? 回答1: Here's how I would do it: char line[256]; if (fgets