华科C++大一MOOC

第五章 编程题

1

题目

字符转换(10分)
题目内容:
编写程序输入一个最大长度为80的字符串,将字符串中的所有小写字符转换成大写字符,并输出转换后的字符串和所转换的字符个数。

输入格式:
输入一个包含任意大、小写字母、数字字符(不包括空格字符)的字符串(不大于80个字符)
输出格式
输出转换后的字符串,然后换行输出转换字符的个数

输入样例
ABCqwerRTY23

输出样例
ABCQWERRTY23
4

参考答案

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    char str[80];
    int count = 0;
    cin >> str;
    for (int i = 0;i < strlen(str);i++){
        if (str[i] >= 'a' && str[i] <= 'z'){
            str[i] += 'A' - 'a';
            count++;
        }
    }
    cout << str << endl << count;
}

2

题目

在一堆数据中,查找特殊数据及其所处位置信息(10分)
题目内容:
在包含10个数的一维整数数组a中查找输入的数据num。如果找到则输出该数在数组中的所有出现的位置;若没找到则输出“数组中没有该数”字样。

输入格式:
在一行中输入10个数,每个数据间用空格分隔
第二行输入查找数据
输出格式
如果找到输出相应的位置,如果有多个位置则分行显示
如果没有要找的数,输出“数组中没有该数”,(此输出信息程序中可以直接复制使用。)

输入样例
1 2 3 4 2 5 7 8 9
2

输出样例
2
5

参考答案

#include <iostream>
using namespace std;
int main(){
    int array[10];
    for (int i = 0;i < 10;i++){
        cin >> array[i];
    }
    int search;
    bool thereBe = false;
    cin >> search;
    for (int i = 0;i < 10;i++){
        if (array[i] == search){
            thereBe = true;
            cout << i + 1 << endl;
        }
    }
    if(!thereBe)
        cout << "数组中没有该数";
}

3

题目

学校教学管理要求,每个学期末教学管理老师必须按要求整理学生成绩并输出学生成绩汇总表。(10分)
题目内容:
假定有学生3位,3门课程。学期末要求汇总成绩,请用一个二维数组存储这些学生的成绩数据,同时
将每位学生所有课程成绩的总分、平均分和名次列入学生成绩汇总表(二维数组)中的后三列上。要求输入学生各科课程成绩数据后并输出成绩汇总表。(要求:输出每个数据间用2个英文空格隔开,且每行最后一个数据后也有2个英文空格)

输入格式:
在一行输入所有数据,数据间用一个空格分隔
输出格式
按名次从高到低分行输出每位学生的每门课程成绩及总分、平均分及名次数据表(数据间用2个英文空格间隔,每行行末也有2个英文空格)

输入样例
1 2 3 4 5 6 7 8 9

输出样例
7 8 9 24 8 1
4 5 6 15 5 2
1 2 3 6 2 3

参考答案

#include <iostream>
using namespace std;
int main(){
    float score[3][5];
    for (int i = 0; i < 3;i++){
        float sum = 0;
        for (int j = 0; j < 3; j++){
            cin >> score[i][j];
            sum += score[i][j];
        }
        score[i][3] = sum;
        score[i][4] = sum / 3;
    }
    int rank[3];
    if (score[1][3] > score[2][3]){
        rank[0] = 1;
        rank[1] = 2;
    }
    else {
        rank[0] = 2;
        rank[1] = 1;
    }
    if (score[0][3] > score[ rank[0] ][3]){
        rank[1] = rank[0];
        rank[0] = 0;
    }else if (score[0][3] > score[ rank[1] ][3]){
        rank[2] = rank[1];
        rank[1] = 0;
    }else rank[2] = 0;
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 5; j++){
            cout << score[ rank[i] ][j] << "  ";
        }
        cout << i + 1 << "  " << endl;
    }
}

第六章 编程题

1

题目

运用指针技术实现,将输入的两个字符串中第二个字符串插入到第一个字符串中指定输入数字位置字符的后面。(10分)
题目内容:
运用指针技术实现,将输入的两个字符串中第二个字符串插入到第一个字符串中指定输入数字位置字符的后面。

输入格式:
aghijk
bcdef
1
输出格式
abcdefghijk

输入样例
12345
9876
2

输出样例
129876345

参考答案

#include <iostream>
using namespace std;
int main(){
    char str1[80], str2[40];
    int p;
    for (int i = 0; i < 80; i++){
        str1[i] = 0;
    }
    cin >> str1 >> str2 >> p;
    char *point = &str1[p];
    char *pt;
    for (int i = 0; str2[i] != 0; i++){
        for (pt = str1; *pt; pt++);
        for (; pt != point; pt--){
            *pt = *(pt - 1);
            
        }
        *point = str2[i];
        point++;
    }
    cout << str1;
}

