写一个c语言程序(输入两个复数(格式如2+3i),输出两个复数相加的结果。)
输入两个复数(格式如2+3i),输出两个复数相加的结果。假定实部和虚部都是正整数。要求按复数格式输入输出。要求程序的运行结果形式如下:Pleaseinputcomplex...
输入两个复数(格式如2+3i),输出两个复数相加的结果。假定实部和虚部都是正整数。要求按复数格式输入输出。要求程序的运行结果形式如下:
Please input complex 1:3+5i<Enter>
Please input complex 2:8+9i<Enter>
The Sum is=11+14i 展开
Please input complex 1:3+5i<Enter>
Please input complex 2:8+9i<Enter>
The Sum is=11+14i 展开
6个回答
展开全部
#include<stdio.h>
int main()
{
int x1,y1, x2, y2;
printf("Please input complex 1:");
scanf("%d+%di", &x1, &y1);
printf("Please input complex 2:");
scanf("%d+%di", &x2, &y2);
printf("The sum is=%d+%di\n", x1+x2, y1+y2);
return 0;
}
以上程序是最简单的形式,输入时要注意只能x+yi这样输入,各个字符间不能有空格
int main()
{
int x1,y1, x2, y2;
printf("Please input complex 1:");
scanf("%d+%di", &x1, &y1);
printf("Please input complex 2:");
scanf("%d+%di", &x2, &y2);
printf("The sum is=%d+%di\n", x1+x2, y1+y2);
return 0;
}
以上程序是最简单的形式,输入时要注意只能x+yi这样输入,各个字符间不能有空格
展开全部
#include <iostream>
using namespace std;
class complex
{public:
complex(){real=0;imag=0;}
complex(double r,double i) {real=r;imag=i;}
complex operator + (complex &c2);//声明重载运算符+的函数
void display();
private:
double real;
double imag;
};
complex complex :: operator +(complex &c2)//定义重载运算符+的函数
{complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
void complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{complex c1(3,5),c2(8,9),c3;
c3=c1+c2; //运算符+用于复述运算
cout<<"Please input complex 1:"<<"c1=";c1.display();
cout<<"Please input complex 2:"<<"c2=";c2.display();
cout<<"The Sum is:"<<"c1+c2=";c3.display();
return 0;
}
using namespace std;
class complex
{public:
complex(){real=0;imag=0;}
complex(double r,double i) {real=r;imag=i;}
complex operator + (complex &c2);//声明重载运算符+的函数
void display();
private:
double real;
double imag;
};
complex complex :: operator +(complex &c2)//定义重载运算符+的函数
{complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
void complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{complex c1(3,5),c2(8,9),c3;
c3=c1+c2; //运算符+用于复述运算
cout<<"Please input complex 1:"<<"c1=";c1.display();
cout<<"Please input complex 2:"<<"c2=";c2.display();
cout<<"The Sum is:"<<"c1+c2=";c3.display();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
void main()
{
int i11,i12,i21,i22;
printf("Please input complex 1:");
scanf("%d+%di",&i11,&i12);
printf("\nPlease input complex 2:");
scanf("%d+%di",&i21,&i22);
printf("\nThe Sum is=%d+%di",i11+i21,i12+i22);
}
你运行一下试试。
void main()
{
int i11,i12,i21,i22;
printf("Please input complex 1:");
scanf("%d+%di",&i11,&i12);
printf("\nPlease input complex 2:");
scanf("%d+%di",&i21,&i22);
printf("\nThe Sum is=%d+%di",i11+i21,i12+i22);
}
你运行一下试试。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <fstream>
using namespace std;
class complex{
public:
int real;
int imag;
complex(int r=0,int i=0){
real=r;
imag=i;
}
};
//定义重载运算符+的函数
complex operator+(complex& a,complex& b){
int r=a.real+b.real;
int i=a.imag+b.imag;
return complex(r,i);
}
void main(){
complex x(1,2),y(3,4),z;
z=x+y;
cout<<z.real<<"+"<<z.imag<<"i"<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-20
展开全部
简单,汗,我只给你写主函数体。
(1) typedef struct complex *Complex;
struct complex;
int main(void)
{
Complex a,b;
scanf("%f %f %f %f",a->re,a->im,b->re,b->im);
printf("%f+%fi",(a->re+b->re),(a->im+b->im));
}
(2)int main(viod)
{
char a;
scanf("%c",&a);
if(a>='a'&&a<='z')
{
a-=32;
}
else if(a>='A'&&a<='Z')
{
a+=32;
}
else
{
a='0';
}
printf("%c",a);
}
(3)不明白你想说什么,好像为了前进一步就绕地球后退一圈加一步。这样写总明白了
void swap(int *a;int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int a=3; int b=6;int c=9;
swap(&a,&b);
swap(&b,&c);
}
(1) typedef struct complex *Complex;
struct complex;
int main(void)
{
Complex a,b;
scanf("%f %f %f %f",a->re,a->im,b->re,b->im);
printf("%f+%fi",(a->re+b->re),(a->im+b->im));
}
(2)int main(viod)
{
char a;
scanf("%c",&a);
if(a>='a'&&a<='z')
{
a-=32;
}
else if(a>='A'&&a<='Z')
{
a+=32;
}
else
{
a='0';
}
printf("%c",a);
}
(3)不明白你想说什么,好像为了前进一步就绕地球后退一圈加一步。这样写总明白了
void swap(int *a;int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int a=3; int b=6;int c=9;
swap(&a,&b);
swap(&b,&c);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询