zoukankan      html  css  js  c++  java
  • 函数:使用函数重载及默认参数实现程序

    #include <iostream>
    
    unsigned long left(unsigned long num, unsigned ct);
    char * left(const char *str, int n=1);
    
    int main(void)
    {
    	using namespace std;
    	char *temp,*trip = "Hawaii!!";
    	unsigned long n=12345678;
    	int i;
    	
    	for(i=1; i<10; i++)
    	{
    		cout << left(n, i) << endl;
    		temp = left(trip, i);
    		cout << temp << endl;
    		delete[] temp;
    	}
    	return 0;
    }
    unsigned long left(unsigned long num, unsigned ct)
    {
    	unsigned digits = 1;
    	unsigned long n = num;
    	
    	if(ct == 0|| num == 0)return 0;
    	
    	while(n/=10) digits++;
    	
    	if(digits > ct)
    	{
    		ct=digits-ct;
    		while(ct--) num/=10;
    		
    		return num;
    	}
    	else
    		return num;
    }
    char * left(const char * str, int n)
    {
    	if(n < 0)
    		n=0;
    	char *p = new char[n+1];
    	int i;
    	for(i=0; i<n&&str[i]; i++) p[i]=str[i];
    	
    	while(i<=n) p[i++]='';
    	
    	
    	return p;
    }

  • 相关阅读:
    HTML 拖放 和 地理定位
    HTML web存储
    HTML 语义元素 和 MathML元素
    Docker Swarm
    Docker Machine
    Docker Compose
    Docker 的网络模式
    数据共享与持久化
    镜像和容器的基本操作
    Docker 简介
  • 原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732344.html
Copyright © 2011-2022 走看看