有聊的程序员看这里。评选我心目中的处女级C/C++代码

phoneix818 2012-05-28 12:11:27
写出自己的处女级C/C++代码哈。我先上一个


#include <stdio.h>
#define sex int
void touch(sex male, sex female)
{
printf("%d*%d=%d\t", male, female, male*female);
if (male==female)
{
printf("\n");
}
}

#define makelove touch(male,female)
void exchange(sex male, sex female)
{
if (female==1)
{
makelove;
}
else if (male==1)
{
exchange(female-1, female-1);
makelove;
}
else
{
exchange(male-1, female);
makelove;
}
}

sex main(void)
{
exchange(9,9);

return 0;
}
...全文
188 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
phoneix818 2012-05-28
  • 打赏
  • 举报
回复
《至少还有你》世贸版—转自高中同学录,不知道old否?
发信站: BBS 水木清华站 (Wed Oct 17 07:03:03 2001)

我怕来不及,我要撞掉你,
直到看到整个纽约有了地震的痕迹。
直到肯定你是倒了,
直到烟灭灰飞,
为报仇,我愿意。
人民也心痛,股市也颤动,
直到仿佛整个白宫有了痛苦的回忆。
直到总统变得愤怒,
直到球赛延期,
让我们欢庆胜利。

如果上天再给我一架飞机,
我也要用它撞向美国大地,
布什能躲过就是生命的奇迹。
也许阿富汗我也可以躲避,

只是不愿意失去父母兄弟,
为真理正义我要坚决斗到底。

我们好不容易,我们精心算计。
我怕速度太快,不够将你撞彻底;
我怕角度太刁,让你倒的太容易。
恨不得亲自驾驶飞机,直接奔向你。
羽飞 2012-05-28
  • 打赏
  • 举报
回复
无聊
测试NULL 2012-05-28
  • 打赏
  • 举报
回复

/**************************************************************/
/* Selection function: Standard proportional selection for */
/* maximization problems incorporating elitist model - makes */
/* sure that the best member survives */
/**************************************************************/
/*比例选择*/
void select(void)
{
int mem, i, j, k;
double sum = 0;
double p;
/* find total fitness of the population */
for (mem = 0; mem < POPSIZE; mem++)
{
sum += population[mem].fitness;
}

/* calculate relative fitness 相关适应度等于个体适应度比所有个体适应度之和*/
for (mem = 0; mem < POPSIZE; mem++)
{
population[mem].rfitness = population[mem].fitness/sum;
}
population[0].cfitness = population[0].rfitness;

/* calculate cumulative fitness */
for (mem = 1; mem < POPSIZE; mem++)
{
population[mem].cfitness = population[mem-1].cfitness +
population[mem].rfitness;
}
/* finally select survivors using cumulative fitness. */
/*选择依据累积适应度*/
for (i = 0; i < POPSIZE; i++)
{
p = rand()%1000/1000.0;
if (p < population[0].cfitness)
newpopulation[i] = population[0];
else
{
for (j = 0; j <POPSIZE;j++)
if (p >= population[j].cfitness&&
p<population[j+1].cfitness)
newpopulation[i] = population[j+1];
}
}
/* once a new population is created, copy it back */
for (i = 0; i < POPSIZE; i++)
population[i] = newpopulation[i];
}

/***************************************************************/
/* Crossover selection: selects two parents that take part in */
/* the crossover. Implements a single point crossover */
/***************************************************************/
/*单点交叉*/
void crossover(void)
{
int i, mem, one;
int first = 0; /* count of the number of members chosen */
double x;

for (mem = 0; mem < POPSIZE; ++mem)
{
x = rand()%1000/1000.0;
if (x < PXOVER)
{
++first;
if (first % 2 == 0)
Xover(one, mem);
else
one = mem;
}
}
}
/**************************************************************/
/* Crossover: performs crossover of the two selected parents. */
/**************************************************************/

voidXover(int one, int two)
{
int i;
int point; /* crossover point */

/* select crossover point */
if(NVARS > 1)
{
if(NVARS == 2)
point = 1;
else
point = (rand() % (NVARS - 1)) + 1;

for (i = 0; i < point; i++)
swap(&population[one].gene[i], &population[two].gene[i]);

}
}

/*************************************************************/
/* Swap: A swap procedure that helps in swapping 2 variables */
/*************************************************************/

void swap(double *x, double *y)
{
double temp;

temp = *x;
*x = *y;
*y = temp;

}

