求以下问题的完整代码,要求使用c++面向对象的程序设计和构造函数方法
运行结果:
代码:
#include <iostream>
using namespace std;
class Trans
{
private:
int mNmb;
public:
Trans(int n){mNmb = n;}
void toNjz(int b)
{
int a[20],n=0;
int t = mNmb;
if(b == 10)
{
cout << mNmb<<endl;
return ;
}
while(t)
{
a[n] = t%b;
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>
#include <string>
#include <vector>
#include <algorithm>
class Converter {
private:
int _val;
public:
Converter(int val) : _val(val) {}
std::string ToBaseN(int n) {
std::string result;
std::vector<int> baseN;
while (_val > 0) {
baseN.push_back(_val % n);
_val /= n;
}
std::reverse(baseN.begin(), baseN.end());
for (int i : baseN) {
result += std::to_string(i);
}
return result.empty() ? "0" : result;
}
};
int main() {
int n;
std::cin >> n;
while (n--) {
int a, b;
std::cin >> a >> b;
Converter c(a);
std::cout << c.ToBaseN(b) << std::endl;
}
return 0;
}
#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