2011复试代码练习

1.编写一个程序,利用下面的公式计算 ex 的值,精确到10^-10 (不写了)
思路:

  • 阶乘函数, cmath的pow(int x,int k) 来求x的k次方。
  • 循环,终止条件为temp>=10e-10,说明已经求到了小数点后十位
  • 用iomanip的setprecision(10)来输出

2. 编写一个程序,利用下面的公式计算pi的值,要求小数点后的位数为计算机可表达的最大范围
思路:

  • 分母为间隔2,分子不变,因此唯一与上题不同不需要用结成函数和pow,只是需要注意 -1 的n次方,来改变+ -情况的出现
#include<iostream>
using namespace std;
int main()
{
	double ans=0;
	double temp;
	double i=1;
	double k=1;
	do
	{
		temp=4/i;
		ans+= (temp*k);
		i+=2;
		k*=-1;
	}while(temp>=10e-5);
	cout<<"answer"<<ans<<endl;
	return 0;
}

3.编写一个递归函数模板,从一个数组中找出最小值,并返回该值的数组元素下标。

#include<iostream>
using namespace std;

template <class T>

int getmin(T a[],int start, int end)
{
	static int min = 0;
	if(start<end)
	{
		if(a[start]<a[min])
			min=start;
		getmin(a,start+1,end);
	}
	return min;
}

