c++学习第二天

1.八位二进制转换
#include<iostream>
using namespace std;
double power (double x, int n){


	double value = 1.0;
	while (n--)
		value *= x;
		
	return value;
}
//double power (double x, int n);
int main(){
	
	int value = 0;
	
	
	cout << "Enter the number:" << endl;
	for (int i = 7; i >= 0; i--){
		char ch;
		cin >> ch;
		if (ch == '1'){
		value += static_cast<int>(power(2,i));
		}
	}	
	cout << value << endl;
	return 0;
		
} 
2.回文数
#include<iostream>
using namespace std;
bool symm(int n){
	int i = n;
	int m = 0;
	while (i != 0){
		m = m*10 + i%10;
		i /= 10;
	}
	return m == n;
}
int main(){
	for (int i = 11; i <= 1000; i++){
		if (symm(i*i) && symm(i*i*i) && symm(i)){
			cout << i <<"    " <<i*i <<"     "<<i*i*i <<endl;
		}
	}
	return 0;
}
3.求Π的值
#include<iostream>
#include<cmath> 
using namespace std;
double arctan(double x){
	double e = x;
	double spr = -x*x;
	double r = 0;
	int i = 1;
	while (abs(e/i) > 1e-15){
		double f = e/i;
		//r = (i % 4 == 1) ? r +f : r - f;
		r = r + f;
		e = e * spr;
		i += 2;
	}
	return r;
} 
int main(){
	double a = 16.0 * arctan(1/5.0);
	double b = 4.0 * arctan(1/239.0);
	cout <<(a - b);
}
4.骰子比赛
#include<iostream>
#include<cstdlib>

using namespace std;

int rollDice(){
	
	int die1 = 1 + rand()%6;
	int die2 = 1 + rand()%6;
	int sum = die1 + die2;
	
	cout << "Player rolled " <<die1<< " + " << die2 << " = " <<sum<<endl;
	return sum;
	
} 

enum GameStatus{WIN, LOSE,PIAYING};

int main(){
	
	int sum, mypoint;
	GameStatus status;
	unsigned seed; 
	
	cout <<"Please Enter an unsigned seed:"<<endl;
	cin >> seed;
	srand(seed);
	
	sum = rollDice();
	
	switch(sum){
		
		case 7:
		case 11:
			status = WIN;
			break; 
			
		case 2:
		case 3:
		case 12:
			status = LOSE;
			break;
		
		default:
			status = PIAYING;
			mypoint = sum;
			cout << "mypoint is "<< mypoint<<endl;
			break;
		 		
	}
	
	while (status == PIAYING){
		sum = rollDice();
		if (sum == mypoint){
			status = WIN;
		}
		else if (sum == 7){
			status = LOSE;
		}
		
	}
	
	if (status == WIN){
		cout <<"player win"<<endl;
		
	}
	else{
		cout <<"player lose"<<endl;
	}
	return 0;
}
5.输出星星
#include<iostream>
using namespace std;
int main(){
	
	const int N = 4;
	
	for (int i = 1; i <= N; i++){
		for (int j = 1; j <= 20; j++){
			cout << " ";
		}
		for (int j = 1; j <= 8-2*i; j++){
			cout << " ";
		}
		for (int j = 1; j <= 2*i-1; j++){
			cout <<"*";
		}
		cout << endl;
		
	}
	
	for (int i = 1; i <= N-1; i++){
		for (int j = 1; j <= 20; j++){
			cout << " ";
		}
		for (int j = 1; j <= 7-2*i; j++ ){
			cout <<"*";
		}
		cout << endl;
	}
	return 0;	
	
} 
6.参数传递
  • 值传递
  • 引用传递
    引用可以认为是另外一个变量的别名,int &r = i;r是i的别名,指向同一处内存空间
7.内联函数
  • inline 类型说明符 函数名(形参列表){函数体}
  • 编译时把这段代码嵌入主调函数中
8.带默认形参的函数
  • 有默认值得形参必须放在形参列表得最后
  • 原型声明时形参带默认值,后续定义不可以再带默认值
9.函数重载
  • 函数名相同,形参列表变量的数量或者类型必须不同
10.面对对象程序设计特点
  • 抽象:共性概括,数据抽象,行为抽象
  • 封装:将数据与行为结合,形成类
  • 继承:父类子类
  • 多态:强制转换(数据显式,隐式转换)、重载多态、类型参数化多态、包含多态
11.类与对象
  • 类:自定义的数据类型,包括属性及行为,相当于数据类型
  • 对象:类的一个具体事例,相当于定义的变量
12.访问控制
  • public:对外接口
  • protected
  • private
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值