首页 > 编程语言 >c++ 期末编程题

c++ 期末编程题

时间:2024-01-05 21:55:06浏览次数:51  
标签:std cout int 编程 namespace c++ 期末 using include

当然,我可以帮你整理一下你提供的C++代码,并为每个代码片段添加相应的标题。请看下面的整理:

1. 计算两点之间的距离

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int x1, x2, y1, y2;
    cout << "请输入x1, x2, y1, y2的值"; cin >> x1 >> x2 >> y1 >> y2;
    cout << "两点之间的距离为: " << sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
    return 0;
}

2. 成绩评定

#include <iostream>
using namespace std;
int main() {
    float score;
    char grade;
    cout << "输入分数";  cin >> score;
    while (score > 100 || score < 0) {
        cout << "数据错误" << endl;
        cin >> score;
    }
    switch (int(score / 10)) {
        case 10:
        case 9: grade = 'A'; break;
        case 8: grade = 'B'; break;
        case 7: grade = 'C'; break;
        case 6: grade = 'D'; break;
        default: grade = 'E'; break;
    }
    cout << "分数等级: " << grade;
    return 0;
}

3. 完数

#include <iostream>
using namespace std;
int main() {
    int m, i, s;
    for (m = 2; m < 100; m++) {
        s = 0;
        for (i = 1; i < m; i++)
            if (m % i == 0)
                s += i;
        if (s == m)    cout << m << "是完数" << endl;
    }
    return 0;
}

4. 最大、最小、平均值

#include <iostream>
using namespace std;
int main() {
    int a, max, min, s;
    cin >> a;
    max = min = s = a;
    for (int i = 1; i < 20; i++) {
        cin >> a;
        if (max < a) max = a;
        if (min > a) min = a;
        s += a;
    }
    cout << "最大值: " << max << ", 最小值: " << min << ", 平均值: " << s / 20 << endl;
    return 0;
}

5. 正数、负数、零的个数

#include<iostream>
using namespace std;
int main() {
    int n = 0, m = 0;
    for (int i = 0; i < 20; i++) {
        int a;
        cin >> a;
        if (a > 0) n++;
        else if (a < 0) m++;
    }
    cout << "正数个数: " << n << " 负数个数: " << m << endl;
    return 0;
}

6. 奇数和、偶数和

#include<iostream>
using namespace std;
int main() {
    int n = 0, m = 0;
    for (int i = 1; i < 51; i++)
        if (i % 2) n += i;
        else m += i;
    cout << "奇数和: " << n << " 偶数和: " << m;
    return 0;
}

7. 素数

#include <iostream>
using namespace std;
bool isprime(int num) {
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0)    return false;
    }
    return true;
}
int main() {
    int count = 0;
    for (int i = 2; i <= 1000; i++)
        if (isprime(i)) {
            cout << i << '\t';
            count++;
            if (count % 5 == 0)  cout << endl;
        }
    cout << endl;
    return 0;
}

8. 找最大值

#include <iostream>
using namespace std;
int max(int a, int b, int c = 0) {
    if (a < b) a = b;
    if (a < c) a = c;
    return a;
}
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    cout << "max(a, b, c): " << max(a, b, c) << endl;
    cout << "max(a, b): " << max(a, b) << endl;
    return 0;
}

9. 类与对象 - 计算体积

#include <iostream>
using namespace std;
class Box {
    int length, width, height;
public:


    void get_value() {
        cout << "输入长度 宽度 高度: " << endl;
        cin >> length >> width >> height;
    }
    float display() {
        cout << length * width * height << endl;
    }
};
int main() {
    Box box1, box2, box3;
    box1.get_value();
    cout << "box1的体积为 ";
    box1.display();
    box2.get_value();
    cout << "box2的体积为 ";
    box2.display();
    box3.get_value();
    cout << "box3的体积为 ";
    box3.display();
    return 0;
}

10. 类与对象 - 时间显示

#include <iostream>
using namespace std;
class Time {
    int hour, minute, second;
public:
    Time(int h, int m, int s) {
        hour = h; minute = m; second = s;
    }
    void show_time() {
        cout << hour << ":" << minute << ":" << second << endl;
    }
};
int main() {
    Time t(9, 10, 0);
    t.show_time();
    return 0;
}

11. 类与对象 - 长方体比较

#include <iostream>
using namespace std;
class Cuboid {
    int length, width, height;
public:
    Cuboid(int l = 3, int w = 4, int h = 5) {
        length = l; width = w; height = h;
    }
    int getL() { return length; }
    int getW() { return width; }
    int getH() { return height; }
    int getArea() {
        return length * width * height;
    }
    bool isEqual(Cuboid &b) {
        return length == b.getL() && width == b.getW() && height == b.getH();
    }
};

12. 类与对象 - 矩形面积和周长

#include <iostream>
using namespace std;
class Rectangle {
    float length, width;
public:
    Rectangle(float l = 20, float w = 30) { length = l; width = w; }
    float Area() {
        return length * width;
    }
    float Perimeter() {
        return (length + width) * 2;
    }
};

13. 类与对象 - 日期

