近来,将以前的编程代码进行了总结,第一波,我将为大家呈现一些关于选择结构的例题代码,希望有需要的朋友们能够得到一定的帮助。
话不多说,代码呈上:
1.检测结果
#include<stdio.h>
int main()
{
int m = 5;
if (m++ > 5)
printf("%d\n", m);
else
printf("%d\n", m);
getchar();
return 0;
}
2.检测结果
#include<stdio.h>
int main()
{
int a = -1, b = 3, c = 3, s = 0, w = 0, t = 0;
if (c > 0)
s = a + b;
if (a <= 0)
{
if (b > 0)
{
if (c <= 0)
w = a - b;
}
}
else if (c > 0)
w = a - b;
else
t = c;
printf("%d %d %d\n",s,w,t);
getchar();
return 0;
}
3.检测结果
#include<stdio.h>
int main()
{
int a = 1;
int b = 2;
int z = a > b ? a : b + 1;
printf("%d\n", z);
getchar();
return 0;
}
4检测结果
#include<stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int z = a > b ? a : c > d ? c : d;
printf("%d\n",z);
getchar();
return 0;
}
5检测结果
#include<stdio.h>
int main()
{
int a = 10;
int c = 9;
int z = (--a != c++) ? --a : ++c;
printf("%d\n",z);
getchar();
return 0;
}
6检测结果
#include<stdio.h>
int main()
{
int x = 1;
int y = 2;
int z = 3;
int w = (x < y ? x : y) == z++;
printf("%d\n",w);
getchar();
return 0;
}
7检测结果
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int p = 0;
printf("please input a and b:>");
scanf("%d %d",&a,&b);
getchar();
p = a*a + b*b;
if (p > 100)
p = p / 100;
else
p = a + b;
printf("%d\n",p);
getchar();
return 0;
}
8检测结果
#include<stdio.h>
int main()
{
int x = 0;
int y = 0;
printf("please input x:>");
scanf("%d",&x);
getchar();
switch (x == 0 ? 0 : x > 0 ? 1 : -1)
{
case 0:y = 0; break;
case 1:y = 1; break;
default:y = -1;
}
printf("%d",y);
getchar();
return 0;
}
9检测结果
#include<stdio.h>
int main()
{
char c = ' ';
printf("please input character:>");
scanf("%c",&c);
getchar();
if ((c >= 'a') && (c <= 'z'))
c = c - 32;
printf("%c\n",c);
getchar();
return 0;
}
10检测结果
#include<stdio.h>
int main()
{
int year = 0;
int month = 0;
int days = 0;
int p = 0;
printf("please input year and month:>");
scanf("%d %d",&year,&month);
getchar();
if (month == 2)
{
p = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
if (p)
days = 29;
else
days = 28;
}
else if (((month <= 7) && (month % 2 == 0)) || ((month >= 8) && (month % 2 != 0)))
days = 30;
else
days = 31;
printf("days=%d",days);
getchar();
return 0;
}
11检测结果
#include<stdio.h>
int main()
{
int x = 0;
int y = 0;
printf("please input x:>");
scanf("%d",&x);
getchar();
if ((x < 0) && (x != -3))
y = x*x + 2 * x - 6;
else if ((x >= 0) && (x < 10) && (x != 2) && (x != 3))
y = x*x - 5 * x + 6;
else
x = x*x - x - 15;
printf("y=%d", y);
getchar();
return 0;
}
12检测结果
#include<stdio.h>
int main()
{
int t = 0;
int p = 0;
printf("please input t:>");
scanf("%d",&t);
getchar();
if (t <= 160)
p = 5 * t;
else
p = 5 * t + 4 * (t - 160);
printf("p=%d\n",p);
getchar();
return 0;
}
13检测结果
#include<stdio.h>
#define limt 0.000001
int main()
{
double x = 0;
double y = 0;
printf("please input x:>");
scanf("%lf",&x);
getchar();
y = x*x*x*x - 3 * x*x - 8 * x - 30.0;
if ((y > -limt) && (y < limt))
printf("Y");
else
printf("N");
getchar();
return 0;
}
14检测结果
#include<stdio.h>
void max(int * x, int * y)
{
int t = 0;
if (*x <* y)
{
t = *x;
*x = *y;
*y = t;
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
printf("please input a, b, c and d:>");
scanf("%d %d %d %d",&a,&b,&c,&d);
getchar();
max(&a,&b);
max(&a,&c);
max(&a,&d);
max(&b,&c);
max(&b,&d);
max(&c,&d);
printf("%d %d %d %d",a,b,c,d);
getchar();
return 0;
}