1、实验一顺序结构的程序设计 1.输入X的值,根据下面的多项表达式求y的值: 3 5,8 ,y= — x +12x -7. 3x3-—x~+2x-4-43 (1) 编程,注意要有输入数据的提示语句。 #include "stdio.h" main() {double x,y;printf("please input x:\n");scanf(”%lf',&x);y=3.0/4*x*x*x*x*x+12*x*x*x*x-7.3*x*x*x+2*x-4;primf(”y=%10.41f\n”,y);(2) x取下列值时,分别运行此程序 键盘输入(x=) 分析结果 运行结果 程序
2、的错误之处
1
23
789
0. l*10-2
123.456
2.任意输入一个三位整数,然后逆序输出。如输入为123,则输出为321 °编程,注意要有输入数据的提示语句用八进制的形式输出各位数字的ASCII码。
(1) 输入取下列值时,分别运行此程序#include
3、0)/10; 键盘的输入 分析结果 运行结果 程序的错误之处 1 123 2 4567 3 indiv=x% 10; /*indiv=(x-hundred* 1 ()())-ten* 10;*/ inversed=indiv*l 00+ten*l 0+hundred;printfC'thc original number is %d\n”,x); printfC'the inversed number is %d\n",inversed);串并输出统计结果。 (1) 作出流程图 (2) 用值传送的方式实现函数调用,分别从键盘上输入三个
4、不同字符串,并记录下运 行的过程和结果。 #includc "stdio.h" int stall (char cl) { if(c 1 >='a'&&c 1 <=,z'||c 1 >='A'&&c 1 v=Z)return 0; else if(cl=-')return 1; else if(cl>='0'&&clv=9)return 3; elsereturn 4; } main() ( char c; int i,letters=0,space=0,digit=0,others=0; printf("please input some characters。")
5、 while((c=getchar())!=,\n') {switch(statl(c)){case 0: lcttcrs++;brcak;case 1: space++;break;case 2: digit++;break;default: others++;} )printf("stat2:char=%d space=%d digit=%d others=%d\n", letters,space,digit,othcrs); } (3) 用字符数组传送的方式实现函数调用,分别从键盘上输入三个不同字符串,并记 录下运行的过程和结果。 #define N 100 #inclu
6、de "stdio.h" #include "string.h" int letters=0,space=0,digit=0,others=0; void stat2(char p2[NJ) int i; for(i=0;p2[i]!='\0,;i++) (if(p2[i]>='a,&&p2[i]<='z'||p2[i]>=,A'&&p2[i]<=,Z') letters++;else if(p2[i]=='')space++;else if(p2[i]>=,0,&&p2[i]<=,9,) digit++;elseothers++;) ) main() ( char str
7、[N]; printfC'please input some characters。"); gets(str); stat2(str);printf("stat2:char=%d space=%d digit=%d others=%d\n", letters,space,digit,others); } (4) 用指针变量传送的方式实现函数调用,分别从键盘上输入三个不同字符串,并记 录下运行的过程和结果。 #define N 100 #includc "stdio.h" #include "string.h" int letters=0,space=0,digit=0,oth
8、ers=0; void stat3(char *p3) ( for(;*p3!=,\0';p3++) ( if(*p3>='a,&&*p3<='z,||*p3>='A,&&*p3<-Z')letters+4-; else if(*p3=-')space++; else if(*p3>=O&&*p3<=9)digit++; elseothers++; } ) main() char str[N]; printfC'please input some characters。"); gets(str); stat3(str);printf("stat3:char=%d
9、space=%d digit=%d others=%d\n", letters,space,digit,others);
printf(nthe oct number is %0,%0,%0\n",indiv+,0,,ten+'0',hundred+'0'); }
#include
10、)/10-ten)/10)% 10; inversed=indiv* 1 OO+ten* 10+hundred; printf("lhe original number is %d\n",x); printfC'the inversed number is %d\n",inversed); printf("the oct number is %0,%0,%0\n",indiv+'0',ten+,0',hundred+,0'); )实验二选择结构的程序设计 1. 编程计算下面的分段函数。 TWxVO OWxCl 1WxV2 4x-8 y=l 3x2+10x-18x3-3xj+2
11、x~1
(1) 事先编好程序和流程图。要求用if语句结构实现,注意数据类型和输入提示语句。
#include 12、盘上输入-1, -0.5, 0, 0.5, 1, 1.5这六个数,记录下运行结果并核对结果的正确性。
键盘的输 入
分析结果
运行结果
调试说明
键盘输入T
键盘输入-0. 5
键盘输入0
键盘输入0.5
键盘输入1
键盘输入1.5
(3)再将程序运行二遍,分别从键盘上输入-3.5和2二个数,记录下运行结果并核对 结果的正确性。
键盘的输入
观察到的运行结果
程序的错误之处和调试 说明
键盘输入-3. 5
键盘输入2
④修改程序,使之能正确处理任何数据。当输入数据大 13、于等于2或小于-1时,通知用户 "Data Error”,程序结束。
键盘的输入
运行结果
程序的错误之处和调试 说明
键盘输入T. 5
键盘输入T
键盘输入0
键盘输入1
键盘输入2
键盘输入2. 5
4.水果店有苹果,一级5. 50元/kg,二级4. 20元/kg,三级3. 00元/kg,四级2. 50/kg, 输入苹果的等级、数最以及顾客的付款数,显示出苹果的等级、数昂:、应付款数以及找给顾 客的钱数。
(1)事先编好程序,分别用if语句和用switch语句结构实现,要求程序能正确处理 任何数据。当输入苹果的等级、 14、数量以及顾客的付款数不合要求时,通知用户"Data Error”, 程序结束。
注意苹果等级、数量以及顾客付款数数据的类型和输入数据的提示语句。
(2)将程序运行六遍,分别从键盘上输入六组数,记录运行步骤、过程和结果,并核 对结果的正确性。
键盘的输入
预期结果
运行结果
调试说明
输入 1,3. 5, 20
输入 2, 2. 0, 10
输入 3, 3. 5, 11
输入 4, 2. 5, 6. 25
输入5, 3, 7
输入 2, 3. 5,-10
#include 15、n()
float weight,money,paid;
int rank;
printf ("input rank, weight, money: \n");
scanf ("%d", &rank);
printf ("inpul weight:\n");
scanf (u%f,&weight);
printf ("input moneyAn");
scanf ("%f',&money);
switch (rank)
(
case 1: paid=5.5*weight; break;
case 2: paid=4.2*weight; break;
case 3: p 16、aid=3.0*weight; break;
case 4: paid=2.5*weight; break;
default: paid=();
}if (weight<=()||money<=0||paid<=0||(money-paid)<0) printfC'DATA ERROR!'1);
else
(printf ("the rank is %d!"» rank);printf ("the weight is % 1().If!", weight);printf ("paid %10.2f yuan", paid);printf ("the change is %10.2f 17、yuan", money-paid);实验三循环结构的程序设计
1. 编程序计算 1! +2! +3! +4! +5!
(1)分别用单层循环和双层循环编写此程序,作出流程图。
单层循环
#include 18、n",sum);
}
双层循环:
#include 19、
(3) 若计算£小 运行程序观察程序结果是否正确,若有错误如何修改。
r=l
不正确,数据越界,将int sum=0, p=l:改为long sum=0, p=l;
printf ("The result is %d:\n", sum);改为 printf ("The result is %ld:\nw, sum);20
(4) 若计算£,!,运行程序观察程序结果是否正确,若有错误如何修改。
*=i
不正确,数据越界,将 int sum=0, p=l;改为 unsigned long sum=0, p=l: printf CThe result is %d:\n*, sum); 20、改为 printf ("The result is %ld:\n", sum);
2. 编写程序并上机调试
电视大奖赛有20名评委给选手评分。评分规则是:去掉1个最高分,去掉1个最低分, 其余18名评委的平均分选手的得分。设计评分程序。
#include 21、 22、己选择)
#define N 8
#include "sldio.h"
main()
(
int count[N+2],i,j,n;
printf("input %d numbers:", N);
fbr(i=l;i<=N;i++)scanf("%d",&count[i]);
printf(侦');
for(i=l;ivN;i++)
for(j=i+l;jv=N;j++)
if(count[j]>count[i])
{n=count[ij;count[i]=count[j];count|j]=n;
)
printf("the sorted numbers:。"); 23、for(i=l;i<=N;i++)printf("%d ",count[i]);
)
#define N 8
#include "stdio.h"
main()
(
int counl[N+2],i,j,n;
printf("input %d numbcrs:\n'\ N); for(i=l;i<=N;i++)scanf("%d",&count[i]);
printf(侦'); for(i=l;i 24、fj+l ]=n;}
printf("the sorted numbers:\nM); for(i=l;i<=N;i++)printf("%d ",count[i]);
}
(3) 用指针的方法编写程序(排序算法可自己选择)
#define N 8
#include "stdio.h"
main()
(
int count[N+2],i,j,n,*p;
p=count;
printf("input %d numbers:\nn, N);
for(i=l;i<=N;i++)scanf(”%d”,p+i);
printf(u\nM);
for(i=l;ivN;i++)
25、for(j=i+l ;j<=N;j++)if(*(p+j)>*(p+i))(n=*(p+i);*(p+i)=*(p+j);*(p+j)=n;I
prinlf("the sorted numbers:");
for(i=l;i<=N;i++)printf(H%d ”,*(p+i));
}
#defineN 8
#include "stdio.h"
main()
{
int count[N+2],i,j,n,*p;
p=count;
printf("input %d numbers:\n");
for(i=l;i<=N;i++)scanf(”%d”,p+i);
printf 26、侦');
for(i=l;i 27、de "stdio.h"
#include "siring.h"
main()
(
char strl[NUM],str2[NUM],str3[NUM]=;
int ijen;
printfC'input string l:\n");
gets(strl);
printf(”input string 2:\n”);
gets(str2);
strcat(strl,slr2);
printf("the concatenation strings:%s\n",strl);
len=strlen(strl);
printfC'the concatenation string 28、s's len:%d\n",len);
for(i=0;i 29、g l:\n");
gets(pl);
printf("input string 2:\n");
gets(p2);
strcat(pl,p2);
printf(Hthe concatenation strings:%s\n'\pl);
len=strlen(pl);
printf(Hthe concatenation strings's len:%d\n",len);
for(i=0;i






