《C++ 面向对象程序设计(第2版) 谭浩强编》——课后习题之编程题——第1章

本文提供了一系列C++编程实例,包括求最大数、排序算法、字符串操作等,涵盖了基本的数据处理和算法实现,适合初学者和进阶者练习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.7 求2个或3个正整数中的最大数,用带有默认参数的函数实现

#include <iostream>

using namespace std;

int max(int a, int b){//求两个数的最大值
    if(a > b)
        return a;
    else
        return b;
}
int main()
{
    int x, y ,z;
    cout << "enter 3 integers:";
    cin >> x >> y >> z;
    cout << "The max integer is:" << max(x, max(y, z)) << endl;
    return 0;
}

1.8 输入两个整数,将它们按由大到小的顺序输出。要求使用变量的引用

#include <iostream>

using namespace std;

void exchange(int &a, int &b)
{
    int temp;
    if(a < b)
    {
        temp = a;
        a = b;
        b = temp;
    }
}

int main()
{
    int x, y;
    cout << "enter 2 integers:";
    cin >> x >> y;
    exchange(x, y);
    cout << x << " > " << y << endl;
    return 0;
}

 1.9 对3个变量按由小到大顺序排序,要求使用变量的引用。

#include <iostream>

using namespace std;

void mySort(int &a, int &b, int &c){//要使a<b<c: 1.a<b 2.a<c 3.b<c
    int temp;
    if(a>b)//使a<b
    {
        temp=b;
        b=a;
        a=temp;
    };
    if(a>c)//使a<c
    {
        temp=c;
        c=a;
        a=temp;
    };
    if(b>c)//使b<c
    {
        temp=b;
        b=c;
        c=temp;
    };

}
int main()
{
    int x, y ,z;
    cout << "enter 3 integers:";
    cin >> x >> y >> z;
    mySort(x, y, z);
    cout << x << " < " << y <<  " < " << z << endl;
    return 0;
}

 

 1.10 编一个程序,将两个字符串连接起来, 结果取代第一个字符串。要求用string方法。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1 = "stu", s2 = "dent";
    cout<<"s1 = "<<s1<<endl;
    cout<<"s2 = "<<s2<<endl;
    s1 = s1 + s2;
    cout<<"new s1 is "<< s1 <<endl;
    return 0;
}

1.11 输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。要求用string方法。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    cout << "please input a string:" << endl;
    cin >> str;
    int len = str.length();//取str的长度
    for(int i = len-1; i >=0; i--){
        cout << str[i];
    }
        cout << " is reversed by " << str << "." << endl;


    return 0;
}

1.12 有5个字符串,要求对它们由小到大顺序排列,用string方法。

#include<iostream>
#include<string>

using namespace std;

int main()
{
    const int LEN = 5;
	string strs[LEN];  //定义一个字符串数组。
	cout << "Enter 5 strings:" << endl;
	cin>>strs[0]>>strs[1]>>strs[2]>>strs[3]>>strs[4];
    cout << "before sorted:" << endl;
	for(int i=0;i<5;i++)
	{
		cout<<strs[i]<<"\t";
	}
	cout << endl;
	string temp;
	for(int i = 0; i < LEN - 1; i++)   //通过冒泡排序进行排序
	{
		for(int j = 0; j < LEN - 1 - i; j++)
		{
			if(strs[j] > strs[j+1])//小数放在前
			{
				temp = strs[j];
				strs[j] = strs[j+1];
				strs[j+1] = temp;
			}
		}
	}
	cout << "after sorted:" << endl;
	for(int i=0; i < LEN; i++)
	{
		cout << strs[i] << endl;
	}
	return 0;
}

1.13 用同一个函数名对N个数据进行从小到大排序,数据类型可以是整形、单精度型、双精度型. 用重载函数实现

#include<iostream>
using namespace std;
float mySort(float *a,int n)
{
    float temp;
    int i,j;
    for(i=0; i<n-1; i++)
    {
        for(j=0; j<n-i-1; j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j+1];
                a[j+1]=a[j];
                a[j]=temp;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}
int mySort(int *a,int n)
{
    int t;
    int i,j;
    for(i=0; i<n-1; i++)
    {
        for(j=0; j<n-i-1; j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j+1];
                a[j+1]=a[j];
                a[j]=t;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

double mySort(double *a,int n)
{
    double t;
    int i,j;
    for(i=0; i<n-1; i++)
    {
        for(j=0; j<n-i-1; j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j+1];
                a[j+1]=a[j];
                a[j]=t;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}
int main()
{
    int a[5]= {22,13,26,55,5};
    float b[5]= {1.2f,3.4f,2.2f,5.4f,4.5f};
    double c[5]= {1.2201,3.2121,0.3322,5.4433,2.1122};

    mySort(a,5);
    mySort(b,5);
    mySort(c,5);
    return 0;
}

1.14  用同一个函数名对n个数据进行从小到大排序 用函数模板

#include <iostream>

using namespace std;
template <typename T>//模板声明,其中T为类型参数
T mySort(T *a, int n){//定义一个通用函数,用T作虚拟的类型名
    T t;
    for(int i=0; i<n-1; i++)
    {
        for(int j=0; j<n-i-1; j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j+1];
                a[j+1]=a[j];
                a[j]=t;
            }
        }
    }
    for(int i=0; i < n; i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

int main()
{
    int a[5]= {22,13,26,55,5};
    float b[5]= {1.2f,3.4f,2.2f,5.4f,4.5f};
    double c[5]= {1.2201,3.2121,0.3322,5.4433,2.1122};

    mySort(a,5);
    mySort(b,5);
    mySort(c,5);

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cheeky_man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值