2

题目

运用指针技术实现,将输入的十六进制数的字符串转换为对应的整数数。(10分)
题目内容:
运用指针技术实现,将输入的十六进制数的字符串转换为对应的十进制整数并输出。

输入格式:
以字符串形式输入一个16进制数
输出格式
输出该16进制数转换后的十进制数

输入样例
12AB3d

输出样例
1223485

参考答案

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    char xnum[40];
    int tnum = 0, sxtn = 1;
    cin >> xnum;
    char *point = &xnum[strlen(xnum) - 1];
    while (*point != '\0'){
        if (*point >= '0' && *point <= '9')
            tnum += (*point - '0') * sxtn;
        else if (*point >= 'A' && *point <= 'F')
            tnum += (*point - 'A' + 10) * sxtn;
        else if (*point >= 'a' && *point <= 'f')
            tnum += (*point - 'a' + 10) * sxtn;
        else if (*point == '-')
            tnum = -tnum;
        else {
            cout << "input data error!" << endl;
            return 0;
        }
        sxtn *= 16;
        point--;
    }
    cout << tnum << endl;
}

3

题目

运用指针技术实现,统计输入字符串的各个字母各出现了多少次。区分大小写。(10分)
题目内容:
运用指针技术实现,统计输入字符串的各个字母各出现了多少次。区分字母大小写。

输入格式
在一行上输入所有的字符串。如下:
abab
输出格式:
输出时,每行输出一个字符和个数,每个字符和个数数值之间空一格,按字符的ASCII码由小到大输出。
a 2
b 2

输入样例
acba

输出样例
a 2
b 1
c 1

参考答案

#include <iostream>
using namespace std;
int main(){
    char str[40];
    int sum[64];
    for (int i = 0; i < 64; i++)
        sum[i] = 0;
    cin >> str;
    char *pch = str;
    while (*pch != '\0'){
        sum[*pch - 'A']++;
        pch++;
    }
    for (int i = 0; i < 64; i++){
        if (sum[i])
            cout << (char)(i + 'A') << ' ' << sum[i] << endl;
    }
}

第七章 编程题

1

题目

机器人排序(30分)
题目内容:
最近,Dr.Kong 新设计一个机器人Bill。这台机器人很聪明,会做许多事情。惟独对自然数的理解与人类不一样,它是从右往左读数。比如,它看到 123 时,会理解成 321。让它比较 23 与 15 哪一个大,它说 15 大。原因是它的大脑会以为是 32 与 51 在进行比较。
问题:编写函数 sort,按照给定的两个自然数 A 和 B(1<=A<=B<=200000 B-A<=50),依照Bill的眼光,将 [A,B] 区间中的所有自然数按从小到大排序出来。
提示:
1、如果 A=22,B=37,则输出:30 31 22 32 23 33 24 34 25 35 26 36 27 37 28 29
2、数据之间用一个空格分隔;
代码:(编写函数 sort,完善以下代码)

#include <iostream>
using namespace std;
int main()
{
	int A,B,i,k;
	int yuan[50]; 
	cin>>A>>B;
	for(k=0,i=A;i<=B;i++) yuan[k++]=i;
	sort(yuan, k); 
	cout<<yuan[0];
	for(i=1;i<k;i++) cout<<" "<<yuan[i];
	cout<<endl;
	return 0;
}

输入格式:
输入以空格分隔的两个自然数A和B
输出格式
按照Bill的眼光,输出【 A,B】之间的排序后的所有数据(数据之间用一个空格分隔)

输入样例1
22 37
输出样例1
30 31 22 32 23 33 24 34 25 35 26 36 27 37 28 29

输入样例2
1 22
输出样例2
1 10 2 20 3 4 5 6 7 8 9 11 21 12 22 13 14 15 16 17 18 19

参考答案

#include <iostream>
using namespace std;
int exchange(int num){
    int exnum = 0;
    while (num) {
        exnum = exnum * 10 + num % 10;
        num /= 10;
    }
    return exnum;
}
void sort(int *array, const int num){
    int brray[num];
    int t1, t2;
    for (int i = 0; i < num; i++)
        brray[i] = exchange(array[i]);
    for (int i = 1; i < num; i++){
        for (int j = i; brray[j] < brray[j - 1] && j; j--){
            t1 = brray[j];
            t2 = array[j];
            brray[j] = brray[j - 1];
            array[j] = array[j - 1];
            brray[j - 1] = t1;
            array[j - 1] = t2;
        }
    }
}
int main()
{
    int A, B, i, k;
    int yuan[50];
    cin >> A >> B;
    for (k = 0, i = A; i <= B; i++)
        yuan[k++] = i;
    sort(yuan, k);
    cout << yuan[0];
    for (i = 1; i < k; i++)
        cout << " " << yuan[i];
    cout << endl;
    return 0;
}

