如何在各种语言中实现以下功能?
(x,y)在给定输入值的情况下计算圆周上的点:
我有一些变量和函数的基类以及多个子类。我想尽可能减少子类中所需的代码量。
我的代码示例:
#include <iostream>
class base{
public:
int a = 10;
int b;
void print()
{
std::cout << a <<std::endl;
}
};
class child: public base
{
public:
int a;
};
int main()
{
child ch;
ch.a = 20;
ch.print();
}
Run Code Online (Sandbox Code Playgroud)
由于打印了结果编号 10,这意味着使用了基类变量a,但我需要使用子类变量(如果存在)。所以这个例子的预期输出是 20。
#include <iostream>
#include <chrono>
#include <time.h>
#include <stdio.h>
using namespace std;
using namesapce chrono;
int main() {
int f;
time_t start, end;
time (&start);
cin >> f;
time (&end);
double dif = difftime (end, start);
printf ("Elapsed time is %.2lf seconds.", dif );
}
Run Code Online (Sandbox Code Playgroud)
大家好,我目前正在进行C++任务,基本上我需要在10秒内让用户输入内容.我设法找出如何计算秒的时间,但我需要它是毫秒,因为我必须找出10秒以上的毫秒数.我不熟悉C++,并且非常感谢任何可能有助于引导我朝着正确方向前进的建议.非常感谢
请参阅以下C代码.
#include <stdio.h>
int main(void)
{
char c1 = 3000;
char c2 = 250;
printf("%d\n",c1);
printf("%d\n",c2);
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是
-72
-6
Run Code Online (Sandbox Code Playgroud)
请解释此处应用的整数到字符转换规则,因为3000和250都在char范围之外(-128到127).
所以我得到了这个数组:
int arr[] = {60,70,30,15,17,80,16,75,90,85,40,75};
我必须在数组的总和小于500的同时“拟合”尽可能多的元素(基本上删除最大的元素,直到总和小于500)。
这是我尝试过的:
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int largestOfArray(int number[]);
int main()
{
int sum = 0;
int i = 0;
int arr[] = {60,70,30,15,17,80,16,75,90,85,40,75};
list<int> ar(arr,arr+12);
for (i = 0; i < 12;i++)
sum += arr[i];
while (sum > 500)
ar.remove(largestOfArray(arr[12]));
for (i = 0; i < 12;i++)
sum += arr[i];
for (i = 0;i < 12; i++)
cout << arr[i];
cout << sum;
return 0;
}
int largestOfArray(int number[12]){ …Run Code Online (Sandbox Code Playgroud) 我整天都在寻找这个.所以,C++如果你有这个代码:
#include <iostream>
struct a {
int x, y;
a (int aa = 0, int bb = 0) : x{aa}, y{bb} {
}
};
void printit (a*);
int main (void) {
printit (new a {1,3});
return 0;
}
void printit (a *aa) {
std::cout << aa->x << " - " << aa->y;
}
Run Code Online (Sandbox Code Playgroud)
如果给我printit ()函数(意味着我无法访问printit()代码)并且printit()不删除对象,有没有办法可以删除我创建的对象?
printit (new a {1,3}); // <--- this object
Run Code Online (Sandbox Code Playgroud)
为了澄清,我的问题是如何在没有附加变量的情况下执行上述操作,并确保已创建的对象已被删除.