int main()
{
	int a[15] = {56,3,-2,77,6,5,89,666,23,22,-23,0,-1,1,1000};
	int min = getmin(a,0,15);
	for(int i=0;i<15;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
	cout<<min<<" "<<a[min]<<endl;
	return 0;
 } 

4.编写两个函数 SortOne 和 SortTwo,分别对字符串数组实现插入排序和选择排序。

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

void SortOne(string s[],int n) // 插入排序是以当前位置向前比较,直到找到合适的插入位置
{
	string temp;
	int j; 
	for(int i=1 ; i<n ; i++ )
	{
		j=i;
		string temp=s[i];
		while(j>0 && s[j-1]>temp)
		{
			s[j]=s[j-1];
			j--;
		}
		s[j]=temp;
	}
}
void SortTwo(string s[],int n) //选择排序主要是找到以当前位置循环找最小值,然后交换
{
	for(int i=0;i<n;i++)
	{
		int min_pos=i;
		for(int j=i+1;j<n;j++)
		{
			if(s[min_pos]>s[j])
				min_pos=j;
		}
		swap(s[min_pos],s[i]);
	} 
}
void swap(string &a,string &b)
{
	string temp;
	temp=a;
	a=b;
	b=temp;
}
void print(string s[],int k)
{
	for(int i=0;i<k;i++)
	{
		cout<<s[i]<<'\t';
	}
	cout<<endl;
}
int main()
{
	string s1[7] = {"abcd","e","abc","abd","bcd","bf","bbzzzz"};
	int k= sizeof(s1)/sizeof(*s1);
	SortOne(s1,k);
	print(s1,k);
	
	string s2[6] = {"eabc","af","ez","fee","fdee","aeee"};
	k= sizeof(s2)/sizeof(*s2);
	SortTwo(s2,k);
	print(s2,k);
	return 0;
}

5.对于一个数组 Array 类的 chess 对象
对于一个数组 Array 类的 chess 对象通过调用运算符重载函数(),可实现 chess(row,column)代替 chess[row][column],请完成:(1)、Array 类的基本定义,包括构造函数、析构函数、拷贝构造函数和基本数据成员;(2)、运算符重载函数()的 定义。

#include<iostream>
using namespace std;
class Array
{
	private:
		int row,col;
		int *a;
	public:
		Array(int r=0,int c=0)
		{
			if(r==0&&c==0)
			{
				a = new int [1];
				*a=0;
			}
			else
			{
				row=r;
				col=c;
				a = new int [row*col];
				for(int i=0 ; i<row*col ; i++) //把数组初始化 
				{
					a[i]=0;
				}
			}
		}
		~Array()
		{
			delete a;
			a=NULL;
		}
		Array(Array &other)
		{
			delete this->a;
			this->row=other.row;
			this->col=other.col;
			this->a = new int [row*col];
			for(int i=0;i<row*col;i++)
			{
				this->a[i]=other.a[i];
			}
		} 
		friend ostream &operator << (ostream &output, Array &b )
		{
			for(int i=0;i<b.row*b.col;i++)
			{
				if(i%b.col==0&&i!=0)
					cout<<endl;
				output<<b.a[i]<<'\t'; 
			}
			return output;
		}
		friend istream &operator >>(istream &input, Array &b)
		{
			cout<<"input "<<b.row<<"row "<<b.col<<"col :"<<endl;
			for(int i=0;i<b.row*b.col;i++)
			{
				input>>b.a[i];
			}
			return input;
		}
		int &operator ()(int r,int c)
		{
			return this->a[(r-1)*c+(c-1)];
		}
};
int main()
{
	Array a1(2,3);
	cin>>a1;
	cout<<a1<<endl;
	cout<<a1(1,1);
	return 0;
}

在这里插入图片描述

6.定义一个具有多态性的基类 Shape,派生出三个类:圆 Circle(坐标点和半径),矩形 Rec 类(两点不同坐标),三角形 Tri 类(三个不同坐标),每个类中至少有一个计算面 积的函数。编写程序,从文件 file.txt 中读取数据来创建各类的对象,并放在 Shape 指针向量中,最后循环处理每个对象并输出面积。
假设 file.txt 中的数据如下(加了两行便于测试):
C:123,5,40;T:1,2,32,50,60,3;R:6,8,8,100
C:2.2,1.1,3;T:1,1,3,1,2,2;R:1.3,1.9,3.3,2.9
C:100,0.01,1.2;T:1,1.1,4,1.1,1,5.1;R:0,0,-2,-2

  • 类的定义,注意一下三角型的计算就行,用node代表一个坐标点
  • 文件中字符串的按行读取
  • 字符串的分割
    我觉得应该把这个问题分割成这样,然后再写

//定义

#include<iostream>
#include<algorithm>
#include<fstream>
#include<cmath>
using namespace std;

struct Node
{
	double x;
	double y;
};
double getl(double x, double y,double m, double n)
{
	double l;
	l=(x-m)*(x-m)+(y-n)*(y-n);
	l=sqrt(l);
	return l;
}
double hailun(int a,int b,int c)
{
	if((a+b)<c || (a+c)<b || (b+c)<a) // 两边之和小于第三遍判定三角形 
	{
		return 0;
	}
	else
	{
		double p;
		p=(a+b+c)/2;
		return sqrt(p*(p-a)*(p-b)*(p-c));
	}
}
class Shape
{
	public:
		virtual double getarea()=0;
};
class Circle:public Shape
{
	private:
		Node n; // 圆形坐标 
		double r; // 圆的半径
	public:
		Circle(double c_x, double c_y, double c_r)
		{
			n.x=c_x;
			n.y=c_y;
			r=c_r;
		}
		double getarea()
		{
			return r*r*PI; 
		} 
};
class Rec:public Shape
{
	private:
		Node n[2];
	public:
		Rec(double r_x, double r_y ,double r_m ,double r_n)
		{
			n[0].x=r_x; n[0].y=r_y;
			n[1].x=r_m; n[1].y=r_n;
		}
		double getarea()
		{
			return fabs(n[0].x-n[1].x)*fabs(n[0].y-n[1].y);
		}
};
class Tri:public Shape
{
	private:
		Node n[3];
	public:
		Tri(double t_x, double t_y ,double t_m ,double t_n, double t_p,double t_q)
		{
			n[0].x=t_x; n[0].y=t_y;
			n[1].x=t_m; n[1].y=t_n;
			n[2].x=t_p; n[2].y=t_q;
		}
		double getarea()
		{
			double a=0,b=0,c=0;//代表三条边
			a=getl(n[0].x , n[0].y , n[1].x ,n[1].y);
			b=getl(n[1].x , n[1].y , n[2].x ,n[2].y);
			c=getl(n[0].x , n[0].y , n[2].x ,n[2].y);
			return hailun(a,b,c);
		}	
};
int main()
{
	Shape *s; //定义一个基类指针
	Circle c1(123,5,40);
	Rec r1(6,8,8,100);
	Tri t1(1,2,32,50,60,3);
	s=&c1;
	cout<<"Circle: "<<s->getarea()<<endl;
	s=&r1;
	cout<<"Rec: "<<s->getarea()<<endl;
	s=&t1;
	cout<<"Tri: "<<s->getarea()<<endl; 
	return 0;
}

// 文件

string ctos(char *buf)
{
	string str="";
	for(int i=0;buf[i];i++)
	{
		str+=buf[i];
	}
	return str;
}
void readtostring(char *filename)
{
	ifstream infile(filename)
	char buf[100];
	while(!infile.eof())
	{
		infile.getline(buf,100,'\n');
		string str=ctos(buf);
	}
}

俺不会写分割。。想了好久,改天再来。。留着

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值