#include <iostream>
using namespace std;
class CDate {
    int Year, Month, Day;
public:
    CDate() { Year = 2024; Month = 1; Day = 5; }
    CDate(int y = 2024, int m = 1, int d = 1) { Year = y; Month = m; Day = d; }
    void PrintDate() { cout << Year << "-" << Month << "-" << Day; }
    void SetDate(int sy, int sm, int sd) { Year = sy; Month = sm; Day = sd; }
};

14. 类与对象 - 日期

#include <iostream>
using namespace std;
class Date {
    int Month, Day, Year;
public:
    Date(int y = 2019, int m = 1, int d = 1) { Year = y; Month = m; Day = d; }
    void Display() { cout << Month << '/' << Day << '/' << Year; }
};

15. 类与对象 - 复数相加

#include <iostream>
using namespace std;
class Complex {
    double real, image;
public:
    Complex(double r, double i) { real = r; image = i; }
    void Show() { cout << real << ',' << image << endl; }
    Complex operator+(Complex &c2) {
        return Complex(real + c2.real, image + c2.image);
    }
};

16. 类与对象 - 继承

#include <iostream>
using namespace std;
class rectangle {
    double length, width;
public:
    rectangle(double l, double w) { length = l; width = w; }
    float Area() { return length * width; }
};
class rectangular : public rectangle {
    double height;
public:
    rectangular(double l, double w, double h) : rectangle(l, w) { height = h; }
    double volume() { return Area() * height; }
};
int main() {
    rectangle obj1(2, 8);
    rectangular obj2(3, 4, 5);
    cout << "长方形的面积为: " << obj1.Area() << endl;
    cout << "长方体的体积为: " << obj2.volume() << endl;
    return 0;
}

这样应该更容易理解了。如果有任何问题或需要进一步解释,请随时告诉我。

标签:std,cout,int,编程,namespace,c++,期末,using,include
From: https://www.cnblogs.com/chatBlog/p/17948180

相关文章

  • Android期末作业课程设计-爱篮球论坛+源代码+文档说明
    项目介绍界面预览项目备注1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作......
  • Android期末大作业-Note便签本应用+源代码+文档说明
    项目介绍界面预览项目备注1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作......
  • 安卓期末小项目TrackTable收支表+源码
    一、需求分析这是一款账目记录、分析App,本系统主要功能有:用户登录注册、首页账单分析、上传账单、搜索账单信息、个人信息、重置密码、数据效验。系统功能图系统总用例图二、系统开发平台环境IDE:AndroidStudio 2021.1.x插件:simpleUMLCE工具:Visustinv8DemoJava版本:Java11OS:win11......
  • python学习----编程题02
    题目:企业发放的奖金根据利润提成。利润(0)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成540万60万之间时高于40万元的部分,可提成3%60万到100万之间时,高于60......
  • (五十二)C#编程基础复习——C#点阵列
    在C#中,点阵列类用来管理一个紧凑型的位值数组,数组中的值均为布尔类型,其中true(1)表示此位为开启,false(0)表示此位为关闭。当你需要存储位(英文名“bit”数据存储的最小单位,也可称为比特),但事先又不知道具体位数时,就可以使用点阵列。当需要访问点阵列中的元素时,可以使用整型索引从点阵......
  • (五十一)C#编程基础复习——C#队列
    在C#中,队列类与堆栈类类似,它代表了一个先进先出的对象结合,当你需要对项目进行先进先出访问时,则可以使用队列。向队列中添加元素称为入队,从堆栈中移除元素称为出队。一、队列类中的属性下表中列出了队列类中的一些常用属性二、队列类中的方法下表列出了队列类的一些常用方法......
  • 面向对象编程(上)
    面向对象内容的三条主线1.Java类及类的成员:属性、方法、构造器;代码块、内部类2.面向对象的三大特征:封装性、继承性、多态性、(抽象性)3.其它关键字:this、super、static、final、abstract、interface、package、import等面向对象的思想概述Java语言的基本元素:类和对象类......
  • 面向对象编程(中)
    关键字:static(静态)作用范围可以用来修饰的结构:主要用来修饰类的内部结构属性、方法、代码块、内部类static修饰属性静态变量(或类变量)静态属性vs非静态属性属性,是否使用static修饰,又分为:静态属性vs非静态属性(实例变量)实例变量:我们创建了类的多个对象,每个对象都独立的......
  • (五十)C#编程基础复习——C#堆栈
    在C#中,堆栈类表示一个后进先出的对象集合,当你需要对项目进行后进先出的访问时,则可以使用堆栈。向堆栈中添加元素称为推入元素,从堆栈中移除元素称为弹出元素。一、堆栈类中的属性下表列出了堆栈类中的一些常用的属性二、堆栈类中的方法下面列出了堆栈类中一些常用的方法示例......
  • 【教3妹学编程-算法题】经营摩天轮的最大利润
    3妹:“打个中国结,再系个红腰带,愿善良的人们天天好运来,你勤劳生活美,你健康春常在,你一生的忙碌为了笑逐颜开。”2哥 :3妹,元旦快乐啊。3妹:2哥元旦快乐~。2哥:祝新的一年,3妹技术突飞猛进,工资涨涨涨。3妹:祝新的一年,2哥能够找到女朋友,哈哈哈......