大二上学期期末C++课程设计 发表于 2019-01-12 | 阅读次数: 题目一矩阵运算123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182#include<iostream>using namespace std;class Matrix{private: int a[2][2]; int b[2][2]; int result[2][2];public: Matrix(); void add(); void subtraction(); void multiplication(); void division(); void transpose();};Matrix::Matrix(){ a[2][2]=0; b[2][2]=0; result[2][2]=0; cout<<"请输入矩阵A:"<<endl; int i,j; for (i = 0; i < 2; ++i) for (j = 0; j < 2; ++j) { cin>>a[i][j]; } cout<<"请输入矩阵B:"<<endl; for (i = 0; i < 2; ++i) for (j = 0; j < 2; ++j) { cin>>b[i][j]; } cout<<"你输入的矩阵A为"<<endl; for (i = 0; i < 2; ++i){ for (j = 0; j < 2; ++j) { cout<<a[i][j]<<" "; }cout<<endl; } cout<<endl<<"你输入的矩阵B为"<<endl; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { cout<<b[i][j]<<" "; }cout<<endl; }}void Matrix::add(){ int i,j; cout<<"相加结果为"<<endl; for(i=0;i<2;++i) { for(j=0;j<2;++j) { result[i][j] = a[i][j]+b[i][j]; cout<<result[i][j]<<" "; }cout<<endl; }}void Matrix::subtraction(){ int i,j; cout<<"相加结果为"<<endl; for(i=0;i<2;++i) { for(j=0;j<2;++j) { result[i][j] = a[i][j]-b[i][j]; cout<<result[i][j]<<" "; }cout<<endl; }}void Matrix::multiplication(){ for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { result[i][j] = a[i][j]*b[i][j] + a[i][j]*b[j][i]; cout<<result[i][j]<<" "; }cout<<endl; }}void Matrix::division(){}void Matrix::transpose(){ int i,j; cout<<"A转置为:"<<endl; for(i=0;i<2;++i) { for(j=0;j<2;++j) { result[i][j]=a[j][i]; cout<<result[i][j]<<" "; }cout<<endl; } cout<<"B转置为:"<<endl; for(i=0;i<2;++i) { for(j=0;j<2;++j) { result[i][j]=b[j][i]; cout<<result[i][j]<<" "; }cout<<endl; }}int main(){ int h;cout<<"-------------------"<<endl;cout<<": 菜单 "<<endl;cout<<": 1.加法 "<<endl;cout<<": 2.减法 "<<endl;cout<<": 3.乘法 "<<endl;cout<<": 4.除法 "<<endl;cout<<": 5.转置 "<<endl;cout<<": 0.退出 "<<endl;cout<<"-------------------"<<endl;cout<<"请输入要进行的操作"<<endl;cin >>h;cout<<endl;switch(h){ case 1: { Matrix M; M.add(); break; } case 2: { Matrix M; M.subtraction(); break; } case 3: { Matrix M; M.multiplication(); break; } case 4: { Matrix M; M.division(); break; } case 5: { Matrix M; M.transpose(); break; } case 0: { cout<<"退出成功"; return 0; } default: cout<<"输入错误"<<endl;}} 题目二 求不同图形面积12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#include <bits/stdc++.h>using namespace std;class Graphic{ public: string name; double length; double tall; Graphic(string s,double a,double b=0):name(s),length(a),tall(b){} virtual void show()=0;};class Square:public Graphic{public: Square(double a):Graphic("Square",a) {} void show(){ cout<<"Square'area:"<<length*length<<endl; } // ~Square();};class Circle:public Graphic{public: Circle(double a):Graphic("Circle",a){} void show(){ cout<<"Circle'area:"<<length*length*3.14<<endl; }};class triangle:public Graphic{public: triangle(double a,double b):Graphic("Circle",a,b){} void show(){ //double i //i = cout<<"triangle'area:"<<(length*tall)/2<<endl; }};int main(){ Graphic *gp; string type; // 定义图形的类型 double value; double tall; cout<<"what'type"<<endl; cin>>type; // cout<<"what'value"<<endl; // cin>>value; if(type=="Square") { cout<<"what'value"<<endl; cin>>value; gp = new Square(value); gp ->show(); } if(type=="Circle") { cout<<"what'value"<<endl; cin>>value; gp = new Circle(value); gp ->show(); } if(type=="triangle") { cout<<"what'value"<<endl; cin>>value; cout<<"what'tall"<<endl; cin>>tall; gp = new triangle(value,tall); gp ->show(); }} 题目三 运算符重载12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273#include<iostream>using namespace std;class Complex{public: Complex(double r=0.0,double i=0.0); void print(); friend Complex operator+ (Complex& a,Complex& b); friend Complex operator- (Complex& a,Complex& b); friend Complex operator* (Complex& a,Complex& b); friend Complex operator/ (Complex& a,Complex& b);private: double real; double imag;};Complex::Complex(double r,double i){ real = r; imag = i;}Complex operator+ (Complex& a,Complex& b){ Complex temp; temp.real = a.real + b.real; temp.imag = a.imag + a.imag; return temp;}Complex operator- (Complex& a,Complex& b){ Complex temp; temp.real = a.real - b.real; temp.imag = a.imag - a.imag; return temp;}Complex operator* (Complex& a,Complex& b){ Complex temp; temp.real = a.real * b.real + a.imag * b.imag; temp.imag = a.real * b.imag + a.imag * b.real; return temp;}Complex operator/ (Complex& a,Complex& b){ Complex temp; double t; t = 1/(b.real * b.real + b.imag * b.imag); temp.real = (a.real * b.real + a.imag * b.imag)*t; temp.imag = (b.real * b.imag + a.imag * b.real)*t; return temp;}void Complex::print(){ cout<<real; if(imag>0) cout<<"+"; if(imag!=0) cout<<imag<<'i'<<endl;}int main(){ Complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6; A3 = A1 + A2; A4 = A1 - A2; A5 = A1 * A2; A6 = A1 / A2; A1.print(); A2.print(); A3.print(); A4.print(); A5.print(); A6.print(); return 0;} 学生管理系统123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346#include<iostream>#include<string>#include<iomanip>using namespace std;int num; //总学生人数class student { friend class studentMessage; // 友员函数private: string ID; string name; double score[5]; //1-4代表math、English、OPP和总成绩 ,0号单元空出 student * next;public: student(); string getName(){return name;} string getID(){return ID;} double getMath(){return score[1];} double getEnglesh(){return score[2];} double getOPP(){return score[3];} double gettotol(){return score[4];} student* getNext(){return next;} static void analyse(int ,student * ); //分析成绩 void display(); //打印成绩 void swap(); //交换对象和对象->next的信息};student::student(){ cout<<"请输入学生学号 : ";cin>>ID; cout<<"请输入学生姓名 : ";cin>>name; cout<<"请输入学生的数学、英语和OPP成绩 : "<<endl; int a,b,c; cin>>a; while(a<0||a>100) { cout<<"输入成绩有误,请重新输入: "<<endl; cin>>a; } cin>>b; while(b<0||b>100) { cout<<"输入成绩有误,请重新输入: "<<endl; cin>>b; } cin>>c; while(c<0||c>100) { cout<<"输入成绩有误,请重新输入: "<<endl; cin>>c; } score[1]=a; score[2]=b; score[3]=c; score[4]=a+b+c; next=NULL;}void student::analyse(int a,student * first){ int you=0,liang=0,zhong=0,jige=0,bujige=0; student * t=first; double average=0; //平均分 while(t) { int b = int(t->score[a]/10); switch(b) { case 10: case 9:you++;break; case 8:liang++;break; case 7:zhong++;break; case 6:jige++;break; default:bujige++; } average+=t->score[a]; t=t->next; } average=average/num; cout<<" 考生总人数 : "<<num<<" 平均分 : "<<average<<endl; cout<<"\n 优 良 中 及格 不及格 "<<endl; cout<<" "<<you<<" \t"<<liang<<" \t"<<zhong<<"\t"<<jige<<"\t"<<bujige<<endl;}void student::display(){ cout<<" "<<setw(3)<<ID<<" "<<setw(3)<<name<<" "; for (int i=1;i<=4;i++) { cout<<setw(3)<<score[i]<<" "; } cout<<endl;}void student::swap(){ int j; string na,id; double sco[5]; na=name;name=next->name;next->name=na; id=ID;ID=next->ID;next->ID=id; for(j=1;j<=4;j++) { sco[j]=score[j]; score[j]=next->score[j]; next->score[j]=sco[j]; }}class studentMessage{private: student * first; student * last;public: studentMessage(); student* getFirst(){return first;} student* getLast(){return last;} int getNum(){return num;} void add(); //向表中增加学生成绩记录 void search(); //在表中查找学生成绩记录 void del(); //在表中删除学生成绩记录 void showOne(); //分析一门课的成绩 void showAll(); //输出所有同学所有科目的成绩 void sort(); //按一门课成绩或总成绩排序 void clear(); //清空所有信息并退出系统};studentMessage::studentMessage(){ first=NULL; last=NULL; num=0;}void studentMessage::add(){ student * t =new student; student *p = first; while(p){ if(p->ID==t->ID) { cout<<"\n学号输入错误或该学生成绩已经存在!(如需修改,请先删除再重新录入)"<<endl; return; } p=p->next; } num++; if(first==NULL) { first=last=t; } else { last->next=t; last=last->next; }}void studentMessage::search(){ string a; cout<<"\n请输入要查找的学生的学号:";cin>>a; student *t = first; while(t){ if(t->ID==a) break; t=t->next; } if(!t) { cout<<"\n未找到要查找学生!"<<endl; return; } cout<<"\n查找成功!"<<endl; cout << " 学号 姓名 数学 英语 OPP 总成绩" << endl; t->display(); }void studentMessage::del() { string a; cout<<"\n请输入要删除的学生的学号: ";cin>>a; student *t = first; student *p=NULL; while(t){ if(t->ID==a) break; p=t; t=t->next; } if(!t) { cout<<"\n未找到要删除学生!"<<endl; return; } if(!p) { first=first->next; cout<<"\n成功删除学生"<<a<<endl; delete t; } else { p->next=t->next; cout<<"成功删除学生"<<a<<endl; delete t; } num--;}void studentMessage::showOne(){ int a; while(1) { cout<<"\n想要分析哪一门成绩?请输入相应序号(1:math 2:English 3:OPP): ";cin>>a; if(a!=1&&a!=2&&a!=3) cout<<"\n输入序号有误,请重新输入!"<<endl; else break; } cout<<"\n\t成绩分析如下: \n"<<endl; student::analyse(a,first);}void studentMessage::showAll(){ cout << "---------------------成绩列表----------------------"<< endl; cout << " 学号 姓名 数学 英语 OPP 总成绩" << endl; student *t = first; while(t){ t->display(); t=t->next; }}void studentMessage::sort(){ int a,n=0; //n--排名 cout<<"想要按照什么成绩排序?请输入相应序号(1.数学 2.英语 3.OPP 4.总成绩): "; while(1) { cin>>a; if (a!=1&&a!=2&&a!=3&&a!=4) cout<<"\n输入序号有误,请重新输入 : "; else break; } student *t=first; student *p=t; while(p->next) { t=first; while(t->next) { if(t->next->score[a]>=t->score[a]) { t->swap(); } t=t->next; } p=p->next; } t = first; cout<<"\n\t 成绩表如下: "<<endl; cout<<"学号 姓名 成绩 排名"<<endl; while(t){ n++; cout<<" "<<t->getID()<<" \t"<<t->getName()<<" \t"; switch(a) { case 1 :cout<<t->getMath()<<" \t"<<n<<endl;break; case 2 :cout<<t->getEnglesh()<<" \t"<<n<<endl;break; case 3 :cout<<t->getOPP()<<" \t"<<n<<endl;break; case 4 :cout<<t->gettotol()<<" \t"<<n<<endl;break; } t=t->next; }}void studentMessage::clear(){ int x; cout<<"确认要清空所有信息?请输入1或2 (1:确认 2:取消) : ";cin>>x; if (x==2) { cout<<"\n取消清空\n"<<endl; return; } student *t = first; student *p; while(t){ p=t; t=t->next; delete p; } cout<<"\n清空成功!\n"; cout<<"退出成功!\n";}void showMenu(){ cout << " \n"; cout << "===============================\n"; cout << " 学生成绩管理系统\n\n"; cout << " 1.显示所有学生成绩\n"; cout << " 2.添加信息\n"; cout << " 3.查询信息\n"; cout << " 4.删除信息\n"; cout << " 5.成绩排序\n"; cout << " 6.单科成绩分析\n"; cout << " 7.清空所有信息并退出系统\n"; cout << " 0.退出系统\n"; cout << "===============================\n"; cout << " \n";}int main(){ int h; studentMessage stuM=studentMessage(); while(1) { showMenu(); cout << "请输入操作对应的序号 : "; cin >>h; cout<<endl; switch(h) { case 1: stuM.showAll(); break; case 2: stuM.add(); break; case 3: stuM.search(); break; case 4: stuM.del(); break; case 5: stuM.sort(); break; case 6: stuM.showOne(); break; case 7: stuM.clear(); return 0; case 0: cout<<"\n退出成功!";return 0; default:cout<<"\n输入序号有误!请输入正确的序号。"<<endl; } }} 打赏 微信支付 支付宝