永发信息网

C++程序设计!急!谢谢!

答案:1  悬赏:0  手机版
解决时间 2021-06-01 06:21
  • 提问者网友:临风不自傲
  • 2021-06-01 02:39

1. 编写函数以计算两个正整数的最大公约数,主函数接收键盘输入的两个正整数,调用函数输出它们的最大公约数。

2. 编写程序以计算S=1+22+33+……nn

3. 设计一个教师类,其属性有:编号、性别、职称、部门等,其中,部门要求是一个“部门”类的内切子对象,部门类要求有编号、名称、负责人等。要求用成员函数实现教师信息的录入和显示。要求设计出的类包含构造函数、内联成员函数、带默认值的成员函数等。

4. 声明一个点类和直线类,编写一程序,求一点到直线的距离。

最佳答案
  • 五星知识达人网友:第幾種人
  • 2021-06-01 03:34

#include<iostream.h>
int gys(int x,int y)
{
return x%y!=0?gys(y,x%y):y;
}
void main()
{
int a,b;
int result;
cin>>a>>b;
result=gys(a,b);
cout<<result;

}


-----------------


小心溢出


#include<iostream.h>
#include<math.h>
int sum(int n)
{
int sum1=0;
int i;
for(i=1;i<=n;i++)
sum1+=pow(i,2);
return sum1;
}
void main()
{
int n;
int result;
cin>>n;
result=sum(n);
cout<<result;
}



------------------


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

class DEP
{
public:
DEP(){Num=0;Name="Unkown";Charge="Unkown";};
inline void inputDEP(int n=0,string name="Unkown",string charge="Unkown");
int get_depnum();
string get_depname();
string get_depcharge();
private:
int Num; //编号
string Name; //名称
string Charge; //负责人
};
void DEP::inputDEP(int n,string name,string charge)
{
Num=n;
Name=name;
Charge=charge;
}
int DEP::get_depnum()
{
return Num;
}
string DEP::get_depname()
{
return Name;
}
string DEP::get_depcharge()
{
return Charge;
}
class teacher
{
public:
teacher(){Num=0;Sex=0;Title="Unkown";DEP();};
inline void input(int n,int s,string t,DEP d);
int get_num();
int get_sex();
string get_title();
void get_dep();
private:
int Num; //编号
int Sex; //性别
string Title; //职称
DEP Department; //部门
};
void teacher::input(int n,int s,string t,DEP d)
{
Num=n;
Sex=s;
Title=t;
Department=d;
}
int teacher::get_num()
{
return Num;
}
int teacher::get_sex()
{
return Sex;
}
string teacher::get_title()
{
return Title;
}
void teacher::get_dep()
{
cout<<Department.get_depnum();
cout<<Department.get_depname();
cout<<Department.get_depcharge();
}
---------------------------


4


#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <Windows.h>
using namespace std;
class Point
{
private: double x,y;
public:
Point(double q,double w)
{
x=q;
y=w;
cout<<"Point is :\n"<<endl;
cout<<"("<<x<<","<<y<<")"<<endl;
}
friend class Line;
};
class Line
{
private:double a,b,c;
public:
Line(double a,double b,double c)
{
cout<<"Line is :\n"<<endl;
cout<<a<<"x+"<<b<<"y+"<<c<<"=0"<<endl;
}
double Value(Point m)
{
double d;
d=fabs(a*m.x+b*m.y+c)/(sqrt(a*a+b*b));
return d;
}
};
int _tmain(int argc, _TCHAR* argv[])
{

Point point(5,6);
Line line(1,9,6);
double dis;
dis=line.Value(point);
cout<<dis<<endl;
Sleep(-1);
return 0;
}



我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