请C语言高手帮我编写几个小程序~(一定要用C++编写噢~)

供稿:hz-xin.com     日期:2025-01-12
请大侠帮我写几个C++的程序,谢谢

1、
#include
#include

void main()
{
char str[81];
int length;
int i;
printf( "please input a string:
" );
scanf( "%s", str );
length = strlen( str );
printf( "the result is:
" );
for( i = length - 1; i >= 0; i-- )
printf( "%c", str[i] );
return;
}

#include
using namespace std;
class Complex
{
public:
Complex( float x=0,float y=0):real(x),imag(y){}
Complex operator=(const Complex&);
~Complex(){}
friend istream& operator>>(istream &,const Complex&);
friend ostream& operator<<(ostream &,const Complex&);
friend Complex operator+(const Complex&,const Complex&);
friend Complex operator-(const Complex&,const Complex&);
friend Complex operator*(const Complex&,const Complex&);
friend Complex operator/(const Complex&,const Complex&);
Complex operator++();
Complex operator++(int);
Complex operator--();
Complex operator--(int);
Complex Power(int n);
float Fabs()const;
Complex Conjugate() const;//共轭复数
float Get_real()const {return real;}
float Get_imag()const {return imag;}
private:
float real,imag;
};


Complex Complex::operator=(const Complex&m)
{
real=m.Get_real();
imag=m.Get_imag();
return *this;
}


istream& operator>>( istream &input,const Complex &c)
{
input>>c.real>>c.imag;
return input;
}
ostream& operator<<( ostream &output,const Complex&c)
{
output<<"("<<c.real;
if(c.imag>=0)
cout<<"+";

cout<<c.imag<<"i"<<")"<<endl;
return output;
}
Complex operator+(const Complex &m,const Complex&n)
{
return Complex(m.real+n.real,m.imag+n.imag);
}

Complex operator-(const Complex &m,const Complex &n)
{
return Complex(m.real-n.real,m.imag-n.imag);
}

Complex operator*(const Complex &m,const Complex&n)
{
return Complex(m.real*n.real-m.imag*n.imag,m.real*n.imag+m.imag*n.real);
}

Complex operator/(const Complex &m,const Complex&n)
{

Complex temp;
temp=m*n.Conjugate();
temp.real=temp.real/n.Fabs();
temp.imag=temp.imag/n.Fabs();
return temp;

}
Complex Complex::operator++()
{
real++;
imag++;
return(*this);
}
Complex Complex::operator++(int)
{
Complex temp(*this);
real++;
imag++;
return(temp);
}

Complex Complex::operator--()
{
real--;
imag--;
return(*this);
}
Complex Complex::operator--(int)
{
Complex temp(*this);
real--;
imag--;
return(temp);
}
Complex Complex::Power(int n)
{
Complex temp(*this);
for(int i=1;i<n;i++)
temp=temp*(*this);
return temp;
}
float Complex::Fabs() const
{
return sqrt(real*real+imag*imag);
}

Complex Complex::Conjugate() const
{
return Complex(real,-imag);
}

int main()
{
Complex a(3,4),b(5,6),c,d;
cout<<"【前置++运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"(++a)="<<++a<<"(++b)="<<++b<<endl;
cout<<"a="<<a<<"b="<<b<<endl;

cout<<"【后置++运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"(a++)="<<a++<<"(b++)="<<b++<<endl;
cout<<"a="<<a<<"b="<<b<<endl;

cout<<"【前置--运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"(--a)="<<--a<<"(--b)="<<--b<<endl;
cout<<"a="<<a<<"b="<<b<<endl;

cout<<"【后置--运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"(a--)="<<a--<<"(b--)="<<b--<<endl;
cout<<"a="<<a<<"b="<<b<<endl;

cout<<"【+运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"a+b="<<a+b<<endl;

cout<<"【-运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"a-b="<<a-b<<endl;

cout<<"【*运符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"a*b="<<a*b<<endl;

cout<<"【/运算符重载】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"a/b="<<a/b<<endl;

cout<<"【复数乘方】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"a^3="<<a.Power(3)<<endl<<"b^2="<<b.Power(2)<<endl;

cout<<"【复数取绝对值】"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"|a|="<<a.Fabs()<<endl<<"|b|="<<b.Fabs()<<endl;

return 0;
}

第一个
void reverse_merge(List &A,List &B,List &C){
InitList(C);
i=j=1; k=0;
la_len=ListLength(A);
lb_len=ListLength(B);
while((i<=la_len)&&(j<=lb_len)){
GetElem(A,i,ai);
GetElem(B,i,bi);
if(ai<=bj){
ListInsert(C,++k,ai); ++i;}
else ListInsert(C,++k,bj); ++j;}
}
while(i<=la_len){
GetElem(A,i++,ai); ListInsert(C,++k,ai);}
while(j<=lb_len){
GetElem(B,j++,bj); ListInsert(C,++k,bj);}
}
第二个的话可以参考http://hi.baidu.com/mianchuang/blog/item/6632a4b383c64959082302b5.html

貌似书上是有滴,
数据结构(C语言版)严蔚敏 吴伟民 编著 —— 这我用过的教材
不过,是C写的伪代码,转换至C++也不难啊,懂得思想就简单了

这个。。。也来问啊

请C语言高手帮我编写几个小程序~(一定要用C++编写噢~)
while(i<=la_len){ GetElem(A,i++,ai); ListInsert(C,++k,ai);} while(j<=lb_len){ GetElem(B,j++,bj); ListInsert(C,++k,bj);} } 第二个的话可以参考http:\/\/hi.baidu.com\/mianchuang\/blog\/item\/6632a4b383c64959082302b5.html ...

