当前位置:编程学习 > C/C++ >>

求两段C++代码

要两段简单的C++代码,要原创,尽量多用各种符号。
追问:那个被无缘无故的删除了,大哥快发
答案:
又来了

#include<iostream>
using namespace std;

class Date
{
public:
 Date():m_iYear(2011),m_iMonth(1),m_iDay(1){}
 Date(int year,int month,int day):m_iYear(year),m_iMonth(month),m_iDay(day){}
 Date(const Date& date){m_iYear=date.m_iYear;m_iMonth=date.m_iMonth;m_iDay=date.m_iDay;}
 virtual ~Date(){}
 inline int GetYear(){return m_iYear;}
 inline int GetMonth(){return m_iMonth;}
 inline int GetDay(){return m_iDay;}
 inline void SetYear(int year){m_iYear=year;}
 inline void SetMonth(int month){m_iMonth=month;}
 inline void SetDay(int day){m_iDay=day;}
 int GetDayPerYear();
 bool CorrectDate();
 friend istream& operator >>(istream& in,Date& date)
 {
  in>>date.m_iYear>>date.m_iMonth>>date.m_iDay;
  return in;
 }
 friend ostream& operator <<(ostream& out,Date& date)
 {
  out<<date.m_iYear<<"-"<<date.m_iMonth<<"-"<<date.m_iDay;
  return out;
 }
private:
 int m_iYear;
 int m_iMonth;
 int m_iDay;
 static const int days1[];
 static const int days2[];
};

const int Date::days1[]={31,29,31,30,31,30,31,31,30,31,30,31};
const int Date::days2[]={31,28,31,30,31,30,31,31,30,31,30,31};

bool Date::CorrectDate()
{
 bool flag=false;

 int year=m_iYear,month=m_iMonth,day=m_iDay;

 if (m_iYear > 3000)
  m_iYear=3000;
 else if (m_iYear < 0)
  m_iYear=0;

 if (m_iMonth > 12)
  m_iMonth=12;
 else if(m_iMonth < 1)
  m_iMonth=1;

 if (m_iYear%4==0 && m_iYear%100!=0 || m_iYear%400==0)
 {
  if (m_iDay>days1[m_iMonth-1])
   m_iDay=days1[m_iMonth-1];
  else if (m_iDay<1)
   m_iDay=1;
 }
 else
 {
  if (m_iDay>days2[m_iMonth-1])
   m_iDay=days2[m_iMonth-1];
  else if (m_iDay<1)
   m_iDay=1;
 }

 if (m_iDay!=day || m_iMonth!=month || m_iYear!=year)
  flag=true;

 return flag;
}

int Date::GetDayPerYear()
{
 int sum=0;
 if (m_iYear%4==0 && m_iYear%100!=0 || m_iYear%400==0)
 {
  for (int i=0;i!=m_iMonth-1;++i)
   sum+=days1[i];
  sum+=days1[m_iMonth-1];
 }
 else
 {
  for (int i=0;i!=m_iMonth-1;++i)
   sum+=days2[i];
  sum+=days2[m_iMonth-1];
 }

 return sum;
}


int main()
{
 Date d;
 d.SetYear(2011);
 d.SetMonth(10);
 d.SetDay(28);
 d.CorrectDate();
 cout<<"当前日期为:"<<d<<endl;
 cout<<"当前日期是一年的第"<<d.GetDayPerYear()<<"天"<<endl;

 cout<<"请输入新日期(年月份以空格隔开):"<<endl;
 cin>>d;
 d.CorrectDate();
 cout<<"当前日期为:"<<d<<endl;
 cout<<"当前日期是一年的第"<<d.GetDayPerYear()<<"天"<<endl;
 return 0;
}

把以前的回答给你

#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;
int main()
{
 string s;
 cout<<"请输入整数:";
 cin>>s;
 int len = s.length();
 cout<<"每个整数:"<<endl;
 for(int  i=0;i<s.length();i++){
  cout<<s[i]<<" ";
 }
 cout<<endl;
    for (int i = 0; i<len/2; i++)
    {
          //前后交换
       char temp = s[i];
       s[i] = s[len-i-1];
       s[len-i-1] = temp;
    }   //输出交换后的字符串
 cout<<"整数长度:"<<s.size()<<"翻转字符串:"<<s<<endl;
 return 0;

}

翻转字符串

#include<iostream>
using namespace std;
class employee
{
private:
char *name ;
public:
employee(char *a);
};

employee::employee(char *a)
{
strcpy(name,a);
}

int main()
{
employee e("Hello");
return 0;
}
你可以使用mytc6.0进行编译运行,我编译过了,是成功滴,希望对你有用,请予以采纳;

上一个:有没有C++学习的PDF
下一个:快速排序的C++代码

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,