Programming job 20191104082520 (branch and jump) to blog

1

Write a program to read the input, stop reading the #character, and report the number of spaces, line breaks, and all other characters read.

#include<stdio.h>
int main(void)
{
    char ch;
    int e=0,n=0;
    while( (ch=getchar())!='#' ){
        if( ch=='e' )
            e=1;
        else{
            if( ch=='i' && e==1 )n++;
            e=0;
        }
    }
    printf( "%d\n", n );
    return 0;
}

2

Write a program to read the input and stop reading the #character.The program prints each character entered and the corresponding ASCII code (decimal).Print 8 characters on one line.Suggestion: Use the character count and modulo operator (%) to print a line break every eight cycles.

#include<stdio.h>
int main(void)
{
    char ch;
    const int LINE = 8; 
    int chsum = 0;
    printf("Enter a sentence: \n");
    while((ch = getchar()) != '#')
    {
        printf("%c: %d\t",ch,ch);  
        chsum++;  
       if(chsum % LINE == 0)  
           printf("\n");  
    }

        printf("\n"); 
    return 0;
}
3

Write a program that reads integers until the user enters 0.When the input is complete, the program should report the number of even numbers (excluding 0) entered by the user, the average of these even numbers, the odd number of inputs, and the average of their odd numbers.

#include<stdio.h>
int main(void)
{
    int n, even_num = 0, odd_num = 0;
    double even_aver = 0.0, odd_aver = 0.0;
    int even_sum = 0, odd_sum = 0;

    printf("Enter integers; ");
    printf("Enter 0 to quit.\n");
    while((scanf("%d", &n) == 1) && (n != 0)){
        if(n % 2 == 0){
            even_num++;
            even_sum += n;
        }
        else{
            odd_num++;
            odd_sum += n;
        }
    }

    printf("even_num: %d", even_num);
    if(even_num > 0){
        even_aver = even_sum/even_num;
        printf(" even_aver: %.2f", even_aver);
    }
    printf("\n");

    printf("odd_num: %d", odd_num);
    if(odd_num > 0){
        odd_aver = odd_sum/odd_num;
        printf(" odd_aver: %.2f", odd_aver);
    }
    printf("\n");
    printf("Done!\n");
    return 0;
}

 

4

Use the if else statement to write a program to read the input and read to #stop.Replace the period with an exclamation point, replace the original exclamation point with two exclamation points, and finally report how many replacements have been made.

#include<stdio.h>
int main()
{
    char ch;
    int i = 0,j = 0;
    while((ch = getchar()) != '#'){
        if(ch == '.'){
            putchar('!');
            i++;
        }
        else if(ch == '!'){
            putchar('!');
            putchar('!');
            j++;
        }
        else
            putchar(ch);
    }

    printf("\nthe times of '.' replaced is: %d\n", i);
    printf("the times of '!' replaced is: %d\n", j);
    return 0;
}

5

Write a program that reads the input, reads #stop, and reports the number of times the ei occurs.
Be careful
The program records the previous and current characters.Test with an input such as "Receive your ei eieio award".

#include <stdio.h>
int main(void)
{
	char ch,sh;
	int i=0;
	ch=getchar();
	while(ch!='#'&&(sh=getchar())!='#'){
		if(ch=='e'&&sh=='i')
		i++;
		ch=sh;
	}
	printf("%d",i);
	return 0;
}

6

Write a program that prompts the user to enter hours of work per week, then prints the total wages, taxes, and net income.Make the following assumptions:
a. Base salary = $1,000 per hour
b. Overtime (over 40 hours) = 1.5 times longer
c. Tax rate: 15% for the first 300 US dollars
Continue $150 for 20%
The remaining 25%
Define symbol constants with #define.It doesn't matter if it complies with the current tax law.

#include <stdio.h>
#define JBGZ 10
#define JB 40
#define JBL 1.5
#define SLZ1 300
#define SLZ2 450
#define SL1 0.15
#define SL2 0.2
#define SL3 0.25
int main(void)
{
    int n, money;
    float sl, jgz;

    printf("Please enter the time you work(# to quit):  ");
    while(scanf("%d", &n) == 1){
        if(n == '#') 
            break;
        if(n <= 40)
            money = n * JBGZ;
        else
            money = ((n - 40) * JBL + 40) * JBGZ;
        if(money <= SLZ1)
            sl = money * SL1;
        else if( money <= SLZ2)
            sl = (money - SLZ1) * SL2 + 300 * SL1;
        else if( money > SL2)
            sl = (money -SLZ2) * SL3 + SLZ1 * SL1 + (SLZ2 -SLZ1) * SL2;
        jgz = money - sl;
        printf("Your total salary is%d,Tax is%.2f,Net wage is%.2f.\n", money, sl, jgz);
        printf("Please enter the time you work: \n");
    }
    return 0;
}

