呵呵,没关系,既然你喜欢这种方式,再给你写一个好了。
程序代码:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<malloc.h> int random(int max) { return ((rand() << 16)|((rand() & 1) << 15)|rand()) % max; } int main() { int n, a, c, t, g, i, j; double d; char * s, tmp; FILE *fo; srand(time(NULL)); do{ printf("输入序列长度:"); scanf("%d", &n); }while(n <= 0); do{ printf("输入A的百分占比:"); scanf("%lf", &d); a = (int)(d / 100 * n + 0.5); }while(d < 0 || a > n); do{ printf("输入C的百分占比:"); scanf("%lf", &d); c = (int)(d / 100 * n + 0.5); }while(d < 0 || a + c > n); do{ printf("输入T的百分占比:"); scanf("%lf", &d); t = (int)(d / 100 * n + 0.5); }while(d < 0 || a + c + t > n ); g = n - a - c - t; if(!(s = (char *)malloc(n + 1))) return 0; for(i = 0; a--; s[i++] = 'A'); for(; c--; s[i++] = 'C'); for(; t--; s[i++] = 'T'); for(; g--; s[i++] = 'G'); s[n] = '\0'; for(i = 0; i < n; i++) { j = random(n); tmp = s[i]; s[i] = s[j]; s[j] = tmp; } if(!(fo = fopen("out.txt", "w"))) { puts("无法创建文件out.txt\n"); free(s); return 0; } fputs(s, fo); fclose(fo); free(s); return 0; }
重剑无锋,大巧不工