其他分享
首页 > 其他分享> > 【C语言】北邮国际学院大一下平时作业

【C语言】北邮国际学院大一下平时作业

作者:互联网

这里写一下平时作业题一些解答思路

这是老师发的一些题,自取

链接:https://pan.baidu.com/s/1G_BuO8v4Juk4Y34_y-HWqQ 
提取码:ouh3 

题目要求

Exercise 1: Programming Environment and Basic Input/Output

  1. Write a program that prints “This is my first program!” on the screen.
    1. Save this program onto your own disk with the name of e2-1a;
    2. Run this program without opening Turbo C;
    3. Modify this program to print “This is my second program!”, then save it as e2-1b. Please do not overwrite the first program.
#include <stdlib.h> 
#include <stdio.h>
int main()
{
	printf("This is my first program!");
	
	system("pause");
	return 0;
}

#include <stdio.h>
int main()
{
	printf("This is my second program!");
	
	return 0;
}
  1. Write a program that prints the number 1 to 4 on the same line. Write the program using the following methods:
    1. Using four “printf” statements.
    2. Using one “printf” statement with no conversion specifier (i.e. no ‘%’).
    3. Using one “printf” statement with four conversion specifiers
#include <stdio.h>
int main()
{
	//a
	printf("1");
	printf("2");
	printf("3");
	printf("4\n");
	
	//b
	printf("1234\n");
	
	//c
	printf("%d%d%d%d\n",1,2,3,4);
	return 0;
}

3.(a) Write a program that calculates and displays the number of minutes in 15 days.

   (b) Write a program that calculates and displays how many hours 180 minutes equal to.

   (c) (Optional) How about 174 minutes?

#include <stdio.h>
int main()
{
	//a
	printf("the number of minutes in 15 days is %d\n",15*24*60);
	
	//b
	printf("180 minutes equal to %d hours.\n",180/60);
	
	//c
	printf("174 minutes equal to %3.1f hours.\n",174.0/60.0);
	
	return 0;
}

Exercise 2: Data Types and Arithmetic Operations

1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write and execute a C program that calculates and displays the total purchase price (net price + sales tax).

#include <stdio.h>

int main()
{
	float price , tax;
	price=889;
	tax=price *0.06;
	printf("The total purchase price is $%5.2f\n",price + tax);
	
	return 0;
}

2.Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the value 3.14159 for “p”.

#include <stdio.h>
int main()
{
	float R , D , C , S ;
	
	printf("Please type in the radius:");
	scanf("%f",&R);
	D=R*2;
	C=3.14159 * D;
	S=3.14159 * R * R;
	
	printf("The circle's diameter is %f\n",D);
	printf("The circle's circumference is %f\n",C);
	printf("The circle's area is %f\n",S);
	return 0;
}

3.Write a program that reads in two numbers: an account balance and an annual interest rate expressed as a percentage. Your program should then display the new balance after a year. There are no deposits or withdraws – just the interest payment. Your program should be able to reproduce the following sample run:

Interest calculation program.

Starting balance? 6000

Annual interest rate percentage? 4.25

Balance after one year: 6255.00

#include <stdio.h>
int main()
{
	float bal1 , p , bal2;
	printf("Starting balance? ");
	scanf("%f",&bal1);
	printf("Annual interest rate percentage? ");
	scanf("%f",&p);
	bal2=bal1 + bal1 * p / 100;
	printf("Balance after one year: %7.2f \n",bal2);
	
	return 0;
}

Exercise 3:   Selection structure

Write a C program that accepts a student’s numerical grade, converts the numerical grade to Passed (grade is between 60-100), Failed (grade is between 0-59), or Error (grade is less than 0 or greater than 100).

#include <stdio.h>
int main()
{
	int gra;
	printf("please type in the student's numerical grade: ");
	scanf("%d",&gra);
	if(gra >= 60 && gra <= 100)
		printf("Passed\n");
	
	else if(gra >=0 && gra<=59)
			printf("Failed\n");
		else
			printf("Error\n");

	return 0;
}

Write a program that asks the user to enter an integer number, then tells the user whether it is an odd or even number.

#include <stdio.h>
int main()
{
	int num1 , num2;
	printf("Please type in an integer number: ");
	scanf("%d", & num1);
	num2 = num1 % 2;
	switch(num2)
	{
		case 0:
			printf("The integer number is an even number.\n");
			break;
		case 1:
			printf("The integer number is an odd number.\n");
			break;
	}
	return 0;
}

