C语言作业EX06&EX07(修改一次)
鉴于老师可能没时间讲函数,但是又必须交函数的作业。我把写的代码分享一下,希望能有所帮助。全抄有风险,如果有问题欢迎指正。
这次作业让我明白一个道理:管它写的有多烂,能跑起来,是对的就行。
(CPF群里面有人说:取整的话,正数可以直接加0.5取整,但是负数不行,所以为了严谨,加上负数的取整)
Exercise 6: Simple Functions
Write a C program that reads several numbers and uses the function round_to_nearest to round each of these numbers to the nearest integer. The program should print both the original number and the rounded number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <stdio.h>
int round_to_nearest(float num);
int main()
{
float num;
int result;
while(scanf("%f",&num)!=EOF)
{
result = round_to_nearest(num);
printf("%d\n",result);
}return 0;
}
int round_to_nearest(float num)
{
if (num>=0) //正数取整
return ((int)(num+0.5));
else //负数取整
return ((int)(num-0.5));
}Write a program that reads three pairs of numbers and adds the larger of the first pair, the larger of the second pair and the larger of the third pair. Use a function to return the larger of each pair.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include<stdio.h>
float max(float,float);
int main()
{
int i;
float first,second,result,larger,sum=0;
for(i=0;i<3;i++)
{
scanf("%f %f",&first,&second);
result = max(first,second);
sum+=result;
printf("%f\n",result);
}
printf("%f",sum);
return 0;
}
float max(float first,float second){
float larger;
if(first>=second)
larger = first;
else
larger = second;
return larger;
}A car park charges a £2.00 minimum fee to park for up to 3 hours, and an additional £0.50 for each hour or part hour in excess of three hours. The maximum charge for any given 24-hour period is £10.00. Assume that no car parks for more than 24 hours at a time.
Write a C program that will calculate and print the parking charges for each of 3 customers who parked their car in the car park yesterday. The program should accept as input the number of hours that each customer has parked, and output the results in a neat tabular form, along with the total receipts from the three customers:
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
TOTAL 29.5 14.50The program should use the function calculate_charges to determine the charge for each customer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include<stdio.h>
//这个如果用for的话,我没想到能打印出表格效果的方法。
float fee(float);
int main(){
int i,money,TOTAL;
float fee1,fee2,fee3,time,first=0,second=0,third=0,sum=0,hours=0;
printf("The first car's hours is ");
scanf("%f",&first);
printf("\nThe second car's hours is ");
scanf("%f",&second);
printf("\nThe third car's hours is ");
scanf("%f",&third);
fee1 = fee(first);
fee2 = fee(second);
fee3 = fee(third);
time=first+second+third;
sum=fee1+fee2+fee3;
printf ("\nCar Hours Charge\n");
printf (" 1 %.1lf %.2lf\n ",first,fee1);
printf ("2 %.1lf %.2lf\n ",second,fee2);
printf ("3 %.1lf %.2lf\n ",third,fee3);
printf ("Total %.2lf %.2lf",time,sum);
return 0;
}
float fee(float hours){
float money;
if(hours<=3)
money = 2.00;
else if(hours>=3&&hours<=19)
money = 2 + 0.5*(hours-3);
else
money = 10;
return money;
}这个是用for循环写的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include<stdio.h>
//用了For循环的,但是无法打印出表格的效果
float fee(float);
int main(){
int i,money,TOTAL;
float charge,time,sum=0,hours=0;
printf("Car Hours\n");
for(i=0;i<3;i++)
{
printf("%d\t",i+1);
do{
scanf("%f",&hours);
}while(hours<0&&hours>24);
charge = fee(hours);
time+=hours;
sum+=charge;
printf("Charge %.2f\n",charge);
}
printf("Total %.2lf %.2lf",time,sum);
return 0;
}
float fee(float hours){
float money;
if(hours<=3)
money = 2.00;
else if(hours>=3&&hours<=19)
money = 2 + 0.5*(hours-3);
else
money = 10;
return money;
}
Exercise 7: More Functions
Write a program that uses sentinel-controlled repetition to take an integer as input, and passes it to a function even which uses the modulus operator to determine if the integer is even. The function even should return 1 if the integer is even, and 0 if it is not.
The program should take the value returned by the function even and use it to print out a message announcing whether or not the integer was even.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <stdio.h>
int even(int num);
int main()
{
int a,num;
while(scanf("%d",&num)!=EOF)
{
a = even(num);
if(a==1)
printf("%d is an even\n",num);
else if (a==0)
printf("%d is an odd\n",num);
}
return 0;
}
int even(int num){
if(num%2)
return 0;
else
return 1;
}Write a C program that uses the function integerPower1(base, exponent) to return the value of:
baseexponent
so that, for example, integerPower1(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero integer, and base is an integer. The function should use a for loop, and make no calls to any math library functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include<stdio.h>
int intergerPower1(int base,int exponent);
int main()
{
int base,exponent,result;
while(scanf("%d %d",&base,&exponent)!=EOF)
{
result = intergerPower1(base,exponent);
printf("%d\n",result);
}
return 0;
}
int intergerPower1(int base,int exponent){
int i,result1=1;
for(i=0;i<exponent;i++)
result1*=base;
return result1;
}Write a C program that uses the recursive function integerPower2(base, exponent) to return the value of:
baseexponent
so that, for example, integerPower2(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero integer, and base is an integer. The function should make no calls to any math library functions.
(Hint: the recursive step will use the relationship:baseexponent = base . baseexponent - 1
and the base case will be when exponent is 1 since : base1 = base.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include<stdio.h>
int intergerPower2(int base,int exponent);
int main()
{
int base,exponent,result;
while(scanf("%d %d",&base,&exponent)!=EOF)
{
result = intergerPower2(base,exponent);
printf("%d\n",result);
}
return 0;
}
int intergerPower2(int base,int exponent){
int result2=1;
if(exponent<=0)
return 1;
return base*intergerPower2(base,exponent-1);
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!