zoukankan      html  css  js  c++  java
  • 第二周:程序设计预算法(三)

    题面

    001:编程填空:学生信息处理程序

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <sstream>
     6 #include <cstdlib>
     7 using namespace std;
     8 
     9 class Student {
    10 // 在此处补充你的代码
    11 private:
    12     char name[25];
    13     int age,number;
    14     double a1,a2,a3,a4,ave;
    15     char c;
    16 public:
    17     void input()
    18     {
    19         cin.getline(name,20,',');//cin.getline输入长度指定字符结束
    20         cin>>age>>c>>number>>c;
    21         cin>>a1>>c>>a2>>c>>a3>>c>>a4;
    22     }
    23     void calculate()
    24     {
    25         ave=(a1+a2+a3+a4)/4;
    26     }
    27     void output()
    28     {
    29         cout<<name<<c<<age<<c<<number<<c<<ave;
    30     }
    31 };
    32 
    33 int main() {
    34     Student student;        // 定义类的对象
    35     student.input();        // 输入数据
    36     student.calculate();    // 计算平均成绩
    37     student.output();       // 输出数据
    38 }

    002:奇怪的类复制

     1 #include <iostream>
     2 using namespace std;
     3 class Sample {
     4 public:
     5     int v;
     6 // 在此处补充你的代码
     7     Sample(int n=0)//构造函数
     8     {
     9         v=n;
    10     }
    11     Sample(const Sample &x)//类的对象的构造函数
    12     {
    13         v=x.v+2;
    14     }
    15 };
    16 void PrintAndDouble(Sample o)
    17 {
    18     cout << o.v;
    19     cout << endl;
    20 }
    21 int main()
    22 {
    23     Sample a(5);
    24     Sample b = a;
    25     PrintAndDouble(b);
    26     Sample c = 20;
    27     PrintAndDouble(c);
    28     Sample d;
    29     d = a;
    30     cout << d.v;
    31     return 0;
    32 }

    003:超简单的复数类

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     4 using namespace std;
     5 class Complex {
     6 private:
     7     double r,i;
     8 public:
     9     void Print() {
    10         cout << r << "+" << i << "i" << endl;
    11     }
    12 // 在此处补充你的代码
    13     Complex(){}//空构造函数
    14 
    15     Complex(const char x[])
    16     {
    17         r=x[0]-'0';//和0的ASCII相减得到对应的数
    18         i=x[2]-'0';
    19     }
    20 };
    21 int main() {
    22     Complex a;
    23     a = "3+4i"; a.Print();//构造函数的应用,赋值时利用构造函数赋值
    24     a = "5+6i"; a.Print();
    25     return 0;
    26 }

    004:哪来的输出

     1 #include <iostream>
     2 using namespace std;
     3 class A {
     4     public:
     5         int i;
     6         A(int x) { i = x; }
     7 // 在此处补充你的代码
     8         ~A()//析构函数的输出
     9         {
    10             cout<<i<<endl;
    11         }
    12 };
    13 int main()
    14 {
    15     A a(1);
    16     A * pa = new A(2);
    17     delete pa;
    18     return 0;
    19 }

  • 相关阅读:
    cron表达式
    mybatis中的#{}和${}
    mysql免安装版使用
    关于jpa添加一对多数据时外键值为空的问题
    关于时间格式化和格式转换
    属性和属性节点
    ErrorException: ORA-00904: : 标识符无效
    345. 反转字符串中的元音字母
    java 解决nginx代理的跨域访问问题
    633. 平方数之和
  • 原文地址:https://www.cnblogs.com/waryan/p/12182266.html
Copyright © 2011-2022 走看看