C++ 小案例,你以前肯定做过

今天复习C++的时候看见过自己曾经做过的小案例,觉得挺有意思的,分享给大家一起看看吧。又有哪些是你们做过的呢?

🐧1.字符案例 - 大小写转换

需求:键盘输入一个字符 如果是大写就改成小写 如果是小写就改成大写 其他字符不转换

#include<iostream>
using namespace std;
void test()
{
    //定义一个字符变量
    char ch = '\0';

    //给字符变量获取键盘输入
    cout<<"请输入一个字符:";
    cin>>ch;
    //大小写转换
    if(ch>='a' && ch<='z')
    {
        //小写转大写
        ch = ch-('a'-'A');
    }
    else if(ch>='A' && ch<='Z')
    {
        //大写转小写
        ch = ch+('a'-'A');
    }
    //输出
    cout<<"ch="<<ch<<endl;
}
int main()
{

   test();

   system("pause");

   return 0;
}

 🐧2.判断data是否能被3整除


#include<iostream>
using namespace std;
void test()
{
    int data;
    cout<<"请输入一个int数据:";
    cin>>data;

    if(data%3==0)
    {
        cout<<data<<"能被3整除"<<endl;
    }
    else
    {
        cout<<data<<"不能被3整除"<<endl;
    }
}
int main()
{

    test();
    system("pause");

    return 0;
}

🐧3.键盘输入1~7的数值 判断是星期几


#include<iostream>
using namespace std;
void test()
{
     int data = 0;
     cout<<"请输入1~7之间的数字:";
     cin>>data;
     if(data<1 || data>7)
     {
        cout<<"输入无效数值,请输入1~7之间的数字";
        return;
     }

     switch(data)
     {
                    case 1:
                         cout<<"星期一"<<endl;
                         break;
                    case 2:
                        cout<<"星期二"<<endl;
                        break;
                    case 3:
                        cout<<"星期三"<<endl;
                        break;
                    case 4:
                        cout<<"星期四"<<endl;
                        break;
                    case 5:
                        cout<<"星期五"<<endl;
                        break;
                    case 6:
                        cout<<"星期六"<<endl;
                        break;
                    case 7:
                        cout<<"星期日"<<endl;
                        break;
     }
}
int main()
{
   
   test();
   
   system("pause");
   return 0;
}

🐧4. ++i 和 i++ 的区别


#include<iostream>
using namespace std;
void test()
{
    int i = 3;
    //自增或者自减作为一条独立的语句 那么i++和++i是没有什么区别的
    //i++;
    ++i;
    cout<<i<<endl;

    int x=3;
    int y =0;
    y = x++;//先赋值 在+1
    cout<<"x="<<x<<","<<"y="<<y<<endl;//4 3

    int a=3;
    int b =0;
    b = ++a;//先+1 再赋值
    cout<<"a="<<a<<","<<"b="<<b<<endl;//4 4
}
int main()
{

   test();
   
   system("pause");
   return 0;
}

🐧 5. for循环求1~100的和

#include<iostream>
using nameapce std;
void test()
{
    int i = 0;
    int sum =0;

    for(i=1;i<=100;i++)
    {
        sum+=i;
    }
     cout<<"sum="<<sum<<endl;
}
int main()
{
     
    
    test();
    system("pause");
    
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

易点点心动

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

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

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

打赏作者

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

抵扣说明:

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

余额充值