黄浩老师cpp平时作业(十一)矩阵乘法&&矩形类&&读写文件与结构体

“读写文件与结构体”这道题被我跳过去了

理由:它是个压缩包,第一次整体写题的时候按word写把它给忘了;它太麻烦了码量太大现在没时间(懒,得,理,直,气,壮)

//后记 诶呀,我在翻老师课件的时候发现这是道老师作为讲结构体文件读写的原题,也就是说tas上有完整答案

//也就是说以下是tas上的完整代码

#include <iostream>
#include <sstream> 
#include <fstream>
#include <iomanip>
using namespace std;
struct Student{
    	int id;
    	string name;
    	float regularScore;
    	float finalScore;
    	float overallScore;
	};

void bubbleSortScore(Student stu[], int len){
  for (int i=0; i<len-1; i++) {    
    for (int j=0; j<len-i-1 ; j++)
	 if (stu[j].overallScore<stu[j+1].overallScore)
	{     Student temp;
	      temp = stu[j];
	      stu[j] = stu[j+1];
	      stu[j+1] = temp;
	}	
  } 
} 

void bubbleSortId(Student stu[], int len){
  for (int i=0; i<len-1; i++) {    
    for (int j=0; j<len-i-1 ; j++)
	 if (stu[j].id>stu[j+1].id)
	{     Student temp;
	      temp = stu[j];
	      stu[j] = stu[j+1];
	      stu[j+1] = temp;
	}	
  } 
} 
	
void split(Student *s, string str){	
	istringstream istr(str);
	istr>>s->id;
	istr>>s->name;
	istr>>s->regularScore;
	istr>>s->finalScore;
	istr.clear();
}

void scoreCalculate(Student stu[], int len){
	for(int i=0; i<len; i++){
		stu[i].overallScore = stu[i].regularScore * 0.4 + stu[i].finalScore * 0.6;
	}
}

void outputFlie(Student stu[], int len){
	fstream outFile("result.txt", ios::out);
	outFile<<"学号	姓名	平时	期末	总评"<<endl;
	cout<<setiosflags(ios::fixed)<<setprecision(2);	
	for(int r=0; r<len; r++){
		outFile<<stu[r].id<<"	"<<stu[r].name<<"	"<<stu[r].regularScore<<"	"<<stu[r].finalScore<<"	"<<stu[r].overallScore<<endl;
	}
	outFile.close(); 
}

int main()
{   string s;
    int lines = 0;
    fstream file("example.txt",ios::in);
    if(!file){ 
	    cout<<"打开文件失败! 文件:"<<"example.txt"<<endl; 
	    return 0; 
	}
	getline(file,s); //读第一行,标题行	
	while(getline(file,s)){
		lines++;
	}
    file.close();

	Student stuList[lines];
	int j = 0;
	fstream file2("example.txt",ios::in);
	if(!file2){ 
	    cout<<"打开文件失败! 文件:"<<"example.txt"<<endl; 
	    return 0; 
	} 
	getline(file2,s); //读第一行,标题行 
	while(getline(file2,s)){
		Student *stu = new Student();
		split(stu, s);
		stuList[j] = *stu;
		j++;
	}
	file2.close();
	
	scoreCalculate(stuList, lines);
	
	cout<<"Unsorted:"<<endl;
	cout<<setiosflags(ios::fixed)<<setprecision(2);	
	for(int r=0; r<lines; r++){
		cout<<stuList[r].id<<"	"<<stuList[r].name<<"	"<<stuList[r].regularScore<<"	"<<stuList[r].finalScore<<"	"<<stuList[r].overallScore<<endl;
	}
	bubbleSortId(stuList, lines);
	cout<<"Sorted by Id:"<<endl;
	for(int r=0; r<lines; r++){
		cout<<stuList[r].id<<"	"<<stuList[r].name<<"	"<<stuList[r].regularScore<<"	"<<stuList[r].finalScore<<"	"<<stuList[r].overallScore<<endl;
	}	
	bubbleSortScore(stuList, lines);
	cout<<"Sorted by score:"<<endl;
	for(int r=0; r<lines; r++){
		cout<<stuList[r].id<<"	"<<stuList[r].name<<"	"<<stuList[r].regularScore<<"	"<<stuList[r].finalScore<<"	"<<stuList[r].overallScore<<endl;
	}
    outputFlie(stuList, lines);
}

