atof

新手C++ 练习项目--计算器

醉酒当歌 提交于 2019-12-01 07:42:06
本篇博客用于记录我自己用C++实现的一个计算器,目标是完成加减乘除带括号的四则运算,并在后期用工厂设计模式加以优化。 Part 1:calculate 1+1=2 实现这样的一个式子的计算,只需要用到字符串分割即可,一开始尝试了stringstream去先读入一整个字符串"1+2",然后创建了两个临时变量int和一个char,用>>去读入,但是发现读入的char放在中间被忽略掉了 string s ="12+34"; // stringstream ss(s); // int n1,n2,c; // ss>>n1>>c>>n2;//c is 34 ,the '+' is ignored 然后我就换用了substr去进行分割,substr有两个参数,一个是开始分出字串的位置,另一个是字串的长度,第二个参数默认值是npos,也就是字符串末尾。 int i =0; while(isdigit(s[i])) { ++i;//pos } // int n =atof("22.0"); // cout<<n; //float d = atof(s.substr(0,i).c_str()); cout<<cal( atof(s.substr(0,i).c_str()) , atof(s.substr(i+1).c_str()) ,s[i])<<endl;    //string to const

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 ,

converting string to a double variable in C

谁都会走 提交于 2019-11-29 09:26:53
I have written the following code....It should convert a string like "88" to double value 88 and print it void convertType(char* value) { int i = 0; char ch; double ret = 0; while((ch = value[i] )!= '\0') { ret = ret*10 +(ch - '0'); ++i; } printf("%d",ret);//or %f..what is the control string for double? } //input string :88 But it always prints 0...But when i change type of ret to int ...it works fine...when the type is float or double,it prints zero...so why am i getting this ambiguous results? Use sscanf (header stdio.h or cstdio in C++): char str[] = "12345.56"; double d; sscanf(str, "%lf",

Locale-independent “atof”?

徘徊边缘 提交于 2019-11-28 21:17:13
I'm parsing GPS status entries in fixed NMEA sentences, where fraction part of geographical minutes comes always after period. However, on systems where locale defines comma as decimal separator, atof function ignores period and whole fraction part. What is the best method to deal with this issue? Long/latitude string in stored in character array, if it matters. Example Code: m_longitude = atof((char *)pField); Where pField[] = "01000.3897"; Cross-platform project, compiled for Windows XP and CE. Comment to solution: Accepted answer is more elegant, but this answer (and comment) is also worth

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;

Not including stdlib.h does not produce any compiler error!

送分小仙女□ 提交于 2019-11-28 12:38:36
Hopefully this is a very simple question. Following is the C pgm (test.c) I have. #include <stdio.h> //#include <stdlib.h> int main (int argc, char *argv[]) { int intValue = atoi("1"); double doubleValue = atof("2"); fprintf(stdout,"The intValue is %d and the doubleValue is %g\n", intValue, doubleValue); return 0; } Note that I am using atoi() and atof() from stdlib.h, but I do not include that header file. I compile the pgm (gcc test.c) and get no compiler error! I run the pgm (./a.out) and here is the output, which is wrong. The intValue is 1 and the doubleValue is 0 Now I include stdlib.h

Locale-independent “atof”?

折月煮酒 提交于 2019-11-27 13:52:16
问题 I'm parsing GPS status entries in fixed NMEA sentences, where fraction part of geographical minutes comes always after period. However, on systems where locale defines comma as decimal separator, atof function ignores period and whole fraction part. What is the best method to deal with this issue? Long/latitude string in stored in character array, if it matters. Example Code: m_longitude = atof((char *)pField); Where pField[] = "01000.3897"; Cross-platform project, compiled for Windows XP and

Converting char* to float or double

ε祈祈猫儿з 提交于 2019-11-26 17:48:17
问题 I have a value I read in from a file and is stored as a char*. The value is a monetary number, #.##, ##.##, or ###.##. I want to convert the char* to a number I can use in calculations, I've tried atof and strtod and they just give me garbage numbers. What is the correct way to do this, and why is the way I am doing it wrong? This is essentially what I am doing, just the char* value is read in from a file. When I print out the temp and ftemp variables they are just garbage, gigantic negative

python 度分秒转度

会有一股神秘感。 提交于 2019-11-26 12:42:45
#必须是u类型==================u==================== by gisoracle def dmstod(dms): #arcpy.AddMessage("======================gisoracle==========="+dms+"==============================") try: p = dms.find('°') if p<0: return str(dms) #arcpy.AddMessage("p="+str(p)) d=string.atof(dms[0:p].strip()) #arcpy.AddMessage("d="+str(d)) p1=dms.find('′') #arcpy.AddMessage("p1="+str(p1)) if p1<0: p1=dms.find("'") f=0 # if p1>0: f=string.atof(dms[p+1:p1].strip()) else: p1=p #arcpy.AddMessage("f="+str(f)) p2=dms.find('″') if p2<0: p2=dms.find('"') #arcpy.AddMessage("p2="+str(p2)) s=0 # if p2>0: s=string.atof(dms[p1+1

How to convert string to float?

▼魔方 西西 提交于 2019-11-26 07:28:58
问题 #include<stdio.h> #include<string.h> int main() { char s[100] =\"4.0800\" ; printf(\"float value : %4.8f\\n\" ,(float) atoll(s)); return 0; } I expect the output should be 4.08000000 whereas I got only 4.00000000 . Is there any way to get the numbers after the dot? 回答1: Use atof() or strtof() * instead: printf("float value : %4.8f\n" ,atof(s)); printf("float value : %4.8f\n" ,strtof(s, NULL)); http://www.cplusplus.com/reference/clibrary/cstdlib/atof/ http://www.cplusplus.com/reference/cstdlib