资源描述
实验二 各种分支结构
一. 实验目的
1. 了解C语言中表示逻辑量的方法(以0代表“假”,以1代表“真”)。
2. 正确使用C语言中的逻辑运算符和逻辑表达式。
3. 熟练掌握if语句,if-else语句和switch-case语句。
二. 实验要点
在C语言中主要用以下方法实现分支结构:
1. if-else语句 格式:if(表达式)
语句块1
else
语句块2
若表达式的值为真,就执行语句块1;否则,执行语句块2。
2.switch-case语句
if语句只有两个分支可供选择,switch语句可以提供多个分支选择。
switch-case语句格式:
switch (表达式)
{ case 常量1: 语句1;
case 常量2: 语句2;
case 常量3: 语句3;
case 常量4: 语句4;
default: 语句n
}
三. 实验要求
1. 上机前编写好以下程序。(1为程序改错,2-6为填空)
2. 上机输入和调试自己所编的程序并存在自己的软盘上。
3. 检查实验结果是否正确。
4. 上机结束后,整理实验报告。
四. 实验内容
上机调通以下程序,并写出以下程序的输出结果。
改正以下程序的错误,并上机调通,程序完成输入两个数,按由小到大的顺序输出。
main(){
int a,b;
scanf(“%d,%d”,a,b);
if(a>b)
c=b;b=a;a=c;
printf(“%d,%d”,a,b);}
输入45,-55<CR>(<CR>表回车)
main()
{
int a,b,c;
scanf("%d,%d",&a,&b);
if(a>b)
{
c=b;
b=a;
a=c;
}
printf("%d,%d",a,b);
}
45,-55
-55,45Press any key to continue
以下程序的输出结果是( )。
main(){
printf(“%d\t”,3&&6&&9);
printf(“%d\t”,3||6||!0);
printf(“%d\n”,0&&1||0);
}
1 1 0
Press any key to continue
有一函数 x (x<1)
y= 2x-1 (1≤x<10)
3x-11 (x≥10)
用scanf函数输入x的值,求y的值。请在【 】内填入正确内容,并上机把程序调通,写出运行结果。
main(){
int x,y;
printf(“Please input x:”);
scanf(“%d”,【 】);
if(x<1)
{ y=x;
printf(“x=%d, y=x=%d\n”,x,y);
}
else if(【 】)
{ y=2*x-1;
printf(“x=%d,y=2*x-1=%d\n”,x,y);}
else
{ y=【 】;
printf(“x=%d,y=3*x-11=%d\n”,x,y);}
}
运行结果:
Please input x:4<CR>(<CR>表回车)
输出
Please input x:-1<CR>(<CR>表回车)
输出
Please input x:20<CR>(<CR>表回车)
输出
main(){
int x,y;
printf("Please input x:");
scanf("%d",&x);
if(x<1)
{ y=x;
printf("x=%d, y=x=%d\n",x,y);
}
else if(x<10)
{ y=2*x-1;
printf("x=%d,y=2*x-1=%d\n",x,y);}
else
{ y=3*x-11;
printf("x=%d,y=3*x-11=%d\n",x,y);}
}
Please input x:4
x=4,y=2*x-1=7
Press any key to continue
Please input x:-1
x=-1, y=x=-1
Press any key to continue
Please input x:20
x=20,y=2*x-1=39
Press any key to continue
4. 以下程序对输入的一个小写字母,将字母循环后移5个位置后输出.如’a’成’f’, ‘w’变成’b’.请在【 】内填入正确内容.
#include “stdio.h”
main()
{ char c;
c=getchar();
if (【 】) c=c+5;
else if (c>=’v’ && c<=’z’) 【 】;
putchar(c);
}
输入x<CR>(<CR>表回车)
输出
#include "stdio.h"
main()
{
char c;
c=getchar();
if (c>=’a’&&c<'v')
c=c+5;
else if (c>='v' && c<='z')
c=c+5-26;
putchar(c);
}
x
cPress any key to continue
5. 以下程序完成输入数字1-7,打印出对应的英文(Monday-Sunday). 请在【 】内填入正确内容。
#include <stdio.h>
main()
{
int a;
printf("please input a number\n");
scanf("%d",&a);
if (【 】)
{ switch (a)
{ case 1: printf("Monday"); 【 】;
case 2: printf("Tuesday");break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
【 】: printf("Friday"); break;
case 6: printf("Saturday");break;
case 7: printf("Sunday"); break;
}
}
else printf("input error");
}
运行结果:
please input a number
1<CR>(<CR>表回车)
输出
please input a number
15<CR>(<CR>表回车)
输出
#include <stdio.h>
main()
{
int a;
printf("please input a number\n");
scanf("%d",&a);
if (a >= 1 && a <= 7)
{
switch (a)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
}
else printf("input error");
}
please input a number
1
MondayPress any key to continue
please input a number
15
input errorPress any key to continue
6.以下程序给出一百分制成绩,要求输出成绩等级‘A’90分以上,B:80-89,C:70-79,D:60-69,E: 60分以下,请在【 】内填入正确内容。
#include <stdio.h>
main()
{
int score,temp,log;
char grade;
log=1;
while(log)
{ printf(" please enter score:");
scanf("%d",【 】);
if((score>100)||(score<0))
printf(“\n error,try again!\n”);
else log=0;}
if(score==100)temp=9;
else temp=(score-score%10)/10;
switch(temp)
【 】
case 1:
case 2:
case 3:
case 4:
case 5:
case 0: grade ='E'; break;
case 6: 【 】 break;
case 7: grade='C';break
case 8: grade=’B’;break;
case 9: 【 】
}
printf( "score=%d,grade=%c\n",score,grade);
}
运行结果:
please enter score:
90<CR>(<CR>表回车)
输出
please enter score:
68<CR>(<CR>表回车)
输出
#include <stdio.h>
main()
{
int score,temp,log;
char grade;
log=1;
while(log)
{
printf(" please enter score:");
scanf("%d",&score);
if((score>100)||(score<0))
printf("\n error,try again!\n");
else
log=0;
}
if(score==100)
temp=9;
else
temp=(score-score%10)/10;
switch(temp)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 0: grade ='E'; break;
case 6: grade='D' ; break;
case 7: grade='C';break;
case 8: grade='B';break;
case 9: grade='A';break;
}
printf( "score=%d,grade=%c\n",score,grade);
}
please enter score:90
score=90,grade=A
Press any key to continue
please enter score:68
score=68,grade=D
Press any key to continue
展开阅读全文