模板实现二维数组

数据结构课程一项作业

代码风格混乱

#include<cstdio>
#include<cstdlib>
#include<unordered_set>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
typedef class student{
	public:
		bool flagexist;
		string name;
		int id,score;
		student():name("None"){id=0;score=0;flagexist=false;}
		bool operator == (const  student &s2)
		{
			if(this->id==s2.id&&this->score==s2.score&&this->name==s2.name&&this->flagexist&&s2.flagexist)
				return true;
			else return false;	
		}
		friend istream & operator >> (istream & in ,student &obj)
		{
			obj.flagexist = true;
			cout<<"请输入学生名:";
			in>>obj.name;
			cout<<"请输入学号:";
			in>>obj.id;
			cout<<"请输入总分:";
			in>>obj.score;
		}
		friend ostream & operator << (ostream &out, student &obj)
		{
			out<<"学生名:"<<obj.name<<"\n学号为:"<<obj.id<<"\n总分为:"<<obj.score<<"\n";
			return out; 
		}
		~student(){}
}Stu;
template<typename T>
class Array{
	public:
		int size;
		int row,col;
		T *val;
		void printoffset(int i)
		{
			cout<<*(val+i)<<" ";
		}
		Array(){}
		Array(const Array &t)		//拷贝构造函数 
		{
			size=t.size;
			row=t.row;
			col=t.col;
			val=(T *)new T[row*col];
			for(int i=0;i<row;i++)
				for(int j=0;j<col;j++)
				*(val+i*col+j)=*(t.val+i*col+j);
							
		}
		Array(int n,int m):val()
		{
			row=n;
			col=m;
			val=(T *)new T[n*m];
			
		}
		T * operator [](int i)
		{
			return (val+i*col);
		}
		void check();
		~Array()				//释放空间 
		{
			delete []val;	
		}
		void print();
		void updateonearr(int,int);
};
template<typename T>
void  Array<T>::check()
{
	cout<<"重复的元素有:";
	for(int i=0;i<row*col;i++)
	{
		bool flag=1;
		for(int j=i+1;j<row*col;j++)
		{
			if(*(val+i)==*(val+j))
			{
				flag=false;			
			}				
		}
		
		if(flag==false)
			printoffset(i);
	}

}
template<typename T>
void Array<T>::print()
{
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<col;j++)
		{
			cout<<*(val+i*col+j)<<" ";
		}
		cout<<endl;
	}
}
template<typename T>
void printonearr(int i,int j,Array<T> &arr)
{
	if(i<0||i>arr.row||j<0||j>arr.col)		//防止访问越界 
	{
		cout<<"访问越界"<<endl;
		return;
	}
	cout<<arr[i][j]<<" ";
}
template<typename T>
void Array<T>::updateonearr(int i,int j)
{
	if(i<0||i>row||j<0||j>col)		//防止访问越界 
	{
		cout<<"访问越界"<<endl;
		return;
	}
	cin>>*(val+i*col+j);
}
void printmenu()
{
	cout<<"\n1.输出整个数组\n2.访问某个元素\n3.统计是否具有数值相同的元素\n4.根据i.j赋值\n5.退出程序\n"<<endl;
	cout<<"请选择:"<<endl;
}
int main(){
	int n,m,choose,total=0,temp1,temp2,tempnum;


	cout<<"int类型二维数组:\n";
	cout<<"(0,0)表示第一个元素\n请分别输入数组行数与列数:"; 
	scanf("%d%d",&n,&m);
	Array <int> arrnum(n,m);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			arrnum[i][j]=total;
			total++;
		}
	}
	while(1)
	{
		printmenu();
		cin>>choose;
		if(choose==1)
		{
			arrnum.print();
		}
		else if(choose==2)
		{
			cout<<"请输入i j(以空格分隔)";
			cin>>temp1>>temp2;
			cout<<"元素为:"<<arrnum[temp1][temp2]<<endl;
		}
		else if(choose==3)
		{
			arrnum.check();
		}
		else if(choose==4)
		{
			cout<<"请输入i j(以空格分隔)";
			cin>>temp1>>temp2;
			cout<<"请输入需要赋的值:";
			arrnum.updateonearr(temp1,temp2);
		}
		else if(choose==5) 
		{
			break;
		}
	}
	
	cout<<"student类型二维数组:\n";
	cout<<"请分别输入数组行数与列数:"; 
	scanf("%d%d",&n,&m);
	Array<Stu> Arrstu(n,m);
	cout<<"请先初始化5个学生元素\n";
	cin>>Arrstu[0][0]>>Arrstu[0][1]>>Arrstu[0][2]>>Arrstu[1][0]>>Arrstu[1][1];
	while(1)
	{
		printmenu();
		cin>>choose;
		if(choose==1)
		{
			Arrstu.print();
		}
		else if(choose==2)
		{
			cout<<"请输入i j(以空格分隔)";
			cin>>temp1>>temp2;
			cout<<"元素为:"<<Arrstu[temp1][temp2]<<endl;
		}
		else if(choose==3)
		{
			Arrstu.check();
		}
		else if(choose==4)
		{
			cout<<"请输入i j(以空格分隔)";
			cin>>temp1>>temp2;
			cout<<"请输入需要赋的值:";
			Arrstu.updateonearr(temp1,temp2);
		}
		else if(choose==5) 
		{
			break;
		}
	}
	return 0;
} 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现二维数组的表格,可以使用el-table组件的嵌套表头功能,并根据二维数组的结构动态生成表格列和数据。 首先,需要定义二维数组数据结构,例如: ```javascript data() { return { tableData: [ ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'] ] } } ``` 然后,在模板中使用el-table组件,并使用v-for指令动态生成表头和表格数据: ```html <el-table :data="tableData"> <el-table-column v-for="(row, rowIndex) in tableData" :key="rowIndex" label="Row"> <template slot-scope="{ row }"> <el-table-column v-for="(cell, cellIndex) in row" :key="cellIndex" :label="'Column ' + (cellIndex + 1)"> {{ cell }} </el-table-column> </template> </el-table-column> </el-table> ``` 在这个例子中,我们使用v-for指令遍历二维数组的每一行,并使用el-table-column动态生成每一行的表头。然后,再在每一行的slot-scope中使用v-for指令遍历该行的每一个单元格,并使用el-table-column动态生成每一列的表头和数据。 最终效果就是一个二维数组的表格,每一行作为一个表头,每一列作为一个表格列,其中的数据来自于二维数组。 完整的代码示例: ```html <template> <el-table :data="tableData"> <el-table-column v-for="(row, rowIndex) in tableData" :key="rowIndex" label="Row"> <template slot-scope="{ row }"> <el-table-column v-for="(cell, cellIndex) in row" :key="cellIndex" :label="'Column ' + (cellIndex + 1)"> {{ cell }} </el-table-column> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'] ] } } } </script> ``` 这样就可以实现一个二维数组的表格,每一行作为表头,每一列作为表格列,并显示相应的数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值