"Fundamentals of programming 2021" topic set 3 problem solution I

Posted by neuroxik on Wed, 08 Dec 2021 12:58:28 +0100

catalogue

5-1

5-2

5-3

5-4

5-5

5-6

5-1

Calculate the piecewise function, and the test data are - 2, 3 and 7 respectively.

 

#include <stdio.h>
#include<math.h>
int main( ){ 
    float x,f;
    int i;
    for(i=0;i<3;i++){
    scanf("%f",&x);
    if(x<0) f=fabs(x+1);
    else if(x<=5) f=2*x+1;
    else f=sin(x)+5;
    printf("x=%.2f,y=%.2f\n",x,f);
    }
    return 0;
}

5-2

The following program counts the number of letters, numbers, spaces and other characters in the input line of characters (the end of the line ends with a newline character, and the last newline character is not counted). For example, if a line of characters entered is aB 3 *, there are 2 letters, 1 number, 1 space and 1 other character.

#include <stdio.h>
int main(){
    int letters,digits,spaces,others;
    letters=0,digits=0,spaces=0,others=0;
    char ch;
    while(ch=getchar(),ch!='\n'){
        if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
            letters++;
        else if(ch>='0'&&ch<='9')
            digits++;
        else if(ch==' ')
            spaces++;
        else
            others++;
    }
    printf("Letters, numbers, spaces and other characters are:%d %d %d %d individual\n",letters,digits,spaces,others);
}

If I were you, my definition and initialization would be carried out together, and there must be a return 0;   

5-3

In order to encourage residents to save water, the water company adopts the method of step pricing according to water consumption. The water fee y (yuan) payable by residents is related to the monthly water consumption x (ton): when X does not exceed 15 tons, y=4x/3; After exceeding, y=2.5x − 10.5. Please write a program to calculate the water charge. It is required to define and call the function f(x) to calculate the water charge. The type of X is double and the type of function is double.

#include<stdio.h>
double f(double x);
int main(){
    double x, y;
    scanf ("%lf",&x); 
	y=f(x);
    printf ("f(%.2f) = %.2f\n",x,y);
    return 0;
}
double f(double x){
    double result;
    if (x<=15){ 
        result=4*x/3;
    }else{
        result=2.5*x-10.5; 
    }
	return result;
}

5-4

Enter the character 'y' or 'y', and the string "Yes." will be output on the screen; If you enter other characters, the string "No." will be output on the screen. It is required to define and call the function YesNo(ch). When ch is' y 'or' y ', it outputs "Yes." and when ch is other characters, it outputs "No.".

 

#include <stdio.h>
void YesNo(char ch); 
int main(void){
    char ch;
    ch=getchar();
	YesNo(ch);
    return 0;
}
void YesNo(char ch){
    switch(ch){
        case 'y': 
        case 'Y':printf("Yes.");return;
        default:printf("No.");return;
    }
}

5-5

Please improve the program and realize the following program functions: palindrome number refers to the number that is the same for both positive reading and negative reading. For example, 12321, 55455 and 35553 are palindromes. Please write a program to read a 5-bit positive integer from the keyboard and judge whether it is a palindrome number. First output the integer, and then output whether it is a palindrome. If it is a palindrome, output "Yes", otherwise output "No", separated by a space.

 

#include <stdio.h>
int main(void){
	int i;for(i=1;i<=2;i++){ //This cycle is used for automatic marking. Please ignore it
		int n,g,s,q,w;
		//n represents the input 5-bit positive integer, and g represents a number
		//s represents ten digits and q represents thousands of digits
		//w represents ten thousand digits
		scanf("%d",&n);
		g=n%10;
		s=n/10%10;
		q=n/1000%10;
		w=n/10000;
		if(g==w&&s==q)  
			printf("%d Yes\n",n);
		else  
			printf("%d No\n",n);
	    } //This cycle is used for automatic marking. Please ignore it
 	return 0;
}

5-6

Please improve the program to realize the following program functions: first input two non-0 double precision real numbers, then input an operator (+, -,, /), then perform corresponding calculation and output the result (keep two decimal places, and finally wrap the line). If the input operators are not these four kinds, the information "operator input error!" will be output.

 

#include <stdio.h>
int main(void){
	int k;for(k=1;k<=5;k++){ //This cycle is used for automatic marking. Please ignore it
	 	double a,b,c; //Variable c is used to store the operation results
	 	char ysf;
	 	scanf("%lf%lf",&a,&b);
	 	ysf=getchar( );//Input operator
	 	if(ysf=='+'){  //The operator entered is+
	   		c=a+b;
	    printf("%.2lf+%.2lf=%.2lf\n",a,b,c);
	   	}
	 	if(ysf=='-'){  //The operator entered is-
	   		c=a-b;
	    	printf("%.2lf-%.2lf=%.2lf\n",a,b,c);
	   	}
		if(ysf=='*'){  //The operator entered is*
	   		c=a*b;
	    	printf("%.2lf*%.2lf=%.2lf\n",a,b,c);
	   	}
		if(ysf=='/'){  //The operator entered is/
	   		c=a/b;
	    	printf("%.2lf/%.2lf=%.2lf\n",a,b,c);
	   	}
		if(ysf!='+'&&ysf!='-'&&ysf!='*'&&ysf!='/')
	  		printf("Operator input error!\n");
  	} //This cycle is used for automatic marking. Please ignore it
 	return 0;
}

Topics: C