Write a program that reads in three integers and then determines and prints the largest in the group.

#include <stdio.h>
int main()
{
	int a , b , c;
	printf("please type in three number: ");
	scanf("%d%d%d", &a , &b , &c);
	if(a >= b)
		b = a;
	if(b >= c)
		c = b;
	printf("The largest number in the group is %d.\n",c);

	return 0;
}

Write a program that reads three integers an abbreviated date (for example: 26  12  94) and that will print the date in full; for example: 26th December 1994. The day should be followed by an appropriate suffix, ‘st’, ‘nd’, ‘rd’ or ‘th’. Use at least one switch statement.

#include <stdio.h>
int main()
{
	int a , date , month , year;
	char a1 , a2 ;
	char *m[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
	

	printf("please type in three integers an abbreviated date: ");
	scanf("%d%d%d", &date , &month , &year);
 	a = date%10;
	if(a!= 1 && a!= 2 && a!= 3)
 		a1 = 't' , a2 ='h';
 		else
		switch(a)
		{
			case 1:
			a1 = 's' , a2 ='t';
			break;
		
			case 2:
			a1 = 'n' , a2 ='d';
			break;
		
			case 3:
			a1 = 'r' , a2 ='d';
			break;
			
		}	
	
	printf("The date is %d%c%c %s 19%d\n",date,a1,a2,m[month-1],year);

	return 0;
}

Exercise 4:  Simple “while” repetition statement

1.Write a C program that uses a while loop to calculate and print the sum of the even integers from 2 to 30.

#include <stdio.h>
int main()
{
	int num , total;
	num = 2;
	total = 0;
	while(num <= 30)	
	{
		total = total + num ;
		num += 2 ;
	} 
	printf(" the sum of the even integers from 2 to 30 is %d .",total);

	return 0;
}

2. A large chemical company pays its sales staff on a commission basis. They receive £200 per week plus 9% of their gross sales for that week. For example, someone who sells £5000 of chemicals in one week will earn £200 plus 9% of £5000, a total of £650. Develop a C program that will input each salesperson’s sales for the previous week, and print out their salary. Process one person’s figures at a time.

  Enter sales in pounds (-1 to end): 5000.00

  Salary is: 650.00

  Enter sales in pounds (-1 to end): 00.00

  Salary is: 200.00

  Enter sales in pounds (-1 to end): 1088.89

  Salary is: 298.00

  Enter sales in pounds (-1 to end): -1

#include <stdio.h>
#define CUTOFF -1
int main()
{
	float sales,salary;
	sales = 0;
    while (sales != CUTOFF)
	{
		printf("Enter sales in pounds (-1 to end):");
		scanf("%f", &sales);
		salary = 200 + sales * 0.09	;
		printf("Salary is %6.2f\n\n",salary);
	}
	return 0;
}

Optional:

3.    A mail order company sells five different products whose retail prices are shown in the following table:

       Product Number                Retail Price (in pounds)

                                  1                2.98

                                  2                4.50

                                  3                9.98

                                  4                4.49

                                  5                6.87

       Write a C program that reads in a series of pairs of numbers as follows:

        (1). Product number

        (2). Quantity sold for one day

       Your program should use a switch statement to help determine the retail price for each product, and should use a sentinel-controlled loop to calculate the total retail value of all products sold in a given week (7days).

#include<stdio.h>

int main()
{
	int day, number , quantity;
	float total;
	day = 1 ;
	
	while(day<=7)
	{
	printf("Please type in the product number and the quantity sold for day %d  .",day);
	scanf("%d%d",&number,&quantity);
	day = day + 1;
	switch(number)
	{
	case 1:
         total = total + 2.98 * quantity;
         break;
	case 2:
         total = total + 4.50 * quantity;
         break;
	case 3:
         total = total + 9.98 * quantity;
         break;
	case 4:
         total = total + 4.49 * quantity;
         break;
	case 5:
         total = total + 6.87 * quantity;
         break;
    default:
         printf("Error.\n");
	}
	
	}
	printf("The total retail value of all products sold in this week is %6.2f \n",total);
	
	return 0;
}

Exercise 5:  ‘for’ and ‘do … while” repetition statements

1.   Write a program which uses a do/while loop to print out the first 10 powers of 2 other than 0 (ie. it prints out the values of 21, 22, ..., 210). Use a for loop to do the same.

#include <stdio.h>
#include <math.h>  

// uses a do/while loop
int main()
{
	int a = 1 , b;
	do{
		b = pow(2 , a);
		a++;
		printf("%d ",b);
	}while(a <= 10);
	printf("\n");
	return 0;
}

#include <stdio.h>
#include <math.h>  

// uses a for loop
int main()
{
	int a , b;
	
	for(a = 0;a <= 10;a++)
	{
		b = pow(2 , a);
		printf("%d ",b);
	}	
	printf("\n");
	return 0;
}

2.    The constant p can be calculated by the infinite series:

                          p = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +....

         Write a C program that uses a do/while loop to calculate p using the series. The program should ask the user how many terms in the series should be used. Thus if the user enters ‘3’, then the program should calculate p as being 4 - 4/3 + 4/5.

#include <stdio.h>
#include <math.h>
int main()
{
	int c;
	float a = 1 ,  b , sum = 0;
	printf("please type in the number of terms in the series should be used:");
	scanf("%d",&c);
	do
	{
		b = pow(-1,(a - 1)/2);
		sum = sum + 4 * b / a;
		a += 2;
	}
	while(a < 2*c);
	printf("The value of πis %f", sum);
	return 0;
}

Nested repetition

3. Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.

*

***

*****

*******

*********

*******

*****

***

*

#include <stdio.h>
int main()
{
	int n = 1, b , i = 1;
	while(n<= 5)
	{
			b = (9-(2*n-1))/2;
			for(i = 1;i <= b;)
			{
			printf(" ");
			i++;
			}
			for(i = 1;i <= 2*n-1;i++)
			printf("*");
			for(i = 1;i <= b;)
			{
			printf(" ");
			i++;
			}
			printf("\n");
			n++;
	}
		n=10-n;
		while(n<= 5 && n >= 0)
		{
			b = (9-(2*n-1))/2;
			for(i = 1;i <= b;)
			{
			printf(" ");
			i++;
			}
			for(i = 1;i <= 2*n-1;i++)
			printf("*");
			for(i = 1;i <= b;)
			{
			printf(" ");
			i++;
			}
			printf("\n");
			n--;
		}
	return 0;
}

4. Write a program to print a table as follows:

1*1= 1

2*1= 2  2*2= 4

3*1= 3  3*2= 6  3*3= 9

….

9*1= 9  9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72 9*9=81

#include <stdio.h>
int main()
{
	int n,i;
	for(n = 1;n<= 9;n++)
	{
		for(i = 1;i <= n;i++)
		printf("%d*%d=%-3d",n,i,n*i);
		printf("\n");
	}
	return 0;
}

Exercise 6:  Simple Functions

  1. 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.
#include <stdio.h>
int main()
{
	int round_to_nearest(float);
	float a;
	printf("Please type in a number:");
	while(scanf("%f",&a) != EOF)
	{
		
		printf("%f %d\n",a,round_to_nearest(a));
		printf("Please type in a number:");
	}
	return 0;
}
int round_to_nearest(float x)
{
	int integer;
	if(x - (int)x < 0.5)
		integer = (int)x;
	else
		integer = (int)x + 1;
	return integer;	
}
  1. 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.
#include <stdio.h>
int main()
{
	float larger(float , float);
	float a1 , a2 , b1 , b2 , c1 , c2 , sum;
	printf("Please type in three pairs of numbers\n");
 	printf("The first pair of number is :");
 	scanf("%f %f", & a1 ,& a2);
	
 	printf("The second pair of number is :");
 	scanf("%f %f", & b1 ,& b2);
 	
 	printf("The third pair of number is :");
 	scanf("%f %f", & c1 ,& c2);
 
 	sum = larger(a1 , a2) + larger(b1 , b2) + larger(c1 , c2);
 	printf("The sum is %f.\n",sum);
	return 0;
}
float larger(float x , float y)
{
	float lar;
	if(x > y)
		lar = x;
	else
 		lar = y;
	return lar;
}

3.    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.50              

       The program should use the function calculate_charges to determine the charge for each customer.

#include <stdio.h>
int main()
{
	float calculate_charges(float);
	float h1 , h2 , h3 , total1 =1, total2 , total3;
	h1 = 1.5 , h2 = 4.0 , h3 = 24.0; 
	total1 = calculate_charges(h1);
	total2 = calculate_charges(h2);
	total3 = calculate_charges(h3);
	printf(" Car    Hours    Charge\n");
	printf("  1     %.1f%11.2f\n",h1,total1);
	printf("  2     %.1f%11.2f\n",h2,total2);
	printf("  3     %.1f%11.2f\n",h3,total3);
	printf("TOTAL   %.1f%11.2f\n",h1+h2+h3,total1+total2+total3);
	return 0;
}
float calculate_charges(float x)
{
	float total;
	if(x <= 3)
		total = 2;
	else if(x > 3 && x < 19)
		total = (x - 3) * 0.5 + 2;
		else
		total = 10;
	return total;
}

Exercise 7:  More Functions

1.     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.

#include <stdio.h>
int main()
{
	int even(int);
	int num;
	printf("Please type in an integer :");
	while(scanf("%d" , & num) != EOF)
	{
		if(even(num) == 1)
		printf("It's an even number.\n\n");
		else
		printf("It's not an even number.\n\n");
		printf("Please type in an integer :");
	}
	
	return 0;
}
int even(int x)
{
	int a;
	a = (x + 1) % 2;
	return a;
}	

2.    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.

#include <stdio.h>
int main()
{
	int integerPower1(int , int);
	int a , b;
	printf("please type in a base and an exponent:");
	scanf("%d%d",& a , & b);
	printf("The value is %d", integerPower1(a , b));
	return 0;
}
int integerPower1(int x , int y)
{
	int i , result = 1;
	for(i = 1;i <= y;i++)
		result = result * x;
	return result;
}

3.     Write a C program that uses the recursive function integerPower2(base, exponent) to return the value of:

                                                            base^exponent

         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:

                                                            base^exponent = base . base^(exponent - 1)

         and the base case will be when exponent is 1 since : base1 = base.)

