m_stPara.StopInsAlt = atoi((Ini.GetValue("固定点的选择", "固定点高度")).c_str()); double类型数据的小数点部分被截断
时间: 2024-11-03 10:15:13 浏览: 26
`m_stPara.StopInsAlt = atoi((Ini.GetValue("固定点的选择", "固定点高度")).c_str());` 这行代码是从配置文件(Ini)获取名为"固定点的高度"的值,将其转换为整型(integer),然后赋给 `m_stPara.StopInsAlt` 变量。`atoi()` 函数用于将字符串转换成整数。
但是,如果原始的 Ini 文件中的"固定点的高度"值是一个包含小数点的双精度浮点数(double),比如 "10.5",使用 `atoi()` 函数会发生截断。因为 `atoi()` 只能处理整数,小数点后的内容会被忽略,只取到第一个非数字字符前的部分,即得到的结果是 10。
如果你需要保留小数部分并存储为 double 类型,你应该使用 `atof()` 或 `stod()` 函数来正确地从字符串解析浮点数。例如:
```cpp
double stopInsAlt;
std::string strHeight = (Ini.GetValue("固定点的选择", "固定点高度")).c_str();
stopInsAlt = std::stod(strHeight);
```
这将会完整地读取字符串中的数值,并将其转换为 double 类型,包括小数部分。
相关问题
Fraction strToFraction(string str) { Fraction f; stringstream ss(str); string num, den, tmp; getline(ss, num, '/'); getline(ss, den); f.numerator = atoi(num.c_str()); f.denominator = atoi(den.c_str()); return simplify(f); }
这段代码是一个将字符串转化为分数的函数。它先定义了一个Fraction类型的变量f,然后使用stringstream把输入的字符串str按照"/"分割成两个子串num和den,分别表示分数的分子和分母。接着使用atoi函数将两个子串转化为整数类型,并将这两个整数分别赋值给f的numerator和denominator成员变量。最后返回调用了simplify函数的f,这个函数的作用是将分数进行约分。
//添加一个新的博主 void add_blogger(char *name, char *group_names, char *group_fans) { strcpy(bloggers[blogger_count].name, name); //复制姓名 bloggers[blogger_count].fans = 0; //初始化粉丝数为0 bloggers[blogger_count].group_count = 0; //初始化粉丝群数量为0 char *token1 = strtok(group_names, ","); //使用逗号分隔粉丝群名字 char *token2 = strtok(group_fans, ","); //使用逗号分隔粉丝群人数 while (token1 != NULL && token2 != NULL) { //循环读取每个粉丝群名字和人数,并添加到groups数组中 strcpy(bloggers[blogger_count].groups[bloggers[blogger_count].group_count].name, token1); //复制粉丝群名字 bloggers[blogger_count].groups[bloggers[blogger_count].group_count].fans = atoi(token2); //转换并赋值粉丝群人数 bloggers[blogger_count].groups[bloggers[blogger_count].group_count].head = NULL; //初始化第一个粉丝指针为NULL bloggers[blogger_count].fans = bloggers[blogger_count].fans+atoi(token2); //累加该博主的粉丝数 bloggers[blogger_count].group_count++; //增加该博主的粉丝群数量 token1 = strtok(NULL, ","); //继续读取下一个粉丝群名字 token2 = strtok(NULL, ","); //继续读取下一个粉丝群人数 } blogger_count++; //增加博主数量 }优化该函数
优化该函数的一种方法是使用字符串分割函数`strsep`代替`strtok`,因为`strtok`在多线程环境下可能会导致不可预期的结果。另外,可以避免重复计算博主的粉丝数和粉丝群数量。
以下是优化后的代码:
```c
void add_blogger(char *name, char *group_names, char *group_fans) {
strcpy(bloggers[blogger_count].name, name); //复制姓名
bloggers[blogger_count].fans = 0; //初始化粉丝数为0
bloggers[blogger_count].group_count = 0; //初始化粉丝群数量为0
char *group_name = strdup(group_names); //复制粉丝群名字的副本
char *group_fan = strdup(group_fans); //复制粉丝群人数的副本
char *token1 = strsep(&group_name, ","); //使用逗号分隔粉丝群名字
char *token2 = strsep(&group_fan, ","); //使用逗号分隔粉丝群人数
while (token1 != NULL && token2 != NULL) { //循环读取每个粉丝群名字和人数,并添加到groups数组中
strcpy(bloggers[blogger_count].groups[bloggers[blogger_count].group_count].name, token1); //复制粉丝群名字
bloggers[blogger_count].groups[bloggers[blogger_count].group_count].fans = atoi(token2); //转换并赋值粉丝群人数
bloggers[blogger_count].groups[bloggers[blogger_count].group_count].head = NULL; //初始化第一个粉丝指针为NULL
bloggers[blogger_count].fans += atoi(token2); //累加该博主的粉丝数
bloggers[blogger_count].group_count++; //增加该博主的粉丝群数量
token1 = strsep(&group_name, ","); //继续读取下一个粉丝群名字
token2 = strsep(&group_fan, ","); //继续读取下一个粉丝群人数
}
free(group_name); //释放内存
free(group_fan); //释放内存
blogger_count++; //增加博主数量
}
```
这样做可以避免多线程环境下可能出现的问题,并且更加简洁和安全。
阅读全文
相关推荐

















