2010复试程序练习

1、输入n个十进制数转换成二进制写到文件,n是随机得到

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<fstream>
#include<string>
using namespace std;
string reverse(string temp)
{
	string t="";
	for(int i=0;i<temp.length();i++)
	{
		t+=temp[temp.length()-1-i];
	}
	return t;
}
string ToBinary(int n)
{
	string temp;
	while(n)
	{
		if(n%2==1)
			temp+="1";
		if(n%2==0)
			temp+="0";
		n/=2;
	}
	return reverse(temp);
}
int main()
{
	int n;
	int number;
	cout<<"please input n- number :";
	cin>>n;
	srand((int)time(NULL));
	
	ofstream outfile("D:\\mytest1.txt");
	for(int i=0; i<n ; i++)
	{
		number=rand();
		cout<<number<<endl;
		outfile<<number<<'\t';
		outfile<<ToBinary(number)<<endl;
	}
	outfile.close();
	return 0;
}

进制的话,还可以用setbase(int n)使用

2、写两个模板函数:插入排序法的迭代实现与递归实现
插入排序的基本思想是:每一步将待排元素插入到适当的位置中。

迭代

template<class T>
void InsertSort(T a[], int n)
{
	int i,j;
	T temp;
	for(int i=1; i<n ; i++)
	{
		int j=i; // 从后往前排序
		T temp = a[i];
		while(j>0&&temp<a[j-1])
		{
			a[j]=a[j-1];
			j--;
		}
		a[j]=temp;
	}
}

递归

#include<iostream>
using namespace std;
template<class T>
void insertsort(T a[],int n)
{
	static int i=1;
	if(i<n)
	{
		int j=i;
		T temp=a[i];
		while(j>0&&temp<a[j-1])
		{
			a[j]=a[j-1];
			j--;
		}
		a[j]=temp;
		i++;
		insertsort(a,n);
	}
}
int main()
{
	int a[]={9,1,2,3,0,-1,5};
	for(int i=0;i<7;i++)
	{
		cout<<a[i]<<" "; 
	}
	cout<<endl;
	insertsort(a,7);
	for(int i=0;i<7;i++)
	{
		cout<<a[i]<<" "; 
	}
}

不知道这样对不- -

3、文件中有类似的一行行字符串“(010)(15012345678)|123|(430070)”,按以下格式输出:“区号| 电话号码| 城市编号| 邮编”
(具体的字符串格式记不清了,但就是考字符串的解析)

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
string readfileintostring(char *filename) //读取文件,以字符串形式输出
{
	ifstream infile(filename);
	ostringstream buf;
	char ch;
	while(buf&&infile.get(ch))
		buf.put(ch);
	return buf.str();
}
int main()
{
	string str;
	int pos1,pos2;
	string a,b,c,d; //“区号| 电话号码| 城市编号| 邮编"
	char *filename = "D:\\mytest2.txt"  ;
	
	str=readfileintostring(filename);
	cout<<str<<endl;
	
	pos1=str.find_first_of('(',0);
	pos2=str.find_first_of(')',0);
	a=str.substr(pos1+1,pos2-pos1-1);
	
	pos1=str.find_first_of('(',pos2);
	pos2=str.find_first_of(')',pos1);
	b=str.substr(pos1+1,pos2-pos1-1);
	
	pos1=str.find_first_of('|',pos2);
	pos2=str.find_first_of('|',pos1+1);
	c=str.substr(pos1+1,pos2-pos1-1);
	
	pos1=str.find_first_of('(',pos2);
	pos2=str.find_first_of(')',pos1);
	d=str.substr(pos1+1,pos2-pos1-1);
		
	cout<<a<<" | "<<b<<" | "<<c<<" | "<<d<<endl;
	return 0;
}

4、设计一个多项式类Polynomial(包括构造函数、复制构造函数、析构函数、赋值函数、实现两个多项式相加)
(写了一个巨简单的,改天再改。)

#include<iostream>
#include<cstdio>
using namespace std;
class Poly
{
	public:
		int N[10];// 这里我设置的是最多10次方即指数,每个位置代表其系数 
		int length;
	public:
		Poly()
		{
			 int length=10;
			 for(int i=0;i<length;i++)
			 {
			 	N[i]=0;
			 }
		}
		Poly(Poly &other)
		{
			this->length=other.length;
			for(int i=0;i<length;i++)
			{
				N[i]=other.N[i];
			}
		}
		void addPoly(int coef,int exp) // 保证在10次方里哦 
		{
			if(exp<10)
			{
				N[exp]+=coef;
			}
		}
		Poly & operator+(Poly &other)
		{
			for(int i=0;i<10;i++)
			{
				N[i]+=other.N[i];
			}
		}
		void printpoly()
		{
			for(int i=0;i<10;i++)
			{
				if(N[i]==0)
				 	continue;
				else
				{
					cout<<i<<"x^"<<N[i]<<" ";
				}
			}
			cout<<endl;
		}
};
int main()
{
	Poly p1;
	int coef,exp,n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d %d",&coef,&exp);
		p1.addPoly(coef,exp); 
	}
	p1.printpoly();
	Poly p2;
	for(int i=0;i<n;i++)
	{
		scanf("%d %d",&coef,&exp);
		p1.addPoly(coef,exp); 
	}
	Poly p3;
	p3=p1+p2;
	p3.printpoly(); 
	return 0;
}	

