Three simple program questions in C language (with notes) (mid-term test questions in a university)

Please program and implement: input an arithmetic expression (any one of addition, subtraction, multiplication and division) from the keyboard at will, and then the program you write should be able to judge whether the arithmetic operation is correct, display "yes" correctly, and display "no" wrongly. For example, if you enter "3 + 2 = 5↙" on the keyboard, the screen will display "yes"; if you enter "3 * 2 = 5↙" on the keyboard, the screen will display "no".

#include<stdio.h>
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int div(int a,int b);

int add(int a,int b)//Define add() to calculate a+b 
{
	return (a + b);
}
int sub(int a,int b)//Define sub() and calculate a-b
{
	return (a - b);
}
int mul(int a,int b)//Define mul() and calculate a*b
{
	return (a * b);
}
int div(int a,int b)//Define div() and calculate a/b
{
	return (a / b);
}
 
int main()
{
	int a,b,c;
	char op;
	printf("please input:");
	scanf("%d%c%d=%d",&a,&op,&b,&c);//Initial value for a,b,c,op 
//If the input result c is equal to the result calculated by a and b, then output yes, otherwise output no 
//Here, the program simply uses if to judge whether the results are equal, but the special case of 2 + 2 = 4 is not handled 
	if(c == add(a,b)||sub(a,b)||mul(a,b)||div(a,b)) 
		printf("yes");
	else 
		printf("no");
	
	return 0;
}

Please program and implement: input any number of students' single subject percentage system scores from the keyboard. When an illegal score is input, the program will no longer receive the input data, count and output the following information:

  1. How many students' single subject scores are recorded;
  2. What are the highest score, the lowest score and the average score respectively;
  3. How many people failed.
    Requirement: do not use array or dynamic memory allocation function.
#include<stdio.h>
int main()
{
	int n,i=1,m=0,sum=0,max=0,min=0,ave=0,f=0;
	printf("Please input the number of the score:");
	scanf("%d",&n);
	printf("Please input the score:");
	scanf("%d",&m);
	sum=max=min=m;
	while(i<n){
		scanf("%d",&m);
		sum=sum+m;		//Use the count variable instead of the array and dynamic memory allocation functions as required 
		i++;
		if(m>max){//Iterative method, so that max is always the maximum value 
			max=m;
		}
		if(m<min){//Iterative method, so that max is always the minimum value
			min=m;
		}
		if(m<60){//Judge whether it fails, and add up the number of people 
			++f;
		}
		ave=sum/n;//Get the average 
	}
	printf("The total,average,maximum and minimum of the scores are:%d %d %d %d \n",n,ave,max,min);
	printf("failed students number is%d",f);
	return 0;
}

Please program and implement: input a positive integer x from the keyboard to determine whether it is isomorphic. If it is isomorphic, the output "Yes" will be displayed on the screen, if not, the output "No". If a number appears to the right of its square, it is an isomorphic number. For example, 5 appears to the right of its square 25, 25 appears to the right of its square 625, so both 5 and 25 are isomorphic.

#include <stdio.h>
int tg(int n)//Define tg function to determine whether the input x is isomorphic 
{
   int flag=10,m;
 
   if(n==flag)
   {
     flag=flag*10;
   }
   
   m=n*n;
   
   if(m%flag==n)
	   return 1;
   else 
	   return 0;
 
}

int main()
{
	int x;
	printf("please input x:");
	scanf("%d",&x);
	if(tg(x)==1) //If it is isomorphic, output yes, otherwise, output no 
	printf("yes");
	else 
	printf("no"); 
 	return 0;
}

Posted by doddatika on Tue, 12 Nov 2019 13:45:58 -0800