1 #include<string.h>
2 #include<stdio.h>
3 #include<stdio.h>
4 void r(int a, int b, int c)
5 {
6 if (a + b > c && a + c > b && b + c > a) {
7 if (a == b && a == c) {
8 printf("等边三角形\n");
9 } else if (a == b || a == c || b == c) {
10 printf("等腰三角形\n");
11 } else {
12 printf("一般三角形\n");
13 }
14 } else {
15 printf("不能构成三角形\n");
16 }
17 }
18 int main(int argc, const char *argv[])
19 {
20 int a, b, c;
21 printf("请输入三角形三条边:");
22 scanf("%d%d%d", &a, &b, &c);
23 triangleType(a, b, c);
24 return 0;
25 }
作业2:使用有参无返回值函数实现如下应用题
电暖气是一种将电能转化为热能的产品。目前,国内的电采暖方式主要分为发热电缆地板辐射采暖、电热膜采暖和电暖气等。
编程实现输入用电量,计算电费功能.
注:电费采用的是阶段电费:
如果用电量在1-100度之间电费单价是0.35元
如果用电度数为101---200则电费单价是0.46元
超过200度则电费单价是0.68元
例如:如果用电量为120度,则前100 度按第一阶梯收费,剩余20度以第二阶梯价收费。
电费=100*0.35+(120-100)*0.46;
要求主调函数中输入所用电量,被调函数中输出所要缴纳的电费
1 #include<string.h>
2 #include<stdio.h>
3 #include<stdio.h>
4 float fun(float a)
5 {
6 float jg=0.00;
7 if(a>=1 && a<=100)
8 {
9 return a*0.35;
10 }
11 else if(a>=101 && a<=200)
12 {
13 return (a-100)*0.46+100*0.35;
14 }
15 else
16 {
17 return(a-200)*0.68+200*0.64+100*0.35;
18 }
19
20 }
21
22
23 int main(int argc, const char *argv[])
24 {
25 float a = 0.00;
26 printf("请输入您使用的电量:");
27 scanf("%.2f",&a);
28 printf("电费为%.f元",fun(a));
29 return 0;
30 }
作业3:定义有参无返回值函数,在主函数中完成输入字符串,将数组名作为函数参数传递,在被调函数中,将字符串中的数字字符存入新数组,分别统计奇偶个数、和、平均值输出。
4 void fun(int arr[],int n)
5 {
6 int a = 0,b = 0,c = 0;
7 double ac = 0;
8 printf("arr的字符如下:\n");
9 for(int j = 0;j < n; j++)
10 {
11 printf("%d\t",arr[j]);
12 c+=arr[j];
13 }
14 printf("\n");
15 ac=(double)c/n;
16 for(int j = 0;j < n; j++)
17 {
18 if(arr[j] %2 == 0)
19 {
20 b++;
21 }else
22 {
23 a++;
24 }
25 }
26 printf("奇数有%d个,偶数有%d个,和为%d,平均值:%lf\n",a,b,c,ac);
27 }
28
29 int main(int argc, const char *argv[])
30 {
31 int a[10],i = 0;
32 for(i = 0;i < 10; i++)
33 {
34 printf("请输入第%d位字符串:",i+1);
35 scanf("%d", &a[i]);
36 }
作业4∶定义函数实现:
主调函数中定义一个数组,存储6名学生的成绩。
调用自定义的有参无返回值函数完成6名学生的成绩录入。调用自定义的有参无返回值函数完成对成绩的输出。调用有参无返回值函数,完成对成绩的升序排序。
调用自定义的有参有返回值函数,求出总成绩后,在主调函数输出总成绩
1 #include<string.h>
2 #include<stdio.h>
3 #include<stdio.h>
4 void score(int arr[],int n){
5 int s = 0;
6 printf("6名学生的成绩如下:\n");
7 for(int i = 0; i < n; i++){
8 printf("请输入第%d位学生的成绩:", i+1);
9 scanf("%d", &arr[i]);
10 s += arr[i];
11 }
12 printf("\n");
13 int temp = 0;
14 int i,j;
15 for(int i = 1;i < n; i++)
16 {
17 for(j = 0;j < n - i; j++)
18 {
19 if(arr[j] > arr[j + 1])
20 {
21 temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
22 }
23 }
24 }
25
25 printf("排序后的结果如下:\n");
26 for(int i = 0;i < n; i++){
27 printf("%d\t", arr[i]);
28 }
29 printf("\n");
30 }
31 int s(int arr[],int n)
32 {
33 int s = 0;
34 for(int i = 0; i < n; i++)
35 {
36 s = s + arr[i];
37 }
38 return s;
39 }
40 int main(int argc, const char *argv[])
41 {
42 int a[6];
43 score(a,6);
44 int h = s(a,6);
45 printf("总成绩为:%d\n",h);
46 return 0;
47 }