c语言:一组数据中只有一个数字出现了一次。(使用位运算)
程序:
#include<stdio.h>
int main()
{
int arr[] = { 1,3,3,1,4,9,4,5,5 };
int i = 0;
int len = sizeof(arr) / sizeof(arr[0]);
for (i = 1; i < len; i++)
{
arr[0] = arr[0] ^ arr[i];
//将这组数中所有数取按位或,由于相异为1,相同为0,故位运算剩下为1的就是单数
}
printf("这个单数是:%d", arr[0]);
return 0;
}
结果:
这个单数是:9请按任意键继续. . .
程序: #includestdio.hint main(){int arr[] = { 1,3,3,1,4,9,4,5,5
};int i = 0;int len = sizeof(…
此C语言函数—A
函数名: abort
功 能: 异常终止一个进程
用 法: void abort(void);
程序例:
#include
#include
int main(void)
{
printf("Calling abort()\n");
abort();
return 0; /* This is never reached */
}
函数名: abs
功 能: 求整数的绝对值
用 法: int abs(int i);
程序例:
#include
#include
int main(void)
{
int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
函数名: absread, abswirte
功 能: 绝对磁盘扇区读、写数据
用 法: int absread(int drive, int nsects, int sectno, void *buffer);
int abswrite(int drive, int nsects, in tsectno, void *buffer);
程序例:
/* absread example */
#include
#include
#include
#include
int main(void)
{
int i, strt, ch_out, sector;
char buf[512];
printf("Insert a diskette into drive A and press any key\n");
getch();
sector = 0;
if (absread(0, 1, sector, &buf) != 0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK\n");
strt = 3;
for (i=0; i<80; i++)
{
ch_out = buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
程序例:
#include
#include
int file_exists(char *filename);
int main(void)
{
printf("Does NOTEXIST.FIL exist: %s\n",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
函数名: acos
功 能: 反余弦函数
用 法: double acos(double x);
程序例:
#include
#include
int main(void)
{
double result;
double x = 0.5;
result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}
函数名: allocmem
功 能: 分配DOS存储段
用 法: int allocmem(unsigned size, unsigned *seg);
程序例:
#include
#include
#include
int main(void)
{
unsigned int size, segp;
int stat;
size = 64; /* (64 x 16) = 1024 bytes */
stat = allocmem(size, &segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",
stat);
return 0;
}
函数名: arc
功 能: 画一弧线
用 法: void far arc(int x, int y, int stangle, int endangle, int
radius);
程序例:
#include
#include
#include
#include
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult(); /* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw arc */
arc(midx, midy, stangle, endangle, radius);
/* clean up */
getch();
closegraph();
return 0;
}
函数名: asctime
功 能: 转换日期和时间为ASCII码
用 法: char *asctime(const struct tm *tblock);
程序例:
#include
#include
#include
int main(void)
{
struct tm t;
char str[80];
/* sample loading of tm structure */
t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated
string */
strcpy(str, asctime(&t));
printf("%s\n", str);
return 0;
}
函数名: asin
功 能: 反正弦函数
用 法: double asin(double x);
程序例:
#include
#include
int main(void)
{
double result;
double x = 0.5;
result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}
函数名: assert
功 能: 测试一个条件并可能使程序终止
用 法: void assert(int test);
程序例:
#include
#include
#include
struct ITEM {
int key;
int value;
};
/* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
}
int main(void)
{
additem(NULL);
return 0;
}
函数名: atan
功 能: 反正切函数
用 法: double atan(double x);
程序例:
#include
#include
int main(void)
{
double result;
double x = 0.5;
result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}
函数名: atan2
功 能: 计算Y/X的反正切值
用 法: double atan2(double y, double x);
程序例:
#include
#include
int main(void)
{
double result;
double x = 90.0, y = 45.0;
result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}
函数名: atexit
功 能: 注册终止函数
用 法: int atexit(atexit_t func);
程序例:
#include
#include
void exit_fn1(void)
{
printf("Exit function #1 called\n");
}
void exit_fn2(void)
{
printf("Exit function #2 called\n");
}
int main(void)
{
/* post exit function #1 */
atexit(exit_fn1);
/* post exit function #2 */
atexit(exit_fn2);
return 0;
}
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
程序例:
#include
#include
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
#include
#include
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);
程序例:
#include
#include
int main(void)
{
long l;
char *str = "98765432";
l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
return(0);
}
由 书画小说软件 整理 更惬意的读、更舒心的写、更轻松的发布
函数名: abort 功 能:
异常终止一个进程 用 法: void abort(void); 程序例: #include #include
int main(void) { printf(“Calling abort()\n”); abort(…
一、 什么是函数
任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”。所以,你可以说C语言程序是由函数构成的。
比如你用C语言编写了一个MP3播放器程序,那么它的程序结构如下图所示:
1 #include <stdio.h> 2 3 /* 4
1.什么情况下定义函数:添加一个常用的新功能 5 6 2.函数的定义格式 7
返回值类型 函数名(形式参数列表) 8 { 9 函数体 10 } 11 12
3.定义函数需要明确的东西 13 1> 起一个有意义的函数名 14 2> 15 */
16 17 int printLine() 18 { 19 printf(“————-\n”); 20 return 0;
21 } 22 23 int average(int num1, int num2) 24 { 25 return (num1 +
num2)/2; 26 } 27 28 int main() 29 { 30 /* 31 printLine(); 32 33
printLine(); 34 35 printLine(); 36 */ 37 38 int a = 10; 39 int b = 9;
40 41 int c = average(a, b); 42 43 printf(“c is %d\n”, c); 44 45 int a1
= 11; 46 int b1 = 20; 47 48 int d = average(a1, b1); 49 printf(“d is
%d\n”, d); 50 51 return 0; 52 }
2.
1 #include <stdio.h>
2 /*
3 参数注意点
4 1.形式参数:定义函数时函数名后面中的参数,简称形参
5 2.实际参数:调用函数式传入的具体数据,简称实参
6 3.实参个数必须等于形参个数
7 4.函数体内部不能定义和形参一样的变量
8 5.如果是基本数据类型作为函数形参,纯粹是值传递,修改函数内部形参的值,并不会影响外面实参的值
9 6.一个函数可以没有形参,也可以有无限多个形参
10 */
11
12 // 形式参数,简称形参
13 int sum(int num1, int num2)
14 {
15 // 函数体内部不能定义和形参一样的变量
16 // int num1;
17
18 num1 = 50;
19
20 return num1 + num2;
21 }
22
23 /*
24 return的作用:
25 1> 退出函数
26 2> 返回一个具体值给函数调用者
27
28 返回值注意点
29 1> void代表没有返回值
30 2> 如果没有明确说明返回值类型,默认是返回int类型
31 3> 就算明确声明了返回值类型,也可以不返回任何值
32
33 默认情况下,C语言不允许两个函数的名称一样
34 */
35
36 char test()
37 {
38 return 'A';
39 }
40
41 /*
42 void test(int a, int b)
43 {
44
45 }*/
46
47 void test5()
48 {
49
50 }
51
52 /* 伪代码
53 void login(QQ, 密码)
54 {
55 // 1.验证QQ有没有值
56 if (QQ没有值) return;
57
58 // 2.验证密码有没有值
59 if (密码没有值) return;
60
61 // 3.把QQ、密码发送到服务器
62
63 }*/
64
65 int test3()
66 {
67 printf("999999999\n");
68 }
69
70 // 如果不明确声明返回值类型,默认就是int类型
71 test2()
72 {
73 printf("888888888\n");
74 return 10;
75 }
76
77 int main()
78 {
79 int c = test2();
80 printf("c=%d\n", c);
81
82 test3();
83 //test();
84
85 /*
86 int a = 100;
87 int b = 27;
88
89 // a、b称为函数的实际参数,简称实参
90 int c = sum(a, b);
91
92
93 printf("a=%d, b=%d, c=%d\n", a, b, c);*/
94
95 return 0;
96 }
3.函数练习
1 /*
2 求两个整数的差
3 打印一条横线
4 打印N条横线
5
6 定义函数的步骤
7 1> 根据函数的作用,起一个有意义的名称
8 2> 确定函数的形参个数
9 3> 确定函数的返回值
10 */
11 #include <stdio.h>
12
13 void printLines(int n)
14 {
15 for (int i = 0; i<n; i++)
16 {
17 printf("-------------------\n");
18 }
19 }
20
21 void printLine()
22 {
23 printf("-------------------\n");
24 }
25
26 int minus(int a, int b)
27 {
28 return a - b;
29 }
30
31 int main()
32 {
33 printLines(10);
34 //printLine();
35 //printf("%d\n", minus(100, 29));
36
37 return 0;
38 }
4.函数注意点
1 #include <stdio.h>
2 /*
3 1.默认情况下,不允许有函数的名称一样
4 2.函数不能嵌套定义
5 3.函数不能重复定义,但是可以重复声明
6 4.如果有函数的声明,没有函数的定义
7 1> 编译可以通过,因为编译器只会检测语法合不合理,并不会检测函数有没有定义
8 2> 链接报错,因为链接的时候会检测函数是否定义
9
10 */
11
12 // 函数的声明
13 //void printLine();
14 //void printLine();
15 //void printLine();
16 //void printLine();
17 //void printLine();
18 //void printLine();
19
20 int main()
21 {
22 void printLine();
23
24 printLine();
25 return 0;
26 }
27
28 // 函数的定义
29
30 void printLine()
31 {
32 printf("--------\n");
33 }
一、
什么是函数
任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序…