ios学习记录 day2
1.&&和||的短路现象
2.条件 ? 表达式1 :表达式2 ;(条件为真时执行表达式1,否则执行表达式2)
作业:
1.铁路托运行李规定:行李重不超过50公斤的托运费按0.15元每公斤计算,超过50公斤的部分每公斤加收0.1元。
int main(int argc, const char * argv[])
{
float weight = 0,money = 0;
printf("输入货物重量:");
scanf("%f",&weight);
if (weight <= 50) {
money = weight * 0.15;
}else if (weight > 50){
money = (weight - 50) * 0.25 + 50 * 0.15;
}
printf("托运费为:%.2f\n",money);
return 0;
}
2.完成四则运算,如:用户输入34+56 输出90.00。用户输入两个数和运算符。通过运算符输出结果。
(1)
int main(int argc, const char * argv[])
{
float result = 0,a = 0,b = 0;
char c = '\0';
printf("请输入表达式:");
scanf("%f%c%f",&a,&c,&b);
if (c == '+') {
result = a + b;
}else if (c == '-'){
result = a - b;
}else if (c == '*'){
result = a * b;
}else if (c == '/'){
result = a / b;
}else {
printf("输入错误\n");
}
printf("结果是:%.2f\n",result);
return 0;
}
(2)
int main(int argc, const char * argv[])
{
float a = 0,b = 0;
char c = '\0';
printf("输入计算内容:");
scanf("%f%c%f",&a,&c,&b);
switch (c) {
case '+':
printf("结果是:%.2f\n",a + b);
break;
case '-':
printf("结果是:%.2f\n",a - b);
break;
case '*':
printf("结果是:%.2f\n",a * b);
break;
case '/':
printf("结果是:%.2f\n",a / b);
break;
default:
printf("输入错误\n");
break;
}
return 0;
}
3.输入三个数,求最大值和中间值。
(1)
int main(int argc, const char * argv[])
{
int a = 0,b = 0,c = 0,max = 0,mid = 0;
scanf("%d,%d,%d",&a,&b,&c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("最大值是:%d\n",max);
if ((a - c) * (a - b) <= 0) {
mid = a;
}else if ((b - a) * (b - c) <= 0){
mid = b;
}else{
mid = c;
}
printf("中间值是:%d\n",mid);
(2)
int a = 0,b = 0,c = 0,mid = 0;
printf("请输入三个数:");
scanf("%d,%d,%d",&a,&b,&c);
if (a >= b) {
if (a >= c) {
(b >= c) ? (mid = b) : (mid = c);
}else if (a < c){
mid = a;
}
}else if(a < b){
if (a >= c) {
mid = a;
}else if(a < c){
(b >= c) ? (mid = c) : (mid = b);
}
}
printf("中间数是:%d\n",mid);
(3)
int a = 0,b = 0,c = 0,mid = 0;
printf("请输入三个数:");
scanf("%d,%d,%d",&a,&b,&c);
if (a > b && a > c) {
mid = (b > c) ? b : c;
}else if (b > c && b > a){
mid = (a > c) ? a : c;
}else{
mid = (a > b) ? a : b;
}
printf("中间值是:%d\n",mid);
(4)
int a = 0,b = 0,c = 0,mid = 0;
scanf("%d,%d,%d",&a,&b,&c);
if (a >= b && a <= c) {
mid = a;
}else if (b >= a && b <= c){
mid = b;
}else{
mid = c;
}
printf("中间值是:%d\n",mid);
(5)
int a = 0,b = 0,c = 0,mid = 0,max = 0,min = 0,sum = 0;
scanf("%d,%d,%d",&a,&b,&c);
max = a > b ? a : b;
max = max > c ? max : c;
min = a < b ? a : b;
min = min < c ? min : c;
sum = a + b + c;
mid = sum - max - min;
printf("中间值是:%d\n",mid);
return 0;
}
4.有一个函数:x<1时,y=x;1<=x<10时,y=2x-1;x>=10时,y=3x-11;输入x,输出y。
int main(int argc, const char * argv[])
{
int x = 0,y = 0;
scanf("%d",&x);
if (x < 1) {
y = x;
}else if (x >= 1 && x <= 10){
y = 2 * x - 1;
}else if(x > 10){
y = 3 * x - 11;
}
printf("y的值是:%d\n",y);
return 0;
}
5.给出一个百分制成绩,90分以上为‘A’,80~89为‘B’,70~79为‘C’,60~69为‘D’,60以下为‘E’。
int main(int argc, const char * argv[])
{
int x = 0;
scanf("%d",&x);
if (x >= 90 && x <= 100) {
printf("A\n");
}else if (x >= 80 && x <= 89){
printf("B\n");
}else if (x >= 70 && x <= 79){
printf("C\n");
}else if (x >= 60 && x <= 69){
printf("D\n");
}else if (x >= 0 && x <= 59){
printf("E\n");
}else{
printf("输入错误\n");
}
return 0;
}
6.输入一个五位数,判断是否为回文数(如12321)
int main(int argc, const char * argv[])
{ int a = 0,wuwei = 0,siwei = 0,sanwei = 0,erwei = 0,yiwei = 0;
printf("输入一个五位数:");
scanf("%d",&a);
wuwei = a / 10000;
siwei = (a - wuwei * 10000) / 1000;
sanwei = (a - wuwei * 10000 - siwei * 1000) / 100;
erwei = (a - wuwei * 10000 - siwei * 1000- sanwei * 100) / 10;
yiwei = a - wuwei * 10000 - siwei * 1000- sanwei * 100 - erwei * 10;
if (wuwei == yiwei && siwei == erwei) {
printf("这个五位数是回文数\n");
}else{
printf("这个五位数不是回文数\n");
}// 第一种
int a = 0;
scanf("%d",&a);
if ((a / 10000 == a % 10) && (a / 1000) % 10 == (a % 100) / 10) {
printf("是回文数\n");
}else{
printf("不是回文数\n");
}//第二种
return 0;
}