• 单元综合测试一(1-5章)
    已有复数Complex类 ,编写一个类Root实现求解一元二次方程组的程序。

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;class Complex {public:Complex() {}void setC(double i, double j) { real = i; image = j; }void show();
    protected:double real, image;
    };
    void Complex::show() {cout << "(" << real << "," << image << "i)" << endl;
    }class Root {public:Root(double i, double j, double k) {a = i; b = j; c = k;}void show();void solve();void solve2();
    protected:double a, b, c;
    };void Root::show() {cout << "方程 " << a << setiosflags(ios::showpos) << "*x*x" << b << "*x" << c << "=0 " << resetiosflags(ios::showpos) << "的解是:" << endl;
    }/*
    求根公式:x = (-b±√b²-4ac)/2a对于实数系一元二次方程 ax² + bx + c = 0(a≠0),△=b²-4ac称作一元二次方程组根的判别式.根据判别式,根有三种可能的情况:1.△=0 有两个相等的实根.这两个根为:x1 = x2 = -b/2a2.△>0 有两个不相同的实根。如果系数都为有理数,且△是一个完全平方根,则两根都是有理数,否则为无理数.3.△<0 有两个不同的复数根,且为共轭复根。这时根为:x1 = -b/2a + i((√b²-4ac)/2a)x2 = -b/2a - i((√b²-4ac)/2a)其中 i² = -1.
    */
    void Root::solve() {show();double x = -b / 2 / a;double dt = b * b - 4 * a * c;Complex c1, c2;if (dt == 0) {c1.setC(x, 0);c2.setC(x, 0);cout << "\nx1=x2=";c1.show();}else if (dt > 0) {c1.setC(x + sqrt(dt) / (2 * a), 0);c2.setC(x - sqrt(dt) / (2 * a), 0);c1.show();c2.show();}else {c1.setC(x, sqrt(-dt) / (2 * a));c2.setC(x, -sqrt(-dt) / (2 * a));c1.show();c2.show();}
    }void Root::solve2() {show();double dt = b * b - 4 * a * c;double x = -b / 2 / a;if (dt == 0) {cout << "x1=x2=" << x;}else if (dt > 0) {cout << "x1=" << (-b + sqrt(dt)) / 2 / a << endl;cout << "x2=" << (-b - sqrt(dt)) / 2 / a << endl;}else {Complex c1, c2;c1.setC(x, sqrt(-dt) / 2 / a);c2.setC(x, -sqrt(-dt) / 2 / a);cout << "x1=";c1.show();cout << "x2=";c2.show();}
    }
    int main()
    {double a, b, c;cout << "\n输入一元二次方程组的系数a,b,c:";cin >> a >> b >> c;Root root(a, b, c);root.solve();return 0;
    }
    
  • 单元综合测试二(6-9章)
    定义堆栈类模板Stack(先进后出),栈的大小由使用者决定。要求该类提供如下两种操作:1.push入栈。2.pop出栈,用数组来实现。

    #include <iostream>
    using namespace std;template <class T, int size>class Stack {T x[size];int current;
    public:Stack() {current = 0;}void push(T);void pop(T);};template<class T, int size>
    void Stack<T, size>::push(T t) {if (current == size) {cout << "The Stack is full!" << endl;exit(0);}else {x[current] = 1;current++;}
    }template<class T, int size>
    void Stack<T, size>::pop(T t) {if (current == 0) {cout << "The Stack no object in the Stack!" << endl;exit(0);}else {T a = x[current - 1];current--;return a;}
    }int main() {return 0;
    }
  • 全真模拟演练一
    复数类Complex,私有成员变量实部real,虚部image。添加构造函数,并使用友元函数add实现复数加法。

    #include <iostream>
    using namespace std;
    class Complex {private:double real, image;
    public:Complex() {}Complex(double a, double b) {real = a;image = b;}void setRI(double a, double b) {real = a;image = b;}double getReal() {return real;}double getImage() {return image;}void print() {if (image > 0) {cout << "复数:" << real << "+" << image << "i" << endl;}if (image < 0) {cout << "复数:" << real << "-" << image << "i" << endl;}}friend Complex add(Complex, Complex);
    };Complex add(Complex c1, Complex c2) {return Complex(c1.real + c2.real, c1.image + c2.image);
    }
    int main() {Complex c1(19, 0.864), c2,c3;c2.setRI(90, 125.012);c3 = add(c1, c2);cout << "复数1:"; c1.print();cout << "复数2:"; c2.print();cout << "复数3:"; c3.print();return 0;
    }
  • 全真模拟演练二
    编写一个函数to_lower,实现将字符串大写转小写

    #include <iostream>
    #include <string>
    using namespace std;int main() {void to_lower(char a[]);char str[10];cin >> str;to_lower(str);cout << str << endl;/*void to_uper(char a[]);char str2[10] = { 'a','b','c','d' };to_uper(str2);cout << str2 << endl;*/
    }
    void to_lower(char a[]) {for (int i = 0; i < 10; i++) {if (a[i] >= 'A' && a[i] <= 'Z') {a[i] += 32;}}
    }
    /*
    void to_uper(char a[]) {for (int i = 0; i < 10 && a[i] != '\0'; i++) {if (a[i] >= 'a' && a[i] <= 'z') {a[i] -= 32;}}
    }
    */
  • 全真模拟演练三
    输入10个数,写一个求平均值的函数,并输出结果.

    #include <iostream>
    #include<fstream>
    using namespace std;void main() {float average(float a[]);float score[10];for (int i = 0; i < 10; i++)cin >> score[i];cout << average(score) << endl;
    }float average(float a[]) {float sum = 0;for (int i = 0; i < 10; i++)sum += a[i];return (sum / 10);
    }
    
  • 全真模拟演练四
    在字符串类str中实现一个判断函数,该函数的功能是统计某一字符串类对象(仅单词和空格组成)单词数量,同时保存所有单词在字符串中的起始地址(设改字符串不超过100个单词)

    #include <iostream>
    #include<fstream>
    using namespace std;
    class str {public:string s;int n, a[100], j, l;str(string& a) {s = a;n = 0;j = 0;l = 0;}int test(str ms);int* geta() { return a; }
    };int test(str ms) {while (ms.l < ms.s.length()) {if (ms.s[ms.l] != '\0') {if (ms.j == 0) {ms.a[ms.n] = ms.l;ms.j = 1;ms.n++;}}else {if (ms.j == 1) {ms.j = 0;}}ms.l++;}return ms.n;
    }
    int  main() { return 0;
    }
    
  • 全真模拟演练五
    定义一个抽象类Shape,由它派生三个类,Square(正方形),Trapezoid(梯形),Triangle(三角形),用虚函数分别计算几种图形面积,并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象。

    #include <iostream>
    using namespace std;class Shape {public:virtual double area()const = 0;
    };class Square :public Shape {public:Square(double s) :side(s) {}double area() const { return side * side; }
    private:double side;
    };class Trapezoid :public Shape {public:Trapezoid(double i, double j, double k) :a(i), b(j), h(k) {}double area() const { return (a + b) * h / 2; }
    private:double a, b, h;
    };class Triangle :public Shape {public:Triangle(double i, double j) :w(i), h(j) {}double area() const { return w * h / 2; }
    private:double w, h;
    };void  main() {Shape* p[3];Square se(5);Trapezoid td(2, 5, 4);Triangle te(5, 8);p[0] = &se;p[1] = &td;p[2] = &te;double da = 0;for (int i = 0; i < 3; i++) {da += p[i]->area();}cout << da << endl;
    }
  • 全真模拟演练六
    编写一个类模板void change(T &i,T &j),实现整型、实数、字符串的交换。并编写测试主函数。

    #include <iostream>
    using namespace std;
    template <class T>void change(T& i, T& j) {T temp;temp = i;i = j;j = temp;
    }int  main() {int a, b;cout << "输入两个整数a,b:";cin >> a >> b;change(a, b);cout << "交换后为a,b:" << a << " " << b << endl;double c, d;cout << "\n输入两个实数c,d:";cin >> c >> d;change(c, d);cout << "交换后为c,d:" << c << " " << d << endl;cin.get();char* s1, * s2;char str1[20], str2[20];s1 = str1; s2 = str2; cout << "\n输入第一个字符串s1:"; cin.getline(s1, 20); cout << "输入第二个字符串s2:";cin.getline(s2, 20);change(s1, s2);cout << "交换后的为s1,s2:" << s1 << " \t" << s2 << endl;return 0;
    }
  • 考前深度密押(一)

    定义一个图形类figure,其中有保护成员:高度(height),宽度(width),和一个公有的构造函数。该图形派生类有矩形类和等腰三角形类。每个派生类中包含一个函数 area() 用来计算面积。

    #include <iostream>
    using namespace std;class figure {protected:double height, width;
    public:figure(double h = 0, double w = 0) :height(h), width(w) {}
    };class triangle :public figure {protected:double height, width;
    public:triangle(double h = 0, double w = 0) :height(h), width(w), figure(w, h) { }double area() {return height * width / 2;}
    };class rectangle :public figure {protected:double height, width;
    public:rectangle(double h = 0, double w = 0) :height(h), width(w), figure(w, h) { }double area() {return height * width;}
    };void main() {triangle tri(2, 3);rectangle rec(2, 3);cout << tri.area() << endl;cout << rec.area() << endl;
    }
    
  • 考前深度密押(二)
    定义一个生日类,成员变量有年、月、日。定义一个人员类,成员变量有姓名,性别,生日。
    人员类中的生日是生日类的对象,两个类都有构造函数和显示函数。在主函数中声明一个人员类对象,显示数据。

    #include <iostream>
    #include <string>
    using namespace std;class birth {private:int year, month, day;
    public:birth(int y, int m, int d) :year(y), month(m), day(d) {}void show() {cout << year << "/" << month << "/" << day << endl;}
    };class person {private:char name[10];char sex[10];birth birdy;
    public:person(const char* p, const char* q, int y, int m, int d) :birdy(y, m, d) {strcpy_s(name, strlen(p) + 1, p);strcpy_s(sex, strlen(q) + 1, q);}void show() {cout << name << " " << sex << " ";birdy.show();}
    };void main() {person pe("Tom", "男", 1994, 11, 16);pe.show();
    }
    
  • 2020

    1.编写一个函数模板,实现将n个数据进行由小到大排序.

    #include <iostream>
    using namespace std;
    template<class T>
    void Sort(T a[], int n) {for (int i = 1; i < n; i++)for (int j = 0; j < n - i; j++)if (a[j] > a[j + 1]) {T temp;temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}
    };
    void  main() {int a[5] = { 10,-1,1,3,0 };Sort(a, 5);for (int i = 0; i < 5; i++)cout << a[i] << " ";
    }
    

    2.现有学校School类,保护成员Number(编号),Name(姓名)。它的派生类学生Student,数据成员Class_Num(班号),Total(总成绩),用成员函数Display()实现 学生张三,编号2020150601,班级四班,总成绩678 的输出,要求编写派生类的构造函数。

    #include <iostream>
    using namespace std;class School {public:School(int nu, const char* s) :Number(nu) {strcpy_s(Name, strlen(s) + 1, s);}
    protected:int Number;char Name[10];
    };class Student :public School {public:Student(int nu, const char* na, const char* cn, double t) :Total(t), School(nu, na) {strcpy_s(Class_Num, strlen(cn) + 1, cn); }void Display() {cout << Number << " " << Name << " " << Class_Num << " " << Total << endl;}
    protected:char Class_Num[10];double Total;
    };int  main() {Student stu(2020150601, "张三", "四班", 678);stu.Display();return 0;
    }

    dev c++

    #include <iostream>
    #include <cstring>
    using namespace std;class School {public:School(int nu = 0, const char* s = "") {Number = nu;strcpy(Name, s);}
    protected:int Number;char Name[10];
    };class Student :public School {public:Student(int nu = 0, const char* na = "", const char* cn = "", double t = 0) :School(nu, na) {strcpy(Class_Num, cn);Total = t;}void Display() {cout << Number << " " << Name << " " << Class_Num << " " << Total << endl;}
    protected:char Class_Num[10];double Total;
    };int  main() {Student stu(2020150601, "张三", "四班", 678);stu.Display();return 0;
    }
    
  • 2021
    1.从键盘输入学号,姓名,成绩,存入score.txt,学号10字节,姓名20字节,成绩整型.

    #include <iostream>
    #include <fstream>
    using namespace std;int main() {char number[10], name[20];int score; ofstream outf("score.txt", ios::out);if (!outf) {cout << "打开文件失败";return 0;  }cout << "输入学号,姓名,成绩:\n";while(cin >> number >> name >> score)outf << number << " " << name << " " << score<<endl;outf.close();return 0;
    }
    

    2.创建Employee类,保护数据成员:姓名,街道地址,市,省,邮政编码;函数:构造函数,ChangeName函数改变姓名,Display函数打印完整数据 ,定义在类外.

    #include <iostream>
    using namespace std;class Employee {protected:char name[10], address[50], city[10], province[10];int code;
    public:Employee(const char*, const char*, const char*, const char*, int cd);void ChangeName(const char*);void Display();
    };Employee::Employee(const char* n, const char* ad, const char* ct, const char* pve, int cd = 0) :code(cd) {strcpy_s(name, strlen(n) + 1, n);strcpy_s(address, strlen(ad) + 1, ad);strcpy_s(city, strlen(ct) + 1, ct);strcpy_s(province, strlen(pve) + 1, pve);}void Employee::ChangeName(const char* n) {strcpy_s(name, strlen(n) + 1, n);
    }void Employee::Display() {cout << name << " " << province << " " << city << " " << address << " " << code << endl;
    }int main() {Employee emp("张三", "某某区某街道某号", "长沙市", "湖南省", 435600);emp.Display();return 0;
    }

    dev c++

    #include <iostream>
    #include <cstring>
    using namespace std;class Employee {protected:char name[10], address[50], city[10], province[10];int code;
    public:Employee(const char*, const char*, const char*, const char*, int cd);void ChangeName(const char*);void Display();
    };Employee::Employee(const char* n = "", const char* ad = "", const char* ct = "", const char* pve = "", int cd = 0) {strcpy(name, n);strcpy(address, ad);strcpy(city, ct);strcpy(province, pve);code = cd;
    }void Employee::ChangeName(const char* n) {strcpy(name, n);
    }void Employee::Display() {cout << name << " " << province << " " << city << " " << address << " " << code << endl;
    }int main() {Employee emp("张三", "某某区某街道某号", "长沙市", "湖南省", 435600);emp.Display();return 0;
    }

