分数类相加
【问题描述】
请设计一个分数类(fenshu),
包含两个数据成员:fz(int )和 fm(int ),
并支持分数与分数相加、分数的输入和输出。
请参照给出的主程序代码补充完成该类的设计。
分子和分母均为[ 1 , 10000 ] 范围内的整数,且运算结果不需要化简为最简分数形式。
【输入形式】
总共两行输入,
第一行输入两个非零整数代表分数f1的分子和分母,
第二行输入两个非零整数代表分数f2的分子和分母。
【输出形式】
总共一行输出,包括两个分数及其相乘的结果。
其中分数的分子和分母之间用‘/ ’连接,无空格。
输出样式形如“A + B = C”,运算符'+' 和'=' 前后各有一个空格。
【样例输入】
1 2
3 4
【样例输出】
1 / 2 + 3 / 4 = 10 / 8
【样例说明】
第一个分数等于1 / 2 ,第二个分数等于3 / 4 ,所以分数相加的结果等于10 / 8 。
#include <iostream>
using namespace std;
class fenshu
{
public :
fenshu ( ) ;
friend istream & operator >> ( istream& , fenshu& ) ;
friend ostream & operator << ( ostream& , fenshu& ) ;
fenshu operator + ( fenshu& ) ;
private :
int fz;
int fm;
} ;
fenshu:: fenshu ( )
{
fz= 0 ; fm= 1 ;
}
istream & operator >> ( istream& input, fenshu& c)
{
input>> c. fz>> c. fm;
return input;
}
ostream & operator << ( ostream& output, fenshu& c)
{
output<< c. fz<< "/" << c. fm;
return output;
}
fenshu fenshu:: operator + ( fenshu& c)
{
fenshu p;
int max= this - > fm* c. fm;
p. fm= max;
p. fz= this - > fz* ( max/ this - > fm) + c. fz* ( max/ c. fm) ;
return p;
}
int main ( )
{
fenshu f1, f2;
cin>> f1>> f2;
fenshu f3= f1+ f2;
cout<< f1<< "+" << f2<< "=" << f3<< endl;
return 0 ;
}
#include <iostream>
using namespace std;
class Role
{
public :
Role ( ) { }
virtual void attack ( ) = 0 ;
} ;
class Devil : public Role
{
public :
void attack ( ) { cout<< "Devil is attacking" << endl; }
} ;
class Boss : public Devil
{
public :
void attack ( ) { cout<< "Boss is attacking" << endl; }
} ;
class Game
{
public :
Game ( ) { }
void play ( Role* r)
{
r- > attack ( ) ;
}
} ;
int main ( )
{
int n;
cin>> n;
int a[ n] ;
for ( int i= 0 ; i< n; i++ )
cin>> a[ i] ;
Role * role[ n] ;
for ( int i= 0 ; i< n; i++ )
{
switch ( a[ i] )
{
case 1 : role[ i] = new Devil; break ;
case 2 : role[ i] = new Boss; break ;
}
}
cout<< "***" << endl;
Game game;
for ( int i= 0 ; i< n; i++ )
{
game. play ( role[ i] ) ;
}
for ( int i= 0 ; i< n; i++ )
{
delete role[ i] ;
}
return 0 ;
}
输出串中相同首字符
【问题描述】
阅读代码,根据已给出的代码,完成fun函数的定义片段。
Fun函数,如果a中字符串首字符与b中字符串首字符相同,输出该首字符
【输入形式】
第一行输入一个整数n,说明a和b字符串数组各有n个字符串
接下来2 n行,每行输入一个字符串,串长度不超过10 ,回车结束。
奇数行初始化a字符串数组的串,偶数行初始化b字符串数组的串
【输出形式】
输出一行,每个首字符前面有一个空格。
【样例输入】
2
a a
ab
ba
aa
【样例输出】
a a
【样例说明】
a中第一个字符串的首字符,和b中两个字符串的首字母都相同,
所以输出两次字符a,每次输出之前,先输出一个空格再输出a。
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
void fun ( char * a[ ] , char * b[ ] , int n)
{
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n; j++ )
if ( a[ i] [ 0 ] == b[ j] [ 0 ] ) cout<< " " << a[ i] [ 0 ] ;
}
}
int main ( )
{
int n;
cin>> n;
cin. get ( ) ;
char * a[ n] ;
char * b[ n] ;
for ( int i= 0 ; i< n; i++ )
{
string str1, str2;
getline ( cin, str1) ;
a[ i] = new char [ str1. length ( ) ] ;
for ( unsigned int j= 0 ; j<= str1. length ( ) - 1 ; j++ )
a[ i] [ j] = str1[ j] ;
getline ( cin, str2) ;
b[ i] = new char [ str2. length ( ) ] ;
for ( unsigned int j= 0 ; j<= str2. length ( ) - 1 ; j++ )
b[ i] [ j] = str2[ j] ;
}
fun ( a, b, n) ;
return 0 ;
}
复数取反
【问题描述】请仔细阅读给定代码,完善复数类Complex的定义与实现,重载运算符“- ”、“<< ”和“>> ”,使之能用于复数对象的取反、输出和输入运算。其中,取反运算是更新原复数,将其实部和虚部的数据都变成相反数;输入输出格式为详见样例。
【输入说明】输入为一行,包括两个实数,以空格分隔,分别构成复数对象的实部和虚部。
【输出说明】输出为一行,即取反后的复数结果,形如( 实部 , 虚部i) ,其中所有字符均为英文半角字符,i为虚部的标识,‘, ’的前后各有一个空格。0 取反仍为0 。
【样例输入】
1 - 0.5
【样例输出】
( - 1 , 0.5 i)
【样例说明】
从键盘输入得到该复数为1 - 0.5 i,取反后,该复数为(- 1 )+ 0.5 i,所以输出( - 1 , 0.5 i) 。
【注意】
题目和代码中的单词拼写要保持一致,包括类名、函数名、类型标识符、变量名等!
#include <iostream>
using namespace std;
class Complex
{
public :
Complex ( ) { real= 0 ; imag= 0 ; }
friend istream & operator >> ( istream& , Complex& ) ;
friend ostream & operator << ( ostream& , Complex& ) ;
Complex & operator - ( ) ;
private :
double real;
double imag;
} ;
istream & operator >> ( istream& input, Complex& c)
{
input>> c. real>> c. imag;
return input;
}
ostream & operator << ( ostream& output, Complex& c)
{
output<< "(" << c. real<< "," << c. imag<< "i" << ")" << endl;
return output;
}
Complex & Complex:: operator - ( )
{
if ( this - > real!= 0 ) this - > real= - ( this - > real) ;
this - > imag= - ( this - > imag) ;
return * this ;
}
int main ( )
{
Complex c;
cin>> c;
cout<< - c;
return 0 ;
}
* * 复数的乘法运算* *
【问题描述】
定义一个复数类Complex,重载构造函数和运算符“* ”,
并添加输出函数display ( ) ,使之能用于复数的初始化、乘法运算和复数输出。
成员函数或友元函数形式不限。请仔细阅读给定代码,完善程序。
复数乘法的运算规则规定如下:
设z1= a+ bi,z2= c+ di ( a、b、c、d∈R) 是任意两个复数,
那么它们的积( a+ bi) ( c+ di) = ( ac- bd) + ( bc+ ad) i。
【输入形式】
输入有两行;
第一行为第一个复数c1的实部和虚部,以空格分隔;
第二行为第二个复数c2的实部和虚部,以空格分隔。
【输出形式】
输出有1 行,分别为两个复数的积的实部与虚部,以空格分隔。
【样例输入】
2 4
1 3
【样例输出】
- 10 10
【样例说明】
( 2 + 4 i) * ( 1 + 3 i) = ( 2 * 1 - 4 * 3 ) + ( 4 * 1 + 2 * 3 ) i = - 10 + 10 i
#include <iostream>
using namespace std;
class Complex {
public :
Complex ( ) ;
Complex ( double r, double i) ;
Complex operator * ( Complex& ) ;
void display ( ) ;
private :
double real;
double imag;
} ;
Complex:: Complex ( ) {
real= 0.0 ;
imag= 0.0 ;
}
Complex:: Complex ( double r, double i) {
real= r;
imag= i;
}
Complex Complex:: operator * ( Complex& c) {
Complex p;
p. real= this - > real* c. real- this - > imag* c. imag;
p. imag= this - > real* c. imag+ this - > imag* c. real;
return p;
}
void Complex:: display ( ) {
cout<< real<< " " << imag<< endl;
}
int main ( ) {
double a, b, c, d;
cin>> a>> b;
cin>> c>> d;
Complex c1 ( a, b) , c2 ( c, d) , c3;
c3 = c1* c2;
c3. display ( ) ;
return 0 ;
}