7

Write a program that accepts only positive integer input and displays all prime numbers less than or equal to that number.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
    long long num;
    int x = 1;
    while(x){
        if(scanf("%d", &num) == 1){
            if(num < 1){
                continue;
            }
            else{
                x = 0;
                const int len = num;
                int a[len + 1];
                memset(a, 0, sizeof(int) * (len + 1));
                for(int i = 2; i <= num; i++){
                    if(!a[i]){
                        for(int j = i + i; j <= num; j += i){
                            a[j] = 1;
                        }
                    }
                }
                for(int i = 2; i <= num; i++){
                    if(!a[i]){
                        printf("%d ", i);
                    }
                }
                printf("\n");
            }
        }
    }
 
    return 0;
}

8

The United States Federal Tax Plan of 1988 was the simplest tax plan in modern times.It is divided into four categories, each with two levels.Here is a summary of the tax plan (taxable income in US dollars):

For example, a single taxpayer with a salary of $2000 would pay a tax of $0.15 x 17850+0.28 x (20,000_17850).Write a procedure that allows users to specify the type of tax payable and the amount of tax payable, and then calculate the tax.Programs should allow users to enter multiple times through loops.

#include <stdio.h>
int main(void)
{
    int x;
    float jz[]={0,17850,23900,29750,14785};
    float sr,ss;
    while(scanf("%d%f",&x,&sr)==2){
        if(sr<=jz[x])
            ss = sr * 0.15;
        else
            ss = jz[x]*0.15 + (sr-jz[i])*0.28;
        printf("%f\n",ss);
    }
}

9

ABC grocery stores sell artichokes for $2.05 per pound, beets for $1.15 per pound and carrots for $1.09 per pound.There is a 5% discount on $100 orders before shipping is added.Orders with less than or equal to 5 pounds charge a freight and packing fee of $6.5, orders with 5 to 20 pounds charge a freight and packing fee of $14, and orders with more than 20 pounds add $0.5 per pound renewal from $14.Write a program that uses the switch statement in a loop to achieve different responses when the user enters different letters. That is, the response to enter a lets the user enter pounds of artichokes, b pounds of beets, c pounds of carrots, and q pounds of out of order.The program records the cumulative weight.That is, if the user enters 4 pounds of beet and then 5 pounds of beet, the program should report 9 pounds of beet.The program then calculates the total price of the goods, discounts (if any), freight and packing charges.The program should then display all purchase information: the price of the item, the weight ordered (in pounds), the cost of the vegetables ordered, the total cost of the order, discounts (if any), freight and packaging costs, and the total amount of all costs.

