物联1234 C语言第三次作业

第一题

下面程序运行结果为( )。请解释原因。

void main ( ) 
{ 
	char c = 'a'; 
	if ('a' < c <= 'z')
	{
		printf ("LOW"); 
	}
	else
	{
		printf ("UP"); 
  	}
}

A. LOW
B. UP
C. LOWUP
D. 程序语法错误

答案:A、LOW
分析:if 语句括号内 ‘a’ < c <= ‘z’,首先判断的是 ‘a’ < c,显然c取的是 ‘a’ 的 ASCLL 码值,不成立,所以 ‘a’ < c 整体取为 0。因为 0 < ‘z’,所以 if 语句的条件成立,执行 printf(“LOW”) 。

第二题

请阅读以下程序,该程序()。请对选项进行解释。

void main() 
{ 
	int a = 5, b = 0, c = 0; 
	if (a = b + c) 
		printf ("***\n"); 
	else 
		printf ("$$$\n"); 
} 

A. 有语法错不能通过编译
B. 可以通过编译但不能通过连接
C. 输出***
D. 输出$$$

答案:D
分析:if 语句条件判断中 b + c 的值将会赋值给 a,导致条件恒为假,执行else语句。

第三题

填空:以下程序运行结果是_____________,请解释。

void main() 
{ 
	int m = 5; 
	if (m++ > 5) 
		printf("%d\n", m); 
	else
		printf("%d\n", m--); 
}

答案:6
分析:m++ 是先取值再自增一,所以 m++ > 5 为假,执行 else 语句——打印 m 自增后的的值,打印完后 m 自减一。

第四题

已知 int x = 10, y = 20, z = 30; 以下语句执行后x =__, y=,z=_______。请解释。
if (x > y) z = x; x = y; y = z;

答案:x = 20, y = 30, z = 30
分析:x > y 为假,不执行 z = x ,执行后续的语句。

第五题

下面的程序的功能是根据输入的百分制成绩score,转换成相应的五分制成绩grade并打印输出。转换的标准为:
当90≦score≦100时,grade为A;
当80≦score﹤90时,grade为B;
当70≦score﹤80时,grade为C;
当60≦score﹤70时,grade为D;
当score﹤60时,grade为E;
请填空:
(1)__________ (2)__________ (3)__________
(4)__________ (5)__________ (6)__________ (7)__________

#include <stdio.h> 
void main ()
{ 
	int score, mark; 
	scanf ("%d",____(1)______); 
	mark = _____(2)_____; 
	switch (mark) 
	{ 
		default: printf ("%d--E", score); ______(3)____; 
		case 10: 
		case___(4)___: printf ("%d--A", score); break; 
		case___(5)___: printf ("%d--B", score); break; 
		case___(6)___: printf ("%d--C", score); break; 
		case___(7)___: printf ("%d--D", score); break; 
	} 
}

(1)score (2)score/10 (3)printf(“%d–A”);break;
(4) 9 ,(5) 8, (6) 7, (7) 6

第六题

程序实现:输入1个年份 year,判断year是否是闰年?

代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int year;
    while (1)    // 循环输入
    {
        printf("请输入你要查询的年份:");
        scanf("%d", &year);
        getchar();  // 吃掉scanf没有读取的'\n' 
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            printf("%d年是闰年\n", year);
        else
            printf("%d年不是闰年\n", year);
        system("pause");
        printf("\n");
    }
    return 0;
}

程序演示

在这里插入图片描述

第七题

程序实现:输入1个年份 year,判断year是否是闰年?

代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numA, numB, numC;
    scanf("%d %d %d", &numA, &numB, &numC);
    int max, min;
    max = numA;
    min = numA;
    if (max < numB)
        max = numB;
    if (max < numC)
        max = numC;
    if (min > numB)
        min = numB;
    if (min > numC)
        min = numC;
    printf("这三个数中最大值为%d,最小值为%d。\n", max, min);
    system("pause");
    return 0;
}

程序演示

在这里插入图片描述

升级版

#include <stdio.h>
#include <stdlib.h>

#define NUMBER 100 // 可输入数字的最大族数

int main()
{
    int cnt = 0; // 计数输入了多少组数字
    int num[NUMBER];
    for (int i = 0; i < NUMBER; i++)
    {
        scanf("%d", &num[i]);
        cnt++;
        if (getchar() == '\n')
            break;
    }
    int max = num[0], min = num[0];
    for (int i = 1; i < cnt; i++)
    {
        if (max < num[i])
            max = num[i];
        if (min > num[i])
            min = num[i];
    }
    printf("这%d组数字中最大值是%d,最小值是%d。\n", cnt, max, min);
    system("pause");
    return 0;
}

在这里插入图片描述

第八题

程序实现:仿照课堂PPT,实现一个简单的计算器程序(只要求计算加减乘除)。要求:用户从键盘输入如下形式的表达式:操作数1 运算符op 操作数2, 比如:1 + 2, 1 / 2, 1 * 2 等,能够输出对应的结果,比如:1 + 2 = 3,1 / 2 = 0.500000, 1 * 2 = 2.

代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double a, b;
    char c;
    while (1)
    {
        scanf("%lf%c%lf", &a, &c, &b);
        switch (c)
        {
        case '+':
            printf("%f + %f = %f\n", a, b, a + b);
            break;
        case '-':
            printf("%f - %f = %f\n", a, b, a - b);
            break;
        case '/':
            printf("%f / %f = %f\n", a, b, a / b);
            break;
        case '*':
            printf("%f * %f = %f\n", a, b, a * b);
            break;
        default:
            printf("格式错误!\n");
        }
        getchar();
        system("pause");
        printf("\n");
    }
    return 0;
}

程序演示

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值