2

题目

手机营运商的识别(35分)
题目内容:
给出一个手机号,依据其前 3 位,判断其为:中国电信、中国联通、中国移动、不确定。
中国手机号码一共 11 位数字,前 3 位代表网络识别号,也是移动接入码,可用来判断其营运商。
中国电信号码段:133,149,153,173,177,180,181,189,191,199;
中国联通号码段:130,131,132,145,155,156,166,171,175,176,185,186;
中国移动号码段:134,135,136,137,138,139,147,150,151,152,157,158,
159,172,178,182,183,184,187,188,198;
问题:请编写函数 detection,完成手机营运商的判断,如营运商不是电信、联通或移动,则返回假值,否则返回真值。
代码:(编写 detection 函数,完善以下代码)

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	char number[12];
	char merchant[9];
	cin>>number;
	if(detection(number, merchant)) 
		cout<<merchant<<endl;
	else
		cout<<"不确定"<<endl;
	return 0;
}

输入格式:
11位的手机号码
输出格式
运营商的名称或者“不确定”

输入样例
13871434786

输出样例
中国移动

参考答案

#include <iostream>
#include <cstring>
using namespace std;
bool detection(char *num, char *merchant){
    const int mbc = 21;
    const int ucc = 12;
    const int tcc = 10;
    int Mobile[21] = {134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 172, 178, 182, 183, 184, 187, 188, 198};
    int Unicom[12] = {130, 131, 132, 145, 155, 156, 166, 171, 175, 176, 185, 186};
    int Telecom[10] = {133, 149, 153, 173, 177, 180, 181, 189, 191, 199};
    int code = 0;
    for (int i = 0; i < 3; i++){
        code = code * 10 + (num[i] - '0');
    }
    for (int i = 0; i < mbc + ucc + tcc; i++){
        if (i < mbc){
            if (code == Mobile[i]){
                strcpy(merchant, "中国移动");
                return true;
            }
        } else if (i < mbc + ucc){
            if (code == Unicom[i - mbc]){
                strcpy(merchant, "中国联通");
                return true;
            }
        } else if (code == Telecom[i - mbc - ucc]){
            strcpy(merchant, "中国电信");
            return true;
        }
    }
    return false;
}
int main(){
    char number[12];
    char merchant[9];
    cin >> number;
    if (detection(number, merchant))
    cout << merchant << endl;
    else
    cout << "不确定" << endl;
    return 0;
}

3

题目

卡特兰数(35分)
题目内容:
卡塔兰数是组合数学中,一个常在各种计数问题中出现的数列。
卡塔兰数的定义:
⑴ 第 0 项为 1;
⑵ 第 1 项为 1;
⑶ 第 n 项的计算公式
Cn = C0*Cn-1 + C1*Cn-2 + C2*Cn-3 + … + Cn-1*C0
卡特兰数是从第0项开始,其前10项为:
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862
问题:编写函数CTL,计算并输出卡特兰数的前 n 项(n>=3),项与项之间用一个空格分隔;
提示:随着项数 n 的增加,卡特兰数会急剧增加,请适当地考虑长整型。
代码:(编写函数CTL,完善以下代码)

#include <iostream>
using namespace std;
int main()
{
	long *lngCTL;
	int n, i;
	cin>>n;
	CTL(lngCTL,n);
	return 0;
}

输入格式:
卡特兰数的项数 n
输出格式
卡特兰数的前 n 项,项与项之间用一个空格分隔

输入样例
10

输出样例
1 1 2 5 14 42 132 429 1430 4862

参考答案

#include <iostream>
using namespace std;
long C(int num){
    int result = 0;
    if (num == 0 || num == 1)
        return 1;
    else {
        for (int i = 0; i < num; i++){
            result += C(i) * C(num - 1 - i);
        }
        return result;
    }
}
void CTL(long *lngCTL,const int num){
    lngCTL = new long[num];
    for (int i = 0; i < num; i++){
        lngCTL[i] = C(i);
        cout << lngCTL[i];
        if (i < num - 1)cout << ' ';	//注意:此处最后一项后不得有空格,否则会提示格式错误
    }
    cout << endl;
}
int main(){
    long *lngCTL;
    int n, i;
    cin >> n;
    CTL(lngCTL, n);
    return 0;
}