/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and */
/* upper bounds of this variable */
/**************************************************************/
/*随机均匀变异*/
void mutate(void)
{
int i, j;
doublelbound, hbound;
double x;

for (i = 0; i < POPSIZE; i++)
for (j = 0; j < NVARS; j++)
{
x = rand()%1000/1000.0;
if (x < PMUTATION)
{
/* find the bounds on the variable to be mutated */
lbound = population[i].lower[j];
hbound = population[i].upper[j];
population[i].gene[j] = randval(lbound, hbound);
}
}
}

/***************************************************************/
/* Report function: Reports progress of the simulation. Data */
/* dumped into the output file are separated by commas */
/***************************************************************/

void report(void)
{
int i;
doublebest_val; /* best population fitness */
doubleavg; /* avg population fitness */
doublestddev; /* std. deviation of population fitness 偏差*/
doublesum_square; /* sum of square for std. calc */
doublesquare_sum; /* square of sum for std. calc */
double sum; /* total population fitness */

sum = 0.0;
sum_square = 0.0;

for (i = 0; i < POPSIZE; i++)
{
sum += population[i].fitness;
sum_square += population[i].fitness * population[i].fitness;
}

avg = sum/(double)POPSIZE;
square_sum = avg * avg * POPSIZE;
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));
best_val = population[POPSIZE].fitness;

fprintf(galog, "\n%5d, %6.3f, %6.3f, %6.3f \n\n", generation,
best_val, avg, stddev);
}
/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied */
/**************************************************************/
int main(void)
{
int i;

if ((galog = fopen("galog.txt","w"))==NULL)
{
exit(1);
}
generation = 0;

fprintf(galog, "\n generation best average standard \n");
fprintf(galog, " number value fitness deviation \n");

initialize();/*初始化*/
evaluate();/*评估*/
keep_the_best();
while(generation<MAXGENS)
{
generation++;
select();
crossover();
mutate();
report();
evaluate();
elitist(); /*保存最好的淘汰最差的*/
}
fprintf(galog,"\n\n Simulation completed\n");
fprintf(galog,"\n Best member: \n");

for (i = 0; i < NVARS; i++)
{
fprintf (galog,"\n var(%d) = %3.3f",i,population[POPSIZE].gene[i]);
}
fprintf(galog,"\n\n Best fitness = %3.3f",population[POPSIZE].fitness);
fclose(galog);
printf("Success\n");
}
/***************************************************************/

测试NULL 2012-05-28
  • 打赏
  • 举报
回复
随便发个代码:

/**************************************************************************/
/* This is a simple genetic algorithm implementation where the */
/* evaluation function takes positive values only and the */
/* fitness of an individual is the same as the value of the */
/* objective function */
/**************************************************************************/
/* population[POPSIZE].fitness保存最好的适应度*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Change any of these parameters to match your needs */

#define POPSIZE 50 /* population size染色体个数 */
#define MAXGENS 1000 /* max. number of generations迭代次数 */
#define NVARS 3 /* no. of problem variables 变量个数*/
#define PXOVER 0.8 /* probability of crossover 交叉概率*/
#define PMUTATION 0.15 /* probability of mutation变异概率*/
#define TRUE 1
#define FALSE 0

int generation; /* current generation no. 当前代数*/
int cur_best; /* best individual 最好个体*/
FILE *galog; /* an output file 输出文件*/
/*染色体结构体*/
struct genotype /* genotype (GT), a member of the population 染色体成员*/
{
double gene[NVARS]; /* a string of variables一连串变量 */
double fitness; /* GT's fitness 个体适应度*/
double upper[NVARS]; /* GT's variables upper bound 个体上界*/
double lower[NVARS]; /* GT's variables lower bound 个体下界*/
double rfitness; /* relative fitness相关适应度等于个体适应度比所有个体适应度之和**/
double cfitness; /* cumulative fitness 累积适应度是相关是适应度的累积*/
};
/*声明个体*/
struct genotype population[POPSIZE+1]; /* population */
struct genotype newpopulation[POPSIZE+1]; /* new population; */
/* replaces the */
/* old generation */

/* Declaration of procedures used by this genetic algorithm */
/*声明的程序使用的遗传算法*/
void initialize(void);/*初始化函数*/
doublerandval(double, double);
void evaluate(void);/*评估*/
void keep_the_best(void); /*保持最好的*/
void elitist(void);
void select(void);/*选择*/
void crossover(void);/*交叉*/
voidXover(int,int);
void swap(double *, double *);
void mutate(void);/*变异*/
void report(void);

