一、实验目的
1.学会正确使用逻辑运算符和逻辑表达式;
2.熟练掌握if语句和switch 语句。
二、实验内容
1.任意输入4个整数,从大到小排列输出。
2.输入一组成绩,要求输出成绩等级为A,B,C,D,E。其中90—100为A,80—89为B,70—79为C,60—69为D,60以下为E。
3.有一个函数
写一程序,输出y值。
三、实验记录
3.1 四个数字排序
(1)源代码
# include <stdio.h>
int main(void)
{
int a,b,c,d,t;
printf("Please enter the data(a,b,c,d):\n");
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a<b)
{
t = a;
a = b;
b = t;
}
if(a<c)
{
t = a;
a = c;
c = t;
}
if(a<d)
{
t = a;
a = d;
d = t;
}
if(b<c)
{
t = b;
b = c;
c = t;
}
if(b<d)
{
t = b;
b = d;
d = t;
}
if(c<d)
{
t = c;
c = d;
d = t;
}
printf("The retult is: %d %d %d %d\n",a,b,c,d);
return 0;
}
(2)运行结果截图
3.2 判断分数等级
(一)源代码
# include <stdio.h>
/*函数声明*/
void inputgrade(int * grade,int lg);
int main(void)
{
int grade[10];
int lg,i;
printf("How many of the data?\n");
scanf("%d",&lg);
printf("Please enter the grade:\n");
inputgrade(grade,lg);
for(i = 0;i<lg;++i)
{
if(grade[i]<=100 && grade[i]>=90)
printf("The %dth degree is A!\n",i+1);
else if(grade[i]<90 && grade[i]>=80)
printf("The %dth degree is B!\n",i+1);
else if(grade[i]<80 && grade[i]>=70)
printf("The %dth degree is C!\n",i+1);
else if(grade[i]<70 && grade[i]>=60)
printf("The %dth degree is D!\n",i+1);
else if(grade[i]<60)
printf("The %dth degree is E!\n",i+1);
else
printf("The %dth data is wrong!");
}
return 0;
}
/*输入数据的函数,当用户输入0时表示输入已完成。*/
void inputgrade(int * grade,int lg)
{
int i;
for(i = 0;i<lg;++i)
{
printf("Please enter the %dth data:\n",i+1);
scanf("%d",grade+i);
}
return;
}
(2)运行结果截图
3.3 已知x的值求y的值
(1)程序框图
(2)源代码
# include <stdio.h>
int main(void)
{
int x,y;
printf("Please enter x:\n");
scanf("%d",&x);
if(x<1)
printf("y = %d\n",x);
else if(x>=1 && x<10)
printf("y = %d\n",2*x+1);
else
printf("y = %d\n",3*x-1);
return 0;
}
(3)运行结果截图
3.4 设计程序计算需要如何付钱
(1)源代码
# include <stdio.h>
int main(void)
{
int a = 0,b = 0,c = 0,d = 0,e = 0,f = 0,g = 0,h = 0,k = 0,i,j;
float mon;
printf("How much the money we need?\n");
scanf("%f",&mon);
/*判断需要多少张100元的钞票*/
while(mon>=100)
{
a++;
mon = mon - 100;
}
/*判断需要多少张50元的钞票*/
while(mon>=50)
{
b++;
mon = mon - 50;
}
/*判断需要多少张10元的钞票*/
while(mon>=10)
{
c++;
mon = mon - 10;
}
/*判断需要多少张5元的钞票*/
while(mon>=5)
{
d++;
mon = mon - 5;
}
/*判断需要多少张2元的钞票*/
while(mon>=2)
{
e++;
mon = mon - 2;
}
/*判断需要多少张1元的钞票*/
while(mon>=1)
{
f++;
mon = mon - 1;
}
/*判断需要多少张0.1元的钞票*/
while(mon>=0.1)
{
g++;
mon = mon - 0.1;
}
/*判断需要多少张0.05元的钞票*/
while(mon>=0.05)
{
h++;
mon = mon - 0.05;
}
/*判断需要多少张0.01元的钞票*/
while(mon>0)
{
k++;
mon = mon - 0.01;
}
printf("We need:\n");
printf("100 yuan %d page(s)\n50 yuan %d page(s)\n10 yuan %d page(s)\n5 yuan %d page(s)\n2 yuan %d page(s)\n1 yuan %d page(s)\n0.1 yuan %d page(s)\n0.05 yuan %d page(s)\n0.01 yuan %d page(s)\n",a,b,c,d,e,f,g,h,k);
return 0;
}
(2)程序运行截图