第一篇:C Primer Plus 第七章编程练习参考答案
/* programming exercise 7-1 */ #include
} char ch;int sp_ct, nl_ct, other;sp_ct = nl_ct = other = 0;
while((ch = getchar())!= '#'){
} printf(“%d whitespace, %d newline, %d other.n”,return 0;sp_ct, nl_ct, other);if(ch == ' ')sp_ct++;else if(ch == 'n')nl_ct++;else other++;/* programming exercise 7-2 */ #include
} char ch;int count = 0;
while((ch = getchar())!= '#'){
} printf(“That's all!”);
return 0;if(ch == 'n')continue;count ++;putchar(ch);printf(“/%d ”, ch);if(count % 8 == 0)printf(“n”);/* programming exercise 7-3 */ #include
int num;int e_ct, o_ct;double e_sum, o_sum;e_ct = o_ct = 0;e_sum = o_sum = 0.0;
printf(“Enter the integer:(0 to quit).n”);while(scanf(“%d”, &num)== 1 && num!= 0){
if(num % 2 == 0){
} else { o_ct++;o_sum += num;e_ct++;e_sum += num;
}
} } printf(“Enter next integer:(0 to quit).n”);printf(“%d even entered.”, e_ct);if(e_ct > 0)printf(“ Average is %g.”, e_sum / e_ct);putchar('n');printf(“%d odd entered.”, o_ct);if(o_ct > 0)printf(“ Average is %g.”, o_sum / o_ct);putchar('n');printf(“Done!n”);
return 0;/* programming exercise 7-4 */ #include
int fs_ct = 0;int em_ct = 0;
while((ch = getchar())!= '#'){
} printf(“%d times fs to em, %d times em to double em.n”,fs_ct, em_ct);if(ch == '.'){
} else if(ch == '!'){
} else putchar(ch);putchar(ECM);putchar(ECM);em_ct++;putchar(ECM);fs_ct++;} return 0;/* programming exercise 7-5 */ #include
int num;int e_ct = 0, o_ct = 0;double e_sum = 0.0, o_sum = 0.0;
printf(“Enter the integer:(0 to quit).n”);while(scanf(“%d”, &num)== 1 && num!= 0){
switch(num % 2){
case 0:
e_ct++;e_sum += num;break;case 1: o_ct++;
}
}
} o_sum += num;break;printf(“Enter next integer:(0 to quit).n”);printf(“%d even entered.”, e_ct);if(e_ct > 0)printf(“ Average is %g.”, e_sum / e_ct);putchar('n');printf(“%d odd entered.”, o_ct);if(o_ct > 0)printf(“ Average is %g.”, o_sum / o_ct);putchar('n');printf(“Done!n”);
return 0;/* programming exercise 7-6 */ #include
char ch;char prev = 0;/* 记得对prev进行0填充,否则有可能定义的内存的垃圾数据恰好为e */ int count = 0;
while((ch = getchar())!= '#'){
} switch(count){
case 0: printf(“"ei” no apeared.n“);break;if(ch == 'i' && prev == 'e')count++;prev = ch;case 1: printf(”“ei” apeared 1 time.n“);break;default: printf(”“ei” apeared %d times“, count);
} }
return 0;/* programming exercise 7-7 */ #include
int main(void){
float hours;float total, tax, net;
printf(”Please enter your work time: “);
scanf(”%f“, &hours);if(hours > BASEHRS)total = BASEPAY *(BASEHRS +(hoursLEVEL1)* TAX2;else tax = LEVEL1 * TAX1 + LEVEL2 * TAX2 +(totalLEVEL2)* TAX3;
}
/* programming exercise 7-8 */ #include
else total = hours * basepay;if(total <= T_LEVEL1)tax = total * TAX1;else if(total <=(T_LEVEL1 + T_LEVEL2))tax = T_LEVEL1 * TAX1 +(totalT_LEVEL1tax;printf(”total = %g, tax = %g, net = %g.n“,}
}
total, tax, net);printf(”Please enter next number:n“);printf(”That's all!n“);
return 0;/* programmming exercise 7-9 */ #include
int num;int div;int count;
printf(”Please enter the limit in integer:n“);scanf(”%d“, &num);while(num >1){
}
} for(div = 2, count = 0;div <= num / 2;div++){
} if(count == 0)printf(”%d “, num);if(num % div == 0)count++;num--;printf(”nThat's all!n“);return 0;/* programming exercise 7-10 */ #include
double tax;double income;int type;long level;
printf(”Please choose your type as follow:(q to quit)n“);printf(”1)Single;
2)Householder;n“);printf(”3)Married;
4)Divorcedn“);while(scanf(”%d“, &type)== 1){
switch(type){
case 1: level = LEVEL1;break;case 2: level = LEVEL2;break;case 3: level = LEVEL3;break;
}
}
} case 4: level = LEVEL4;break;default:
printf(”You should choose the number“ ”between 2 and 4(q to quit).n“);printf(”Please enter the right number:n“);continue;printf(”Please enter your income:n“);scanf(”%lf“, &income);if(income <= level)tax = income * RATE1;else tax = level * RATE1 +(incomeDISCOUNT);discount++;if(total_wt == 0)freight = 0;
else if(total_wt <= 5)freight = FR_LEVEL1;else if(total_wt < 20)freight = FR_LEVEL2;else freight = FR_LEVEL3 + total_wt * FR_OVER;total_cost = cost + freight;printf(”Average cost is $%g per pound, tatol weight is %g %s,n“ ,total_cost / total_wt, total_wt, total_wt > 1 ? ”pounds“ : ”pound“);
} printf(”total cost is $%g, and %d%% off, freight is $%g.n",total_cost, discount == 1 ? 5 : 0, freight);
return 0;
第二篇:简单数控编程练习
简单数控编程练习
华中数控简单的编程做练习
螺纹的宏程序
%5
G54 G0 Z50
M03 S1200
#111=#(“#” 为刀尖的实际回转半径)
G0 X0 Y0
Z1.5(Z轴的起刀点定在正1.5是方便螺纹加工,向下加工的深度位置)
G42 G1 X19 Y0 D111 F100
M98 P11 L9(调用子程序9次)
G40 G0 X0 Y0
Z50
M30
%11
G91 G02 I-19 Z-1.5 F100(联动加工铣削螺纹)M99
9圈就到了Z负12
第三篇:数学建模编程练习
数学建模编程练习
1.二分法计算fx = x^3-2*x-5 的零点;
functionT=lingdian()
m=2
n=3
whilem*m*m-2*m-5~=0 && n*n*n-2*n-5~=0t=(m+n)/2
if t*t*t-2*t-5<0
m=t
else n=t
end
end
if m*m*m-2*m-5==0
T=m
else
T=n
end
end
t =
2.0946
2编程求n1
function y=qiuhe(k)
n=1:k;
for y=sum(factorial(n));
end
y
end
y=qiuhe(5)
y =
153
3.设计一个算法求pi, 精确到10位
n!k
4求1到n的素数
functionY=sushu(n)Y=[];
for i=2:n
if factor(i)==i
Y=[Y,i];end
end
Y
> Y=sushu(19)
Y =
2357
13191117
第四篇:c语言编程练习
本实验所有题目均要求使用指针。
1.写一函数,将一个3*3的整型矩阵转置。
2.将两个按升序排列的数组合并成一个数组,并使合并后的数组也按升序排列。要求:(1)输入两个数组(按升序);
(2)将两个数组合并成第三个数组;(有能力的同学可以将第二个数组合并到第一个数组中)
第五篇:VB编程题及答案
1定义一个下标上界为10整型数组,并用随机函数为其赋值(值处于[0,100]之间),计算数组中所有偶数的和。Private Sub Command1_Click()Randomize Dim a%(10)Dim i%, s% For i = 0 To 10 a(i)= 101 * Rnd()Next For i = 0 To 10 If a(i)Mod 2 = 0 Then s = s + a(i)End If Next Print “所有偶数的和为”, s 2 设 S=35,C=0.3,编写程序,计算并输出下面级数的部分和。结果取小数点后面7位。C/1!-C^3/3!+C^5/5!-„ +(-1)^(S-1)*C^(2*S-1)/(2*S-1)!+ „
参考答案如下:
Private Sub Command1_Click()
Dim n As Integer
Dim x As Double
x = 0.3
Dim item As Double
Dim sum As Double
Dim fac As Double
For n = 1 To 35
fac = 1
Dim i As Integer
For i = 1 To 2 * n1)* x ^(2 * n1
If n Mod i = 0 Then
isprime = False
Exit Function
End If
Next
isprime = True End Function Private Sub Command1_Click()
Dim i As Integer
Dim sum As Long
For i = 1000 To 2000
If isprime(i)= True Then
sum = sum + i
End If
Next
Text1.Text = “sum=” & sum End Sub 'sum= 419684 13.编写一个函数,求某个数的阶乘。
调用该函数计算22!。
Public Function fact(ByVal n As Integer)As
Long
Dim i As Integer, f As Long
f = 1
For i = 1 To n
f = f * i
Next i
fact = f End Function Private Sub Command1_Click()
Dim m As Integer, i As Integer, s As Long
m = InputBox(“输入一个正整数:
”)
s = fact(m)
MsgBox(s)End Sub ‘479001600 14.编写子过程,计算并输出3000-4000之间的素数之和C与其中最小的素数MIN。
Private Sub Command1_Click()
Dim sum, i, min As Integer
min=0
For i = 3000 To 4000
Dim isprime As Boolean = True
Dim j As Integer
For j = 2 To i1)* n *(n + 1)
sum = sum + item
n = n + 2
Loop While Abs(sum)<= 100
MsgBox(sum)End Sub 'sum=-68 编写一个函数,其功能为求两个数的最大公约数。
Public Function
gcd(ByVal m As Integer, ByVal n As Integer)As Integer
Dim i As Integer If m > n Then Dim t As Integer t = m: m = n: n = t End If For i = m To 1 Step-1 If m Mod i = 0 And n Mod i = 0 Then gcd = i Exit Function
End If
Next End Function
20.用InputBox输入一个年份,判断该年份是否是平年,若是则输出“Yes”,否则输出“No”。
Dim year As Integer year = Val(InputBox(“"))If(year Mod 400 = 0)Or(year Mod 4 = 0 And year Mod 100<>0)
Then MsgBox(”No“)Else
MsgBox(”Yes“)
End If 输入两个整数m、n,编写程序使得
m>=n,并输出m、n的值。
Dim m, n As Integer
m = Val(InputBox(”“))n = Val(InputBox(”“))
If m < n Then Dim t As Integer t = m : m = n : n = t End If
MsgBox(”m=“ & m & ”;“ & ”n=“ & n)
22求下面级数前n项的和S,在求和
过程中,当某项(注意,该项参与求和)的绝对值小于0.0001 时求和终止。结果取 5 位小数。
1-1/3+1/5-1/7+1/9-1/11+ „ +(-1)^(n-1)*1/(2n-1)+ „
Private Sub Command1_Click()
Dim n As Integer
Dim item As Double
Dim sum As Double
i = Do While True
item =(-1)^(i1)
整数:”)sum = sum + item If Abs(item)< 0.0001 Then Exit Do Else i = i + 1 End If Loop
MsgBox(Format(sum, “0.0000”))
End Sub '
sum=0.78545 编写一个函数,其功能为判断一个3位正整数是否是“水仙花数”。(“水仙花数”是指各位数字的立方和与该数相等,如153=1^3+5^3+3^3,153就是水仙花数。)
Public Function Panduan(ByVal x As Integer)As Boolean
Dim a As Integer, b As Integer, c As Integer
a = x 100
b =(x Mod 100)10
c = x Mod 10
If x = a ^ 3 + b ^ 3 + c ^ 3 Then
Panduan = True
Else
Panduan = False
End If
End Function Private Sub Command1_Click()
Dim i As Integer
i = InputBox(“输入一个3位正
MsgBox(Panduan(i))End Sub 定义一个下标上界为10整型数组,并为其赋值(值处于[0,100]之间),计算数组中所有不小于60的数据的和。
Dim a(10), i, sum As Integer
For i = 0 To 10
a(i)= Int(Rnd()* 101)
Next
For i = 0 To 10
If a(i)〉=60 Then
sum = sum + a(i)
End If
Next
print sum
25.编写程序,求下面级数前n项的和s=1/1!-1/2!+1/3!-1/4!+....+(-1)^(n-1)*1/n!+.....。
'求和过程当某一项的绝对值小于0.0001时结束(注意,该项参与求和),计算并输出所求的s,要求结果保留6位小数。Private Sub Command1_Click()
Dim m As Double, n As Long, s As Double, f As Integer
s = 0: n =-1: m = 1: f = 1 Do While 1 / m >= 0.0001 m = m * f f = f + 1 n =-n
s = s + n / m Loop
s = Format(s, ”0.000000“)Print s End Sub
26输入一个四位数,分别输出它的个位数字,是十位数字,百位数字,千位数字。Private Sub Command1_Click()Dim a%, j%, k%, i%, m% a = InputBox(”请输入一个四位整数“)j = a 1000 k = a 100 Mod 10 i = a 10 Mod 10 m = a Mod 10 Print ”个位数字;“;m Print ”十位数字;“;i Print ”百位数字;“;k Print ”千位数字;“;j End Sub 6.计算sum=2!+4!+6!+...+10!Private Sub Command1_Click()Dim a%, i% Sum = 0 i = 1: a = 1 Do a = 2 * i If i Mod 2 = 0 Then Sum = Sum + a End If i = i + 1 Loop While i <= 10 Print Sum End Sub
27编写程序,计算并输出下面级数前n项(n=20)中奇数项的和。1*2*3-2*3*4+3*4*5-4*5*6+....+(-1)^(n-1)*n*(n+1)*(n+2)+...Private Sub Command1_Click()Dim s!, i% For i = 1 To 20 Step 2 s = s + i *(i + 1)*(i + 2)Next MsgBox(s)End Sub
28随机输出一个小写字母。Private Sub Command1_Click()Dim a As String Randomize
a = Chr(Int(97 + 26 * Rnd))MsgBox(a)End Sub、29编写程序,计算并输出下面级数前n项(n=50)中偶数项的和。s=1*2+2*3+3*4+4*5+....+n*(n+1)+.....Private Sub Command1_Click()Dim s!, i%
For i = 2 To 50 Step 2 s = s + i *(i + 1)Next
MsgBox(s)End Sub
30斐波那契数列是由计算某类动物繁殖增长量而提出的。数列的前两项是1、1,以后的每一项都是其相邻前两项的和。编写程序求:数列前30项和。
Private Sub Command1_Click()Dim a&, b&, c&, i%
a = 1: b = 1: c = 2: Sum = 0 For i = 1 To 28 c = a + b a = b b = c
Sum = Sum + c Next
MsgBox(Sum)End Sub
31输入三个数a、b、c,编写程序使得a<=b<=c,并输出a、b、c的值。Private Sub Command1_Click()Dim a%, b%, c%, t%
a = Val(InputBox(”请输入a的值!“))b = Val(InputBox(”请输入b的值!“))c = Val(InputBox(”请输入c的值!“))If a > b Then t = b b = a a = t End If
If a > c Then t = a a = c c = t End If If b > c Then t = b b = c c = t End If
Print a, b, c End Sub 32编写程序,设n=20,x=3.4,计算并输出S(n)的值,要求结果保留5位小数。S(n)=Inx/x+In2x/x^2+In3x/x^3+.....+In(n*x)/x^n+.....Private Sub Command1_Click()Dim x#, i% Dim S(20)As Double n = 20 x = 3.4 For i = 1 To 20 S(20)= S(20)+ Log(i * x)/(x ^ i)Next Print Format(S(20), ”0.00000“)End Sub
33输入任意三个数 找出其中最大的数 Private Sub Command1_Click()Dim a!, b!, c!, Max!a = Text1.Text b = Text2.Text c = Text3.Text Max = a If b > Max Then Max = b If c > Max Then Max = c Text4.Text = Max End Sub
输出1到100之间的素数 Print 1;2;a = 3 While a <= 100 i = 2
While i < a
If a Mod i = 0 Then
i = a
End If
i = i + 1
Wend
If i = a Then
Print a;
End If a = a + 1 Wend、34打印斐波那契数列20项 Private sub form_click()A=0 B=1
Text1.text=text1.text&str(A)&str(B)&chr(13)& Chr(10)For i=1 to 9 A=A+B B=A+B
Text1.text=text1.text&str(A)&str(B)&chr(13)& Chr(10)Next i
35判定素数
A=val(inputbox(“input a”))For i=2 to a
If(a mod i=0)then Exit for End if Next i