//练习一、计算若干个浮点数的平均值,以 -1 作输入结束。
void doubles() {
float a = 0, b = 0;
int index = 0;
cout << "输入若干个浮点数,以 -1 作输入结束" << endl;
while (true)
{
cin >> b;
if (b == -1) {
break;
}
a += b;
index++;
}
cout << "平均值为:" << a / index << endl;
}
//练习二、写一个程序读入10个整数,输出所有正数之和,负数之和与所有整数之和。用户可一次性输入
// 这10个整数,而且可以采用任何的顺序。程序不应该要求用户单独输入正数和负数。
void intArr() {
int arr[10];
int a = 0, ua = 0, sum = 0;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
if (arr[i] > 0) {
a += arr[i];
}
else if (arr[i] < 0) {
ua += arr[i];
}
sum += arr[i];
}
cout << "所有正数之和为" << a << endl;
cout << "所有负数之和为" << ua << endl;
cout << "所有整数之和为" << sum << endl;
}
void maths() {
double a = 0, r = 0;
cout << "请输入一个数字" << endl;
cin >> a;
cout << "请猜一个数字" << endl;
double guess = 0;
cin >> guess;
double pre = 0;
while (true) {
r = a / guess;
guess = (guess + r) / 2;
if (abs(pre - guess) <= 0.01) {
cout << "答案是" << endl;
cout << guess << endl;
break;
}
pre = guess;
}
}
void ends() {
int sum = 23;
int a = 0, b = 0;
while (true) {
cout << "还剩下" << sum << "根牙签" << endl;
cout << "请输入取走的牙签数:取1,2或3根牙签" << endl;
while (true) {
cin >> a;
if (a > 0 && a < 4 && a <= sum) {
break;
}
else
{
cout << "一次只能拿走1,2或3根牙签" << endl;
}
}
sum -= a;
if (sum == 0) {
cout << "完家输了" << endl;
}
//system("cls");
//计算机;
if (sum > 4) {
b = 4 - a;
sum -= b;
cout << "计算机取走" << b << "根牙签" << endl;
}
else if (sum >= 2 && sum <= 4) {
sum = sum - (sum - 1);
cout << "计算机取走" << sum - 1 << "根牙签" << endl;
}
else {
sum = sum - 1;
cout << "还剩下" << sum << "跟牙签" << endl;
cout << "计算机输了" << endl;
break;
}
cout << endl;
}
}
//自定义异常类
class erorr1 {
public:
erorr1(string a) :a(a) {}
~erorr1() {}
void getMessage() {
cout << a << endl;
}
private:
string a;
};
void exer1(double a, double b, double c, double& area, double& per) {
if (!(a + b > c && b + c > a && a + c > b)) {
throw erorr1("三角形较小两边之和要大于第三边");
}
if (!(abs(a - b) < c && abs(a - c) < b && abs(b - c) < a)) {
throw erorr1("三角形较大两边之差要小于第三边");
}
per = a + b + c;
double s = (a + b + c) / 2;
double arr = s * (s - a) * (s - b) * (s - c);
area = sqrt(arr);
}
void maino1() {
try
{
//面积
double area = 0, perimeter = 0, a, b, c;
//周长:
cout << "输入三角形三边的长度" << endl;
cin >> a;
cin >> b;
cin >> c;
exer1(a, b, c, area, perimeter);
cout << "三角形的面积为:" << area << endl;
cout << "三角形的周长为:" << perimeter << endl;
}
catch (erorr1 e)
{
e.getMessage();
}
}
//练习六:5X5的螺旋数组
void maino() {
int arr[5][5];
int index = 0;
for (int i = 0; i < 3; i++) {
for (int h = i;h < 5 - i;h++) {
index++;
arr[i][h] = index;
}
for (int s = i + 1;s < 5 - i;s++) {
index++;
arr[s][5 - i - 1] = index;
}
for (int d = 5 - i - 2;d >= i;d--) {
index++;
arr[5 - i - 1][d] = index;
}
for (int j = 5 - i - 2; j >= i + 1;j--) {
index++;
arr[j][i] = index;
}
}
for (int i = 0;i < 5;i++) {
for (int j = 0;j < 5;j++) {
cout << arr[i][j] << "t";
}
cout << endl;
}
}
//练习七、某商店经销一种货物。货物购进和卖出时以箱为单位,各箱的重量不一样,因此,商店需要记
//录目前库存的总重量(使用静态成员变量)。现在用C++模拟商店货物购进和卖出的情况。
//提示:使用链表结构和静态成员变量
//需要完善的内容包括:
//Goods类,包括类的构造函数,析构函数,成员函数,静态成员函数以及私有的成员变量。
//两个单独的函数:购进( purchase )和售出( sale )
//程序要求:
//1.main.cpp中只包含main函数,且不可变更。
//2.程序结构清晰,.h文件中放置声明代码,.cpp文件中放置定义代码
//3.保证程序可以运行且结果正确。
class Goods {
public:
Goods() {
next = NULL;
}
~Goods() {}
static int TotalWeight() {
return weight;
}
//设置重量
void setWeight(int a) {
weight += a;
b = a;
}
void delWeight() {
weight -= this->b;
}
void linGoods(Goods* nest) {
this->next = nest;
}
Goods* getGoods() {
return next;
}
private:
//计算总重量
static int weight;
//
int b = 0;
Goods* next;
};
int Goods::weight = 0;
//从表尾插入
void purchase(Goods*& front, Goods*& rear, int w) {
Goods* abb = new Goods();
abb->setWeight(w);
abb->linGoods(NULL);
if (!front) {
front = abb;
rear = abb;
}
else
{
rear->linGoods(abb);
rear = abb;
}
}
//从表头删除
void sale(Goods*& front, Goods*& rear) {
if (front==NULL) {
return;
}
else if (front == rear) {
delete front;
}
else
{
Goods* temp = front;
front = front->getGoods();
//temp->delWeight();
delete temp;
}
}
void maino2()
{
Goods* front = NULL, * rear = NULL;
int w; int choice;
do
{
cout << "请选择:n";
cout << "1:进货, n2:售出, n0:退出.n";
cin >> choice;
switch (choice) // 操作选择
{
case 1: // 键入1,购进1箱
//货物
{ cout << "请输入重量: ";
cin >> w;
purchase(front, rear, w); // 从表尾插入1个结点
break;
}
case 2: // 键入2,售出1箱货物
{
sale(front, rear);
break;
} // 从表头删除1个结点
case 0: break; // 键入0,结束
}
cout << "现在的总重量为:" << Goods::TotalWeight() << endl;//count
} while (choice);
}
//第八题:
// 练习八、完成复数类的运算符重载函数,包括:
//基本算术运算( + 、 - 、 * 、 / 、 = )
//自增自减运算(前置++、后置++、前置--、后置--)
//流运算符( << 、 >> )
//
//复数的概念:
//复数的形式: a + bi 代表一个复数,其中a为实部,b为虚部,i为复数符号(i2 = -1)。
//运算法则:
//加法:(a + bi) + (c + di) = (a + c) + (b + d)i
//减法:(a + bi) - (c + di) = (a - c) + (b - d)i
//乘法:(a + bi)(c + di) = ac + adi + bci + bdi2 = (ac - bd) + (bc + ad)i
//除法:(a + bi) / (c + di) = (ac + bd) / (c2 + d2) + ((bc - ad) / (c2 + d2))i
//clas
s complex {
private:
double real;
double imag;
public:
friend ostream& operator<<(ostream& out, const complex& c2);
friend istream& operator>>(istream& in,complex& c2);
complex() {
real = 0;
imag = 0;
}
complex(double real, double imag):real(real),imag(imag) {
}
//成员函数重载
complex operator+(const complex& c2) {
this->real = this->real + c2.real;
this->imag = this->real + c2.imag;
return *this;
}
complex operator-(const complex& c2) {
this->real = this->real - c2.real;
this->imag = this->real - c2.imag;
return *this;
}
complex operator*(const complex& c2) {
this->real = this->real * c2.real-this->imag*c2.imag;
this->imag = this->real * c2.imag+this->imag*c2.real;
return *this;
}
complex operator/(const complex& c2) {
this->real = (this->real * c2.real + this->imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag);
this->imag = (this->imag * c2.real - this->real * this->imag) / ((c2.real * c2.real + c2.imag * c2.imag));
return *this;
}
complex operator=(const complex& c2) {
this->real = c2.real;
this->imag = c2.imag;
return *this;
}
complex& operator++() {
this->real++;
this->imag++;
return *this;
}
complex operator++(int) {
this->real++;
this->imag++;
return *this;
}
complex& operator--() {
this->real--;
this->imag--;
return *this;
}
complex operator--(int) {
this->real--;
this->imag--;
return *this;
}
};
ostream& operator<<(ostream& out, const complex& c2) {
out << "real=" << c2.real << " imag=" << c2.imag << endl;
return out;
}
istream& operator>>(istream& in, complex& c2) {
cout << "请输入实数" << endl;
in >> c2.real;
cout << "请输入虚数" << endl;
in >> c2.imag;
return in;
}
//练习十、编写程序统计一个文件中的字符总数、非空白字符总数、字母总数和平均单词长度,并输出到
//屏幕和输出文件output.txt。
//要求:统计的文件为当前工程的main.cpp。输出文件要求和main.cpp在同一级目录,且程序中要求使
//用相对路径表示。
void put() {
ifstream opens("output.txt", ios::in);
if (!opens.is_open())
{
cout << "文件打开失败" << endl;
return;
}
int a = 0, b = 0, c = 0, d = 0;
char abb;
while ((abb = opens.get()) != EOF)
{
a++;
if (abb != 32) {
b++;
}
if ((abb >= 65 && abb <= 90) && (abb >= 97 && abb <= 122)) {
c++;
}
}
d = a / (a - b);
cout << "字符总数=" << a << endl;
cout << "非空白字符总数=" << b << endl;
cout << "字母总数=" << c << endl;
cout << "平均单词长度=" << d << endl;
}