#include <stdio.h>
int main()
{
	int integerPower1(int , int);
	int a , b;
	printf("please type in a base and an exponent:");
	scanf("%d%d",& a , & b);
	printf("The value is %d", integerPower1(a , b));
	return 0;
}
int integerPower1(int x , int y)
{
	int i , result = 1;
	if(y == 1)
	return x;
	else
	return(x * integerPower1(x , y-1));
	return result;
}

Exercise 8:  Arrays

  1. Write a program that reads ten numbers supplied by the user into a single subscripted array, and then prints out the average of them.
#include <stdio.h>
int main()
{
	int i , sum = 0 , ave;
	int num[10];
	printf("You need type in 10 number.\n");
	for(i = 0;i < 10; i++) 
	{
		printf("Please type in a number:");
		scanf("%d", &num[i]);
	}
	for(i = 0;i < 10; i++) 
		sum = sum + num[i];
	ave = sum / 10 ;
	printf("The average of them is %d.\n", ave);
	return 0;
}

2.    Write a program that reads ten numbers supplied by the user into a 2 by 5 array, and then prints out the maximum and minimum values held in:

       (a) each row (2 rows)

       (b) the whole array

#include <stdio.h>
int main()
{
	int i , j , max1 , max2 , min1 , min2 , max , min ;
	int num[2][5];
	for(i = 0 ; i < 2 ; i++)
	{
		for(j = 0 ; j < 5 ; j++)
		{
			printf("Please type in a number:");
			scanf("%d", &num[i][j]);
		}
	}
	max1 = num[0][0], max2 = num[1][0], min1 = num[0][0], min2 = num[1][0], max = num[0][0], min = num[0][0];
	for(j = 0 ; j < 5 ; j++)
	{
		if(max1 >= num[0][j])
			max1 = max1;
		else
			max1 = num[0][j];
			
		if(min1 <= num[0][j])
			min1 = min1;
		else
			min1 = num[0][j];
	}
	for(j = 0 ; j < 5 ; j++)
	{
		if(max2 >= num[1][j])
			max2 = max2;
		else
			max2 = num[1][j];
			
		if(min2 <= num[1][j])
			min2 = min2;
		else
			min2 = num[1][j];
	}
	for(i = 0 ; i < 2 ; i++)
	{
		for(j = 0 ; j < 5 ; j++)
		{
			if(max >= num[i][j])
				max = max;
			else
				max = num[i][j];
		}
	}
	printf("\nIn the first row , the maxinum is %d , the mininum is %d.\n\n", max1 , min1);
	printf("In the second row , the maxinum is %d , the mininum is %d.\n\n", max2 , min2);
	printf("In the total number , the maxinum is %d , the mininum is %d.\n", max , min);
	return 0;
}

3. Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. 

#include <stdio.h>
int main()
{
	int i , j , a = 0 , b = 0;
	int num[20];
	printf("You need type in 20 numbers,and please type an interger between 10 and 100 inclusively.\n");
	for(i = 0;i < 20; i++) 
	{
		printf("Please type in a number:");
		scanf("%d", &num[i]);
	}
		printf("The output number: ");
	while(a < 20)
	{
		b = 0;
		for(j = 0;j <20;j++)
		{
			if(num[a] == num[j])
				b++;
		}	
			if(b == 1)
				printf("%d ",num[a]);

		a++;
	}
	printf(".\n");
	return 0;
}

Exercise 9:  More Arrays

1. Write a program that enters 5 names of towns and their respective distance (an integer) from London in miles. The program will print of the names of the towns that are less than 100 miles from London. Use arrays and character strings to implement your program. 

#include <stdio.h>
int main()
{
	int i;
	int dis[5];
	char town[5][81];
	for(i = 0; i < 5 ;i++)
	{
		printf("\nPlease type in a town: ");
		scanf("%s",&town[i]);
		printf("Please type in the distance to London:");
		scanf("%d",&dis[i]);
	}	
	printf("\nThe city whose distance to London less than 100 miles is: \n");
	for(i = 0; i < 5 ;i++)
	{
		if(dis[i] < 100)
		//printf("%s\n",town[i]);
		puts(town[i]);
	}
	
	
	return 0;
}