第九章 编程题


第九章的题目都是看起来很长,其实要写的也没那么多,主函数干脆就直接给你提供了,要写的只有那几个成员函数而已。


1

题目

请要求完成分数类中缺失的的成员函数,并使用指定的代码测试该类。(25分)
要求:

  1. 创建分数时,分母默认非0,代码无需考虑分母为0时的处理
  2. 分数相除时,除数的分子默认非0,代码无需考虑分子为0时的处理
  3. 分数的加法、除法运算结果需化简为最简分数
  4. 构造函数和重置函数setfrac都不用对分数化简
    ->分数类
class fraction
{
	private:
		int above;         //分子
		int below;         //分母
		int gcd(int,int);//求两个数的最大公约数
		int lcm(int,int);//求两个数的最小公倍数
	public:	
		fraction();	
		fraction(int ,int);	
		void reduction();  //约分	
		fraction add(fraction);      //两分数相加
		fraction div (fraction);      //两分数相除
		void setfrac(int ,int);		//重置分数
		void display();				//显示分数
};
void fraction::display()
{	
	if (above==0) cout<<0<<endl;
	else cout<<above<<'/'<<below<<endl;
}

->测试代码,测试结果应和输出样例中完全相同

int main()
{
		cout<<"--------测试构造函数-----------"<<endl;

		fraction f1(20,-9),f2(-7,-9),f3(5,-9);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=";	f3.display();

		cout<<"--------测试加法函数-----------"<<endl;

		f3=f1.add(f2);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=f1+f2=";f3.display();

		f1.setfrac(-3,-15);f2.setfrac(-10,12); f3=f1.add(f2);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=f1+f2=";f3.display();

		f1.setfrac(12,45);	f2.setfrac(19,-30);	f3=f1.add(f2);	
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=f1+f2=";f3.display();
		
		f1.setfrac(3,5);	f2.setfrac(-9,15);	f3=f1.add(f2);	
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=f1+f2=";f3.display();

		cout<<"--------测试除法函数-----------"<<endl;
		
		f1.setfrac(-24,7);	f2.setfrac(16,35);	f3=f1.div(f2);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();		
		cout<<"f3=f1/f2=";f3.display();

		f1.setfrac(33,-45);	f2.setfrac(-11,20);	f3=f1.div(f2);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=f1/f2=";f3.display();

		f1.setfrac(12,35);	f2.setfrac(-24,7);	f3=f1.div(f2);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();		
		cout<<"f3=f1/f2=";f3.display();

		f1.setfrac(-11,20);	f2.setfrac(33,-50);	f3=f1.div(f2);
		cout<<"f1=";	f1.display();
		cout<<"f2=";	f2.display();
		cout<<"f3=f1/f2=";f3.display();

		cout<<"------------测试结束------------"<<endl;


	return 0;
}

输出样例

--------测试构造函数-----------
f1=-20/9
f2=7/9
f3=-5/9
--------测试加法函数-----------
f1=-20/9
f2=7/9
f3=f1+f2=-13/9
f1=3/15
f2=-10/12
f3=f1+f2=-19/30
f1=12/45
f2=-19/30
f3=f1+f2=-11/30
f1=3/5
f2=-9/15
f3=f1+f2=0
--------测试除法函数-----------
f1=-24/7
f2=16/35
f3=f1/f2=-15/2
f1=-33/45
f2=-11/20
f3=f1/f2=4/3
f1=12/35
f2=-24/7
f3=f1/f2=-1/10
f1=-11/20
f2=-33/50
f3=f1/f2=5/6

参考答案