请C语言高手帮我编写几个数据结构的小程序~(一定要用C++编写噢~)谢啦...
void numQueue(SqQueue *q) \/\/输出队列元素个数 { if(q->rear>=q->front)printf("这个队列的元素个数为:%d\\n",q->rear-q->front);else printf("这个队列的元素个数为:%d\\n",MaxSize-q->front+q->rear);} void DispQueue(SqQueue *q) \/\/输出队列 { int i=0,f=q->front...

谁能用C语言帮我写几个小程序 急求
int max(int x,int y,int z);int a,b,c,d;scanf("%d%d%d",&a,&b,&c); \/\/scanf%d间不能有逗号,后面是输入的地址因此得加& d=max(a,b,c); \/\/输入的是abc,不是x,y,z\/\/x,y,z是形参a,b,c是实参 printf("max=%d",d);\/\/不是print是printf return 0;} int max(int x,...

求几个简单的C语言小程序
int bei(int a,int b,int c){ return (a*b)\/c;} void main(){ int a,b;cout<<"请按从大到小的顺序输入2个要求值的数"<<endl;cin>>a>>b;cout<<"两个数的最大公约数是"<<yue(a,b)<<endl;cout<<"两个数的最小公倍数是"<<bei(a,b,yue(a,b))<<endl;} \/\/求最大公...

高手们,帮我编几个c语言的小程序!!谢谢.明天就要交!!
编一C程序,它能读入两个整数m与n,计算并输出m与n的绝对值的最大公约数及最小公倍数 解:源程序如下:int gys (int x,int y){ int j;j=(x<y)?x:y;for(;j>0;j--)if(x%j==0&&y%j==0)break;else continue;return (j);} int gbs (int z,int w){ int i;i=(z>w)?z:...

哪位c语言大师帮我编几个小程序??
scanf("%c",&c);printf("字符串<%s>中,共有%d个<%c>字符\\n",str,chrn(str,c),c);} int chrn (char *s, char c){ int num=0,frequency=0;do{ if (s[frequency] == c){ num++;} frequency++;} while(s[frequency]!='\\0');return num;} 第二题:include <stdio.h> in...

哪位大侠精通c语言,推荐几个有趣的 实用的 c语言小程序
if(strcmp(write,"我是猪")!=0){ printf("不说是吧?没关系,那就等着关机吧,哈哈哈哈!\\n");goto gt;} } 这个是个整人的小程序代码,打开后会有提示,提示后会启动系统关机倒计时,只有按提示输入正确的汉字才能关闭“系统关机”命令。本人初学者,自己调的,仅供参考。只做娱乐使用,注意保存...

请高手们给我写几个小程序(最初级的)交作业,明天要交作业,多谢
\/\/02 3_MSGS.C include <stdio.h> void main (){ printf ("1001 ");printf ("C and C++ ");printf ("Tips!");} \/\/03 BIT_AND.C include <stdio.h> void main (){ printf("0 & 0 is %d\\n", 0 & 0);printf("0 & 1 is %d\\n", 0 & 1);printf("1 & 1 is %d\\n...

求c语言大佬帮助!帮我写个小程序,谢谢
int n);\/\/在屏幕上输出数组各元素的值(逗号分隔)#define N (10)int main(){ int a[N],b[N],c[N*2],i; srand(time(NULL)); Init(a,N); Print(a,N); Short(a,N); Print(a,N); Init(b,N); Print(b,N); Short(...

用C语言编写一个输出图形的小程序。
void main(){ int i,j;for(i=0;i<4;i++){ for(j=1;j<4-i;j++)printf(" ");for(j=4-i;j<=4+i;j++)printf("*");printf("\\n");} for(i=4;i<7;i++){ for(j=0;j<i-3;j++)printf(" ");for(j=i-3;j<=9-i;j++)printf("*");printf("\\n");} getch(...