求以下问题的完整代码

求以下问题的完整代码,要求使用c++面向对象的程序设计和构造函数方法

img

前两个题目的代码完善一下就可以了。
(1)这个代码是先输入组数,然后输入两个整数的代码。无需输入组数的代码在评论区。
运行结果:

img

代码:

#include <iostream>
using namespace std;

class Trans
{
private:
    int mNmb;
public:
    Trans(int n){mNmb = n;}
    void toNjz(int b)
    {
        char a[40],n=0;
        int t = mNmb;
        if(t==0)
        {
            cout <<"0"<<endl;
            return ;
        }
        while(t)
        {
            int x = t%b;
            if(x<10)
                a[n] = '0'+x;
            else
                a[n] = 'A'+(x-10);
            
            n++;
            t/=b;
        }
        //输出
        for(n--;n>=0;n--)
            cout << a[n];
        cout << endl;
    }

};

int main()
{
    int n;
    int a,b;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> a >> b;
        Trans t(a);
        t.toNjz(b); //a转b进制并输出
    }
    return 0;
}

在类里封装一个函数,转换后的数据放到字符串里

#include <iostream>
using namespace std;
#include <string>
class BaseTrans
{
private:
    int N;
    int R;
    string trans;
    char NTrans(int N,int R)
    {
        char c;
        if(R>10 && N>=10)
        {
            c = N-10+'A';
        }
        else
            c = N+'0';
        return c;
    }
    void Trans(int N,int R)
    {
        if(N<R)
        {
            trans += NTrans(N,R);
            return;
        }
        Trans(N/R,R);
        trans += NTrans(N%R,R);
    }
public:
    BaseTrans() {}
    BaseTrans(int N,int R) : N(N),R(R) {Trans(N,R);}
    string GetTrans() {return trans;}
};