#include <iostream>
using namespace std;
class fraction
{
private:
    int above;         //分子
    int below;         //分母
    int gcd(int,int);  //求两个数的最大公约数
    int lcm(int,int);  //求两个数的最小公倍数
public:
    fraction();
    fraction(int ,int);
    void reduction();  //约分
    fraction add(fraction);      //两分数相加
    fraction div (fraction);      //两分数相除
    void setfrac(int ,int);        //重置分数
    void display();                //显示分数
};
fraction::fraction(){ above = 0; below = 1; }
fraction::fraction(int a, int b = 1):above(a),below(b){}
int fraction::gcd(int a, int b){
    if (a % b == 0)
        return b;
    else return gcd(b, a % b);
}		//辗转相除法
int fraction::lcm(int a, int b){
    return a * b / gcd(a, b);
}
void fraction::reduction(){
    int gcd_of_a_b = gcd(above, below);
    above = above / gcd_of_a_b;
    below = below / gcd_of_a_b;
}
fraction fraction::add(fraction b){
    fraction result;
    result.below = below * b.below;
    result.above = above * b.below + b.above * below;
    result.reduction();
    return result;
}
fraction fraction::div(fraction b){
    fraction result;
    result.above = above * b.below;
    result.below = below * b.above;
    result.reduction();
    return result;
}
void fraction::setfrac(int a, int b){ above = a; below = b; }
void fraction::display()
{
    if (below < 0){
        above = -above;
        below = -below;
    }
    if (above == 0) cout << 0 << endl;
    else if (below == 1) cout << above << endl;
    else cout << above << '/' << below << endl;
}
int main()
{
    cout << "--------测试构造函数-----------" << endl;
    fraction f1(20, -9), f2(-7, -9), f3(5, -9);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=";    f3.display();
    
    cout << "--------测试加法函数-----------" << endl;
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();
    f1.setfrac(-3, -15);
    f2.setfrac(-10,12);
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();
    f1.setfrac(12, 45);
    f2.setfrac(19, -30);
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();
    f1.setfrac(3, 5);
    f2.setfrac(-9, 15);
    f3 = f1.add(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1+f2=";    f3.display();

    cout << "--------测试除法函数-----------" << endl;
    f1.setfrac(-24, 7);
    f2.setfrac(16, 35);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";f3.display();
    f1.setfrac(33,  -45);
    f2.setfrac(-11, 20);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";    f3.display();
    f1.setfrac(12, 35);
    f2.setfrac(-24, 7);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";    f3.display();
    f1.setfrac(-11, 20);
    f2.setfrac(33, -50);
    f3 = f1.div(f2);
    cout << "f1=";    f1.display();
    cout << "f2=";    f2.display();
    cout << "f3=f1/f2=";    f3.display();
    
    cout << "------------测试结束------------" << endl;

    return 0;
}

要说有什么要注意的地方的话,大概就是要注意正负号了。我这里是在display是规范了分数的正负号形式,因为这样写最省事。
还有就是在写约分函数时有个要注意的,不能直接写above = above / gcd(above, below0; below = below / gcd(above, below); 因为第一句修改了above,第二句算below时就会用修改后的above算,刚开始我这里就写错了……
gcd函数我用了辗转相除法,lcm函数则直接用了最小公倍数=两数之积/最大公约数


2

题目

设计一个时间类,其数据成员为hour(小时)、minute(分)和sec(秒),请补充类的成员函数定义,实现时间的加法与减法运算。
并使用测试代码测试该类,运行结果应该和输出样例完全相同(25分)

题目内容:
->时间类

class timeC{
	int hour;
	int minute;
	int sec;
public:
	timeC(int h=0, int m=0, int s=0);
	timeC add(timeC &);
	timeC minus(timeC &);
	void showtimeC();
};
//add函数使用引用作为形参是为了减少函数调用代价
timeC timeC::add(timeC & t)
{
	timeC k;
	k.hour=hour+t.hour;
	k.minute=minute+t.minute;
	k.sec=sec+t.sec;
	int I;
	i=k.sec/60;	k.sec=k.sec%60;
	k.minute+=I;
	i=k.minute/60;	k.minute=k.minute%60;
	k.hour+=I;
	k.hour=k.hour%24;
	return k;
}
void timeC::showtimeC()
{
	cout<<hour<<" : "<<minute<<" : "<<sec<<endl;
}

->测试代码

int main()
{
	
	timeC t1(21,12,34),t2(12,56,45);
	
	timeC t3(9,23);
	t3.showtimeC();
	t3=t3.add(t2);
	t3.showtimeC();
	
	timeC t4;
	t4.showtimeC();
	t4=t1.minus(t2);
	t4.showtimeC();

	return 0;
}

输出样例

9 : 23 : 0
22 : 19 : 45
0 : 0 : 0
8 : 15 : 49

参考答案

#include <iostream>
using namespace std;
class timeC{
    int hour;
    int minute;
    int sec;
public:
    timeC(int h = 0, int m = 0, int s = 0):hour(h), minute(m), sec(s){}
    timeC add(timeC &);
    timeC minus(timeC &);
    void showtimeC();
};
timeC timeC::add(timeC &t)
{
    timeC k;
    k.hour = hour + t.hour;
    k.minute = minute + t.minute;
    k.sec = sec + t.sec;
    int i;
    i = k.sec / 60;
    k.sec = k.sec % 60;
    k.minute += i;
    i = k.minute / 60;
    k.minute = k.minute % 60;
    k.hour += i;
    k.hour = k.hour % 24;
    return k;
}
timeC timeC::minus(timeC &t){
    timeC result;
    result.hour = hour - t.hour;
    result.minute = minute - t.minute;
    result.sec = sec - t.sec;
    if (result.sec < 0){
        result.minute--;
        result.sec += 60;
    }
    if (result.minute < 0){
        result.hour--;
        result.minute += 60;
    }
    if (result.hour < 0)
        result.hour += 24;
    return result;
}
void timeC::showtimeC()
{
    cout << hour << " : " << minute << " : " << sec << endl;
}
int main()
{
    
    timeC t1(21, 12, 34), t2(12, 56, 45);
    
    timeC t3(9,23);
    t3.showtimeC();
    t3 = t3.add(t2);
    t3.showtimeC();
    
    timeC t4;
    t4.showtimeC();
    t4 = t1.minus(t2);
    t4.showtimeC();

    return 0;
}

这道题要写的也就只有构造函数和minus函数,不算难


3

题目

请完整戏票类的实现代码。并使用测试代码测试该类,测试代码的运行结果应和输出示例完全相同(25分)
注意,

  1. booking函数的购票失败提示示例如下,编写代码时请复制其中的标点符号,以免由于中英文编码问题导致判分失效
余票(11)不足,购买失败
  1. 输出的“:”都是中文全拼“:”

题目内容:
->戏票类

class opera
{
	char name[30];//歌剧名称
	int ts[4];	// ts[0]:包厢的总数,ts[1]:一等座的总座位数
			//ts[2]:二等座的总座位数,ts[3]:三等座的总座位数
	int es[4];// es[0]:空包厢的数量,es[1]:一等座的空闲座位数
			//es[2]:二等座的空闲座位数,es[3]:三等座的空闲座位数
	int ps[4];// ps[0]:包厢的票价,ps[1]:一等座的票价
			//ps[2]:二等座的票价,,ps[3]:三等座的票价
	int income;//总收入
public:
	opera(); //默认构造函数,数据成员的初始值请参看输出样例
	void set_name(char *);//更改剧名
	void set_ps(int []);//更改座位价格
	void booking(int,int);//售票,如果余票小于订票数,
				//终止该次售票,并输出购票失败提示信息
	void refund(int,int);//退票,不收手续费
	void print();
	~opera(); //析构函数,完成票房统计,内容与格式请参看输出样例
};
void opera::print()
{
	char line[20]="------------------";
	cout<<line<<"戏曲名:"<<name<<line<<endl;

	char st[4][20]={ "包厢","一等座","二等座","三等座"};
	
	for(int i=0;i<4;i++) 
		cout<<st[i]<<"<"<<"票价:"<<ps[i]<<" /总数:"<<ts[i]<<" /可售:"<<es[i]<<">"<<endl;

	cout<<line<<"总收入:"<<income<<line<<endl<<endl;
}

->测试代码,测试结果应和输出示例完全相同

int main()
{

		opera p1;
p1.print();

		p1.set_name("二进宫");//void set_name(char *),更改剧名

		int p[]={700,380,180,80};
		p1.set_ps(p);//void set_ps(int []),更改座位价格

		p1.booking(0,9);//购买9张包厢票
		p1.booking(1,15);//购买15张一等票
		p1.booking(3,20);//购买20张三等票
		p1.print();

		p1.booking(0,13);//购买13张包厢票
		p1.booking(2,14);//购买14张二等票
		p1.refund(3,6);//退6张三等票
		p1.print();	
return 0;
}

参考答案

#include <iostream>
#include <cstring>
using namespace std;
class opera
{
    char name[30];                      //歌剧名称
    int ts[4];                          // ts[0]:包厢的总数,ts[1]:一等座的总座位数
                                        // ts[2]:二等座的总座位数,ts[3]:三等座的总座位数
    int es[4];                          // es[0]:空包厢的数量,es[1]:一等座的空闲座位数
                                        // es[2]:二等座的空闲座位数,es[3]:三等座的空闲座位数
    int ps[4];                          // ps[0]:包厢的票价,ps[1]:一等座的票价
                                        // ps[2]:二等座的票价,,ps[3]:三等座的票价
    int income;                         //总收入
public:
    opera();                            //默认构造函数,数据成员的初始值请参看输出样例
    void set_name(char *);              //更改剧名
    void set_ps(int []);                //更改座位价格
    void booking(int,int);              //售票,如果余票小于订票数,
                                        //终止该次售票,并输出购票失败提示信息
    void refund(int,int);               //退票,不收手续费
    void print();
    ~opera();                           //析构函数,完成票房统计,内容与格式请参看输出样例
};
opera::opera(){
    strcpy(name, "未命名");
    ts[0] = es[0] = 20; ts[1] = es[1] = 100;
    ts[2] = es[2] = 240; ts[3] = es[3] = 300;
    ps[0] = ps[1] = ps[2] = ps[3] = 0;
    income = 0;
}
void opera::set_name(char *n){ strcpy(name, n); }
void opera::set_ps(int *p){
    for (int i = 0; i < 4; i++)
        ps[i] = p[i];
}
void opera::booking(int rank, int num){
    if (num > es[rank])
        cout << "余票(" << es[rank] << ")不足,购买失败\n";
    else {
        es[rank] -= num;
        income += num * ps[rank];
    }
}
void opera::refund(int rank, int num){
    es[rank] += num;
    income -= ps[rank] * num;
}
void opera::print()
{
    char line[20]="------------------";
    cout<<line<<"戏曲名:"<<name<<line<<endl;

    char st[4][20]={ "包厢","一等座","二等座","三等座"};
    
    for(int i=0;i<4;i++)
        cout<<st[i]<<"<"<<"票价:"<<ps[i]<<" /总数:"<<ts[i]<<" /可售:"<<es[i]<<">"<<endl;

    cout<<line<<"总收入:"<<income<<line<<endl<<endl;
}
opera::~opera(){
    cout << name << "的总收入:" << income << endl;
    cout << name << "的票房如下:\n";
    cout << "包厢售出" << ts[0] - es[0] << endl;
    cout << "一等座售出" << ts[1] - es[1] << endl;
    cout << "二等座售出" << ts[2] - es[2] << endl;
    cout << "三等座售出" << ts[3] - es[3] << endl;
}

int main()
{

    opera p1;
    p1.print();

    p1.set_name("二进宫");//void set_name(char *),更改剧名

    int p[]={700,380,180,80};
    p1.set_ps(p);//void set_ps(int []),更改座位价格

    p1.booking(0,9);//购买9张包厢票
    p1.booking(1,15);//购买15张一等票
    p1.booking(3,20);//购买20张三等票
    p1.print();
    p1.booking(0,13);//购买13张包厢票
    p1.booking(2,14);//购买14张二等票
    p1.refund(3,6);//退6张三等票
    p1.print();
    return 0;
}

4

题目

请完成手机类的成员函数定义。并使用测试代码测试该类,测试代码的运行结果应和输出示例完全相同(25分)
题目内容:
->手机类

class phone{
	char number[12];//11位本机号码
	char city[5];//本机归属地编号
	double fee;//本机话费余额
public:
	phone(); //默认构造函数,初始值请参看输出样例第一行
	void recharge(double m);//给手机充值 
	void insertcard(char[],char[]);//设置手机号码和归属地
	double  getfee();//返回本机话费
	char *getno();//返回本机号码
	char *getcity();//返回归属地
	int call(phone &other,int m);//和手机other通话m分钟,
	/*  1.归属地相同,本机支付话费,话费为每分钟0.7元。
		2.归属地不同,双方都要支付话费,本机话费为每分钟1.2元,other话费为每分钟0.7元。
		3.通话过程中,如果因为某方话费不足,通话中断,函数结束,返回实际通话时长
		通话正常完成,函数结束,返回m */
};

->测试代码

void display( phone t)
{	
	cout<<"手机号码:"<<t.getno()
		<<";归属地:"<<t.getcity()
		<<";话费余额:"<<t.getfee()
		<<endl;
}
int main()
{
	
	phone p1;
	display(p1);
	p1.insertcard("13458901211","027");	p1.recharge(40);

	phone p2,p3;
	p2.insertcard("13652901219","021");	p2.recharge(30);	
	p3.insertcard("15651004523","027");	p3.recharge(50);

	char *line="*-------------------------------------------------*";
	int talk,m;
	m=35;
	talk=p1.call(p2,m);
	cout<<line<<endl;
	cout<<"p1主叫p2......."<<endl;
	if (talk==m) 
		cout<<"通话时长"<<talk<<"分钟。通话结束,祝您愉快"<<endl;
	else 
		cout<<"通话时长"<<talk<<"分钟。余额不足,请尽快预存话费"<<endl;
	display(p1); display(p2);
	cout<<line<<endl;

	p1.recharge(30);
	m=40;
	talk=p3.call(p1,40);
	cout<<line<<endl;
	cout<<"p3主叫p1......."<<endl;
	if (talk==m) 
		cout<<"通话时长"<<talk<<"分钟。通话结束,祝您愉快........"<<endl;
	else 
		cout<<"通话时长"<<talk<<"分钟。电话余额不足.......请尽快预存话费."<<endl;;
	display(p3); display(p1);
	cout<<line<<endl;

	return 0;
}

输出样例

手机号码:08600000000;归属地:***;话费余额:0
*-------------------------------------------------*
p1主叫p2.......
通话时长33分钟。余额不足,请尽快预存话费
手机号码:13458901211;归属地:027;话费余额:0.4
手机号码:13652901219;归属地:021;话费余额:6.9
*-------------------------------------------------*
*-------------------------------------------------*
p3主叫p1.......
通话时长40分钟。通话结束,祝您愉快........
手机号码:15651004523;归属地:027;话费余额:22
手机号码:13458901211;归属地:027;话费余额:30.4
*-------------------------------------------------*
------------测试结束------------

参考答案

#include <iostream>
#include <cstring>
using namespace std;
class phone{
    char number[12];    //11位本机号码
    char city[5];       //本机归属地编号
    double fee;         //本机话费余额
public:
    phone();                        //默认构造函数,初始值请参看输出样例第一行
    void recharge(double m);        //给手机充值
    void insertcard(const char*,const char*); //设置手机号码和归属地
    double  getfee();               //返回本机话费
    char *getno();                  //返回本机号码
    char *getcity();                //返回归属地
    int call(phone &other,int m);   //和手机other通话m分钟,
    /*  1.归属地相同,本机支付话费,话费为每分钟0.7元。
        2.归属地不同,双方都要支付话费,本机话费为每分钟1.2元,other话费为每分钟0.7元。
        3.通话过程中,如果因为某方话费不足,通话中断,函数结束,返回实际通话时长
        通话正常完成,函数结束,返回m */
};
phone::phone(){ strcpy(number, "08600000000"); strcpy(city, "***"); fee = 0; }
void phone::insertcard(const char *num, const char *ct){
    strcpy(number, num);
    strcpy(city, ct);
}
void phone::recharge(double m){ fee += m; }
double phone::getfee(){ return fee; }
char* phone::getno(){ return number; }
char* phone::getcity(){ return city; }
int phone::call(phone &other, int m){
    double cost1, cost2 = 0;
    int minute = 0;
    if (!strcmp(other.city, city)){
        cost1 = 0.7;
    } else {
        cost1 = 1.2;
        cost2 = 0.7;
    }
    while (minute < m){
        if (fee > cost1 && other.fee > cost2){
            minute++;
            fee -= cost1;
            other.fee -= cost2;
        } else
            break;
    }
    return minute;
}
void display( phone t)
{
    cout << "手机号码:" << t.getno()
         << ";归属地:" << t.getcity()
         << ";话费余额:" << t.getfee()
         << endl;
}
int main()
{
    
    phone p1;
    display(p1);
    p1.insertcard("13458901211", "027");
    p1.recharge(40);
    phone p2, p3;
    p2.insertcard("13652901219", "021");
    p2.recharge(30);
    p3.insertcard("15651004523", "027");
    p3.recharge(50);

    char line[] = "*-------------------------------------------------*";
    int talk, m;
    m = 35;
    talk = p1.call(p2, m);
    cout << line << endl;
    cout << "p1主叫p2......." << endl;
    if (talk == m)
        cout << "通话时长" << talk << "分钟。通话结束,祝您愉快" << endl;
    else
        cout << "通话时长" << talk << "分钟。余额不足,请尽快预存话费" << endl;
    display(p1); display(p2);
    cout << line << endl;

    p1.recharge(30);
    m = 40;
    talk = p3.call(p1, 40);
    cout << line << endl;
    cout << "p3主叫p1......." << endl;
    if (talk == m)
        cout << "通话时长" << talk << "分钟。通话结束,祝您愉快........" << endl;
    else
        cout << "通话时长" << talk << "分钟。电话余额不足.......请尽快预存话费." << endl;
    display(p3); display(p1);
    cout << line << endl;

    return 0;
}

这道题唯一的难点也就在call函数了。不过call函数硬用if-else语句也能凑出来,就是会麻烦点。
由于C++11标准不允许把字符串常量传递给char*,我把insertcard函数的形参改成了const char*,当然一般来说不改也能通过编译,就是一些新的编译器会警告⚠️,看起来不爽。


有时间会写写提高题和第九章
学期也快结束了,提高题也懒得写了
(第九章已更新)
Created by goolwind on 2021/1/6
Edited on 2021/1/11

  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值