#include"stdafx.h"
#include
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;
}
#include"stdafx.h"
#include
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
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;
}
#include"stdafx.h"
#include
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 <
#include"stdafx.h"
#include
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;
}
#include"stdafx.h"
#include
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):"<Area() << endl;
s[2] = new square(3);
cout << "正方形面积:" << s[2]->Area() << endl;
for (int i = 0; i < 3; i++)
{
delete s[i];
}
return 0;
}
输入包括一个整数n,(1 ≤ n ≤ 10^5)
输出一个整数,表示n的相反数
1325
6556
#include
#include
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;
}