1. 程式人生 > >vector,string,標準輸入輸出流,檔案輸入輸出流01(C++)

vector,string,標準輸入輸出流,檔案輸入輸出流01(C++)

按要求編寫程式。
a) 定義一個結構體型別 Student,如下所示:
struct Student
{
int ID; //學號
string name; //姓名
float score[3]; //三門課程成績
};
b) 編寫一個函式,按照上述結構體定義,依次從鍵盤輸入 5 個學生的資訊,建立學生向量 A;
c) 編寫一個函式,將上述向量 A 按照學號 ID 進行增序排序,並將排序後的學生資訊顯示在螢幕上,顯示格式要求如下:
《學號》          《姓名》              《課程 1》              《課程 2》                《課程 3》
10 列,右         10 列,右             3 位有效數字,       3 位有效數字,        3 位有效數字, 
對齊                  對齊                     右對齊                      右對齊                     右對齊

         111                     TOM                       89.0                     91.0                           89.0

         122                     MIKE                     71.5                     93.5                            79.5

        ……

d) 編寫一個函式,刪除向量中姓名為“TOM”的學生資訊,並將處理結束後的向量內容輸出到 D 盤根目錄中的“res.dat”中,格式和 c 中的要求相同;
e) 要求使用流的方法來完成程式流程中所有的資料輸入和輸出操作;要求使用向量來儲存學生資訊。

/*===============================================================================================================
*學號:1527403059
*作業:E04
*功能:(1)定義一個結構體型別
       (2)編寫一個函式,依次從鍵盤輸入5個學生的資訊,建立學生向量A
	   (3)編寫一個函式,將上述向量A按照學號ID進行增序排序,並將排序後的學生資訊按要求格式顯示到螢幕上
	   (4)編寫一個函式,刪除向量中姓名為“TOM”的學生資訊,並將處理結束後的向量內容輸出到D盤根目錄中的“res.dat”中,格式
	      同上
	   (5)要求使用流的方法來完成程式流程中所有資料輸入和輸出操作;要求使用向量來儲存學生資訊
*作者:陸胤任
*日期:2016.3.14
*================================================================================================================*/

#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
#include<fstream>
#include<iterator>
#include<sstream>

using namespace std;

typedef struct Student
{
	int ID;                 //學號
	string name;            //姓名
	float score[3];         //三門課程成績
}Tagstudent;

void Input(vector<Tagstudent> &A,int n)                          //輸入學生資訊,構成向量
{
	Tagstudent a;
	char ch;
	int i;
	for(i=0;i<n;i++)
	{
		cout<<"請輸入第"<<i<<"個同學的ID"<<endl;
		cin>>a.ID;
		cout<<"請輸入第"<<i<<"個同學的姓名"<<endl;
		ch=cin.get();                                            //讀'\n'
		getline(cin,a.name);
		cout<<"請輸入第"<<i<<"個同學的成績"<<endl;
		cin>>a.score[0]>>a.score[1]>>a.score[2];
		A.push_back(a);                                              //構成向量
	}
}

void sort_vec(vector<Tagstudent> &A)                            //對輸入的結構體資訊按照ID進行増序排序
{
	Tagstudent temp;
	int min,i,j;
	for(i=0;i<A.size()-1;i++)                                    //選擇排序
	{
		min=i;
		for(j=i+i;j<A.size();j++)
		{
			if(A[min].ID>A[j].ID)
			{
				min=j;
			}
		}
		if(min!=i)
		{
			temp=A[i];
			A[i]=A[min];
			A[min]=temp;
		}
	}
}

void Del_vec(vector<Tagstudent> &A,string name)                  //刪除姓名為name的學生資訊
{
	vector<Tagstudent>::iterator it=A.begin();
	while(it!=A.end())
	{
		if((*it).name==name)
		{
			it=A.erase(it);
			break;
		}
		else
		{
			it++;
		}
	}
} 

void Display(vector<Tagstudent> &A)                             //輸出學生資訊到螢幕上
{
	int i;
	for(i=0;i<A.size();i++)
	{
		cout<<setw(10)<<right<<A[i].ID<<setw(10)<<right<<A[i].name<<setprecision(3)<<right<<A[i].score[0]<<setprecision(3)<<right<<A[i].score[1]<<setprecision(3)<<right<<A[i].score[2]<<endl;
	}
}

void print_file(vector<Tagstudent> &A)
{
	int i;
	fstream in;                                                  
	in.open("D\\res.dat",ios::out); 
	for(i=0;i<A.size();i++)                     //將學生資訊輸入檔案中
	{
		in<<setw(10)<<right<<A[i].ID<<setw(10)<<right<<A[i].name<<setprecision(3)<<right<<A[i].score[0]<<setprecision(3)<<right<<A[i].score[1]<<setprecision(3)<<right<<A[i].score[2]<<endl;
	}
	in.close();
}

int main()
{
	string name="TOM";
	vector<Tagstudent> aa;                 

	Input(aa,5);                             //呼叫函式,輸入資料,構成向量

	sort_vec(aa);                            //按學生ID進行増序排序

	Display(aa);                             //把排序後的向量輸出到螢幕上

	Del_vec(aa,name);                       //刪除向量中名字為"TOM"的學生資料

	print_file(aa);                         //將向量內容輸入檔案中

	return 0;
}