标签:c++中的基础
进制转换
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(void) {
cout << "请输入一个整数:" << endl;
int x = 0;
cin >> x;
cout << oct << x << endl;
cout << dec << x << endl;
cout << hex << x << endl;
cout << "请输入一个布尔值(0、1):"<<endl;
bool y = false;
cin >> y;
cout << boolalpha << y << endl;
system("pause");
return 0;
}命名空间的代码讲解
#include <stdlib.h>
#include <iostream>
using namespace std;
namespace A
{
int x = 1;
void fun()
{
cout << "A" << endl;
}
}
namespace B
{
int x = 2;
void fun()
{
cout << "B" << endl;
}
void fun2()
{
cout << "2B" << endl;
}
}
using namespace B;
int main(void) {
cout << A::x << endl;
fun2();
system("pause");
return 0;
}判断奇数还是偶数代码
#include <iostream>
#include <stdlib.h>
using namespace std;
namespace myNum //填写命名空间的关键字
{
int x = 106;
}
int main()
{
// 使用bool类型定义isOdd,作为状态位
bool isFlag = false;
if (myNum::x % 2 == 0)
{
//改变状态位的值,使其为false
myNum::x = 0;
}
else
{
//改变状态位的值,使其为true
myNum::x = 1;
}
// 判断状态位的值
if (bool(myNum::x))
{
// 如果状态位的值为true,则打印变量x是奇数
cout << "x是奇数" << endl;
}
else
{
// 如果状态位的值为false,则打印变量x是偶数
cout << "x是偶数" << endl;
}
system("pause");
return 0;
}判断数组中最大值和最小值
#include<iostream>
#include<stdlib.h>
using namespace std;
int getMaxOrMin(int *arr, int count, bool isMax)
{
int temp = arr[0];
for (int i = 0; i < count; i++)
{
if (isMax)
{
if (temp < arr[i])
{
temp = arr[i];
}
}
else
{
if (temp > arr[i])
{
temp = arr[i];
}
}
}
return temp;
}
int main(void)
{
int arr1[4] = {3,5,1,7};
bool isMax = false;
cin >> isMax;
cout << getMaxOrMin(arr1, 4, isMax) << endl;
system("pause");
return 0;
}本文出自 “ITAUSTINS” 博客,谢绝转载!
标签:c++中的基础
原文地址:http://9265482.blog.51cto.com/9255482/1961874