2.    Write a program that prompts the user to type in four character strings, places these in an array of strings, and then prints out: (e.g. I am Peter Pan)

       (i) The four strings in reverse order. (e.g. Pan Peter am I)

       (ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)

       (iii) The four strings in reverse order with each string backwards. (e.g. naP reteP ma I)

#include <stdio.h>
int main()
{
	int i , j;
	char str[4][81];
	printf("Please type in four character strings:\n");
	for(i = 0 ; i < 4 ; i++)
		scanf("%s",&str[i]);
	printf("The four strings in reverse order:");
	//(i) The four strings in reverse order. 
	for(i = 3 ; i >= 0 ; i--)
		printf("%s ",str[i]);
	//(ii) The four strings in the original order, but with each string backwards. 
	printf("\nThe four strings in the original order, but with each string backwards:");
	for(i = 0 ; i < 4 ; i++)
	{
	
	for(j = 0 ; str[i][j]!='\0' ; j++);
	for(j = j; j >= 0 ; j--)
	    printf("%c",str[i][j]);
	printf(" ");	
	}
	//(iii) The four strings in reverse order with each string backwards. 
	printf("\nThe four strings in reverse order with each string backwards:");
	for(i = 3 ; i >= 0 ; i--)
	{
	
	for(j = 0 ; str[i][j]!='\0' ; j++);
	for(j = j; j >= 0 ; j--)
	    printf("%c",str[i][j]);
	printf(" ");	
	}
	printf("\n");
	return 0;
}

Exercise 10:  Pointers

1.    Write a program that reads 5 integers into an array, and then uses four different methods of accessing the members of an array to print them out in reverse order.

#include <stdio.h>
int main()
{
	FILE *outfile;
	char name[5][81];
	float year[5] , month[5];
	int i;
	printf("Please type in 5 employees and their annual salary:\n");
	for(i = 0 ; i < 5 ; i++)
	{
		printf("%d.The people name:",i + 1);
		scanf("%s",&name[i]);
		printf("the annual salary of this people:");
		scanf("%f",&year[i]);
	}
	for(i = 0 ; i < 5 ; i++)
		month[i] = year[i] * 0.8/12;
	outfile = fopen("file1.txt","w");
	fprintf(outfile,"No. Employee's Name   Annual salary  Monthly payment after tax\n");
	for(i = 0 ; i < 5; i++)
	{
		fprintf(outfile," %d              ",i+1);
		fputs(name[i],outfile);
		fprintf(outfile,"%19.1f",year[i]);
		fprintf(outfile,"%30.1f",month[i]);
		fprintf(outfile,"\n");
	}
	return 0;
}

2.   Write a program that reads 8 floats into an array and then prints out the second, fourth, sixth and eighth members of the array, and the sum of the first, third, fifth and seventh, using pointers to access the members of the array.

