1.实现一个菜单.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
include <stdio.h> #include <string.h> void mainMenu(); void login(); void regist(); void shuiXianHua(); void returnToMenu(); main() { mainMenu(); } void mainMenu() { int i; printf ( "1.登录\n" ); printf ( "2.注册\n" ); printf ( "3.输出水仙花数\n" ); printf ( "4.退出程序\n" ); printf ( "请你选择序号:" ); scanf ( "%d" ,&i); switch (i) { case 1:login(); break ; case 2:regist(); break ; case 3:shuiXianHua(); break ; } } void login() { char uname[20],upwd[20]; printf ( "请输入你的用户名:" ); scanf ( "%s" ,uname); printf ( "请输入密码:" ); scanf ( "%s" ,upwd); if ( strcmp (uname, "admin" )==0&& strcmp (upwd, "admin" )==0) { printf ( "登陆成功" ); } else { printf ( "登陆失败" ); } returnToMenu(); } void regist() { char uname[20],upwd1[20],upwd2[20]; printf ( "请输入用户名:" ); scanf ( "%s" ,uname); printf ( "请输入密码:" ); scanf ( "%s" ,upwd1); printf ( "请再次输入密码:" ); scanf ( "%s" ,upwd2); if ( strcmp (upwd1,upwd2)==0) { printf ( "注册成功" ); } else { printf ( "注册失败" ); } returnToMenu(); } void shuiXianHua() { int i,ge,shi,bai; i=100; while (i<1000) { ge=i%10; shi=i/10%10; bai=i/100; if (ge*ge*ge+shi*shi*shi+bai*bai*bai==i) { printf ( "水花仙数是%d\n" ,i); } i++; } returnToMenu(); } void returnToMenu() { char answer[20]; printf ( "您是否要退出程序?Y/N,请输入:" ); scanf ( "%s" ,answer); if ( strcmp (answer, "Y" )==0) { printf ( "谢谢使用,再见\n" ); } else { mainMenu(); } } |
2.使用函数,求两个数的最大值,并调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <stdio.h> int max( int a, int b); main() { int a,b; printf ( "请输入两个数,(请用空格隔开):" ); scanf ( "%d %d" ,&a,&b); printf ( "%d\n" ,max(a,b)); } int max( int a, int b) { if (a>b) return a; else return b; } |
3.使用递归函数,求阶乘,并调用求5的阶乘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h> int func( int a); main() { int a; printf ( "请输入你要阶乘到的数:" ); scanf ( "%d" ,&a); printf ( "%d的阶乘结果是%d\n" ,a,func(a)); } int func( int a) { if (a==1||a==0) { return 1; } else return (a*func(a-1)); } |
4.使用函数,判断一个年份是不是闰年,如果是闰年返回1,否则返回0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h> int year( int a); main() { int n; printf ( "请输入你的年份:" ); scanf ( "%d" ,&n); printf ( "%d" ,year(n)); } int year( int a) { if (a%4==0&&a%100!=0||a%400==0) { return 1; } return 0; } |
5.使用函数,求一个数的绝对值,并调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <stdio.h> int jdz( int a); main() { int a; printf ( "请输入一个数:" ); scanf ( "%d" ,&a); printf ( "%d的绝对值是:%d" ,a,jdz(a)); } int jdz( int a) { if (a<0) return -1*a; return a; } |