int main()
{
    int N,R;
    while(scanf("%d%d",&N,&R) != EOF)
    {
        BaseTrans bt(N,R);
        cout<<bt.GetTrans()<<endl;
    }
    system("pause");
}

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7662771
  • 这篇博客也不错, 你可以看下基于邻接矩阵存储的图的广度优先搜索遍历算法的实现(附C++实现代码)
  • 除此之外, 这篇博客: 数据结构-C++下的稀疏矩阵中的 完整代码(计算转置矩阵和矩阵相加) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #ifndef SPARSEMATRIX_H
    #define SPARSEMATRIX_H
    template <class T>
    class SparseMatrix;
    template <class T>
    struct Item
    {
        int col;
        friend class SparseMatrix<T>;
        int row;
        T val;
    };
    #include <iostream>
    using namespace std;
    template <class T>
    class SparseMatrix
    {
    public:
        SparseMatrix(int _maxSize=10)
        {
            if(_maxSize<1)
            {
                throw std::runtime_error(std::string("count error"));
            }
            max_size = _maxSize;
            aItms = new Item<T>[max_size];
            item_count = rows = cols = 0;
        }
        ~SparseMatrix()
        {
            delete []aItms;
        }
        /**
         * @brief Transpose
         * @param b
         * 存储this的转置矩阵到B中
         */
        void Transpose(SparseMatrix<T> &b) const
        {
            if(b.max_size<this->max_size)
            {
                throw std::runtime_error(std::string("count error"));
            }
            /**
              * 正常的矩阵转置后行列数互换
              *
              */
            b.cols = this->rows;
            b.rows = this->cols;
            b.item_count = this->item_count;
            delete []b.aItms;
            int count = 0;
            /**
              * 转置的原理为 行列互换
              * 所有新生成的稀疏矩阵的记录应根据原矩阵纵向非0的排列为转置后的横向顺序 并切换其col/row
              * 所以从第一列开始取原非0值
              *
              */
            for(int iCol=1;iCol<=this->cols;iCol++)
            {
                for(int index=0;index<this->item_count;index++)
                {
                    if(this->aItms[index].col == iCol){
                        b.aItms[count].val = this->aItms[index].val;
                        b.aItms[count].col = this->aItms[index].row;
                        b.aItms[count].row = this->aItms[index].col;
                        count++;
                    }
    
                }
            }
    
        }
        //添加一个非0值
        void Append(const Item<T>& t)
        {
            if(item_count>=max_size)
            {
                throw std::runtime_error(std::string("size error"));
            }
            aItms[item_count++] = t;
        }
        /**
         * @brief Add
         * @param b
         * @return
         * 稀疏矩阵相加
         */
        SparseMatrix<T> Add(const SparseMatrix<T> &b) const
        {
            if(b.cols!=this->cols||b.rows!=this->rows)
            {
                throw std::runtime_error(std::string("col and row error"));
            }
    
            SparseMatrix<T> a(this->max_size);
            a.cols = this->cols;
            a.rows = this->rows;
            a.item_count = 0;
            int count_t = 0;//this的数组下标
            int count_b = 0;//b的数组下标
            while (count_b<b.item_count&&count_t<this->item_count) {
                //计算他们在B和this中的位置
                int idx_b = b.aItms[count_b].row*b.cols+b.aItms[count_b].col;
                int idx_t = this->aItms[count_t].row*this->cols+this->aItms[count_t].col;
    
                if(idx_b<idx_t)
                {
                    a.Append(b.aItms[count_b]);
                    count_b++;
                }
                else if(idx_b==idx_t)//两个在同一个位置
                {
                    //判断是否为0;
                    if(b.aItms[count_b]+this->aItms[count_t]!=0)
                    {
                        Item<T> t;
                        t.col = b.aItms[count_b].col;
                        t.row = b.aItms[count_b].row;
                        t.val = b.aItms[count_b].val+this->aItms[count_t].val;
                        a.Append(t);
                        //同时增加索引
                        count_b++;
                        count_t++;
                    }
    
                }
                else
                {
                    a.Append(this->aItms[count_t]);
                    count_t++;
                }
            }
            //添加没有加进去的
            for(;count_b<b.item_count;count_b++)
            {
                a.Append(b.aItms[count_b]);
            }
            for(;count_t<this->item_count;count_t++)
            {
                a.Append(this->aItms[count_t]);
            }
            return a;
        }
        friend istream& operator>>(istream& input,SparseMatrix<T> &x)
        {
            cout<<"please input rows,cols,item_count:";
            input>>x.rows>>x.cols>>x.item_count;
            if(x.item_count>x.max_size||x.rows*x.cols<x.max_size)
            {
                throw runtime_error(string("size out "));
            }
            for(int i=0;i<x.item_count;i++)
            {
                cout<<"please input"<<i<< "row,col,val:";
                input>>x.aItms[i].row>>x.aItms[i].col>>x.aItms[i].val;
                while(x.aItms[i].row>x.rows||x.aItms[i].col>x.cols)
                {
                    cout<<"col,row error!please input"<<i<< "row,col,val again:";
                    input>>x.aItms[i].row>>x.aItms[i].col>>x.aItms[i].val;
                }
            }
            return input;
        }
        /**
         * @brief operator <<
         * @param output
         * @param x
         * @return
         */
        friend ostream& operator<<(ostream& output,const SparseMatrix<T> &x)
        {
            for(int i=0;i<x.item_count;i++)
            {
                output<<x.aItms[i].row<<","<<x.aItms[i].col<<":"<<x.aItms[i].val<<endl;
            }
            output<<endl;
            for(int i=1;i<=x.rows;i++)
            {
                for(int j=1;j<=x.cols;j++)
                {
                    int n_index=-1;
                    for(int n=0;n<x.item_count;n++)
                    {
                        if(x.aItms[n].col!=j||x.aItms[n].row!=i)
                        {
    
                            //b=false;
                        }
                        else
                        {
                            n_index = n;
                            break;
    
                        }
                    }
                    if(n_index>=0)
                    {
                        output<<x.aItms[n_index].val<<" ";
                    }
                    else
                    {
                        output<<0<<" ";
                    }
                }
                output<<endl;
            }
            return output;
        }
    private:
        int cols;
        int rows;
        int item_count;
        Item<T>* aItms;
        int max_size;
    };
    #endif // SPARSEMATRIX_H
    
    
  • 您还可以看一下 董洪伟老师的算法设计与分析课程中的 分治递归求平面点集的最近点对的C++代码实现小节, 巩固相关知识点
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632