C++复习(六)——进制转换&日期操作

 

题目八:实现十进制向其他进制转换,实现其他进制向十进制转换

题目九:判断 1990.01.01到指定日期一共有多少天

/*
题目八:实现十进制向其他进制转换,实现其他进制向十进制转换

分析:十进制向其他进制转换可以使用栈先进后出的特性不断取余得到
	  进制转换, 其余进制向十进制转换 则可以通过字符串逐级乘出
	  
	  另外C++中也支持直接输出相应进制数字 ,例如:
    cout << std::oct <<189<< endl;  
    cout << std::dec <<189<< endl;  
    cout << std::hex <<189<< endl;  
    cout << bitset<8>(189) << endl;//<8>:表示保留8位输出 

stack:
1.入栈:如s.push(x);
2.出栈:如 s.pop().注意:出栈操作只是删除栈顶的元素,并不返回该元素。
3.访问栈顶:如s.top();
4.判断栈空:如s.empty().当栈空时返回true。
5.访问栈中的元素个数,如s.size();


题目九:判断 1990.01.01到指定日期一共有多少天

分析: 需考虑闰年
	能被4除尽且不能被100除尽,能被400除尽的为闰年
	考虑月份
	闰年二月29天,平年闰月28天 
	
结构体三种定义方式:

//声明了结构体变量s1
struct 
{
} s1;

//此结构体的标签被命名为SIMPLE,用SIMPLE标签的结构体,另外声明了变量t1, t2[20], *t3
struct SIMPLE
{
};
SIMPLE t1, t2[20], *t3; 

//可以用typedef创建新类型,结构体的标签被命名为Simple2,用Simple2作为类型声明新的结构体变量u1, u2[20], *u3
typedef struct
{
} Simple2;
Simple2 u1, u2[20], *u3;

*/
#include <bitset>  
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;  

void other_to_ten(string number,int other) {
	int i,sum = 0,a;
	for(i = 0;number.size()!=i;i++){
		a = number[number.size()-i-1] - '0';
		sum += pow(other,i) * a;
		//cout<<i<<" "<<a<<" "<<sum<<" "<<pow(other,i)<<endl;
		//cout<<number[i] - '0'<<endl;
	}
	cout<<sum<<endl;
}

void ten_to_other(int number,int other){
	stack<int> s;
	if(number == 0){
		cout<<0<<endl;
	}
	for(;number != 0;number/=other){
		s.push(number%other);
	}
	while(!s.empty()){
		cout<<s.top()<<" ";
		s.pop();
	}
	cout<<endl;
}

struct date{
	int year = 1990;
	int month = 1;
	int day = 1;
};

int Year(int year){
	if((year%4 == 0 && year%100 != 0)||(year%400 == 0)){
		return 366;
	}else{
		return 365;
	}
}

int Month(int year,int month){
	if(month != 2){
		switch(month){
			case 1:case 3:case 5:case 7:case 8:case 10:case 12:
				return 31;
				break;
			case 4:case 6:case 9:case 11:
				return 30;
				break;
			}
	}else{
		if(Year(year)==366){
			return 29;
		}else{
			return 28;
		}
	}
}

int main(){  
	//第八题 
	/*
	ten_to_other(123,16);
	other_to_ten("10011",2);
	*/
	
	//第九题
	struct date today,first; 
	cin>>today.year>>today.month>>today.day;
	//cout<<first.year<<" "<<today.year;
	int i,j,total_day = 0;
	for (i = first.year;i<=today.year;i++){
		if(i<today.year){
			if(Year(i)==366){
				total_day += 366;
			}else{
				total_day += 365;
			}
		}else{
			for (j = first.month;j<today.month;j++){
				total_day += Month(i,j);
			}
			total_day += today.day-first.day;
		}
	}
	cout<<total_day<<endl;
    return 0;  
} 

       这其中有不合适或者不正确的地方欢迎指正,我的QQ号码:2867221444(乔金明),谢谢,也可以相互交流下,备注信息随意,只要能看得出是开发者或者学习者即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值