IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++基础练习 -> 正文阅读

[C++知识库]c++基础练习

//练习一、计算若干个浮点数的平均值,以 -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;

}

/*练习三、用于计算数字n的平方根的巴比伦算法如下:

a.先猜一个答案guess(可以将n / 2作为第一个答案)

b.计算r = n / guess

c.guess = (guess + r) / 2

d.如有必要返回第2步重复多次。步骤2和步骤3重复的次数越多,guess就越接近n的平方根。

写一个程序,输入整数作为n的值,重复执行巴比伦算法,直到guess与前一个guess的误差在1 % 范围

内,将答案作为一个double输出。*/

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;

?????? }



}

/*

练习四、“23”游戏是一个双人游戏,道具是23根牙签,玩家轮流取123根牙签。拿到最后一根牙签

的是输家。写程序和计算机玩“23”。总是玩家先走,轮到计算机时,它根据以下规则采取行动:

a.如果剩余牙签多于4根,计算机就取走4-x根,x为玩家上次取走的牙签数

b.如果剩余2-4根牙签,计算机取走足够多的牙签,确保只剩下1

c.如果剩余1根牙签,计算机只能取走它并且认输

玩家输入要取走的牙签数量时,程序应对输入的有效性进行检查。要确定玩家输入的数在13之间,而

且试图取走的不能超过当前剩余的。

程序不允许输出当前剩余牙签数量。

*/

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;

?????? }

}

/*

练习五、任意三角形的面积计算公式为:

面积= 根号下s(s-a)(s-b)(s-c)

其中,a,b,c为三角形的3个边长,s是半周长 s=a+b+c/2

编写函数获取5个参数,其中3个是传值参数,指定了3个边长,另两个是传引用参数,他们根据前3个参

数来计算面积和周长。

注意:构成三角形的三边需要两个满足条件:较小的两边之和大于第三边,较大两边之差小于第三边。

要求:需自定义异常类,当不满足条件时抛出该异常,然后提示用户重新输入。

*/

//自定义异常类

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;

}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-06-06 17:10:30  更:2022-06-06 17:10:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/11 5:59:22-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码