程序设计算法(三)C++编程作业(1-10)

002.难一点的swap

    #include <iostream>
    using namespace std;

    void swap(int* &a,int* &b )  /*a b是指针类型的引用,相当于引用参数,
    可以改变输入参数,因此程序运行后是5,3*/
    /*void swap(int * a,int* b)  虽然a b是指针,但是由于函数内部交换的是指针的值,
    因此相当于传值参数,无法改写输入参数,导致程序运行后是3,5  */
    {
    	int * tmp = a;
    	a = b;
    	b = tmp;
    }
    int main()
    {
    	int a = 3,b = 5;
    	int * pa = & a;
    	int * pb = & b;
    	swap(pa,pb);
    	cout << *pa << "," << * pb;
    	return 0;
    }

005.学生信息处理程序

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <sstream>
    #include <cstdlib>
    using namespace std;

    class Student {
          private:
                  char name[20];
                  int age,number;
                  float first_score,second_score,third_score,forth_score,average_score; //如果定义为整数,则求平均值先转化为float.
          public:
                 char dot;
                 void input()
                 {
                      cin.getline(name,20,',');  
                      /*cin.getline()此函数会一次读取多个字符(包括空白字符)。它以指
                      定的地址为存放第一个读取的字符的位置,依次向后存放读取的字
                      符,直到读满N-1(N为第二个参数),或者遇到指定的结束符(第三个参数)
                      为止。若不指定结束符,则默认结束符为'\n'。*/
                      cin>>age>>dot;
                      cin>>number>>dot;
                      cin>>first_score>>dot;
                      cin>>second_score>>dot;
                      cin>>third_score>>dot;
                      cin>>forth_score;
                 };
                 void calculate()
                 {
                      average_score=(first_score+second_score+third_score+forth_score)/4;
                 };
                 void output()
                 {
                      cout<<name<<","<<age<<","<<number<<","<<average_score<<endl;
                 };                  
                  
    };

    int main() {
    	Student student;        // 定义类的对象
    	student.input();        // 输入数据
    	student.calculate();    // 计算平均成绩
    	student.output();       // 输出数据
    
    	system("pause");
    	return 0;
    }

006.奇怪的类复制

    #include <iostream>
    using namespace std;
    class Sample {
    public:
    	int v;
   	public:
           //Sample(){};  如果有这行代码,后面运行到Sample d时会报错,
           因为不知道调用Sample()还是Sample(int i=0)
           Sample(int i=0){v=i;};
           Sample(const Sample &c)
           {
                         v=c.v+2;             
           };
    };
    void PrintAndDouble(Sample o)
    {
    	cout << o.v;
    	cout << endl;
    }
    int main()
    {
    	Sample a(5); //a.v=5
    	Sample b = a; //执行拷贝构造函数后,b.v=7
    	PrintAndDouble(b);//b.v=9
    	/*会调用拷贝构造函数,
    	属于把对象作为值参数传给函数的情况,
    	另外两种调用拷贝构造函数的情况是定义对象和把对象作为函数返回值 */
    	Sample c = 20;
    	PrintAndDouble(c);//c.v=22
    	Sample d;
    	d = a;
    	cout << d.v;
    	system("pause");
    	return 0;
    }

007返回什么才好呢

#include <iostream>
using namespace std;
class A {
public:
	int val;

	A(int n=123)
        {
              val=n;
        };
        A& GetObj()
        {
           return *this;    
        };
};
int main()
{
	int m,n;
	A a;
	cout << a.val << endl;
	while(cin >> m >> n) {
		a.GetObj() = m;
		cout << a.val << endl;
		a.GetObj() = A(n);
		cout << a.val<< endl;
	}
	return 0;
}

008超简单的复数类

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex
{
private:
    double r,i;
public:
    void Print()
    {
        cout << r << "+" << i << "i" << endl;
    }
    
    Complex(){};
    Complex(char s[])
    {
     r = s[0] - '0';
     i = s[2] - '0'; //由于main函数里a+bi的a和b是个位数,因此可以用这种方法,
     若是通过自己输入的值来初始化,则可以通过字符串对+的定位实现实部虚部分离
    }
    /*
        Complex& operator=(const char*s)
    {
             string str=s;
             int middle=str.find("+",0);
             string strReal=str.substr(0,middle);
             r=atof(strReal.c_str());
             /*atof 是ascII to float的缩写,
             它将ascII字符串转换为相应的单精度浮点数,比如传入"1.234",
             经过处理后就返回float类型的数1.234 */
             string strImag=str.substr(middle+1,str.length()-middle-2);
             i=atof(strImag.c_str());
             return *this;
    }
    */
};
    int main()
    {
    Complex a;
    a = "3+4i"; a.Print();
    a = "5+6i"; a.Print(); 
    system("pause");
    return 0;
    }

009哪来的输出
注意如果是在return前加system(“pause”),显示只有2,因为只有return后才会析构对象a

#include <iostream>
using namespace std;
class A {
	public:
		int i;
		A(int x) { i = x; }
        ~A(){cout<<i<<endl;}
};
int main()
{
	A a(1);
	A * pa = new A(2);
	delete pa;
	return 0;
}

010同第九题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值