【C++初学】课后作业汇总复习(一)概述、输入输出、类的入门——理解封装
一、概述、输入输出、类的入门——理解封装 -
1. a+b
input two number
output sum of a and b;
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
cin >> a >> b;
cout << a+b <<endl;
return 0;
}
2.输入1~7任意一个整数,将其转换为星期输出
要求:使用switch语句,使用cout
输入输出示例:
输入1:1
输出1:Monday
输入2:7
输出2:Sunday
…………
#include <iostream>
using namespace std;
int main()
{
int num = 0;
cin >> num ;
switch(num)
{
case 1:
cout <<"Monday" <<endl;
break;
case 2:
cout <<"Tuesday" <<endl;
break;
case 3:
cout <<"Wednesday" <<endl;
break;
case 4:
cout <<"Thursday" <<endl;
break;
case 5:
cout <<"Friday" <<endl;
break;
case 6:
cout <<"Saturday" <<endl;
break;
case 7:
cout <<"Sunday" <<endl;
break;
}
return 0;
}
3.分数加法
本题输入四个整数
a b c d
计算
分数相加,即a/b + c/d 的最简分数,即 分子/分母
样例输入
2 6 4 8
输出
5/6
注意:
要求实现分数的最简化以及加运算。
要求输出的分子分母均为化简后结果。例如计算结果为 2/6 则应该化简为1/3。
如计算结果为负数,则符号放在分子上。例如 -1/3 而不是 1/-3。
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int a ,int b)
{
if(b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
cin >> a >> b >> c >> d;
int numerator = a *d + c *b;
int denominator = b * d;
int commonDivisor = gcd(abs(numerator), abs(denominator));
numerator /= commonDivisor;
denominator /= commonDivisor;
if(denominator <0)
{
numerator = -numerator;
denominator = -denominator;
}
cout << numerator << "/" <<denominator <<endl;
return 0;
}
4.时间类-类的定义,类作为“零件”的载体,有内部属性,有对外接口
类作为"零件"的载体,有内部属性(private),有对外接口(public), 内部属性的数据成员或函数成员,仅仅供给class内部函数成员使用,不对外开放,public规定的对外开放的接口。
设置Cmytime类。
具有两个成员函数
int Set(int h,int m,int s)
Show()
输入 23 25 38
输出: 23:25:38
//类作为"零件"的载体,有内部属性(private),有对外接口(public), 内部属性的数据成员或函数成员,仅仅供给class内部函数成员使用,不对外开放,public规定的对外开放的接口。
//
//设置Cmytime类。
//
//具有两个成员函数
//
//int Set(int h,int m,int s)
//
//Show()
//
//输入 23 25 38
//
//输出: 23:25:38
#include <iostream>
using namespace std;
class Cmytime
{
private :
int hour;
int minute;
int second;
public:
int Set(int h ,int m,int s)
{
hour = h;
minute = m;
second = s;
return 0;
}
void Show()
{
cout << hour <<":" <<minute <<":" <<second <<endl;
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
return 0;
}
//StudybarCommentEnd
5.对外接口Set,可以限制非法时间值
类作为"零件"的载体,有内部属性(private),有对外接口(public), 内部属性的数据成员或函数成员,仅仅供给class内部函数成员使用,不对外开放,public规定的对外开放的接口。
设置Cmytime类。
具有两个成员函数
int Set(int h,int m,int s)
对于Set函数的要求,
1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
2、赋值成功,返回1,否则返回0。
Show()
输入 23 25 38
输出: 23:25:38
//类作为"零件"的载体,有内部属性(private),有对外接口(public), 内部属性的数据成员或函数成员,仅仅供给class内部函数成员使用,不对外开放,public规定的对外开放的接口。
//
//设置Cmytime类。
//
//具有两个成员函数
//
//int Set(int h,int m,int s)
//
//对于Set函数的要求,
//
// 1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
//
// 2、赋值成功,返回1,否则返回0。
//
//Show()
//
//输入 23 25 38
//
//输出: 23:25:38
#include <iostream>
using namespace std;
class Cmytime
{
private:
int hour;
int minute;
int second;
public:
Cmytime(): hour(0), minute(0), second(0){}
int Set(int h,int m,int s)
{
if(h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >= 0 && s <= 59)
{
hour = h;
minute = m;
second = s;
return 1;
}
else
{
return 0;
}
}
void Show()
{
cout << hour <<":" <<minute <<":" <<second <<endl;
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(1,2,3);
t1.Set(h,m,s);
t1.Show();
return 0;
}
//StudybarCommentEnd
6.增加一个成员函数,计算加一秒的时间
设置Cmytime类。
具有三个成员函数
Show()
int Set(int h,int m,int s)
对于Set函数的要求,
1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
2、赋值成功,返回1,否则返回0。
void AddOneSecond();
实现在原时间的基础上加1秒的时间值。
输入 23 25 38
输出:
23:25:38
0
23:25:38
23:25:39
//## 6.增加一个成员函数,计算加一秒的时间
//
//设置Cmytime类。
//
//具有三个成员函数
//
//Show()
//
//int Set(int h,int m,int s)
//
//对于Set函数的要求,
//
// 1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
//
// 2、赋值成功,返回1,否则返回0。
//
//void AddOneSecond();
//
//实现在原时间的基础上加1秒的时间值。
//
//输入 23 25 38
//
//输出:
//
//23:25:38
//
//0
//
//23:25:38
//
//23:25:39
#include <iostream>
using namespace std;
class Cmytime
{
private:
int hour;
int minute;
int second;
public:
Cmytime(): hour(0),minute(0), second(0){}
int Set(int h,int m, int s)
{
if(h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >=0 && s <= 59)
{
hour = h;
minute = m;
second = s;
return 1;
}
else
{
return 0;
}
}
void Show()
{
cout << hour <<":" <<minute <<":" <<second <<endl;
}
void AddOneSecond()
{
second++;
if(second >= 60)
{
second = 0;
minute++;
if(minute >= 60)
{
minute = 0;
hour++;
}
if(hour >= 24)
{
hour = 0;
}
}
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
t1.AddOneSecond();
cout<<endl;
t1.Show();
return 0;
}
//StudybarCommentEnd
7.增加一个成员函数,计算加n秒的时间
设置Cmytime类。
具有三个成员函数
Show()
int Set(int h,int m,int s)
对于Set函数的要求,
1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
2、赋值成功,返回1,否则返回0。
void AddOneSecond();
实现在原时间的基础上加1秒的时间值。
int AddNSeconds(int n);
实现在原时间的基础上加n秒的时间值。返回值要求,如果加的n秒数,返回时间跨越了0:0:0的次数,换句话说反映了日期上进几天。
输入 23 25 38
输出:
23:25:38
0
23:25:38
23:25:39
2
//## 7.增加一个成员函数,计算加n秒的时间
//设置Cmytime类。
//
//具有三个成员函数
//
//Show()
//
//int Set(int h,int m,int s)
//
//对于Set函数的要求,
//
// 1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
//
// 2、赋值成功,返回1,否则返回0。
//
//void AddOneSecond();
//
//实现在原时间的基础上加1秒的时间值。
//
//int AddNSeconds(int n);
//
//实现在原时间的基础上加n秒的时间值。返回值要求,如果加的n秒数,返回时间跨越了0:0:0的次数,换句话说反映了日期上进几天。
//
//输入 23 25 38
//
//输出:
//
//23:25:38
//
//0
//
//23:25:38
//
//23:25:39
//
//2
#include <iostream>
using namespace std;
class Cmytime
{
private:
int hour;
int minute;
int second;
public:
int Set(int h,int m,int s)
{
if(h >= 0 && h <= 23 && m >= 0 && m <= 59 &&s >= 0 && s <= 59)
{
hour = h;
minute = m;
second = s;
return 1;
}
else
{
return 0;
}
}
void Show()
{
cout <<hour <<":" <<minute <<":" <<second ;
}
void AddOneSecond()
{
AddNSeconds(1);
}
int AddNSeconds(int n)
{
second += n;
int days = 0;
while(second >= 60)
{
second -= 60;
minute++;
if(minute >= 60)
{
minute = 0;
hour++;
}
if(hour >= 24)
{
hour = 0;
days++;
}
}
return days;
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
t1.AddNSeconds(1);
cout<<endl;
t1.Show();
cout<<endl<<t1.AddNSeconds(3600*25);
return 0;
}
//StudybarCommentEnd
8.时间类-构造函数,思考构造函数的执行时刻
设置Cmytime类。
具有三个成员函数
构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
//## 8.时间类-构造函数,思考构造函数的执行时刻
//
//设置Cmytime类。
//
//具有三个成员函数
//
//构造函数
//
//Set(int h,int m,int s)
//
//Show()
//
//输入 23 25 38
//
//输出:
//
//3:2:1
//
//23:25:38
#include <iostream>
using namespace std;
class Cmytime
{
private:
int hour;
int minute;
int second;
public:
Cmytime(int h = 0,int m = 0,int s = 0):hour(h) ,minute(m),second(s){}
void Set(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
}
void Show()
{
cout << hour <<":" <<minute <<":" <<second;
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
return 0;
}
//StudybarCommentEnd
9.求解复系数一元二次方程(无后缀)
鼓励大家完全用C语言的知识,求系数为复数的一元二次方程的解:
a*x2+bx+c=0;
即使a,b,c都是(r+yi)形式的复数,那么经典的求根公式也是成立的。
要求:
1、输入三个系数的实部和虚部,输出两个解(可能相同,也可能不同)。
2、两个解不相同时,需先输出虚部为正的解。
3、对虚部为负的解,输出形式因如样例1。
对虚部为0的解,输出形式因如样例2.
4、输出保留两位小数
提示:1、方程可以用求根公式求解。
2、需考虑当判别式为负实数时的情况。
样例1输入三个复数的系数,a,b,c,(每个系数有实部和虚部两个值):
1 1 //a的实部是1,虚部也是1,即a=1+1i,以下同理
1 1
1 1
输出:
(-0.50+0.87i) //方程的解用(r+yi)形式表示。以下同理
(-0.50-0.87i)
样例2输入:
1 0
-1 -1
0 0
输出:
(1.00+1.00i)
(0.00+0.00i)
提示:当输出(r+yi)时可以:
当y是正数的时候,可以用 printf(“(%.2lf+%.2lfi)”,r,y); 这样的形式来表示。
当y是负数的时候,可以用 printf(“(%.2lf-%.2lfi)”,r,-y); 这样的形式来表示。
#include <stdio.h>
#include <math.h>
// 复数结构体
typedef struct {
double real;
double imag;
} Complex;
// 复数加法
Complex add(Complex a, Complex b) {
Complex result;
result.real = a.real + b.real;
result.imag = a.imag + b.imag;
return result;
}
// 复数减法
Complex subtract(Complex a, Complex b) {
Complex result;
result.real = a.real - b.real;
result.imag = a.imag - b.imag;
return result;
}
// 复数乘法
Complex multiply(Complex a, Complex b) {
Complex result;
result.real = a.real * b.real - a.imag * b.imag;
result.imag = a.real * b.imag + a.imag * b.real;
return result;
}
// 复数除法
Complex divide(Complex a, Complex b) {
Complex result;
double denominator = b.real * b.real + b.imag * b.imag;
result.real = (a.real * b.real + a.imag * b.imag) / denominator;
result.imag = (a.imag * b.real - a.real * b.imag) / denominator;
return result;
}
// 复数平方根
Complex sqrt_complex(Complex c) {
Complex result;
double r = sqrt(c.real * c.real + c.imag * c.imag);
double theta = atan2(c.imag, c.real);
result.real = sqrt(r) * cos(theta / 2);
result.imag = sqrt(r) * sin(theta / 2);
return result;
}
// 打印复数解
void print_complex(Complex c) {
if (c.imag > 0) {
printf("(%.2lf+%.2lfi)\n", c.real, c.imag);
} else if (c.imag < 0) {
printf("(%.2lf-%.2lfi)\n", c.real, -c.imag);
} else {
printf("(%.2lf+%.2lfi)\n", c.real, 0.0);
}
}
int main() {
Complex a, b, c;
// 输入三个复数的系数
scanf("%lf %lf", &a.real, &a.imag);
scanf("%lf %lf", &b.real, &b.imag);
scanf("%lf %lf", &c.real, &c.imag);
// 计算判别式 delta = b^2 - 4ac
Complex b_square = multiply(b, b);
Complex four_a = {4.0, 0.0};
Complex four_a_c = multiply(four_a, multiply(a, c));
Complex delta = subtract(b_square, four_a_c);
// 计算平方根 sqrt(delta)
Complex sqrt_delta = sqrt_complex(delta);
// 计算分母 2a
Complex two_a = multiply((Complex){2.0, 0.0}, a);
// 计算两个解
Complex x1 = divide(subtract((Complex){-b.real, -b.imag}, sqrt_delta), two_a);
Complex x2 = divide(add((Complex){-b.real, -b.imag}, sqrt_delta), two_a);
// 确定哪个解的虚部为正,先输出
if (x1.imag > x2.imag || (x1.imag == x2.imag && x1.real > x2.real)) {
print_complex(x1);
print_complex(x2);
} else {
print_complex(x2);
print_complex(x1);
}
return 0;
}