变量
1.为什么定义变量?
保存数据:密码 金额
2.变量数据类型所占字节
int 占4 个字节- %d
long 占4 个字节 -%ld
float 占4 个字节 -%f
double 占8 个字节 -%f
char 占1个字节 - %c
bool 占1 个字节- %d
输入和输出
#include
#include
int main(){
int count=0;//定义变量 4byte*8=32wei
count=3;
//%d
//%02d 数据共两位,不足两位的用0补齐
//%.2f 保留小数点后两位
printf("i have %02d dogs\n",count) ;//前面是格式,后面是要输出变量的值
//运行结果:i have 3 dogs
long size=230;//10G=10*1024M*1024KB*1024B
printf("%ld\n",size);
//运行结果:230
float score=3.1415926;
printf("%f\n",score);
//运行结果:3.141593
float score1=95.5;
printf("%f\n",score1);
//运行结果:95.500000
float score2=3.1415926;
printf("%.2f\n",score2);
//运行结果:3.14
//中文占据两个字符
//%c 字符
//%d 字符对应ASCII码值
char choose='c';
printf("%c\n",choose);
printf("%d\n",choose);
//运行结果:c
//运行结果:99
//非零即真1 零即假0
bool status=-3;//false
printf("%d\n",status);
//运行结果:1
//scanf格式化输入
/*
int a=123;
char b=' ';
int c=456;
缓冲区: " 123 456'\n' "
终端 :123 456'\n'
*/
/*
jack123 用户输入的格式
注意:不要再scanf里面随便写内容
写的内容就是用户输入的格式
pwd=123
*/
int pwd=0;
//scanf("jack%d",&pwd);//jack123
//scanf("_%d_",&pwd);//_123_
scanf("%d",&pwd);//123
printf("pwd=%d\n",pwd);//123
//运行结果:(输入123) pwd=123
int num1,num2;//12
//scanf("%d%d",&num1,num2) ;
scanf("%d %d",&num1,&num2) ;
printf("%d %d\n",num1,num2) ;
//运行结果:(输入1 2) 1 2
//判断
/*
if(条件){条件成立要做的事
}
if(条件){条件成立要做的事
}else{条件不成立要做的事
}
if(条件1){条件1成立要做的事
}else if(条件2){条件2成立要做的事
}else if(条件3){条件3成立要做的事
}else{以上条件都不成立要做的事
}
*/
/*当某种条件成立才做事情
if(条件-true非0-false=0){
成立要做的事情
}
*/
/*
运算符 + - *
/ 除 取整数部分7除3=2... 1, 则7/3=2
% 取余数部分7除3=2... 1,则 7%3=1
x%16+1=1....16位
x%51+50=50...100位
&& 且 两个同时成立 1>2 && 2>1
//或 只要一个成立就可以了 1>2 // 2>1
++ 自增a++,++a
+= a+=2
-- 自减
== 判断 a==2
= 赋值
!= 不等于 1!=2
*/
float r=3/2;
printf("%f\n",r);
//运行结果:1.000000
int a=20;
if(++a==20){
printf("a=20\n");
printf("a=%d\n",a);
}else{
printf("a!=20\n");
printf("a=%d\n",a);
}
printf("a=%d\n",a);
//运行结果:a!=20
//运行结果:a=21
if(a==20){
printf("a=2\n");
}
//运行结果:
//问号表达式
char c;
if(3>1){
c='y';
} else{
c='n';
}
//运行结果:c='y'
//等价于c=3>1?'y':'n';//三目运算符
//逗号表达式
//result=表达式1,表达式2,表达式3
int temp=20;
int t2=(temp--,temp--,temp--);
if(t2){
printf("temp=%d\n",temp);
printf("t2=%d\n",t2);
}
//运行结果:temp=17
//运行结果:t2=18
return 0;
}
计算到手工资 标准
#include
int main(){
int salary = 0;//定义变量
int pay=0;//税后工资
//接受输入
printf("请输入工资:");
scanf("%d",&salary);
//开始计算
if(salary<=5000){
pay=salary;
}else if("salary>5000&&salary<=8000"){
int tax = (salary-5000)*0.1;
pay = salary-tax;
}else if("salary>8000&&salary<10000"){
int tax = (salary-8000)*0.15+3000*0.1;
pay = salary-tax;
}else{
int tax = (salary-10000)*0.2+3000*0.1+2000*0.15;
pay = salary-tax;
}
printf("到手工资:%d",pay);
return 0;
}
计算到手工资 标准优化
#include
int main(){
int salary = 0;//定义变量
int tax=0;//税
//接受输入
printf("请输入工资:");
scanf("%d",&salary);
//开始计算
if(salary<=5000){
tax=0; //这一步可删除
}else if("salary>5000&&salary<=8000"){
tax = (salary-5000)*0.1;
}else if("salary>8000&&salary<10000"){
tax = (salary-8000)*0.15+3000*0.1;
}else{
tax = (salary-10000)*0.2+3000*0.1+2000*0.15;
}
printf("到手工资:%d",salary-tax);
return 0;
}
输入年龄 显示 your age is 英文数字
//保存个位数
char* gewei[] = {"","one","two","three","four","five" ,"six","seven" ,"eight","nine"};
//保存10-19
char* temp[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
//保存十位
char* shiwei[] = {"twenty","thirty","forty","fifty","sixty","seventy","eighety","ninty"};
// 接受输入
printf("请输入年龄:");
scanf("%d",&age);
char *name;
if(age<10){
//直接访问个位数组
name = gewei[age];
} else if(age>=10&&age<=19){
//获取个位数
int index = age % 10;
name = temp[index];
}else{
//获取个位数
int g = age % 10;
//获取十位数
int s = age / 10;
//取出对应的单词
char* sString = shiwei[s-2] ;
char* gString = gewei[g];
printf("your age is %s-%s\n ",sString,gString);
return 0;//程序还未运行完毕就提前结束
}
printf("yoour age is %s\n",name);
/*
age<10;
age/10;
age%10;
[one two three four five six seven eight nine]
[ten eleven twelve thirteen fourteen fifteen sixteen
seventeen eighteen nineteen]
[twenty thirty forty fifty sixty seventy eighety ninty]
*/
return 0;
}