c++基础练习

  • 打印出"Hello world"。
#include<iostream>
using namespace std;
int main(){
    cout<<"Hello world"<<endl;
    system("pause");
    return 0;
}
  • 用两种方式分别定义年份和月份,并输出
#include<iostream>
#define year 2023
using namespace std;
int main(){
    const int month = 4;
    cout<<"year = "<<year<<endl;
    cout<<"month = "<<month<<endl;
    system("pause");
    return 0;
}
  • 分别打印出int,short,long,float,double所占用的字节数。
#include<iostream>
using namespace std;
int main(){
    cout<<sizeof(int)<<endl;
    cout<<sizeof(short)<<endl;
    cout<<sizeof(long)<<endl;
    cout<<sizeof(float)<<endl;
    cout<<sizeof(double)<<endl;
    system("pause");
    return 0;
}
  • 分别打印出字符a,A,\并其对应的ASCii值,用制表符排版。下一行用字符串显示Hello world ,hello用c语言风格,world用c++风格。
#include<iostream>
using namespace std;
#include<string>//c++风格字符串库函数
int main(){
    char ch1,ch2,ch3;
    char str1[]="Hello ";    //C
    string str2="world";    //C++
    ch1='a',ch2='A',ch3='\\';
    cout<<(int)'a'<<'\t'<<(int)'A'<<'\t'<<(int)'\\'<<endl;
    cout<<(int)ch1<<'\t'<<(int)ch2<<'\t'<<(int)ch3<<endl;
    cout<<str1<<str2<<endl;
    system("pause");
    return 0;
}
  • 创建bool类型变量,做赋值操作,并输出值。打印其所占的字节大小。
#include
using namespace std;
int main(){
bool flag = true;//定义变量
flag = false;//变量赋值
cout<<flag<<endl;
cout<<sizeof(flag)<<endl;
system(“pause”);
return 0;
}
  • cin,四则运算,自增自减运算,比较运算符,逻辑运算符
  • 练习案例: 三只小猪称体重有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重?
  • 三目运算符
  • 练习案例:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身

1^3 + 5^3+ 3^3 = 153

请利用do…while语句,求出所有3位数中的水仙花数

  • 练习案例:敲桌子

案例描述:从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。

  • **练习案例:**乘法口诀表

案例描述:利用嵌套循环,实现九九乘法表

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值