我创建了一个温度转换类,我决定创建一个结构如下的转换表:
temperature | conversion formula
出于这个原因,我创建了一个 std::map,其中的键作为温度索引,然后是公式。在这里你可以看到实现:
enum class TempType {Celsius, Fahrenheit, Kelvin, Rankine, Delisle, Newton, Reaumur, Romer};
class Temperature {
private:
double value; //value of the temperature
TempType kind; //Celsius, Fahrenheit...
//conversion tables
std::map<TempType, std::function<double(double)>> fromCelsius = {
{ TempType::Fahrenheit, [](double x) { return x * (9/5) + 32; } },
{ TempType::Kelvin, [](double x) { return x + 273.15; } },
{ TempType::Rankine, [](double x) { return (x + 273.15) * (9/5); } },
{ TempType::Delisle, [](double x) { return (100 - x) * (3/2); } },
{ TempType::Reaumur, [](double x) { return x * (33/100); } },
{ TempType::Newton, [](double x) { return x * (4/5); } },
{ TempType::Romer, [](double x) { return x * (21/40) + 7.5; } }
};
std::map<TempType, std::function<double(double)>> fromFahrenheit = {
{ TempType::Celsius, [](double x) { return (x - 32) * (5/9); } },
{ TempType::Kelvin, [](double x) { return (x + 459.67) * (5/9); } },
{ TempType::Rankine, [](double x) { return x + 459.67; } },
{ TempType::Delisle, [](double x) { return (212 - x) * (5/6); } },
{ TempType::Newton, [](double x) { return (x - 32) * (11/60); } },
{ TempType::Reaumur, [](double x) { return (x - 32) * (4/9); } },
{ TempType::Romer, [](double x) { return (x - 32) * (7/24) + 7.5; } }
};
std::map<TempType, std::function<double(double)>> fromKelvin = {
{ TempType::Celsius, [](double x) { return x - 273.15; } },
{ TempType::Fahrenheit, [](double x) { return x * (9/5) - 459.67; } },
{ TempType::Rankine, [](double x) { return x * (9/5); } },
{ TempType::Delisle, [](double x) { return (373.15 - x) * (3/2); } },
{ TempType::Newton, [](double x) { return (x - 273.15) * (33/100); } },
{ TempType::Reaumur, [](double x) { return (x - 273.15) * (4/5); } },
{ TempType::Romer, [](double x) { return (x - 273.15) * (21/40) + 7.5; } }
};
std::map<TempType, std::function<double(double)>> fromRankine = {
{ TempType::Celsius, [](double x) { return (x - 491.67) * (5/9); } },
{ TempType::Fahrenheit, [](double x) { return x - 459.67; } },
{ TempType::Kelvin, [](double x) { return x * (5/9); } },
{ TempType::Delisle, [](double x) { return (671.67 - x) * (5/6); } },
{ TempType::Newton, [](double x) { return (x - 491.67) * (11/60); } },
{ TempType::Reaumur, [](double x) { return (x - 491.67) * (4/9); } },
{ TempType::Romer, [](double x) { return (x - 491.67) * (7/24) + 7.5; } }
};
std::map<TempType, std::function<double(double)>> fromDelisle = {
{ TempType::Celsius, [](double x) { return 100 - x * (5/9); } },
{ TempType::Fahrenheit, [](double x) { return 212 - x * (6/5); } },
{ TempType::Kelvin, [](double x) { return 373.15 - x * (2/3); } },
{ TempType::Rankine, [](double x) { return 671.67 - x * (6/5); } },
{ TempType::Newton, [](double x) { return 33 - x * (11/50); } },
{ TempType::Reaumur, [](double x) { return 80 - x * (8/15); } },
{ TempType::Romer, [](double x) { return 60 - x * (7/20); } }
};
std::map<TempType, std::function<double(double)>> fromNewton = {
{ TempType::Celsius, [](double x) { return x * (100/33); } },
{ TempType::Fahrenheit, [](double x) { return x * (60/11) + 32; } },
{ TempType::Kelvin, [](double x) { return x * (100/33) + 273.15; } },
{ TempType::Rankine, [](double x) { return x * (60/11) + 491.67; } },
{ TempType::Delisle, [](double x) { return (33 - x) * (50/11); } },
{ TempType::Reaumur, [](double x) { return x * (80/33); } },
{ TempType::Romer, [](double x) { return x * (35/22) + 7.5; } }
};
std::map<TempType, std::function<double(double)>> fromReamur = {
{ TempType::Celsius, [](double x) { return x * (5/4); } },
{ TempType::Fahrenheit, [](double x) { return x * (9/4) + 32; } },
{ TempType::Kelvin, [](double x) { return x * (5/4) + 273.15; } },
{ TempType::Rankine, [](double x) { return x * (9/4) + 491.67; } },
{ TempType::Delisle, [](double x) { return (80 - x) * (15/8); } },
{ TempType::Newton, [](double x) { return x * (33/80); } },
{ TempType::Romer, [](double x) { return x * (21/32) + 7.5; } }
};
std::map<TempType, std::function<double(double)>> fromRomer = {
{ TempType::Celsius, [](double x) { return (x - 7.5) * (40/21); } },
{ TempType::Fahrenheit, [](double x) { return (x - 7.5) * (24/7) + 32; } },
{ TempType::Kelvin, [](double x) { return (x - 7.5) * (40/21) + 273.15; } },
{ TempType::Rankine, [](double x) { return (x - 7.5) * (24/7) + 491.67; } },
{ TempType::Delisle, [](double x) { return (60 - x) * (20/7); } },
{ TempType::Newton, [](double x) { return (x - 7.5) * (22/35); } },
{ TempType::Reaumur, [](double x) { return (x - 7.5) * (32/21); } }
};
public:
}
#endif // TEMPERATURE_H
现在,如果我想从一个温度对象转换为另一个温度对象,我使用以下代码:
Temperature x(20, TempType::Celsius); //20 °C
double s = x.convertTo(TempType::Fahrenheit); //convert 20 °c to 68 °F
转换代码是这样的:
double Temperature::convertTo(const TempType& temperature) const {
if (temperature == kind) {
return value;
}
double result = -1;
switch (temperature) {
case TempType::Celsius:
result = fromCelsius.at(kind)(value);
break;
case TempType::Fahrenheit:
result = fromFahrenheit.at(kind)(value);
break;
case TempType::Kelvin:
result = fromKelvin.at(kind)(value);
break;
case TempType::Rankine:
result = fromRankine.at(kind)(value);
break;
case TempType::Delisle:
result = fromDelisle.at(kind)(value);
break;
case TempType::Newton:
result = fromNewton.at(kind)(value);
break;
case TempType::Reaumur:
result = fromReamur.at(kind)(value);
break;
case TempType::Romer:
result = fromRomer.at(kind)(value);
break;
default:
break;
}
return result;
}
我使用 map 的键(TempType),然后将参数传递给 lambda。返回的结果始终为 0。有什么想法吗?
我正在使用 QTCreator 和 mingw。
最佳答案
在C++中4/5,21/40,5/6等都是0因为除法是整数算术。
您需要改为编写 4./5。
关于C++ 无法从映射中调用 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48913048/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file