求一道C++编程题目
Description:根据读入的高,勾画ST字符交替的倒三角形。Input:输入数据含有不超过50个正整数n(1≤n≤30)。Output:输出以n为高的ST字符交替的...
Description:
根据读入的高,勾画ST字符交替的倒三角形。
Input:
输入数据含有不超过50个正整数n(1≤n≤30)。
Output:
输出以n为高的ST字符交替的倒三角形,勾画每个三角形时都应另起一行。
Sample Input:
3 10Sample Output:
STSTS
STS
S
STSTSTSTSTSTSTSTSTS
STSTSTSTSTSTSTSTS
STSTSTSTSTSTSTS
STSTSTSTSTSTS
STSTSTSTSTS
STSTSTSTS
STSTSTS
STSTS
STS
S 展开
根据读入的高,勾画ST字符交替的倒三角形。
Input:
输入数据含有不超过50个正整数n(1≤n≤30)。
Output:
输出以n为高的ST字符交替的倒三角形,勾画每个三角形时都应另起一行。
Sample Input:
3 10Sample Output:
STSTS
STS
S
STSTSTSTSTSTSTSTSTS
STSTSTSTSTSTSTSTS
STSTSTSTSTSTSTS
STSTSTSTSTSTS
STSTSTSTSTS
STSTSTSTS
STSTSTS
STSTS
STS
S 展开
2个回答
展开全部
晕,老大你这要求也太多了吧~~
减 乘除都差不多,不会再HI我
#include<iostream>
using namespace std;
//numerator 分子
//denominator分母
//求最大公约数函数,用于约分
int gcd(int m,int n)
{
if(n==0)return m;
return gcd(n,m%n);
}
class fraction
{
private:
int numer;
int denom;
public:
fraction(int n=0,int d=1)//带默认参数的构造函数,整数与分数的运算迎刃而解
{
numer=n;
denom=d;
}
void rev(fraction &a)//倒数
friend ostream& operator<<(std::ostream&, const fraction&);
friend istream& operator>>(std::istream&, fraction&);
friend bool operator==(const fraction&, const fraction&);
friend bool operator<(const fraction&, const fraction&);
friend bool operator>(const fraction&, const fraction&);
friend bool operator>=(const fraction&, const fraction&);
friend bool operator<=(const fraction&, const fraction&);
friend fraction operator +(const fraction&,const fraction&);
friend fraction operator -(const fraction&,const fraction&);
friend fraction operator *(const fraction&,const fraction&);
friend fraction operator /(const fraction&,const fraction&);
friend bool operator!=(const fraction&, const fraction&);
};
fraction operator +(const fraction &a,const fraction &b)
{
fraction p;
p.numer=a.numer*b.denom+b.numer*a.denom;
p.denom=a.denom*b.denom;
return p;
}
ostream& operator<<(ostream& output,const fraction &num)
{
int a=gcd(num.numer,num.denom);
if((num.denom/a)==1)
output<<num.numer/a;
else
output<<num.numer/a<<"/"<<num.denom/a;
return output;
}
istream& operator>>(istream&input, fraction& num)
{
input>>num.numer>>num.denom;
return input;
}
bool operator==(const fraction&num1, const fraction&num2)
{
return (num1.numer*num2.denom)==(num2.numer*num1.denom);
}
bool operator!=(const fraction &num1, const fraction &num2)
{
return (num1.numer*num2.denom)!=(num2.numer*num1.denom);
}
bool operator<(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)<(b.numer*a.denom);
}
bool operator>(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)>(b.numer*a.denom);
}
bool operator<=(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)<=(b.numer*a.denom);
}
bool operator>=(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)>=(b.numer*a.denom);
}
int main()
{
fraction zrf(1,7),ssh(26,65);
fraction p;
p.rev(ssh);
cout<<"ssh="<<ssh<<";1/ssh="<<p<<endl;
cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;
cout<<"(zrf==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh)<<endl ;
cout<<"zrf+ssh="<<zrf+ssh<<endl;
cout<<"2+zrf="<<2+zrf<<endl;
return 0;
}
减 乘除都差不多,不会再HI我
#include<iostream>
using namespace std;
//numerator 分子
//denominator分母
//求最大公约数函数,用于约分
int gcd(int m,int n)
{
if(n==0)return m;
return gcd(n,m%n);
}
class fraction
{
private:
int numer;
int denom;
public:
fraction(int n=0,int d=1)//带默认参数的构造函数,整数与分数的运算迎刃而解
{
numer=n;
denom=d;
}
void rev(fraction &a)//倒数
friend ostream& operator<<(std::ostream&, const fraction&);
friend istream& operator>>(std::istream&, fraction&);
friend bool operator==(const fraction&, const fraction&);
friend bool operator<(const fraction&, const fraction&);
friend bool operator>(const fraction&, const fraction&);
friend bool operator>=(const fraction&, const fraction&);
friend bool operator<=(const fraction&, const fraction&);
friend fraction operator +(const fraction&,const fraction&);
friend fraction operator -(const fraction&,const fraction&);
friend fraction operator *(const fraction&,const fraction&);
friend fraction operator /(const fraction&,const fraction&);
friend bool operator!=(const fraction&, const fraction&);
};
fraction operator +(const fraction &a,const fraction &b)
{
fraction p;
p.numer=a.numer*b.denom+b.numer*a.denom;
p.denom=a.denom*b.denom;
return p;
}
ostream& operator<<(ostream& output,const fraction &num)
{
int a=gcd(num.numer,num.denom);
if((num.denom/a)==1)
output<<num.numer/a;
else
output<<num.numer/a<<"/"<<num.denom/a;
return output;
}
istream& operator>>(istream&input, fraction& num)
{
input>>num.numer>>num.denom;
return input;
}
bool operator==(const fraction&num1, const fraction&num2)
{
return (num1.numer*num2.denom)==(num2.numer*num1.denom);
}
bool operator!=(const fraction &num1, const fraction &num2)
{
return (num1.numer*num2.denom)!=(num2.numer*num1.denom);
}
bool operator<(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)<(b.numer*a.denom);
}
bool operator>(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)>(b.numer*a.denom);
}
bool operator<=(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)<=(b.numer*a.denom);
}
bool operator>=(const fraction&a, const fraction&b)
{
return (a.numer*b.denom)>=(b.numer*a.denom);
}
int main()
{
fraction zrf(1,7),ssh(26,65);
fraction p;
p.rev(ssh);
cout<<"ssh="<<ssh<<";1/ssh="<<p<<endl;
cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;
cout<<"(zrf==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh)<<endl ;
cout<<"zrf+ssh="<<zrf+ssh<<endl;
cout<<"2+zrf="<<2+zrf<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |