关于浮点数的十进制和整数提取以及某些特定小数点的输出,有很多问题和答案.但没有人能解决我的问题.如果有人能帮助我解决我的问题,请
我实际上是试图从浮点数中提取精确的小数部分.我试过这个:
float f=254.73;
int integer = (int)f;
float fractional = f-integer;
printf ("The fractional part is: %f", fractional);
Run Code Online (Sandbox Code Playgroud)
但输出为:0.729996.出于这个原因,当我这样做时:
float f=254.73;
int integer = (int)f;
float fractional = f-integer;
int fractional_part_in_integer = ((int)(f*100)%100);
printf ("The value is: %d", fractional_part_in_integer);
Run Code Online (Sandbox Code Playgroud)
它给了我72作为输出.但是,我想从给定的数字254.73中精确地提取73.我已经知道%.2f在printf()函数期间如何使用打印最多两个十进制数字.但在我的代码中,我现在不想打印这个数字.我有一些计算,其中小数部分为整数形式,即73.所以,我的问题是如何从254.73中提取小数部分,以便我可以得到精确的73作为整数来进行更多的计算?
希望,我能让每个人都理解.
这是我的代码,它打印子集的元素,其总和等于给定的总和,(它仅适用于正数):
#include <bits/stdc++.h>
using namespace std;
void traverse(vector<int> vec) {
for(int a=0; a < vec.size(); a++)
cout << vec[a] << " ";
cout << endl;
}
void possible(vector<int> vec, int sum, vector<int> now) {
if(sum == 0) {
traverse(now);
}
else if(sum < 0) {
now.clear();
}
else if(sum > 0 && vec.size() > 0) {
for(int a = 0; a < vec.size(); a++) {
now.push_back(vec[a]);
vector<int> vecc(vec.begin() + a + 1, vec.end());
possible(vecc, sum - vec[a], now);
now.erase(now.end() - …Run Code Online (Sandbox Code Playgroud) 我正在使用 Codeblocks 17.12 并且已经将编译器设置设置为 C++11 标准。我正在学习 Bjarne Stroustrup 的书“编程 - 使用 C++ 的原则和实践”。在他的书中,他要求包括“std_lib_facilities.h”。我从他的网站复制它并保存在“Mingw”文件夹的“include”文件夹中。之后,我开始制作一个简单的程序:
#include<iostream>
#include "std_lib_facilities.h"
main()
{
std::cout<<"Hello world";
}
Run Code Online (Sandbox Code Playgroud)
但是编译器显示以下错误和警告:
Run Code Online (Sandbox Code Playgroud)warning: This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp] error: template-id 'do_get<>' for 'String > std::__cxx11::messages<char>::do_get(std::messages_base::catalog, int, …
我正在从 HackerRank 运行一个关于指针的简单 C++ 程序,它在网站上运行良好。但是,当我在 MacOS 上运行它时,我得到了error: call to 'abs' is ambiguous并且我不确定到底什么是模棱两可的。
我查看了类似问题的其他答案,但错误消息往往是Ambiguous overload call to abs(double),这不是我遇到的问题,因为我没有使用任何双打。我也试过包括头文件cmath和math.h,但问题仍然存在。
#include <stdio.h>
#include <cmath>
void update(int *a,int *b) {
int num1 = *a;
int num2 = *b;
*a = num1 + num2;
*b = abs(num1 - num2);
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题出现在第 8 …
我试图理解多态性,但我不明白如果静态多态性可以很好地调用类的成员,为什么我们需要运行时多态性。
就像,假设这是一个问题。
#include <bits/stdc++.h>
using namespace std;
class base{
public:
virtual void fun(){
cout<<"base called"<<endl;
}
};
class derived:public base{
public:
void fun(){
cout<<"derived called"<<endl;
}
};
int main() {
base b,*b1;
derived d;
b1 = &d;
b1->fun();
// b.fun();
// d.fun();
}
Run Code Online (Sandbox Code Playgroud)
假设这是我的代码,我想访问派生类或基类的函数,我可以通过简单地创建该类的对象来做到这一点,所以如果没有问题,那么为什么我们尝试使用引用来调用该对象(运行时多态性)。有人可以解释运行时多态性的实际需要吗?或者如果可能的话,您可以通过使用任何现实生活场景来解释它吗?
给定 n 个塔的高度和一个值 k。我们需要将每个塔的高度增加或减少 k(仅一次),其中 k > 0。任务是最小化修改后最长和最短塔的高度之间的差异,并输出该差异。
我得到了解决方案背后的直觉,但我无法评论下面解决方案的正确性。
// C++ program to find the minimum possible
// difference between maximum and minimum
// elements when we have to add/subtract
// every number by k
#include <bits/stdc++.h>
using namespace std;
// Modifies the array by subtracting/adding
// k to every element such that the difference
// between maximum and minimum is minimized
int getMinDiff(int arr[], int n, int k)
{
if (n == 1)
return 0;
// Sort all elements …Run Code Online (Sandbox Code Playgroud) #include <bits/stdc++.h>
using namespace std;
void test(){
int a = 100;
cout << a << endl;
}
int main()
{
void(*b)() = test;
(*b)(); //expression one
b(); //expression two
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test是一个函数指针,不是吗?(*b)()是正确的形式,因为它相当于函数原型。但为什么删除一个符号是正确的呢*?他们都产生了正确的结果。
正如标题所说,我想在 C++ 中的同一个 shell 进程/实例上运行 shell 命令并与其通信,我该怎么做?我查遍了每个角落,但找不到合适/直截了当的答案。我不是C++霸主,我的回答可能很愚蠢。现在我可以在unix上使用 fork/exec 的组合,但我在 Windows 上。如果有跨平台的解决方案,请在下面提及。提前致谢。
伪代码:
SHELL shell = make_shell();
shell.run("cd desktop");
shell.run("dir");
print(shell.stdout)
Run Code Online (Sandbox Code Playgroud) 我刚刚将 GCC 编译器版本从 11.4 更新到 13.1。我发现以下代码曾经在我的旧 GCC 11.4 上运行,但不再在 GCC 13.1 上运行。
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
#include <iostream>
int main() {
std::string s = "Hello World!";
std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器消息:
====================[ Build | run | Debug ]=====================================
"C:\Program Files\JetBrains\CLion 2023.3.2\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\zyk\CLionProjects\run\cmake-build-debug --target run -j 18
[1/2] Building CXX object CMakeFiles/run.dir/main.cpp.obj
FAILED: CMakeFiles/run.dir/main.cpp.obj
C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE -g -fdiagnostics-color=always -MD -MT CMakeFiles/run.dir/main.cpp.obj -MF CMakeFiles\run.dir\main.cpp.obj.d -o CMakeFiles/run.dir/main.cpp.obj -c C:/Users/zyk/CLionProjects/run/main.cpp
In file included from C:/Program Files/JetBrains/CLion 2023.3.2/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/string:43,
from C:/Program Files/JetBrains/CLion …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,很明显,myFn带有两个参数的函数定义应该来自namespace N。但是编译器无法编译它。它是编译器(g ++ 8.3)的限制,还是由C ++标准强加的?
#include <bits/stdc++.h>
using namespace std;
namespace N
{
// Same name function exists in class A
void myFn(int a, int b)
{
cout << a << ' ' << b << endl;
}
}
using namespace N;
class A {
public:
void myFn(int a)
{
#ifdef FINE
// Explicitly specify where should myFn definition come from
N::myFn(a, a);
#else
myFn(a, a);
#endif
}
};
int main()
{
A a;
a.myFn(3);
return 2; …Run Code Online (Sandbox Code Playgroud) c++ ×9
algorithm ×1
arrays ×1
c ×1
cmath ×1
g++ ×1
gcc ×1
greedy ×1
header ×1
namespaces ×1
oop ×1
optimization ×1
performance ×1
polymorphism ×1
shell ×1
subset ×1