#include <stdio.h>
int main(void)
{
    float yj=2.05,tc=1.15,hlb=1.09;//Unit price for three dishes
    float a = 0.0,b = 0.0,c = 0.0;//Purchase weight of three dishes
    float i;
    char ch;
    float heavy;//Total weight
    float Sabc;//Total Price
    float discount;//Discount
    float sent;//freight
        printf("Please choose the type of dish:\n");
        printf("a.Artichoke 2.05$/pound\n");
        printf("b.Beet 1.15$/pound\n");
        printf("c.Carrot 1.09$/pound\n");
        printf("d Key Cancel\n");
        printf("q Key Exit\n");
    
    while (ch!='q') {
    	scanf("%c",&ch);
        switch (ch) {
            case 'a':
            {printf("Please enter the weight of the artichokes you bought:\n");
                scanf("%f",&i);
                a=a+i;
                break;}
            case 'b':
            {printf("Please enter the weight of the beet you bought:\n");
                scanf("%f",&i);
                b=b+i;
                break;}
            case 'c':
            {printf("Please enter the weight of the carrot you bought:\n");
                scanf("%f",&i);
                c=c+i;
                break;}
            case 'd':
            {
            	printf("Please re-select the type of dish you want to add:\n");
        printf("a.Artichoke 2.05$/pound\n");
        printf("b.Beet 1.15$/pound\n");
        printf("c.Carrot 1.09$/pound\n");
        printf("d Key Cancel\n");
        printf("q Key Exit\n"); 
            	continue;}      	
        }//Weight-bearing
        printf("Continue to choose the type of dish you want to add:\n");
        printf("a.Artichoke 2.05$/pound\n");
        printf("b.Beet 1.15$/pound\n");
        printf("c.Carrot 1.09$/pound\n");
        printf("d Key Cancel\n");
        printf("q Key Exit\n"); 

    }
    
    
    heavy=a+b+c;//Total weight
    Sabc=a*yj+b*tc+c*hlb;//Total Price
    if (Sabc>=100) {
        discount=0.05*Sabc;
    }else discount=0;//Discount
    
    if (heavy<=5) {
        sent=6.5;
    }
    else if (heavy<=20){
        sent=14;
    }
    else{
        sent=14+0.5*(heavy-20);
    }//freight
    
    printf("Buy artichokes%fpound,Unit Price%f$/pound,Price%f$\n",a,yj,a*yj);
    printf("Buy beets%fpound,Unit Price%f$/pound,Price%f$\n",b,tc,b*tc);
    printf("Buy carrots%fpound,Unit Price%f$/pound,Price%f$\n",c,hlb,c*hlb);
    printf("The total weight purchased is%fpound,The total price of the goods is%f$\n",heavy,Sabc);
    printf("freight%f$,Order total price is%f$\n",sent,Sabc+sent);
    printf("Discount%f$\n",discount);
    printf("Total cost:%f$",Sabc+sent-discount);
    return 0;
}

10

Rewrite Exercise 4 with switch

#include <stdio.h>
int main(void)
{
    char ch;
    int i=0;
    printf("Please enter:");
    ch=getchar();
    while (ch!='#') {
        if (ch=='.'||ch=='!') {
    switch (ch) {
        case '.':
            printf("!");
            i++;
            break;
        case '!':
            printf("!!");
            i++;
            break;
        default:
            break;
    }
        }
        else
        putchar(ch);
        ch=getchar();
    }
    printf("Cosubstitution%d second",i);
    reyurn 0;
}
11

Modify Hypothesis a of Exercise 6 so that the program can give a menu of wage ratings to choose from.Make the switch complete the wage level selection.After running the program, the menu should look like this:

*****************************************************************

Enter the number corresponding to the desired pay rate or action:

1) $8.75/hr              2) $9.33/hr

3) $10.00/hr             4) $11.20/hr

5) quit

*****************************************************************

If you choose one of the numbers 1-4, the program should ask the user how many hours they work.Procedures run in a loop unless the user enters 5.If you enter a number other than 1-5, the program should remind the user to enter the correct option, and then repeat the display menu to prompt the user for input.Use #define to create symbolic constants to represent wage levels and tax rates.

#include <stdio.h>
int main(void)
{
	float sla;
	char ch;
	int i,j;
	int tax;
	int sum;
	printf("Please select a salary level:\n");
	printf("1.$8.75/hr 2.$9.33/hr 3.$10.00/hr 4.$11.20/hr 5.quit\n");
	switch(ch=getchar())
	{
		case'1':
		   sla=8.75;
		   break;
		case'2':
		   sla=9.33;
		   break;
		case'3':
		   sla=10.00;
		   break;
		case'4':
		   sla=11.20;
		   break;
		case'5':
		   sla=0;
		   break;
	}
while(sla>0){
  printf("Please enter a week's work time:");
  scanf("%d",&i);
  if(i<=40)
  {
  	sum=sla*i;
  	if(sum<=300)
  	{
  		tax=0.15*sum;
  	}
  	else if(sum<=450)
  	{
  		tax=45+0.2*(sum-300);
  	}
  	else
  	{
  		tax=45+30+0.25*(sum-450);
  	}
  	j=sum-tax;
  }
  else
  {
  	sum=sla*40+1.5*(i-40)*sla;
  		if(sum<=300)
  	{
  		tax=0.15*sum;
  	}
  	else if(sum<=450)
  	{
  		tax=45+0.2*(sum-300);
  	}
  	else
  	{
  		tax=45+30+0.25*(sum-450);
  	}
  	j=sum-tax;
  } 
  printf("Total wages%d Taxes%d net income%d \n",sum,tax,j);
  break;
  }
  return 0;
}

 

 

Tags: Programming less ascii

Posted on Thu, 07 Nov 2019 21:07:13 -0500 by Buddha443556