在这里插入图片描述
C的多项式加法

#include<stdlib.h>
#include<stdio.h>
typedef struct PolyNode{
	float coef;
	int exp;
	struct PolyNode *next;
};
typedef struct Polynomial;

Polynomial Add(Polynomial A,Polynomial B)
{
	Polynomial C,S;
	Polynomial Pa,Pb,Pc;
	Pa = A->next;
	Pb = B->next;
	C = (Polynomial *)malloc(sizeof(PolyNode));
	Pc= C;
	Pc->next =NULL;
	int x;
	while(Pa&&Pb)
	{
		if(Pa->exp==Pb->exp)
		{
			x=Pa->coef+Pb->coef; 
			if(x!=0)
			{
				S=(Polynomial *)malloc(sizeof(PolyNode));
				S->coef= x;
				S->exp = Pa->exp;
				S->next=NULL;
				Pc->next = S ;
				Pc = S; 
			}
		Pa=Pa->next;
		Pb=Pb->next;
		}
		else if (Pa->exp<Pb->exp)
		{
			S=(Polynomial *)malloc(sizeof(PolyNode));
			S->coef = Pa ->coef;
			S->exp = Pa ->exp;
			S->next = NULL;
			Pc->next = S;
			Pc=S;
			Pa= Pa->next;
		}
		else
		{
			S=(Polynomial *)malloc(sizeof(PolyNode));
			S->coef = Pb ->coef;
			S->exp = Pb ->exp;
			S->next = NULL;
			Pc->next = S;
			Pc=S;
			Pb= Pb->next;
		}
	}
	while(Pa)
	{
		S=(Polynomial *)malloc(sizeof(PolyNode));
		S->coef = Pa ->coef;
		S->exp = Pa ->exp;
		S->next = NULL;
		Pc->next = S;
		Pc=S;
		Pa= Pa->next;
	} 
	while(Pb)
	{
		S=(Polynomial *)malloc(sizeof(PolyNode));
		S->coef = Pb ->coef;
		S->exp = Pb ->exp;
		S->next = NULL;
		Pc->next = S;
		Pc=S;
		Pb= Pb->next;
	}
	return C;
}

5、几个类(Vehicle类 Car类 Streetwheel类 Brake类)有着必然的联系,设计类与实现
简单写了一下,vehicle是抽象类,streetwheel用来生成car的数据成员,brake普通类来继承特点。

//几个类(Vehicle类 Car类 Streetwheel类 Brake类)有着必然的联系,设计类与实现
#include<iostream>
using namespace std;

class Vehicle{
	virtual void run()=0;
};

class StreetWheel{
	private:
		int number; // 轮子的个数
	public:
		StreetWheel(int n):number(n){} 
		void getwheel()
		{
			cout<<"have "<<number<<" wheels"<<endl;
		}
};

class Brake
{
	public:	
		virtual void stop()
		{
			cout<<"stop-----------"<<endl;
		}
}; 
class Car : public Vehicle,public Brake{
	private:
		StreetWheel sw;
		string brand;
	public:
		Car(StreetWheel s,string b):sw(s)
		{
			brand = b;
		}
		void run()
		{
			cout<<brand<<"is running------------"<<endl;
		} 
		void info()
		{
			cout<<"This Car brand is : "<<brand<<endl;
			sw.getwheel();
			cout<<endl;
		}
};
int main()
{
	StreetWheel sw1(4);
	Car c(sw1,"xuefulan");
	c.info();
	c.run();
	c.stop();
	return 0;
}

6、一个基类 Shape,在基类的基础上继承写一个二维图形类,再继承写一个三维图形类,设计与实现

// 一个基类Shape,在基类的基础上继承写一个二维图形类,再继承写一个三维图形类,设计与实现
#include<iostream>
using namespace std;
class Shape
{
	public:
		virtual void calculate()=0;
};
class _2Shape: public Shape
{
	private:
		double height;
		double weight;
	public:
		_2Shape(double h=0,double w=0):height(h),weight(w){}
		void calculate()
		{
			double area = weight*height;
			cout<<"area: "<<area <<endl;
		}
		void info()
		{
			cout<<"height: "<<height<<endl;
			cout<<"weight: "<<weight<<endl;
		}
		double getheight()
		{
			return height;
		}
		double getweight()
		{
			return weight;
		}
};
class _3Shape:public _2Shape
{
	private:
		double length;
	public:
		_3Shape(double l,double w,double h):_2Shape(h,w)
		{
			length=l;
		}
		void calculate()
		{
			double vol=0;
			vol = length*_2Shape::getheight()*_2Shape::getweight();
			cout<<"vol: "<<vol<<endl;
		}
		void info()
		{
			_2Shape::info();
			cout<<"length: "<<length<<endl;
		}
};
int main()
{
	_2Shape a(3,4);
	a.info();
	a.calculate();
	cout<<"---------------------"<<endl;
	_3Shape b(5,4,3);
	b.info();
	b.calculate();
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值