世界各国导弹技术排名:c++高手帮个忙

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/02 07:52:03
大家好 请给出全部详细代码 麻烦大家了啊

1. 编写一个求三角形面积的C++函数,其函数原型定义如下:
double TriangleArea(double a,double b,double c);
其中,a,b,c分别表示三角形三条边的长度,函数的返回值为三角形的面积。
2. 求正整数m和n的最大公约数及最小公倍数。函数原形如下:
int GreatestCommonDivisor(int m,int n); //返回最大公约数
int LeastCommonMultiple(int m,int n) ; //返回最小公倍数
给出函数的C++代码,并加必要的注释。
3. 定义一个队列类,并给出全部实现代码。
4. 编写一个函数,求出一个正整数的所有因子。函数原型为
int AllFactors(int a[],int k);
其中,所有正整数存放在数组a中,k为数组长度。给出该函数的C++实现代码,结果放在数组a中,函数的返回值为因子的个数。
5. 写一个将整数转换成字符串的函数。函数原型为
int IntegerToString(int k,char s[]);
其中,k为待转换的整数,结果放在数组s中。给出该函数的C++实现代码。
6. 定义一个堆栈类,并给出全部实现代码。

麻烦大家了啊
我会给高分的

1.
double TriangleArea(double a,double b,double c)
{
float t=(a+b+c)/2;
return sqrt(t*(t-a)*(t-b)*(t-c));//利用海轮公式
}

2.
int GreatestCommonDivisor(int m,int n)
{
//辗转相除法
int mi,ma,k;
ma=m>n?m:n;
mi=m<n?m:n;
while(1)
{

k=ma%mi;
if(k==0)
return mi;
ma=mi;
mi=k;

}
return 1;
}

int LeastCommonMultiple(int m,int n)
//两个数的积等于它们最大公约数与最小公倍数的积.
{
int k=GreatestCommonDivisor(m,n);
return m*n/k;
}

3.
struct element
{
int x,y;
element *next;
};
class Que
{
int count;
element *head;
public:
Que(){head=new element;head->next=NULL;}
int GetCount(){return count;}
void push(element *e);
element *pop();
};
void push(element *e)
{
if(!e)
return;

count++;
element *temp=new element;
temp=head;
while(temp->next!=NULL)temp++;
temp->next=e;

}
element *pop()
{
element *t=head;
head=head->next;
return t;

}

4.int AllFactors(int a[],int k)
{
//没测试过,谁边写写
int c=1;
a[0]=1;
while(1)
{
for(int i=2;;i++)
if(!k%a[i-2]*i)
{
a[i-2]=i;
a[i]=k/a[i-2]*i;
if(a[i]==1)break;
c+=2;
}
return c;
}
}

int IntegerToString(int k,char s[])
{
int t,i=0;
while(1)
{
t=k%10;
s[i]=t+32;
t=k/10;
if(t==0)break;
i++;
}
//方法1:using stl
//s.reverse(s.begin,s.end);
//方法2:
int c=i;
while(i--)
{
t=s[0];
s[i]=t;
s[0]=s[i];
}
return c;
}

6.

struct element
{
int x,y;
element *prep;
};
class Que
{
int count;
element *head;
public:
Que(){head=new element;head->prep=NULL;}
int GetCount(){return count;}
void push(element *e);
element *pop();
};
void push(element *e)
{
if(!e)
return;

count++;
e->prep=head;
head=e;
}
element *pop()
{
element *t=head;
head=head->prep;
if(head->prep==NULL)
delete head;
return t;
}

hehe !
我还以为我来帮你做呢,
那些人做的比我还好!
我都不好意思贴出来了,你是不是也是才学的呀,我也是.
那些题都是最近才学过.
还是自己做吧!有收获!
/*编写一个求三角形面积的C++函数*/

#include "iostream.h"
#include "math.h"
void main()
{
double a,b,c,s;
double area;
cout<<"输入边长"<<endl;
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"输出结果"<<area<<endl;
}

第一个问题,海伦公式
#include<math.h>
#include<iostream.h>
double TriangleArea(double a,double b,double c)
{
double s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
};
void main()
{
double a,b,c;
cout<<"ÇëÊäÈëÈý½ÇÐÎÈý±ß³¤¶È:";
cin>>a>>b>>c;
cout<<"Èý½ÇÐÎÃæ»ýΪ:"<<TriangleArea(a,b,c);
}
第三个问题,使用模板定义队列类
#include <assert.h>
template <class Type> class Queue{
private:
int rear,front;
Type *elements;
int maxSize;
public:
Queue(int sz=10);
~Queue() {delete[] elements; }
void EnQueue(const Type& item);
int DeQueue();
Type GetFront();
void MakeEmpty() {front=rear=0;}
int IsEmpty() const {return front==rear;}
int IsFull() const {return (rear+1)%maxSize==front; }
int Length() const {return (rear-front+maxSize)%maxSize; }
void Transfer();
};
template <class Type>Queue<Type>::Queue(int sz):front(0),rear(0),maxSize(sz){
elements=new Type[maxSize];
assert(elements!=0);
}

template <class Type> void Queue<Type>::EnQueue(const Type& item) {
assert(!IsFull());
rear=(rear+1)%maxSize;
elements[rear]=item;
}
template <class Type> int Queue<Type>::DeQueue() {
if (IsEmpty()) return 0;
front=(front+1)%maxSize;
return 1;
}
template <class Type> Type Queue<Type>::GetFront() {
if (IsEmpty()) return NULL;
return elements[(front+1)%maxSize];
}
template <class Type> void Queue<Type>::Transfer() {
if (!IsEmpty()){
cout<<"Êä³ö¶ÓÁÐÔªËØ£º"<<endl;
int p; p=(front+1)%maxSize;
while (p!=rear) {cout<<elements[p]<<" , "; p=(p+1)%maxSize;}
cout<<elements[rear]<<endl;
}
}

求三角形面积可以用海伦公式。

又一个叫人写作业的 呵呵