1. 程式人生 > >基本程式設計筆試題

基本程式設計筆試題

1.定義一個“資料型別”datatype,能處理字元型,整形,浮點型三種資料型別,給出建構函式。

#include"stdafx.h"
#include<iostream>
using namespace std;
class datatype
{
	enum
	{
		Inter, FLoat, Char
	}vartype;
	union 
	{
		char c;
		int i;
		float f;
	};
public:
	datatype(int ii)
	{
		vartype = Inter;
		i = ii;
	}
	datatype(char  cc)
	{
		vartype = Char;
		c = cc;
	}
	datatype(float ff)
	{
		vartype = FLoat;
		f = ff;
	}
	void print();
};
 void datatype::print()
{
	 switch (vartype)
	 {
	 case Inter:
		  cout << "整形:" << i << endl; break;
	 case Char:
		 cout << "字元型:" << c << endl; break;
	 case FLoat:
		 cout << "浮點型:" << f << endl; break;
	 default:
		 break;
	 }

}
 
int main()
{
	datatype A(12), B(16.44F), C('V');
	datatype D = A;
	A.print();
	B.print();
	C.print();
	D.print();
	return  0;
}


2.用窮舉法找出1-100之間的所有質數

#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
	bool bPrime=true;
	int iTotalNum=100;

	for (int i = 1; i <= iTotalNum; i++)
	{
		if (i == 1)
			bPrime = false;
		for (int j = 2; j<=sqrt(i); j++)
		{
			if (i%j == 0)
			{
				bPrime = false;	break;
			}
		}
		if (bPrime)
		{
			cout << i << " ";
		}
		bPrime = true;
	}
	return 0;

}
 

判斷是否是質數

#include"stdafx.h"
#include<iostream>
using namespace std;
bool Prime(int iNum)
{
	if (iNum == 1) return false;
	for (int i = 2; i < sqrt(iNum); i++)
	{
		if (iNum%i == 0)
			return false;
	}
	return true;
}
int main()
{
	bool bPrime = true;
	int iNum;
	while (cin >> iNum)
	{
		if (Prime(iNum))
			cout << iNum << "是質數" << endl;
		else
			cout << iNum << "不是質數" << endl;
	}
	return 0;

}

3.編寫函式求兩個函式的最小公倍數和最大公約數

#include"stdafx.h"
#include<iostream>
using namespace std;
int GreatsetCommon(int A, int B);
int LeastCommon(int A, int B,int ileastcommon);
int main()
{
	int InterA,InterB;
	while (cin >> InterA&&cin >> InterB)
	{
		if (InterA==0||InterB==0)
		{
			cout << "最小公約數無,最大公約數為0!" << endl;
			continue;
		}
		int ileastcommon;
		ileastcommon= GreatsetCommon(InterA, InterB);
		LeastCommon(InterA, InterB,ileastcommon);
	}	 
    return 0;
}
int GreatsetCommon(int A,int B)
{
	int C;
   C = A%B;
	while (B!=0)
	{
		if (C == 0)
		{
			cout << "最大公約數是:" << B << endl; return B;
		}
		else
		{
			A = B; B = C; C = A%B;
		}
	}
	
}
int LeastCommon (int A, int B,int ileastcommon)
{
	int greastcommon;
	if (ileastcommon==0)
	{
		cout << "無最大公倍數!" << endl;
	}
	else
	{
		greastcommon = A*B / ileastcommon;
		cout << "最小公倍數為:" << greastcommon <<endl;
		return  greastcommon;
	}
}

4.定義一個矩形類,有長,有寬兩個屬性,有成員函式計算矩形的面積

#include"stdafx.h"
#include<iostream>
using namespace std;
class Rectangle
{
public:
	Rectangle(double length,double width);
	~Rectangle();
	double GetArea();
	double GetWidth();
	double GetLength();
private:
	double dLength;
	double dWidth;
};

double Rectangle::GetWidth() {return dWidth;}
double Rectangle::GetArea()  {return (dWidth*dLength); }
double Rectangle::GetLength(){return dLength; }
Rectangle::Rectangle(double length, double width)
{
	dLength = length; dWidth = width;
}

Rectangle::~Rectangle()
{
}
int main()
{
	cout << "請輸入矩形的長和寬:" << endl;
	double length, width;
	while (cin>>length&&cin>>width)
	{
		Rectangle rTest= Rectangle(length, width);
		cout << "矩形的面積是:" << rTest.GetArea() << endl;
	}
	return 0;
}


5.定義一個Shape基類,在此基礎上派生出Rectangle和Circle,二者都在GetArea()函式,計算物件的面積,使用Rectangle類建立一個派生類Square

#include"stdafx.h"
#include<iostream>
using namespace std;
#define PI 3.14
class Shape
{
public:
	Shape() {};
	~Shape() {};
	virtual float Area() = 0;
};
class circle :public Shape
{
public:
	circle(float radius) { fRadius = radius; };
	~circle() {};
	float Area() { return (float)(PI*fRadius*fRadius); };
private:
	float fRadius;
};
class rectangle:public Shape
{
public:
	rectangle(float length, float width) { fLength = length; fWidth = width; };
	~rectangle() {};
	float Area() { return fLength*fWidth; }
private:
	float fWidth;
	float fLength;
};
class square :public rectangle
{
public:
	square(float len) :rectangle(len, len) {};
	~square() {};
	float Area(float len) { return len*len; };
};

 
int main()
{
	Shape *s[3];
	s[0] = new circle(2);
	cout <<"圓的面積(2):"<< s[0]->Area() << endl;
	s[1] = new rectangle(2, 4);
	cout << "矩形面積(2,4):"<<s[1]->Area() << endl;
	s[2] = new square(3);
	cout << "正方形面積:" << s[2]->Area() << endl;
	for (int i = 0; i < 3; i++)
	{
		delete s[i];
	}
	return 0;
}


6.為了得到一個數的"相反數",我們將這個數的數字順序顛倒,然後再加上原先的數得到"相反數"。例如,為了得到1325的"相反數",首先我們將該數的數字順序顛倒,我們得到5231,之後再加上原先的數,我們得到5231+1325=6556.如果顛倒之後的數字有字首零,字首零將會被忽略。例如n = 100, 顛倒之後是1. 

輸入描述:
輸入包括一個整數n,(1 ≤ n ≤ 10^5)

輸出描述:
輸出一個整數,表示n的相反數

輸入例子1:
1325
輸出例子1:
6556
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	 int iNum;
	 int iTemp;
	 int iOpsite=0; 
	 while(cin >> iNum)
	 { 
		 iTemp = iNum;
		 while (iTemp)
		 {
			 iOpsite = iOpsite * 10 + iTemp % 10;
			 iTemp /= 10;
		 }
		 cout << "相反數" << iOpsite << endl;
		 cout << iOpsite + iNum;
		 iNum = 0; iOpsite = 0;
	 }
	 
	return 0;
}