挑戰題一(3)

設計課程(course)結構(如p502-2),

   1)重載==運算符,查找出課程名爲“英語”的記錄;

   2)重載==運算符,查找出開課學時爲72的所有記錄;

   3)重載>運算符,查找出開課學時>80的所有課程記錄。

   設計main函數,先創建p51的表2-1,然後實現上述功能。


根課本的例子基本一致

但我做出的界面不好看

不知道怎麼用C++修改字符輸出寬度,細節做得不夠好


#include<iostream>
#include<string>

struct course 
{
	char name[20];
	int period;
	int term;
};

bool operator==(course x, char*key)
{
	if (strcmp(x.name, key) == 0)
		return  true;
	else
		return false;
}

bool operator==(course x, int key)
{
	if (x.period == key)
		return true;
	else
		return false;
}

int operator>(course x, int key)
{
	return x.period > key;
}

void main()
{
	course a[5] = { {"高等數學",90,1},{"離散數學",72,2},{"英語",72,1},{"計算機組成原理",90,2},
	{"程序設計基礎",63,3} };
	int i;
	char *p = "英語";
	std::cout << "查找課程名爲英語的記錄" << std::endl;
	std::cout << " " << std::endl;
	std::cout << "查詢結果爲:" << std::endl;
	for (i = 0; i < 5; i++)
		if (a[i] == p)
		{
			std::cout << "課程名稱   開課學時   開課學期" << std::endl;
			std::cout << a[i].name << "       " << a[i].period << "        " << a[i].term << std::endl;
			std::cout << " " << std::endl;
		}
	
	
	
	std::cout << "查找課時爲72的記錄" << std::endl;
	std::cout << " " << std::endl;
	std::cout << "查詢結果爲:" << std::endl;
	for ( i = 0; i < 5; i++)
		if (a[i] == 72)
		{
			std::cout << "課程名稱   開課學時   開課學期" << std::endl;
			std::cout << a[i].name << "       " << a[i].period << "       " << a[i].term << std::endl;
			std::cout << " " << std::endl;
		}
	
	
	
	std::cout << "查找課時大於80的記錄" << std::endl;
	std::cout << " " << std::endl;
	std::cout << "查詢結果爲:" << std::endl;
	for ( i = 0; i < 5; i++)
		if (a[i] > 80)
		{
			std::cout << "課程名稱   開課學時   開課學期" << std::endl;
			std::cout << a[i].name << "       " << a[i].period << "       " << a[i].term << std::endl;
			std::cout << " " << std::endl;
		}
	
	
	
	
	system("pause");
}


如圖,後面的不能對應,比較難看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章