C/C++程序集
2013-04-08 本文已影响483人
司马懿
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
int s, afd;
int len;
struct sockaddr_in sin, csin;
char buf[32] = "";
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(5678);
sin.sin_addr.s_addr = INADDR_ANY;
//sin.sin_addr.s_addr = INADDR_ANY;
s = socket(PF_INET, SOCK_STREAM, 0);
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
printf("bind error\n");
}
if (listen(s, 32) < 0 )
printf("listen error\n");
while (1) {
afd = accept(s, (struct sockaddr *)&csin, &len);
if (afd < 0) {
printf("accept error\n");
continue;
}
if (read(afd, buf, sizeof(buf)) > 0)
printf("%s\n", buf);
close(afd);
}
}
执行‘telnet localhsot 5678’测试。
tcp 一对一
udp 一对多 多对一 多对多
质数验证程序:
-bash-3.2$ more prime.c
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define E9 1072000000
#define E10 10000000000
#define E11 100000000000
#define E12 1000000000000
char table[E9];
int num = 0;
//int prime[E8];
void cal_table()
{
int j, k;
time_t t1, t2;
t1 = time(NULL);
memset(table, 1, E9);
for (j = 2 ; j < E9; j++) {
if ( table[j] ) {
num++;
for ( k = j + j; k < E9; k += j )
{
table[k] = 0;
}
}
}
t2 = time(NULL);
printf("Totaly %d primes until E9, cost %d time_t.\n", num, t2 - t1);
}
int main()
{
int i;
cal_table();
return 0;
}
-bash-3.2$ gcc prime.c
-bash-3.2$ ./a.out
Totaly 54316419 primes until E9, cost 179 time_t.
-bash-3.2$
Little endian把低字节存放在内存的低位;而Big endian将低字节存放在内存的高位;Intel的CPU是little endian,Sun Sparc的CPU是big endian。
假设从地址0x00000000开始的一个字(WORD)中存有数据0x1234abcd。
Little endian机器,从低到高内存的存放顺序是:0x00000000-0xcd,0x00000001-0xab,0x00000002-0x34,0x00000003-0x12
Big endian机器,从低到高内存的存放顺序是:0x00000000-0x12,0x00000001-0x34,0x00000002-0xab,0x00000003-0xcd
#include <stdio.h>
int main()
{
int a = 0x12345678;
char *p = (char * )&a;
printf("%x %x %x %x\n", *p, *(p+1), *(p+2), *(p+3));
return 0;
}
-bash-3.2$ ./a.out
78 56 34 12
-bash-3.2$ uname -a
Linux fedora.unix-center.net 2.6.27.10-grsec2.1.12 #2 SMP Fri May 8 07:04:03 CST 2009 i686 i686 i386 GNU/Linux
-bash-3.00$ ./a.out
12 34 56 78
-bash-3.00$ uname -a
SunOS t1000 5.10 Generic_118833-33 sun4v sparc SUNW,Sun-Fire-T1000 Solaris
以下程序在Linux上用GCC编译运行,结果是0.000000,其实这种代码编译器应该报错,最起码是警告,可是.......
#include <stdio.h>
double sum();
int main() {
int x = 10;
int y = 10;
double z = sum(x,y);
printf("%lf\n", z);
return 0;
}
double sum(double x, double y) {
return (x+y);
}
反向排列字符串
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char a[128] = "I am fine thank you asdasd aaaaaa bbbbbb cccccc cccccccccccc";
void reserve (char * in, char *out)
{
char *p = in + strlen(in) - 1;
while (1) {
while (1) {
if (*p == ' ' || p == in)
break;
p--;
}
if (p == in) {
strcat(out, p);
return;
}
strcat(out, p+1);
strcat(out, " ");
while (*(p - 1) == ' ')
p--;
*p = '\0';
}
}
int main()
{
char *b;
b = malloc(128);
reserve(a, b);
printf("%s\n", b);
free(b);
return 0;
}
打印1-20的全排列。
#include<stdio.h>
void print_it(int n, int arr[]);
void arrange_all(int size, int arr[], int pos);
int main()
{
int a[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
arrange_all(20, a, 0);
return 0;
}
void arrange_all(int size, int arr[], int pos)
{
int i, tmp;
if (pos + 1== size) {
print_it(size, arr);
return;
}
for (i = pos; i < size; i++) {
tmp = arr[pos];
arr[pos] = arr[i];
arr[i] = tmp;
arrange_all(size, arr, pos + 1);
tmp = arr[pos];
arr[pos] = arr[i];
arr[i] = tmp;
}
}
void print_it(int n, int arr[])
{
int i;
static int cnt = 0;
printf("%21d: ", ++cnt);
for (i = 0; i < n; i++)
printf("%4d ", arr[i]);
printf("");
}
#include <stdio.h>
int func(int n)
{
int a[30];
int i, j, t;
int count=0;
for(i=0;i<n;i++) a[i]=i+1;
for(;;) {
printf("%4d: ", ++count);
for(i=0;i<n;i++) printf("%d ", a[i]);
printf("\n");
for(i=n-2;i>=0;i--) if(a[i]<a[i+1]) break;
if(i<0) break;
for(j=n-1;;j--) if(a[j]>a[i]) break;
t=a[j], a[j]=a[i], a[i]=t;
i++, j=n-1;
while(i<j) {
t=a[j], a[j]=a[i], a[i]=t;
i++, j--;
}
}
return(0);
}
main()
{
func(20);
}