#include <stdio.h>
int main()
{
	int i;
	int dis[3];
	char town[3][81];
	FILE *outfile;
	for(i = 0; i < 3 ;i++)
	{
		printf("Please type in a town: ");
		scanf("%s",&town[i]);
		printf("Please type in the distance to London:");
		scanf("%d",&dis[i]);
	}	
	outfile = fopen("file2.txt","w");
	for(i = 0; i < 3 ;i++)
	{	
		
		fprintf(outfile,"%s %d \n",town[i],dis[i]);
	}	
	return 0;
}

3.   Write a program that use a SINGLE FUNCTION (用一个函数)to find and return simultaneously both the lowest and highest values in an array of type int. Suppose the size of the array is 6.

#include <stdio.h>
int main()
{
	int i;
	int dis[3];
	char town[3][20];
	FILE *infile , *outfile;
	outfile = fopen("file3.txt","w");
	infile = fopen("file2.txt","r");
	 for( i = 0; i < 3; i++) 
		{
  		fscanf(infile, "%s %d \n",  &town[i], &dis[i]);
		if(dis[i] < 100) 
    		fprintf(outfile, "%s %d \n", town[i], dis[i]);
		}
	return 0;
}

Exercise 11:  Files

1.   Write a program that enters the name and the annual salary of 5 employees from the keyboard, calculates the monthly salary of each employee assuming that tax is deducted at a flat rate of 20%. The program prints the name, the annual salary and the monthly payment (net) of each employee in a file. The program adds an employee’s number to each employee before printing the names and values:

     No.   Employee’s Name           Annual salary     Monthly payment after tax

#include <stdio.h>
int main()
{
	int num[5], *addnum;
	int i;
	addnum = num;
	printf("Please type in 5 integers:");
	for(i = 0 ; i < 5 ; i++)
		scanf("%d",&num[i]);
	printf("The reverse order of the integers is:");
	//1
	printf("\n(method 1)\n");
	for(i = 4 ; i >= 0 ; i--)
		printf("%d ",num[i]);
	//2
	printf("\n(method 2)\n");
	for(i = 4 ; i >= 0 ; i--)
		printf("%d ",addnum[i]);
	//3
	printf("\n(method 3)\n");
	for(i = 4 ; i >= 0 ; i--)
		printf("%d ",*(num+i));
	//4
	printf("\n(method 4)\n");
	for(i = 4 ; i >= 0 ; i--)
		printf("%d ",*(addnum+i));	
	printf("\n");
	return 0;
}

2. Write a program that builds a file with information for 20 towns. Each line in the file contains name of a town followed by the distance of the town from London in miles (an integer). 

#include <stdio.h>
int main()
{
	int i;
	float num[8] , sum = 0;
	float *addr = num;
	printf("Please type in 8 floats:\n");
	for(i = 0 ; i < 8 ; i++)
		scanf("%f",&num[i]);
	printf("\nThe second, fourth, sixth and eighth members are:");
	for(i = 1 ; i < 8 ; i = i+2)
		printf("%f ",*(addr+i));
	printf("\nThe sum of the first, third, fifth and seventh mumbers are:");
	for(i = 0 ; i < 8 ; i = i+2)
		sum += *(addr+i);
	printf("%f ",sum);
	return 0;
}

3. Write a program reads the file created by the above program (in Question 2) and prints the names of the towns that are less than 100 miles from London to ANOTHER file. 

#include <stdio.h>
int main()
{
	void SINGLEFUNCTION(int *,int *);
	int num[6] , *addr = num;
	int i;
	printf("Please type in 6 numbers:");
	for(i = 0 ; i < 6 ; i++)
		scanf("%d",&num[i]);
	SINGLEFUNCTION(num , num);
	return 0;
}
void SINGLEFUNCTION(int *max,int *min)
{
	int maxnum , minnum , j;
	maxnum=*max;
	minnum=*min;
	for(j = 0 ; j < 6 ; j++)
	{
		maxnum=maxnum >= *(max+j) ? maxnum : *(max+j);
		minnum=minnum <= *(min+j) ? minnum : *(min+j);
	}
	printf("The max is %d , and the min is %d.\n",maxnum,minnum);
}

标签:return,int,北邮,作业,num,C语言,program,printf,include
来源: https://blog.csdn.net/qq_59362610/article/details/122446137