//by YewLi
#include <iostream> 
#include<cstdlib>
#include<ctime>
#include<cstring>
using namespace std; 
void arrayMultiply(int (*pA)[4], int (*pB)[2], int (*pC)[2]); 
void printArray(int (*pC)[2]);
int main() { 
int A[3][4],B[4][2],C[3][2];
memset(C,0,sizeof(C));
arrayMultiply(A,B,C);
printArray(C);
} 
void arrayMultiply(int (*pA)[4], int (*pB)[2], int (*pC)[2]){
	//initializtion
	srand(time(NULL));
	for(int i = 0;i < 3;i++)
		for(int j = 0;j < 4;j++)
			pA[i][j] = rand();
	for(int i = 0;i < 4;i++)
		for(int j = 0;j < 2;j++)
			pB[i][j] = rand();
		
	for(int i = 0;i < 3;i++)
		for(int j = 0;j < 2;j++)
			for(int k = 0;k < 4;k++)
				pC[i][j] += pA[i][k] * pB[k][j];
			
} 
void printArray(int (*pC)[2]){
	for(int i = 0;i < 3;i++){
		for(int j = 0;j < 2;j++)
			cout<<pC[i][j]<<" ";
		cout<<endl;
	}
}

网上百度一下矩阵乘法原理,然后写就vans,无内鬼


//by 姚皓誉
#include <iostream>
#include <cmath>
using namespace std;
class Point{
	public:
    	double x,y;
    	//在此定义构造函数
    	Point():x(0),y(0){};
    	Point(double x,double y):x(x),y(y){};
    	//在屏幕上打印Point对象的函数 
    	void printInfo(){
			cout<<"x:"<<x<<"  y:"<<y<<endl;
		}
    	//计算这个点到另外一个点的距离的函数 
    	double dis(const Point &a){
			return sqrt((this->x-a.x)*(this->x-a.x)+(this->y-a.y)*(this->y-a.y));
		}
    	//移动点到一个新的位置的函数 
    	Point move(double x,double y){
			this->x+=x,this->y+=y;
			return *this;
		}
};
class Rectangle{
	protected:
    	Point location; //左上角的坐标
    	Point lwc;
	    double width;   //矩形宽度 
    	double height;  //矩形高度
	public:
    	//构造函数
    	Rectangle():location(),width(0),height(0),lwc(){}
    	Rectangle(double x,double y,double width,double height):location(x,y),width(width),height(height),lwc(x+width,y+height){};
    	Rectangle(double width,double height):location(),width(width),height(height),lwc(width,height){};
    	void check(){
			lwc.x=location.x+width,lwc.y=location.y+height;
		}
	    //移动矩形位置的函数 
	    void move(double x,double y){
			location.move(x,y);
			check();
		}
    	//改变矩形大小的函数
    	
	    //计算矩形面积的函数
	    double area(){
			return width*height;
		}
    	//计算矩形周长的函数
    	double perimeter(){
			return 2*(width+height);
		}
	    //判断一个点是否在矩形内部,有2个重载函数 
    	bool isInside(const Point& p){
			if (p.x<location.x||p.x>lwc.x) return false;
			if (p.y<location.y||p.y>lwc.y) return false;
	    } 
    	bool isInside(double x,double y){
			return isInside(Point(x,y));
	    }
    	//求这个矩形和另外一个矩形的交集
	    //返回一个矩形对象,如果不相交,返回NULL
	    Rectangle intersection(Rectangle &other){ 
			if (this->area()<other.area()) return other.intersection(*this);
			Point temp=other.location;
			if (isInside(temp)||isInside(temp.move(width,0))||isInside(temp.move(-width,height))||isInside(temp.move(width,0))){
				double x1=location.x>other.location.x?location.x:other.location.x;
				double y1=location.y>other.location.y?location.y:other.location.y;
				double x2=lwc.x<other.lwc.x?lwc.x:other.lwc.x;
				double y2=lwc.y<other.lwc.y?lwc.y:other.lwc.y;
				return Rectangle(x1,y1,x2,y2);
			}
		}
    	//求这个个矩形和另外一个矩形的并集
	    Rectangle unionWith(const Rectangle &other){
			double x1=location.x<other.location.x?location.x:other.location.x;
			double y1=location.y<other.location.y?location.y:other.location.y;
			double x2=lwc.x>other.lwc.x?lwc.x:other.lwc.x;
			double y2=lwc.y>other.lwc.y?lwc.y:other.lwc.y;
			return Rectangle(x1,y1,x2,y2);
    	} 
	    //在屏幕上输出矩形信息的函数
    	void Print(){
			location.printInfo();
			cout<<"width:"<<width<<"   height:"<<height<<endl<<endl;
		}
};


int main(){
    //定义多个矩形对象和Point对象  //测试每一个成员函数 
    return 0; 
} 

姚皓誉大佬赞助的,不是我写的

然后我们都太懒都没有调试,然后你就会发现主函数是空的QAQ

问题不大,贴了跑路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值