- 打印出"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的倍数,我们打印敲桌子,其余数字直接打印输出。
- **练习案例:**乘法口诀表
案例描述:利用嵌套循环,实现九九乘法表