/***************************************************************/
/* Initialization function: Initializes the values of genes */
/* within the variables bounds. It also initializes (to zero) */
/* all fitness values for each member of the population. It */
/* reads upper and lower bounds of each variable from the */
/* input file `gadata.txt'. It randomly generates values */
/* between these bounds for each gene of each genotype in the */
/* population. The format of the input file `gadata.txt' is */
/* var1_lower_bound var1_upper bound */
/* var2_lower_bound var2_upper bound ... */
/***************************************************************/
/*初始化函数:在定义域内初始化基因并计算适应度,从文件“gadata.txt”中读定义域*/
void initialize(void) /*初始化*/
{
FILE *infile;
int i, j;
doublelbound, ubound;

if ((infile = fopen("gadata.txt","r"))==NULL)
{
fprintf(galog,"\nCannot open input file!\n");
exit(1);
}

/* initialize variables within the bounds */
/*NVARS变量的个数*/
for (i = 0; i < NVARS; i++)
{
fscanf(infile, "%lf",&lbound);
fscanf(infile, "%lf",&ubound);
/*POPSIZE 种群规模*/
for (j = 0; j < POPSIZE; j++)
{
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].lower[i] = lbound;
population[j].upper[i]= ubound;
population[j].gene[i] = randval(population[j].lower[i],
population[j].upper[i]);
}
}
fclose(infile);
}
/*每个变量初始popsize个个体*/
/***********************************************************/
/* Random value generator: Generates a value within bounds */
/***********************************************************/
/*Randval随机在定义域内取值*/
doublerandval(double low, double high)
{
doubleval;
val = ((double)(rand()%1000)/1000.0)*(high - low) + low;
return(val);
}

/*************************************************************/
/* Evaluation function: This takes a user defined function. */
/* Each time this is changed, the code has to be recompiled. */
/* The current function is: x[1]^2-x[1]*x[2]+x[3] 目标函数 */
/*************************************************************/

void evaluate(void)
{
int mem;
int i;
double x[NVARS+1];

for (mem = 0; mem < POPSIZE; mem++)
{
for (i = 0; i < NVARS; i++)
x[i+1] = population[mem].gene[i]; /*个体populartion变量gene*/

population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];
}
}


