程序设计与算法(三)C++:第七章poj代码

课程:北京大学程序设计与算法(三)     MOOC

OJ: OpenJudge

027:简单的SumArray

模板函数代码:

T SumArray(
T* a,T* b)
{
	T sum = *a;
	for (T* c = ++a; c!= b; ++c) {
		sum += *c;
	}
	return sum;
}

完整代码如下:

#include <iostream>
#include <string>
using namespace std;
template <class T>
T SumArray(
T* a,T* b)
{
	T sum = *a;
	for (T* c = ++a; c!= b; ++c) {
		sum += *c;
	}
	return sum;
}
int main() {
	string array[4] = { "Tom","Jack","Mary","John"};
	cout << SumArray(array,array+4) << endl;
	int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
	cout << SumArray(a,a+4) << endl;
	return 0;
}

028:简单的foreach

所写函数代码:

template<class T ,class F>
void  MyForeach(T* a, T* b , F func)
{
	for (T* i = a; i != b; i++)
		func(*i);
}

完整函数代码:

#include <iostream>
#include <string>
using namespace std;
template<class T ,class F>
void  MyForeach(T* a, T* b , F func)
{
	for (T* i = a; i != b; i++)
		func(*i);
}

void Print(string s)
{
	cout << s;
}
void Inc(int & n)
{
	++ n;
}
string array[100];
int a[100];
int main() {
	int m,n;
	while(cin >> m >> n) {
		for(int i = 0;i < m; ++i)
			cin >> array[i];
		for(int j = 0; j < n; ++j)
			cin >> a[j];
		MyForeach(array,array+m,Print);		 
		cout << endl;
		MyForeach(a,a+n,Inc);		 
		for(int i = 0;i < n; ++i)
			cout << a[i] << ",";
		cout << endl;
	}
	return 0;
}

029:简单的Filter

所写部分代码:

template<class T,class F>
T* Filter(T* a, T* b, T* c, F func)
{
	for(T* i=a;i!=b;i++)
		if (func(*i))
		{
			*c = *i;
			c++;
	     }
	return c;
}

完整代码如下:

#include <iostream>
#include <string>
using namespace std;
template<class T,class F>
T* Filter(T* a, T* b, T* c, F func)
{
	for(T* i=a;i!=b;i++)
		if (func(*i))
		{
			*c = *i;
			c++;
	     }
	return c;
}

bool LargerThan2(int n)
{
	return n > 2;
}
bool LongerThan3(string s) 
{
	return s.length() > 3;
}

string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
string as2[5];
int  a1[5] = { 1,2,3,4,5};
int a2[5];
int main() {
	string * p = Filter(as1,as1+5,as2,LongerThan3);
	for(int i = 0;i < p - as2; ++i)
		cout << as2[i];
	cout << endl; 
	int * p2 = Filter(a1,a1+5,a2,LargerThan2);
	for(int i = 0;i < p2-a2; ++i)
		cout << a2[i] << ",";
	return 0;
}

030:你真的搞清楚为啥 while(cin >> n) 能成立了吗?

所写代码如下:

public:
    bool b=1;
     MyCin& operator>>(int & a)
    {
         cin >> a;
         if (a == -1)b = 0;
         return *this;  
    }
     operator bool()
     {
         return b;
     }

完整代码如下:

#include <iostream>
using namespace std;
class MyCin
{
public:
    bool b=1;
     MyCin& operator>>(int & a)
    {
         cin >> a;
         if (a == -1)b = 0;
         return *this;  
    }
     operator bool()
     {
         return b;
     }
};
int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

031:山寨版istream_iterator

所写代码如下:

public:
    T value;

    CMyistream_iterator(istream& cin){
        cin >> value;
    }

    T operator  *() {
        return value;
    }

      CMyistream_iterator & operator++(int ) { 
        cin >> value;
        return * this;
    }

完整代码如下:

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
public:
    T value;

    CMyistream_iterator(istream& cin){
        cin >> value;
    }

    T operator  *() {
        return value;
    }

      CMyistream_iterator & operator++(int ) { 
        cin >> value;
        return * this;
    }
};



int main()  
{ 
	int t;
	cin >> t;
	while( t -- ) {
		 CMyistream_iterator<int> inputInt(cin);
		 int n1,n2,n3;
		 n1 = * inputInt; //读入 n1
		 int tmp = * inputInt;
		 cout << tmp << endl;
		 inputInt ++;   
		 n2 = * inputInt; //读入 n2
		 inputInt ++;
		 n3 = * inputInt; //读入 n3
		 cout << n1 << " " << n2<< " " << n3 << " ";
		 CMyistream_iterator<string> inputStr(cin);
		 string s1,s2;
		 s1 = * inputStr;
		 inputStr ++;
		 s2 = * inputStr;
		 cout << s1 << " " << s2 << endl;
	}
	 return 0;  
}

 032:这个模板并不难

所写代码如下:

public:
T *p;
int size;
 myclass(){}
 myclass(T *t,int l)
 {
     p=new T[l];
     for (int i = 0; i < l; ++i) {
			p[i] = t[i];
		}
     size=l;
 }

完整代码如下:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass {
public:
T *p;
int size;
 myclass(){}
 myclass(T *t,int l)
 {
     p=new T[l];
     for (int i = 0; i < l; ++i) {
			p[i] = t[i];
		}
     size=l;
 }
~myclass( ) {
		delete [] p;
	}
	void Show()
	{
		for( int i = 0;i < size;i ++ ) {
			cout << p[i] << ",";
		}
		cout << endl;
	}
};
int a[100];
int main() {
	char line[100];
	while( cin >> line ) {
		myclass<char> obj(line,strlen(line));;
		obj.Show();
		int n;
		cin >> n;
		for(int i = 0;i < n; ++i)
			cin >> a[i];
		myclass<int> obj2(a,n);
		obj2.Show();
	}
	return 0;
}

033:排序,又见排序!

所写代码如下:

T1 * be,T1 * en,T2  f )
{
   for(T1 *i=be;i!=en;i++)
   {
    for(T1 *j=i+1;j!=en;j++)
    {
        if(!f(*i,*j))
        {
            T1 t=*i;
            *i=*j;
           *j=t;
        }
    }
   }
}

完整代码如下:

#include <iostream>
using namespace std;

bool Greater2(int n1,int n2) 
{
	return n1 > n2;
}
bool Greater1(int n1,int n2) 
{
	return n1 < n2;
}
bool Greater3(double d1,double d2)
{
	return d1 < d2;
}

template <class T1,class T2>
void mysort(
T1 * be,T1 * en,T2  f )
{
   for(T1 *i=be;i!=en;i++)
   {
    for(T1 *j=i+1;j!=en;j++)
    {
        if(!f(*i,*j))
        {
            T1 t=*i;
            *i=*j;
           *j=t;
        }
    }
   }
}
#define NUM 5
int main()
{
    int an[NUM] = { 8,123,11,10,4 };
    mysort(an,an+NUM,Greater1); //从小到大排序 
    for( int i = 0;i < NUM; i ++ )
       cout << an[i] << ",";
    mysort(an,an+NUM,Greater2); //从大到小排序 
    cout << endl;
    for( int i = 0;i < NUM; i ++ )
        cout << an[i] << ","; 
    cout << endl;
    double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
    mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 
    for( int i = 0;i < 6; i ++ )
         cout << d[i] << ","; 
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值