慕课学习0:C++远征之起航篇

记录一下代码,很基础的C++介绍

0、关于输入输出

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(){
	cout<<"请输入一个整数"<<endl;
	int x = 0;
	cin>>x;
	cout<<oct<<x<<endl;//8进制 
	cout<<dec<<x<<endl;//10进制 
	cout<<hex<<x<<endl;//16进制
	
	cout<<"请输入一个布尔值(0,1):"<<endl;
	bool y;
	system("pause");
	return 0; 
	
}

1、关于命名空间

#include <stdlib.h>
#include <iostream>//引用头文件 
//using namespace std;//引用命名空间


namespace A{
	int x = 1;
	void fun(){
		std::cout<<"A"<<std::endl;//如果不引用命名空间,那么这些前面要加std:: 
	}
}

namespace B{
	int x = 2;
	void fun(){
		std::cout<<"B"<<std::endl;
	}
	void fun2(){
		std::cout<<"2B"<<std::endl;
	}
} 

using namespace B;//如果声明了B的命名空间,那么主体函数中用到B的函数、变量等可不用加:: 
int main(){
	std::cout<<A::x<<std::endl;
	fun();
	fun2();
	std::cout<<"hello"<<std::endl;
	system("pause");
	return 0;
}

2、综合练习(依然是命名空间的使用等)

//用到命名空间实现一个数组比较大小
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

//自定义的命名空间 
namespace MaxOrMin{
	int fun(int arr[],int a,bool isMax){
		int signal = arr[0];
		if(isMax == true){//求最大值 
			
			for(int i = 1;i <a;i++){
				if(arr[i]>signal){
					signal = arr[i];
				}
			}
		}
		else{
			for(int i = 1;i <a;i++){
				if(arr[i]<signal){
					signal = arr[i];
				}
			}
		}
		return signal;
	}
}
int main(){
	int arr1[100];
	int n;
	scanf("%d",&n);//输入数组数字的个数 
	for(int i = 0;i <n;i++){//传入数组中数据 
		scanf("%d",&arr1[i]);
	}
	bool isMax = false;//传入是求最大还是最小 
	cin>>isMax;
	cout<<MaxOrMin::fun(arr1,n,isMax)<<endl;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值