-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog5.c
86 lines (73 loc) · 1.59 KB
/
prog5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define DATA_NUM 100
struct let_data
{
int rank;
long int freq;
double prob;
int id;
char let[3];
};
int print_char(int n);
int main(){
FILE *fp1;
char c1[10],c2[10],c3[10],c4[10],c5[10];
int i,j,max_let,tmp;
int let_list[1300000];
long int mam;
struct let_data data[DATA_NUM];
srand((unsigned int)time(NULL));
for(i=0;i<DATA_NUM;i++){
data[i].let[0]=0;data[i].let[1]=0;data[i].let[2]=0;
}
if((fp1 = fopen("rslprog2","r"))==NULL){
fprintf(stderr, "Can not open txte file\n");
return -1;
}
fscanf(fp1,"%s,%s,%s,%s,%s",c1,c2,c3,c4,c5);
i=0;
do{
fscanf(fp1,"%d,%ld,%lf,%d,%s",&data[i].rank,&data[i].freq,&data[i].prob,&data[i].id,data[i].let);
mam+=data[i].freq;
}while(data[i++].freq!=0);
max_let=i;
j=0;tmp=0;
for(i=0;i<max_let;i++){
for( ; j < tmp + data[i].freq; j++){
let_list[j]=data[i].id;
}
tmp=j;
}
printf("%ld\n",mam);
for(i=0;i<100;i++){
print_char(let_list[rand()%mam]);
}puts(" ");
fclose(fp1);
return 0;
}
int print_char(int n){
if(n==0){
printf("\n");
}
if(n==1){
printf(" ");
}
if((n>=2)&&(n<=27)){
printf("%c",n+63);
}
if((n>=28)&&(n<=53)){
printf("%c",n+69);
}
if((n>=54)&&(n<=117)){
printf("%c%c",-61,n-182);
}
if((n==118)||(n==119)){
printf("%c%c",-59,n-228);
}
if(n==120){
printf("%c%c%c",-30,128,153);
}
}