C++中<cmath>和<cstdlib> 45

请教各位大虾:C++中<cmath>和<cstdlib>是否都包含srand()和rand()函数,怎么有的程序,将<cstdlib>改成<cmath>也可以。#incl... 请教各位大虾: C++中<cmath>和<cstdlib> 是否都包含 srand()和rand() 函数,怎么有的程序,将<cstdlib>改成<cmath>也可以。
#include <iostream>
#include <ctime>
#include <cmath> //那为什么写<cmath>不写<cstdlib>也可以运行同样效果
using namespace std;

int main()
{
int correctCount = 0;
int count = 0;
long startTime = time(0);

while (count < 10)
{
srand(time(0));
int number1 = rand() % 10;
int number2 = rand() % 10;

if (number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}

cout << "What is " << number1 << " - " << number2 << "? ";
int answer;
cin >> answer;

if (number1 - number2 == answer)
{
cout << "You are correct!\n";
correctCount++;
}
else
cout << "Your answer is wrong.\n" << number1 << " - " << number2 << " should be " << (number1 - number2) << endl;

count++;
}
long endTime = time(0);
long testTime = endTime - startTime;

cout << "Correct count is " << correctCount << "\nTest time is " << testTime << " seconds\n";

return 0;
}
展开
 我来答
匿名用户
2010-01-09
展开全部
这是由于你包含了iostream,包含了iostream后就可以不再包含stdlib.h而使用里面声明的函数了

***********************************************************

这两个函数的声明在stdlib.h中,在C++中可以通过包含cstdlib现实,math.h中没有.
上海莘默
2024-04-12 广告
**ODU重载连接器**ODU重载连接器是一种高性能、高密度的连接解决方案,广泛应用于各种工业自动化和通信设备中。它采用先进的设计理念和制造技术,具有优异的电气性能和机械强度,能够满足复杂环境下对数据传输和信号稳定的高要求。同时,ODU重载... 点击进入详情页
本回答由上海莘默提供
匿名用户
2010-01-09
展开全部
srand()和rand()在<cstdlib>中
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式