塔里木大学计算机基础课程实验报告
实验步骤与内容:
1、 程序填空:输入正整数,判断是否既是5也是7的倍数,是,输出YES,否则,输出NO。
2、 #include "stdio.h"
3、 void main()
4、 {
5、 int a;
6、 scanf("%d",&a);
7、 if( a%5==0&&a%7==0 )
8、 { printf("YES"); ;
9、 } else
10、 printf("NO"); ;
11、 }
程序填空:从键盘输入一行字符,可以是数字、字母、或是其他字符,对输入的字符进行判断,统计字符串中数字字符、字母字符、空格字符及其他字符的个数,当输入换行符时结束循环。
12、 #include<stdio.h>
13、 main()
14、 {
15、 char c;
16、 int num=0,letter=0,blank,others=0;
17、 ;
18、 while((c=getchar())!= '\n' )
19、 {
20、 if( c>='a'&&c<='z' )
21、 letter++;
22、 else if( c>='0'&&c<='9' )
23、 num++;
24、 else if( c== " " )
25、 blank++;
26、 else others++;
27、 printf("%d,%d,%d",num,letter,others);
28、
29、
30、 }
31、
32、 }
33、 程序改错:从键盘上输入三个整数,并比较大小,按照从小到大的顺序输出。
#include "stdio.h"
void main()
{
int a,b,c,t;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{ t=a,a=b,b=t;}
if(a>c)
{t=a,a=c,c=t;}
if(b>c)
{ t=b,b=c,c=t;}
printf("%d %d %d\n",a,b,c);
}
运行结果:
程序出错分析:int是整形,缺少&,“;”应该只在结尾,
34、 程序改错。
#include<stdio.h>
main()
{
int x,y,t;
scanf("%d,%d\n",&x,&y);
if(x>=y)
{
t=x-y;
printf("%d\n",t);
}
else
{
t=x+y;
printf("%d\n",t);r
}
}}
运行结果:
程序出错分析:main后应该没有“;”,%d后应该有“,”,then应该换成else。
35、 分析程序的运行结果。
#include <stdio.h>
#include <math.h>
main()
{
int a,gewei,shiwei,baiwei;
scanf("%3d",&a); /* 输入一个3位的整数 */
gewei=a%10;
shiwei=a/10%10;
baiwei=a/100;
if(pow(gewei,3)+pow(shiwei,3)+pow(baiwei,3)==a)
printf("YES\n");
else
printf("NO\n");
}
运行结果(输入153和246):
程序分析:判断是否为水仙花数
36、 分析程序的运行结果。
#include "stdio.h"
void main()
{
int x=1,y=0,a=0,b=0;
switch(x)
{
case 1:switch(y)
{
case 0:a++;break;
case 1:b++;break;
}
case 2:a++;b++;break;
}
printf("%d %d\n",a,b);
}
运行结果:
程序分析:x=1,a++,b++,y=0a++。
37、 编写程序:铁路托运行李,假设每张车票可按每公斤0.5元托运50公斤以内的行李,如果超过50公斤时,超过的部分每公斤加价0.3元,试编写一道计算行李托运费的程序。
设托运行李重为W公斤,托运费为X元,要求如下:
①用if 语句实现分支。自变量w与函数值x均采用实型。
②自变量w值从键盘输入,且输入前要有提示信息。
③数据的输出格式采用以下形式: w=输入值,x=计算值
④分别以行李重30公斤,80公斤为自变量,运行该程序。记录运行结果。
#include "stdio.h"
void main()
{int w,x;
scanf("%d",&w);
if(w<=50)
{x=0.5*50;
printf("%d",x);}
else if(w>50)
{x=0.5*50+(w-50)*0.3;
printf("%d",x);}
}
程序:
运行结果:
38、
编写程序:输入实数x,按下列公式求分段函数y的值。
程序:#include "stdio.h"
void main()
{int y,x;
scanf("%d",&x);
if(x<-20)
{y=x*X+0.8*x+6 ;
printf("%d\n",y)}
else if(-20<=x<10)
{y=x*x-0.47*x+3.9
}
else if (x>=10)
{y=x*x*X+2.6x-7;
printf("%d\n",y);}
}
运行结果:
第二篇:C语言程序设计实验报告(二)
C语言程序设计实验报告