| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 839 人关注过本帖
标题:两个编程题目:能帮忙解决下吗
只看楼主 加入收藏
lurrass
Rank: 1
等 级:新手上路
帖 子:17
专家分:1
注 册:2010-9-13
结帖率:42.86%
收藏
 问题点数:0 回复次数:5 
两个编程题目:能帮忙解决下吗
1. 建立一个类STR,将任意整数转换为相应的字符串。例如,整数3456转换为字符串”3456”,具体要求如下:
(1) 私有数据成员
int num; 被转换的整数。
char s[15]; 存放转换后的字符串。
(2)公有成员函数
STR(int x); 构造函数。
void itoa(); 实现将num转换成字符串并存放在字符串数组s中。
void show(); 输出整数及转换后的字符串。
编写主程序测试,其中字符串要求输出其长度。


2. 定义一个满足如下要求的Date类:
(1)用日/月/年的格式输出日期;
(2)可运行在日期上加一天的操作;
(3)设置日期操作。
编写主程序进行测试。
搜索更多相关主题的帖子: 测试 编程 主程序 字符串 
2010-10-22 20:03
bujanbusan
Rank: 2
等 级:论坛游民
帖 子:45
专家分:28
注 册:2010-9-19
收藏
得分:0 
我也是新手 我写了个类的声明 不知道对不 欢迎斧正  下面的定义就请高手写了
Class CStr
{
private:
     int num;
     char s[15];
public:
    STR(int x);
    void itoa();
    void show();
};

#include "stdio.h"
#include <stdio.h>
2010-10-22 20:17
shafeilong
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:236
专家分:1434
注 册:2009-3-21
收藏
得分:0 
1,给个类似的  剩下的自己思考
#include   <stdlib.h>
#include   <stdio.h>

int   main(void)
{
      int   number   =   12345;
      char   string[25];

      itoa(number,   string,   10);
      printf( "integer   =   %d   string   =   %s\n ",   number,   string);
      return   0;
}

2,类的初始化操作
Date(int Y=1990,int M=1,int D=31)
{
}
void SetDate()
{
    year = Y;
    month = M;
    day = D;
}
void AddDate()
{
    day++;
    if(判断条件)  //包括闰年,每个月的天数  定义个全局数组吧
    {
        自己写
    }
}
int GetYear()
{return Year}
月份  天数同上
2010-10-22 21:00
聋眼睛瞎耳朵
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:29
专家分:123
注 册:2010-9-24
收藏
得分:0 
#include<iostream>
using namespace std;
static int i=0;

class STR
{
private:
    int num;
    char s[15];
public:
    STR(int x):num(x){}
   
    void itoa()
    {        
        int temp=num;
        while(temp!=0)
        {
            s[i]=temp%10+'0';
            temp/=10;
            i++;
        }
    }
    void show()
    {
        cout<<"整数为:"<<num<<endl;
        cout<<"字符串为:";
        for(int j=i-1;j>=0;j--)
            cout<<s[j];
        cout<<endl;
    }
};

int main()
{
    int n;
    cout<<"请输入一个数:";
    cin>>n;
    STR s(n);
    s.itoa();
    s.show();
}
2010-10-22 21:22
聋眼睛瞎耳朵
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:29
专家分:123
注 册:2010-9-24
收藏
得分:0 
#include<iostream>
using namespace std;

int DayOfMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

class Date
{
private:
    int year;
    int month;
    int day;
public:
    Date(int y=0,int m=0,int d=0):year(y),month(m),day(d){}

    void Print()
    {
        cout<<"日期为:"<<"\t";
        cout<<this->year<<"年"<<this->month<<"月"<<this->day<<"日"<<endl;
    }

    void SetDate(int y1,int m1,int d1)
    {
        year=y1;
        month=m1;
        day=d1;
    }

    int LeapYear()
    {
        return ( (this->year %4==0)&&(this->year %100!=0) || (this->year %400==0) );
    }

    void AddDay()
    {
        if( LeapYear() )
        {
            DayOfMonth[2]=29;
        }

        ++day;

        if(day>DayOfMonth[this->month])
        {
            ++month;
            day=1;
        }

        if(month>12)
        {
            ++year;
            month=1;
        }
    }
};

int main()
{
    Date d;
    d.SetDate (2010,12,31);
    cout<<"没加天数前的时间为:"<<"\t";
    d.Print ();
    d.AddDay();
    cout<<"加天数后的时间为:"<<"\t";
    d.Print();
}
2010-10-22 21:57
lurrass
Rank: 1
等 级:新手上路
帖 子:17
专家分:1
注 册:2010-9-13
收藏
得分:0 
回复 4楼 聋眼睛瞎耳朵
非常感谢,编的真好、、、、
2010-10-23 11:32
快速回复:两个编程题目:能帮忙解决下吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015679 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved