atoi

C programming: print only int from fgets

♀尐吖头ヾ 提交于 2019-11-30 19:31:34
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? Here's how I would do it: char line[256]; if (fgets(line, sizeof line, stdin) != NULL) { const char *ptr = line; while (*ptr != '\0') { char *eptr = NULL; const

Binary String to Integer with 'atoi()'

允我心安 提交于 2019-11-30 08:02:28
问题 I have a string of binary that I then convert to an integer using atoi() . When I do this it seems to automatically convert the binary to decimal. The issue is that the resulting integer is negative and doesn't agree with any of the online binary-to-decimal converters. Is something broken with atoi() ? Should I be using a different function instead? Code: string myString = "01000101"; int x = atoi(myString.c_str()); cout << x; Thanks 回答1: atoi doesn't handle binary numbers, it just interprets

atoi implementation in C

微笑、不失礼 提交于 2019-11-29 21:28:52
I can't understand the following atoi implementation code, specifically this line: k = (k << 3) + (k << 1) + (*p) - '0'; Here is the code: int my_atoi(char *p) { int k = 0; while (*p) { k = (k << 3) + (k << 1) + (*p) - '0'; p++; } return k; } Can someone explain it to me ? Another question: what should be the algorithm of atof implementation ? R.. k = (k << 3) + (k << 1); means k = k * 2³ + k * 2¹ = k * 8 + k * 2 = k * 10 Does that help? The *p - '0' term adds the value of the next digit; this works because C requires that the digit characters have consecutive values, so that '1' == '0' + 1 ,

VC CString,int,string,char*之间的转换

人盡茶涼 提交于 2019-11-29 19:18:15
1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); char 转 CString CString.format("%s", char*); char 转 string string s(char *); string 转 char * char *p = string.c_str(); CString 转 string string s(CString.GetBuffer()); 1,string -> CString CString.format("%s", string.c_str()); 用c_str()确实比data()要好. 2,char -> string string s(char *); 你的只能初始化,在不是初始化的地方最好还是用assign(). 3,CString -> string string s(CString.GetBuffer()); GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间. 《C++标准函数库》中说的 有三个函数可以将字符串的内容转换为字符数组和C—string 1.data(),返回没有”\0“的字符串数组 2,c_str(),返回有”\0“的字符串数组 3,copy()

How do you use atoi to assign individual elements of a char array?

限于喜欢 提交于 2019-11-29 07:05:45
So as we all probably know, the atoi converts a char to a number. But, what do you do if you only want one of the array elements instead of the whole array? Please look at the following: for (h = 0; h < 5; h++) { num[h] = atoi(temp[h]); } Assume that num is an array of type int and that temp is and array of type char . This gives me one of those annoying conversion problems: Invalid conversion from 'char' to 'const char *' Any suggestions on how to convert a single element of a char array to an int using atoi? James McNellis If you only want to convert a single character you don't need to use

string实用方法总结

痴心易碎 提交于 2019-11-29 01:40:02
更新中...... -------------------------------------------------------------------------------------------------------------------------------------------------------- ·字符串转数字 string str="1234"; int a=atoi(str.c_str()); // a=1234 备注: 1)atoi函数原型:int atoi(char *str),返回值是int,小心不要溢出 2)函数把从当前开始的字符(必须是数字或者'+','-',否则转化失败,返回0)到第一个不是数字的字符之间的字符串转化为整数 string s1="-1234%lalala",s2="lalala1234"; int a1=atoi(s1.c_str()),a2=atoi(s2.c_str()); // a1=-1234;a2=0 来源: https://www.cnblogs.com/MissCold/p/11437944.html

浙大 PAT 甲级 1072 Gas Station Dijkstra算法 单源最短路径

拈花ヽ惹草 提交于 2019-11-29 00:48:05
这题比较难,考察最短路径,加了很多复杂的条件。 第一步,读懂题意 首先题意需要读懂,候选加油站需要满足以下条件: 1、加油站离每个住宅的最短距离要在服务范围内; 2、针对每个候选加油站,求出这个加油站到每个住宅的最短距离,这些最短距离中最小的那一个值,也就是离整个住宅区的最短距离,越大越好; 3、如果有多个这样的候选加油站,那么针对每个候选加油站,求这个加油站到每个住宅的最短距离的和,最短距离和越小越好; 4、如果有多个这样的候选加油站,那么加油站的编号越小越好。 因此,解题思路是: 1、输入数据; 2、用Floyd算法求出两点之间的最短距离; 3、逐个判断哪个加油站是有效最优解; 4、按要求输出结果。 第二步,处理输入 输入有字符有数字,并且还有干扰输出,比如: 1、同一个起点和终点间有两条路,这时候只保存最短的那条; 2、起点和终点是同一个位置,忽略; 3、用字符数组接收编号,如果开头是G,那么为加油站,将它转换成数字N+i(N是住宅的总数,i是该加油站的数字编号)。 第三步,选择算法 大致有两种算法: 1、将问题看作全源最短路径,用Floyd算法求得每两点间的最短距离,从而找出最佳候选位置。 Floyd算法的时间复杂度大约是三次方,最后一个用例会超时。 2、改变思路,将问题看作单源最短路径,针对每个加油站都做一次单源最短路径,用Dijkstra算法,时间复杂度大约是二次方

字符串转换整数 (atoi)

只谈情不闲聊 提交于 2019-11-29 00:31:22
https://leetcode-cn.com/problems/string-to-integer-atoi/ LeetCode第八题 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 int d=0; 5 istringstream is(str); 6 is >> d; 7 return d; 8 } 9 }; 记录这个题主要是通过这个题主要是了解一下C++一些stringstream,重点不在算法 来源: https://www.cnblogs.com/MasterYan576356467/p/11435696.html

atoi implementation in C

纵然是瞬间 提交于 2019-11-28 18:59:28
问题 I can't understand the following atoi implementation code, specifically this line: k = (k << 3) + (k << 1) + (*p) - '0'; Here is the code: int my_atoi(char *p) { int k = 0; while (*p) { k = (k << 3) + (k << 1) + (*p) - '0'; p++; } return k; } Can someone explain it to me ? Another question: what should be the algorithm of atof implementation ? 回答1: k = (k << 3) + (k << 1); means k = k * 2³ + k * 2¹ = k * 8 + k * 2 = k * 10 Does that help? The *p - '0' term adds the value of the next digit;

8. String to Integer (atoi)

♀尐吖头ヾ 提交于 2019-11-28 06:48:48
Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or