C++期末复习练手题

1 . 输出字符 ‘A’ 和 ‘a’ 的ASCII码值。

#include <iostream>
using namespace std;
int main()
{
    int i,j;
    i='A';
    j='a';
    cout<<"ASCII of 'A'="<<i<<endl<<"ASCII of 'a'="<<j<<endl;
    return 0;
}

输出:
ASCII of ‘A’=65
ASCII of ‘a’=97

2 . 输入两个小写字符,将其转换为大写

#include <iostream>
using namespace std;
int main()
{
    char c1,c2;
    cout<<"input two char: ";
    cin>>c1>>c2;
    c1=c1-32;
    c2=c2-32;
    cout<<"c1="<<c1<<endl<<"c2="<<c2<<endl;
}

输入:input two char: lc
输出:
c1=L
c2=C

3.输入两个浮点型数字,但要求输出的是整型

#include <iostream>
using namespace std;
int main()
{
    float c1,c2;
    cout<<"input two float: ";
    cin>>c1>>c2;
    c1=(int)c1;   //方法一
    c2=int(c2);   //方法二
    cout<<"c1="<<c1<<endl<<"c2="<<c2<<endl;
}

输入:input two char: 3.97 7.56
输出:
c1=3
c2=7

4.【密码编译题】输入字符串”CHINA”, 每个字母往后加4个ASCII字符后

#include <iostream>
using namespace std;
int main()
{
    char a[6]; //定义一个字符型数组
    for(int i=1;i<6;i++) 
    {
        cin>>a[i];
        a[i]=a[i]+4;//字符型数组的加法是对其ASCII码进行加减
    }

    for(int i=1;i<6;i++)    
        cout<<a[i];
    cout<<endl;
    return 0;
}

输入:CHINA
输出:GLMRE

5 . 输出整数179的十六进制和八进制表达结果。

#include <iostream>
using namespace std;
int main()
{
    int a=179;
    cout<<"hex="<<hex<<a<<endl<<"oct="<<oct<<a<<endl;
}

输出:
hex=b3
oct=263

6 . 控制符下的输出

#include <iostream>
#include <iomanip> //使用控制符输出要加头文件
using namespace std;
int main()
{
    int a=179;
    cout<<setfill('+')<<setw(10)<<a<<endl;
    //输出宽度为10位,空的位数用"+"填充
}

输出:+++++++179

7.单个字符的输入输出

#include <iostream>
using namespace std;
int main()
{
    char a,c;
    a=getchar();.//从终端输入一个字符
    c=a-32;
    putchar(c);
    putchar(10);//输出一个换行符,与putchar('\n')同义,10是'\n'的ASCII码值
}

输入:b
输出:B

8 . if的嵌套
在if的嵌套中,也可以加花括号来表示层级匹配关系,特别是在if与else数目不一样的情况下:

if()
    {
  if() 语句1}
    else 语句2

9 . 判断某年是否为闰年

#include <iostream>
using namespace std;
int main()
{
    int year;
    cout<<"input the year:";
    cin>>year;
    if ((year%400==0)||(year%100!=0 && year%4==0))//核心代码就这一句话
        cout<<year<<"is leap year"<<endl;
    else cout<<year<<" is not leap year"<<endl;
    return 0;
}

输入:input the year:1900
输出:1900 is not leap year

10.【递归】用递归方法求n阶勒让德多项式的值,递归公式见书p123,习题8。

#include <iostream>
using namespace std;
int main()
{
    double Legendre(double x,int n);
    double a;
    int b;
    cout<<"input the value of x: "; cin>>a;
    cout<<"input the value of n: "; cin>>b;

    cout<<"the value of "<<b<<"-class "<<"Legendre is: "<<Legendre(a,b)<<endl;

}
double Legendre(double x,int n)
{
    double lgd;
    if(n==0) lgd=1;
    else if (n==1) lgd=x;
    else lgd=((2*n-1)*x*Legendre(x,n-1)-(n-1)*Legendre(x,n-2))/n;
    return lgd;
}

这里写图片描述

11.【函数嵌套】写一个函数验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和,如10=3+7,在主函数中输入一个不小于6的偶数,然后调用函数gotbaha,在gotbaha函数中再调用prime函数,prime函数用于判断是否素数。

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int 
  • 27
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值