Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language program switching statement
C language program switching statement
Hello, landlord! Hello, brothers upstairs!

The two buddies upstairs directly took X as an integer, but the landlord didn't say X was an integer, did he?

This is my program:

# include & ltstdio.h & gt

Master ()

{

Double x, y;

int a;

scanf("%lf ",& ampx);

a =(int)x; /* Convert the value of x into an integer, that is, cut off the decimal point and assign it to a*/

Switch (a)

{

Case-4: y = x-1; Break;

Case-3: y = x-1; Break;

Case-2: y = x-1; Break;

case- 1:y = x- 1; Break;

/*********************

In fact, the above paragraph can be written like this.

Case 4:

Case 3:

Case 2:

Case-1:

y = x- 1; Break;

Simplify the process

The following paragraph is the same. ....

***********************/

Case 7: y = x+1; Break;

Case 6: y = x+1; Break;

Case 5: y = x+1; Break;

Case 4: y = x+1; Break;

Case 3: y = x+1; Break;

Case 2: y = x+1; Break;

Case1:y = x+1; Break;

Case 0: y = x; Break;

Default value: printf ("input error \ n"); Go to loop;

}

printf("x=%lf\ny=%lf\n ",x,y);

Loop:;

getch();

}

So it doesn't matter whether you enter an integer or a floating-point number. ....

Actually, the buddy's method on the first floor is super good. If int x, y is changed to double x, y will be perfect ... (this doesn't seem to be a problem ...)

I have tested this program and found no problems. (Hungry just means hungry. No problems were found. Whoever finds the problem will correct it quickly! ! ! )

====================================

Lipbb is right, the program I wrote above is really wrong ... I'm too lazy to change it ... this is another way I think:

Nested with switch, hunger test, no problem found. (Hungry just means hungry. No problems were found. Whoever finds the problem will correct it quickly! ! ! )

# include & ltstdio.h & gt

Master ()

{

Double x, y;

Printf ("Please enter x \ n");

scanf("%lf ",& ampx);

Switch (-5 <; x & amp& ampx & lt0)

{

Case1:y = x-1; Break;

Case 0:

{

Switch (x==0)

{

Case1:y = x; Break;

Case 0:

{

Switch (0<x & amp& ampx & lt8)

{

Case1:y = x+1; Break;

Case 0:printf ("input error \ n"); Go to loop;

}

} broken;

}

} broken;

}

printf("x=%lf\ny=%lf ",x,y);

Loop:;

getch();

}

You can leave me a message if you don't understand.