数据结构课程一项作业
代码风格混乱
#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;
}