學生信息:
1 擊2 主3 楊4 牛5 更6 呀7 主8 啊9 哦10 丫三次分組結果:第1組:2 主9 哦第2組:1 擊4 牛6 呀8 啊第3組:3 楊5 更7 主10 丫**************************第1組:2 主4 牛6 呀10 丫第2組:5 更8 啊第3組:1 擊3 楊7 主9 哦************************第1組:4 牛6 呀9 哦第2組:2 主7 主第3組:1 擊3 楊5 更8 啊10 丫實現代碼:/* * 目的: *從文件中讀取學生,并進行分組。 */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <unistd.h>static int student_number = 0;//學生個數默認為0人static int student_group = 0;//分多少組 默認0組static int student_group_number = 3; //每組人數默認3人//用于分割信息static const char sign_d = ';';static const char sign_e = '@';static char s[2048] = {}; //存放數據信息static char temp[1024] = {}; //緩沖數據static int rand_seed = 0; //防止隨機數,生成嚴重重復typedef struct data{ char *title; char *content; int n; //表示已存放的學生數(棄用) struct data *p;}DATA;void read_count(FILE *fp) //讀取學生信息,保存進 s[]中{ char temp; int i = 0; while((temp = fgetc(fp)) != EOF) { if(temp == ' ') { s[i] = sign_d; i++; continue; } if(temp == '/n') { s[i] = sign_e; i++; student_number++; continue; } s[i] = temp; i++; } student_number++; s[i] = '#';//#標記結束末尾符號}void group(void) //計算出要分幾組{ if(student_number < student_group_number) { student_group = 1; return; } student_group = student_number / student_group_number;}int random_n(int x, int y) //隨機數生成{ srand((unsigned)time(NULL) + rand_seed); rand_seed += 10; int k = x + rand()%(y - x + 1); return k;}DATA *handle(void) //生成一個存放了學生信息的鏈表{ DATA *head = (DATA *)malloc(sizeof(DATA)); DATA *p = head; for(int i = 0; i < student_group; i++) { DATA *node = (DATA *)malloc(sizeof(DATA)); node->title = (char *)malloc(sizeof(char) * 50); node->content = (char *)malloc(sizeof(char) * 1024); sPRintf(temp, "第%d組:", i + 1); strcpy(node->title, temp); memset(temp, 0, sizeof(temp)); node->p = NULL; node->n = 0; head->p = node; head = node; } return p;}void print(DATA *head) //打印信息并錄入{ FILE *fp = fopen("student2.txt", "w"); head = head->p; while(NULL != head) { printf("%s/n%s/n", head->title, head->content); fprintf(fp, "%s/n%s/n", head->title, head->content); //將最終得到的數據錄入文件中 head = head->p; }}void get_student(int n) //獲取一個學生{ int i = 0, j = 0, k = 0; while(j != n - 1) { if(s[i] == sign_e) { i++; j++; continue; } i++; } while(s[i] != sign_e && s[i] != '#') { if(s[i] != sign_d) temp[k] = s[i]; else temp[k] = ' '; k++; i++; }}int temp_r = 0; //保存上一個隨機數與新生成的隨機數是否一樣void student_group_stack(DATA *head) //進行學生分組{ DATA *p = head; int k = random_n(1, student_group); if(temp_r != k) temp_r = k; else while(1) { k = random_n(1, student_group); if(temp_r != k) { temp_r = k; break; } } for(int i = 0; i < k; i++) p = p->p; strcat(p->content, temp); strcat(p->content, "/n");}int main(int argc, char *argv[]){ FILE *fp = fopen("student.txt", "r"); read_count(fp); group(); DATA *head = NULL; head = handle(); for(int i = 0; i < student_number; i++) { memset(temp, 0, sizeof(temp)); get_student(i+1); //printf("%s/n", temp); student_group_stack(head); } print(head); return 0;}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
設定為3人一組,程序運行結果無法100%與預期結果一樣。 需要進行條件限制。
DATA結構體中n代表一組里已有多少學生, 如該n 等于 3(默認一組的人數),就停止向本組分配學生。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
新聞熱點
疑難解答