扬州大学C语言上机作业19整理

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

答案仅供参考

实验一

4.设计main函数实现从键盘上输入两个整型变量a、b的值,交换它们的值并输出。#include<>

void main()

{

int a,b,c;

printf("enter first integer :");

scanf("%d",&a);

printf("enter second integer :");

scanf("%d",&b);

c=a;

a=b;

b=c;

printf("%d,%d\n",a,b);

getch();

}

5.设计main函数实现从键盘上输入两个整型变量a、b的值,并将b的值加入到a中,输出a的值。

#include<>

void main()

{

int a,b,c;

printf("enter first integer :");

scanf("%d",&a);

printf("enter second integer :");

scanf("%d",&b);

c=a+b;

a=c;

printf("a=%d,b=%d\n",a,b);

getch();

}

6.从键盘输入整数x的值,根据公式y=x3+3x2+x-10求y的值,输出x和y的值。

#include<>

void main()

{

int x,y;

printf("enter first integer : ");

scanf("%d",&x);

y=x*x*x+3*x*x+x-10;

printf("x=%d,y=%d\n",x,y);

getch();

}

实验二

1.编写程序,从键盘上输入一个整数(例如560)表示分钟,将其换算成用小时和分钟表示,然后输出至屏幕。

#include <>

void main()

{

int a,b,hour,min;

printf("enter first integer :");

scanf("%d",&a);

b=60;

hour=a/b;

min=a%b;

printf("hour=%d,min=%d\n",hour,min);

getch();

}

2.编写程序,输入两个整数(例如1500和350),求出它们的商和余数并进行输出。

#include <>

void main()

{

int a,b,c,d;

a=1500,b=350;

c=a/b;

d=a%b;

printf("%d,%d",c,d);

getch();

}

3.编写程序,读入3个整数给分别变量a,b,c,然后将a,b,c的值输出到屏幕,再交换它们中的数值,把a中原来的值给b,把b中原来的值赋给c,把c中原来的值赋给a,然后再次输出a,b,c的值到屏幕。

#include <>

void main()

{

int a,b,c,d;

printf("enter first integer : ");

scanf("%d",&a);

printf("enter second integer : ");

scanf("%d",&b);

printf("enter third integer : ");

scanf("%d",&c);

printf("a=%d,b=%d,c=%d\n",a,b,c);

d=c;

c=b;

b=a;

a=d;

printf("a=%d,b=%d,c=%d",a,b,c);

getch();

}

4.编写程序,读入3个双精度数,求它们的平均值输出到屏幕。

#include <>

void main()

{

double sum=0;

double a,b,c,d;

printf("enter first integer :");

scanf("%lf",&a);

printf("enter second integer :");

scanf("%lf",&b);

printf("enter third integer :");

scanf("%lf",&c);

sum=a+b+c;

d=sum/;

printf("d=%lf",d);

getch();

}

5.下列程序中,要求main函数实现如下功能:从键盘上输入3个正整数,求出它们中的最大值。请完善程序,并在程序最后用注释的方式给出你的测试数据及在这组测试数据下的运行结果。

#include <>

void main()

{

int a ,b ,c ,d ,max;

printf(“Enter three integers:”);

scanf(“%d%d%d”,&a,&b,&c);

if(a>b)

max=a;

else

max=b;

if(c>max)

max=c;

printf(“max of the three numbers is %d”,max);

grtch();

}

6. 请编程序:对从键盘上输入的x值,根据以下函数关系计算出相应的y值(设x,y 均为整型量)。

x值的范围计算y的公式

x<00

0<=x<10x

10<=x<2010

20<=x<40-5x+20

#include <>

void main()

{

int x,y;

printf("x=");

scanf("%d" ,&x);

if (x<0)

y=0;

else if (x<10&&x>=0)

y=x;

else if (x>=10&&x<20)

y=10;

else if (x>=20&&x<40)

y=(-5)*x+20;

printf("%d" ,y);

getch();

}

相关文档
最新文档