/***************************************************************/
/* Keep_the_best function: This function keeps track of the */
/* best member of the population. Note that the last entry in */
/* the array Population holds a copy of the best individual */
/***************************************************************/
/*最好个体函数保持副本*/
void keep_the_best()
{
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */
for (mem = 0; mem < POPSIZE; mem++)
{
if (population[mem].fitness > population[POPSIZE].fitness)
{
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++)
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}

/****************************************************************/
/* Elitist function: The best member of the previous generation */
/* is stored as the last in the array. If the best member of */
/* the current generation is worse then the best member of the */
/* previous generation, the latter one would replace the worst */
/* member of the current population */
/****************************************************************/
/*当前代数的最好个体和目前已找到的最好个体比较,最好的保存下来淘汰最差的*/
void elitist()
{
int i;
double best, worst; /* best and worst fitness values */
intbest_mem, worst_mem; /* indexes of the best and worst member */

best = population[0].fitness;
worst = population[0].fitness;
for (i = 0; i < POPSIZE - 1; ++i)
{
if(population[i].fitness > population[i+1].fitness)
{
if (population[i].fitness >= best)
{
best = population[i].fitness;
best_mem = i;
}
if (population[i+1].fitness <= worst)
{
worst = population[i+1].fitness;
worst_mem = i + 1;
}
}
else
{
if (population[i].fitness <= worst)
{
worst = population[i].fitness;
worst_mem = i;
}
if (population[i+1].fitness >= best)
{
best = population[i+1].fitness;
best_mem = i + 1;
}
}
}
/* if best individual from the new population is better than */
/* the best individual from the previous population, then */
/* copy the best from the new population; else replace the */
/* worst individual from the current population with the */
/* best one from the previous generation */

if (best >= population[POPSIZE].fitness)
{
for (i = 0; i < NVARS; i++) /*取代当前已找到最好的个体*/
population[POPSIZE].gene[i] = population[best_mem].gene[i];
population[POPSIZE].fitness = population[best_mem].fitness;
}
else
{
for (i = 0; i < NVARS; i++)/*淘汰当代中最差的个体*/
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
population[worst_mem].fitness = population[POPSIZE].fitness;
}
}
phoneix818 2012-05-28
  • 打赏
  • 举报
回复

标题写错了,应该叫无聊的人看这里
phoneix818 2012-05-28
  • 打赏
  • 举报
回复
常回家看看---恋爱篇 (转载)
发信站: BBS 水木清华站 (Thu Apr 19 20:59:44 2001)

找点儿空闲,找点儿时间
寻个MM,很希望谈谈
问问学校,看看照片
只要PP,很希望谈谈
哥哥看上去有些腼腆
妹妹却不是十分害羞
如果想脱光不要罗嗦
假如要失明就别怕麻烦
很希望谈谈,希望谈谈
哪怕是青蛙王子长得不好看
妹妹不图哥哥长相到底有多帅呀
爱上你不容易就图个有人撒赖
很希望谈谈,希望谈谈
哪怕是恐龙妹妹侏罗纪公园
哥哥不图妹妹美貌比过饭岛爱呀
搞定你不容易总图个有人做饭

标 题: 常回家看看---灌水篇 (转载)
发信站: BBS 水木清华站 (Thu Apr 19 20:59:29 2001)

找点儿空闲,找点儿时间
选个ID,常上来灌灌
喝瓶啤酒,抽根香烟
无论多晚,常上来灌灌
华健准备了一些好坑
大家回敬了一版好水
生活的烦恼不要再多说
工作的事情也不必再谈
常上来灌灌,上来灌灌
哪怕和妹妹几几歪歪聊聊天
挖坑不图是否真有多少人来灌呀
一通宵不容易就图个好好玩玩
常上来灌灌,上来灌灌
哪怕被版主封闭权限N多天
灌水不图最终版主是不是MARK呀
一晚上总熬夜只图个平平安安

muqiuyu1988 2012-05-28
  • 打赏
  • 举报
回复
#include<stdio.h>

int main()
{
printf("Hello World!");
return 0;
}
  • 打赏
  • 举报
回复
A_Zhao 2012-05-28
  • 打赏
  • 举报
回复
printf("Hello Kitty!");
1. C 语言的指针和内存泄漏 5 2. C语言难点分析整理 10 3. C语言难点 18 4. C/C++实现冒泡排序算法 32 5. C++指针和引用的区别 35 6. const char*, char const*, char*const的区别 36 7. C可变参数函数实现 38 8. C程序内存组成部分 41 9. C编程拾粹 42 10. C语言实现数组的动态增长 44 11. C语言的位运算 46 12. 浮点数的存储格式: 50 13. 位域 58 14. C语言函数二维数组传递方法 64 15. C语言复杂表达式的执行步骤 66 16. C语言字符串函数大全 68 17. C语言宏定义技巧 89 18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区溢出方法 126 25. C语言高效编程秘籍 128 26. C运算符优先口诀 133 27. do/while(0)的妙用 134 28. exit()和return()的区别 140 29. exit子程序终止函数与return的差别 141 30. extern与static存储空间矛盾 145 31. PC-Lint与C\C++代码质量 147 32. spirntf函数使用大全 158 33. 二叉树的数据结构 167 34. 位运算应用口诀和实例 170 35. 内存对齐与ANSI Cstruct内存布局 173 36. 冒泡和选择排序实现 180 37. 函数指针数组与返回数组指针的函数 186 38. 右左法则- 复杂指针解析 189 39. 回车和换行的区别 192 40. 堆和堆栈的区别 194 41. 堆和堆栈的区别 198 42. 如何写出专业的C头文件 202 43. 打造最快的Hash表 207 44. 指针与数组学习笔记 222 45. 数组不是指针 224 46. 标准C字符串分割的方法 228 47. 汉诺塔源码 231 48. 洗牌算法 234 49. 深入理解C语言指针的奥秘 236 50. 游戏外挂的编写原理 254 51. 程序实例分析-为什么会陷入死循环 258 52. 空指针究竟指向了内存的哪个地方 260 53. 算术表达式的计算 265 54. 结构体对齐的具体含义 269 55. 连连看AI算法 274 56. 连连看寻路算法的思路 283 57. 重新认识:指向函数的指针 288 58. 链表的源码 291 59. 高质量的子程序 295 60. 高C语言程序员测试必过的十六道最佳题目+答案详解 297 61. C语言常见错误 320 62. 超强的指针学习笔记 325 63. 程序员之路──关于代码风格 343 64. 指针、结构体、联合体的安全规范 346 65. C指针讲解 352 66. 关于指向指针的指针 368 67. C/C++ 误区一:void main() 373 68. C/C++ 误区二:fflush(stdin) 376 69. C/C++ 误区三:强制转换 malloc() 的返回值 380 70. C/C++ 误区四:char c = getchar(); 381 71. C/C++ 误区五:检查 new 的返回值 383 72. C 是 C++ 的子集吗? 384 73. C和C++的区别是什么? 387 74. 无条件循环 388 75. 产生随机数的方法 389 76. 顺序表及其操作 390 77. 单链表的实现及其操作 391 78. 双向链表 395 79. 程序员数据结构笔记 399 80. Hashtable和HashMap的区别 408 81. hash 表学习笔记 410 82. C程序设计常用算法源代码 412 83. C语言有头结点链表的经典实现 419 84. C语言惠通面试题 428 85. C语言常用宏定义 450

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