[C++ 写一个函数将十进制数转换为2到9的任意进制数 练习list容器]

1、为list重载operator<<。
2、定义一个list对象 lst,元素值为{0,1,2,3,4,5,6,7,8,9},显示结果。
3、push_back一个5,显示结果,调用remove算法删除所有的5,显示结果。
4、调用reverse算法反转lst,显示结果。
5、对lst升序排列,显示结果。
6、定义一个deque对象d,使用lst初始化,显示结果。push_front一个
7、定义一个priority_queue,使用d初始化,push一个5。输出top、pop
8、定义一个queue对象,验证queue的基本操作。size、empty、push、pop、front、back等。
9、写一函数string DecToOther( unsigned int dec , unsigned int n),将十进制数转换为2到9的任意进制数,函数返回转换结果,dec为十进制数,n为2~9之一。并编写测试程序。

#include <iostream>
#include <list>
#include <iterator>
#include <deque>
#include <queue>
using namespace std;

//为list<int>重载operator<<。
ostream& operator<<(ostream& os, list<int>::iterator& it)
{
    os << *it << "\t";
    return os;
}

//写一函数string  DecToOther( unsigned int dec , unsigned int n),将十进制数转换为2到9的任意进制数,函数返回转换结果,dec为十进制数,n为2~9之一。并编写测试程序。
void dectoother(unsigned int dec, unsigned int n_num)
{
    int effect = 0;
    int i = dec;
    vector<int> q_dec;

    while (i != 0)
    {
        effect = i % n_num;
        if (i >= n_num)
            i = i / n_num;
        else
        {
            q_dec.push_back(effect);
            break;
        }
        // cout << j << endl;
        q_dec.push_back(effect);
    }

    int nSize = q_dec.size();
    cout << dec << " 的 " << n_num << "进制" <<  " 为 : ";
    for (int i = nSize - 1; i >= 0; --i)
    {
        int &nValue = q_dec[i];
        cout << nValue;
    }
    cout << endl;
}

int main()
{

    // 定义一个list对象  lst,元素值为{0,1,2,3,4,5,6,7,8,9},显示结果。
    cout << "定义一个list对象  lst,元素值为{0,1,2,3,4,5,6,7,8,9},显示结果" << endl << endl;
    list<int>lst;
    int i = 0;
    for (i = 0; i < 10 ; ++i)
    {
        lst.push_back(i);
    }
    list<int>::iterator ite;
    for (ite = lst.begin(); ite != lst.end(); ++ite)
    {
        cout << *ite << "\t";
    }

    //push_back一个5,显示结果,调用remove算法删除所有的5,显示结果。
    cout << "push_back一个5,显示结果,调用remove算法删除所有的5,显示结果" << endl << endl;
    lst.push_back(5);
    for (ite = lst.begin(); ite != lst.end(); ++ite)
    {
        cout << *ite << "\t";
    }
    cout << endl;
    lst.remove(5);
    for (ite = lst.begin(); ite != lst.end(); ++ite)
    {
        cout << *ite << "\t";
    }
    cout << endl;

    //调用reverse算法反转lst,显示结果。
    cout << "调用reverse算法反转lst,显示结果" << endl << endl;

    list<int>::reverse_iterator rite = lst.rbegin();
    for (; rite != lst.rend(); ++rite)
    {
        cout << *rite << "\t";
    }
    cout << endl;

    //5对lst升序排列,显示结果。
    cout << "对lst升序排列,显示结果" << endl << endl;

    lst.sort();
    for (ite = lst.begin(); ite != lst.end(); ++ite)
    {
        cout << *ite << "\t";
    }
    cout << endl;

    //6定义一个deque对象d,使用lst初始化,显示结果。push_front一个-1,显示结果。
    cout << "定义一个deque对象d,使用lst初始化,显示结果。push_front一个-1,显示结果" << endl << endl;
    deque<int>d(lst.begin(), lst.end());
    deque<int>::iterator it = d.begin();
    for (; it != d.end(); ++it)
    {
        cout << *it << "\t";
    }
    cout << endl;
    d.push_front(-1);
    for (i = 0; i<d.size(); ++i)
    {
        cout << d.at(i) << "\t";
    }
    cout<<endl;

    //7定义一个priority_queue,使用d初始化,push一个5。输出top、pop 这两步重复5次。
    cout << "定义一个priority_queue,使用d初始化,push一个5。输出top、pop 这两步重复5次" << endl << endl;
    priority_queue<int>pr(d.begin(), d.end());
    pr.push(5);
    for (i = 1; i <= 5; ++i)
    {
        cout << pr.top() << "\t";
        pr.pop();
    }
    cout << endl;

    //8定义一个queue对象,验证queue的基本操作。size、empty、push、pop、front、back等。
    cout << "定义一个queue对象,验证queue的基本操作。size、empty、push、pop、front、back等" << endl << endl;
    queue<int>q;

    for (i = 1; i <= 10; ++i)
    {
        q.push(i);
    }
    cout << q.size() << "\t" << q.front() << "\t" << q.back() << "\t";
    while (!q.empty())
    {
        for (i = 0; i<q.size(); ++i)
        {
            q.pop();
        }
    }
    cout << endl << endl;

    dectoother(2, 2);
    dectoother(3, 2);
    dectoother(4, 2);
    dectoother(5, 2);
    dectoother(16, 8);
    dectoother(14, 3);

    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值