1. 程式人生 > >C++ 實驗三

C++ 實驗三

//一個班有 5 名學生,每個學生修了五門課,
//求每個學生的平均成績,並輸出每個學生的學號,
//每門課程的成績及平均值。

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#define WIDTH 15

struct stu
{
	string  number;
	double math, english, cpp, analysis, eda;
	double aver;
};

void getInfo(struct stu ssta[5]);
void putRes(struct stu ssta[5]);
void operationOne(struct stu ssta[5]);
void operationTwo(struct stu *ssta);

int main(void)
{
	stu student[5];
	getInfo(student);
	operationOne(student);
	putRes(student);
	cout << '\n' << '\n' << endl;
	operationOne(student);
	putRes(student);

	return 0;
}

void getInfo(struct stu ssta[5])
{
	for (int i = 0; i < 5; ++i)
	{
		cout << "plz input student number: ";
		cin >> ssta[i].number;
		cout << endl;
		cout << "plz input the grade" << endl;
		cout << "Math: ";
		cin >> ssta[i].math;
		cout << endl << "English: ";
		cin >> ssta[i].english;
		cout << endl << "Cpp: ";
		cin >> ssta[i].cpp;
		cout << endl << "Analysis: ";
		cin >> ssta[i].analysis;
		cout << endl << "EDA: ";
		cin >> ssta[i].eda;
	}
}

void putRes(struct stu ssta[5])
{
	cout << setprecision(2);  
	cout << setiosflags(ios::fixed);  
	cout << setiosflags(ios::right); 
	for (int i = 0; i < 5; ++i)
	{
		cout << "Number      Math      English        Cpp          Analysis           EDA         Average" << endl;
		cout << ssta[i].number;
		cout << setw(WIDTH) << ssta[i].math << setw(WIDTH) << ssta[i].english << setw(WIDTH) << ssta[i].cpp;
		cout << setw(WIDTH) << ssta[i].analysis << setw(WIDTH) << ssta[i].eda << setw(WIDTH) << ssta[i].aver << endl; 
	}
}

void operationOne(struct stu ssta[5])
{
	for (int i = 0; i < 5; ++i)
	{
		ssta[i].aver = (ssta[i].math + ssta[i].english + ssta[i].cpp + ssta[i].analysis + ssta[i].eda) / 5;
	}
}
void operationTwo(struct stu *ssta)
{
	for (int i = 0; i < 5; ++i)
	{
		(ssta + i)->aver = ((ssta + i)->math + (ssta + i)->english + (ssta + i)->cpp
				 + (ssta + i)->analysis + (ssta + i)->eda) / 5;
	}
}

//任意矩陣加法計算與乘法計算;

#include "stdafx.h"
#include <stdlib.h>
#include <iomanip>
#include <iostream>
using namespace std;

int   *getMatrix(int row, int column);
void putMatrix(int *ptr, int row, int column);
void clearMatrix(int *ptr, int row, int column); 
void addMatrix(int * a, int * b, int * c, int row, int column);
void multiplyMatrix(int * a, int * b, int * c, int row, int column);

int main(void)
{
	int * aMatrix, * bMatrix;
	int aRowsize, aColumnsize;
	int bRowsize, bColumnsize;
	cout << "plz input your rowsize & column of aMatrix: " << endl;
	cin >> aRowsize >> aColumnsize;
	cout << "plz input your rowsize & column of bMatrix: " << endl;
	cin >> bRowsize >> bColumnsize;
	cout << "plz input your first matrix: " << endl;
	aMatrix = getMatrix(aRowsize, aColumnsize);
	cout << "plz input your second matrix: " << endl;
	bMatrix = getMatrix(bRowsize, bColumnsize);

	if((aRowsize == bRowsize) && (aColumnsize == bColumnsize))
	{
		int * cMatrix = (int *) malloc(sizeof(int) * aRowsize * aColumnsize);
		addMatrix(aMatrix, bMatrix, cMatrix, aRowsize, aColumnsize);
		cout << "the result of adding is: " << endl;
		putMatrix(cMatrix, aRowsize, aColumnsize);
		free(cMatrix);
	}
	else
	{
		cout << "fatal error: cannot add two matrix!" << endl;
	}

	if(aColumnsize == bRowsize)
	{
		int * cMatrix = (int *) malloc(sizeof(int) * aRowsize * bColumnsize);
		clearMatrix(cMatrix, aColumnsize, bRowsize);
		multiplyMatrix(aMatrix, bMatrix, cMatrix, bRowsize, aColumnsize);
		cout << "the result of multiplying is: " << endl;
		putMatrix(cMatrix, aRowsize, bColumnsize);
		free(cMatrix);
	}
	else
	{
		cout << "fatal error: cannot multiply two matrix!" << endl;
	}

	cout << "end~" << endl;
	
	free(aMatrix);
	free(bMatrix);

	return 0;
}

int * getMatrix(int row, int column)
{
	int * ptr = (int *) malloc(sizeof(int) * row * column);
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			scanf("%d", ptr + i * column + j);
		}
	}

	return ptr;
}

void putMatrix(int *ptr, int row, int column)
{
	cout << setiosflags(ios::fixed);  
	cout << setiosflags(ios::left); 
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			cout << setw(5) << *(ptr + i * column + j);
		}
		cout << endl;
	}
}

void clearMatrix(int *ptr, int row, int column)
{
	for (int ctr = 0; ctr < (row+1) * (column+1); ++ctr)
	{
		*(ptr + ctr) = 0;
	}
}

void addMatrix(int * a, int * b, int * c, int row, int column)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			*(c + i * column + j) = *(a + i * column + j) + *(b + i * column + j);
		}
	}
}

void multiplyMatrix(int * a, int * b, int * c, int row, int column)
{
	int count = 0;

	for (int i = 0; i <= row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			count = 0;
			while((count < row) && (count < column))
			{
				*(c + i * column + j) += *(a + i * row + count) * *(b + count * column + j);
				count ++;
			}
		}
	}
}

話說輸出格式真的不做了(摔!

第二個問題拓展為任意矩陣的計算:

使用指標實現由使用者指定任意大小矩陣的動態建立

並使用一下函式實現關於矩陣運算的具體功能。

int   *getMatrix(int row, int column);
void putMatrix(int *ptr, int row, int column);
void clearMatrix(int *ptr, int row, int column); 
void addMatrix(int * a, int * b, int * c, int row, int column);
void multiplyMatrix(int * a, int * b, int * c, int row, int column);
使用指標時格外麻煩的一點是元素的對映關係, 不小心就會指向奇怪的地方很是頭疼.

關於本次實驗使用了C中可愛的malloc()&free()函式(才不會說new和delete總是調不出來)

至於資料輸入部分使用scanf()函式的原因, 到底cin這個傢伙讀的果然還是地址嗎...

在今後的實驗中要多多使用具有C++新特性的方法, 如此希望著.

以上.