C++Primer中文版(第四版)题解整理(21)
时间:2025-04-20
时间:2025-04-20
C++Primer题解
参见2.9.1节。
习题2.33
确定你的编译器提供了哪些提高警告级别的选项。使用这些选项重新编译以前选择的程序,查看是否会报告新的问题。
【解答】
在笔者所用的编译器(MicrosoftVisualC++.NET2003)中,在Project菜单中选择Properties菜单项,在Configuration
Properties→C/C++→General→WarningLevel中可以选择警告级别。习题3.1
用适当的using声明,而不用std::前缀,访问标准库中的名字,重新编写2.3节的程序,计算一给定数的给定次幂的结果。
#include<iostream>
#include"windows.h"
usingnamespacestd;
intmain()
{
intbase,exponent;
longresult=1;
system("CLS");
cout<<"Enterbaseandexponent:"<<endl;
cin>>base>>exponent;
if(exponent<0)
{
cout<<"Exponentcan'tbesmallerthan0"<<endl;
return-1;
}
if(exponent>0)
{
for(intcnt=1;cnt<=exponent;cnt++)
result*=base;
}
cout<<base<<"raisedtothepowerof"<<exponent<<":
"<<result<<endl;
return0;
}