C++试卷(程序设计题)相关推荐

  1. c语言二级考试程序设计题难吗,计算机二级考试:题库抽的不是题是“命”!附赠考试通关全攻略!...

    原标题:计算机二级考试:题库抽的不是题是"命"!附赠考试通关全攻略! 四六级的报名刚截止不久, 考研复试如火如荼地进行, 计算机二级考试如约而至. 今天是考试第一天 老新猜你一定有 ...

  2. 程序设计题:小学 100 以内正整数加减运算

    程序设计题:小学 100 以内正整数加减运算 1 问题描述 设计并实现"小学 100 以内整数加减运算"系统,程序要求如下: (1)随机出 10 道题,这 10 道题目不能重复,如 ...

  3. educoder SML程序设计题线下编译环境搭建

    背景 最近<串并行数据结构与算法设计>的老师在educoder上布置了一些SML程序设计题,虽然网站上有在线编译功能,但还是在线下编译调试方便,特记录编译环境过程如下(我用的GVIM,但N ...

  4. 计算机基础access数据库操作题,2021年3月全国计算机等级考试二级Access数据库程序设计题库及答案讲解...

    原标题:2021年3月全国计算机等级考试二级Access数据库程序设计题库及答案讲解 资料来源:学盛通学习网547所大学考研专业课(历年真题及模拟题可在线作答,系统自动评分,出答案及解析) 本题库是详 ...

  5. c语言上机题库程序设计,C语言上机程序设计题库及答案.docx

    C语言上机程序设计题库及答案 C语言上机程序设计题库[程序设计]功能:根据整型形参m,计算如下公式的值:y=sin(m)*10.[参考代码] double y=0; y=sin(m)*10; retu ...

  6. c语言-基本计算 pm2.5,C语言程序设计题(A卷).doc

    C语言程序设计题(A卷) 商洛学院2007-2008年度第一学期期末考试试题 课程:C语言程序设计(A卷) 适用班级:06物理教育(本)06生物(本) 一.填空题:(每空1分,共10分) 一个C源程序 ...

  7. 04737 c++ 自学考试2019版 第六章课后练习 程序设计题 1

    /* * 04737 c++ 自学考试2019版 第六章课后练习 * 程序设计题 1 * 需求:将第五章习题中设计的交通工具...... */#include<iostream> #inc ...

  8. 04737 c++ 自学考试2019版 第五章程序设计题 1

    /* * 04737 c++ 自学考试2019版 第五章课后练习 * 程序设计题 1 * 需求:交通工具包含汽车,汽车又包含..... */#include<iostream> #incl ...

  9. 04737 c++ 自学考试2019版 第四章课后练习 程序设计题 2

    /* * 04737 c++ 自学考试2019版 第四章课后练习 * 程序设计题 2 * 需求:为第二章习题设计中的二维坐标系下的类point,重载<< >> */ //标准流 ...

  10. 04737 c++ 自学考试2019版 第四章课后程序设计题1

    /** * 04737 c++ 自学考试2019版 * 第四章程序设计题1 * 为程序4-2中的类myComplex重载乘法运算符 */ #include <iostream>//标准流 ...

最新文章

  1. 二叉树的建立和遍历的各种问题
  2. AMD CPU 看清楚
  3. 基于注解的 IOC 配置——创建对象(Component、Controller、Service、Repository)注入数据(Autowired、Qualifier、Resource、Value)
  4. 使用postman发送json数据报错400
  5. fftw库在windows下的的编译和配置
  6. wcf获取MySQL中午乱码,如何启用WCF日志记录,以便它写入数据库?
  7. oracle 备份批处理,windows下oracle自动备份批处理
  8. python后台截屏_Python实现屏幕截图
  9. 关于java中判断字符串相等==和equal 详解
  10. VSS 2005 配置简明手册
  11. 剑指OFFER之二叉搜索树与双向链表(九度OJ1503)
  12. 打车界的“拼多多”?
  13. asp.net 404页面
  14. SpringBoot系列(3):SpringBoot2.1.x源码阅读环境搭建详解
  15. Matlab和Fluent联合仿真实现涡激振动
  16. Cannot access a disposed object. A common cause of this error is disposing a context that ...问题解决
  17. lodop 条码类型说明
  18. 后端开发面试自我介绍_java工程师面试自我介绍范文
  19. 数据查询网站汇总——自用
  20. 数据库服务器使用的RAID存储架构初步介绍

热门文章

  1. 《编程之美》 查找最大(小)的k个元素
  2. uniapp小程序仿抖音切换内容
  3. Chrome中的硬件加速合成
  4. 华为畅享9 plus鸿蒙系统,华为鸿蒙系统支持的手机型号_鸿蒙系统支持华为哪几款手机...
  5. 数据库服务器到底是用机械硬盘还是固态硬盘
  6. MPS模块化生产加工系统QY-JDYT34
  7. Swing + MySQL实现汽车租赁系统4.0
  8. selenium模拟登陆教务系统
  9. 报表相关的同比和环比
  10. 1.mac解除端口占用