-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprb022.cpp
executable file
·140 lines (129 loc) · 2.68 KB
/
prb022.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Names scores
Problem 22
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
*/
#include<iostream>
#include<string>
#include<fstream>
#include<cstdio>
#include<string.h>
using namespace std;
void sort(char name[6000][15],int count);
void calc(char name[6000][15],int count);
int main()
{
char name[6000][15],temp;
int i=0,j,k,index=0,flag=0,highest=0,count=0;
ifstream f1;
f1.open("prb022_names.txt");
if(!f1)
cout<<"\nFile opening error";
while(1)
{
//cout<<"\nInitiating loop....";
f1.get(temp);
if(f1.eof())
break;
if(temp==',')
continue;
else if((temp<='Z' && temp>='A') && flag==1)
name[index][i++]=temp;
else if(temp=='\"' && flag==0)
flag=1;
else if(temp=='\"' && flag==1)
{
flag=0;
name[index][i]='\0';
i=0;
index++;
}
}
cout<<"\nTotal records found : "<<index;
f1.close();
sort(name,index);
calc(name,index);
}
void sort(char name[6000][15],int count)
{
int min,index,i,j;
char temp[15];
for(i=0;i<count-1;i++)
{
min=100;
for(j=i+1;j<count;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
}
void calc(char name[6000][15],int count)
{
int i,sum,l,j,rem,k,carry,endf,endt;
char feed[20],total[100];
for(i=0;i<99;i++)
{
total[i]=0;
}
for(i=0;i<20;i++)
{
feed[i]=0;
}
for(i=0;i<count;i++)
{
sum=0;
l=strlen(name[i]);
for(j=0;j<l;j++)
{
sum=sum + name[i][j] -64;
}
cout<<"\ni= "<<i<<" name[i]="<<name[i];
cout<<"\nSum before multiplication : "<<sum;
sum=sum*(i+1);
cout<<"\nSum after multiplication : "<<sum;
k=0;
while(sum>0)
{
rem=sum%10;
rem=rem+48;
feed[k++]=rem;
sum=sum/10;
}
feed[k]=0;
cout<<endl<<feed;
if(strlen(total)>strlen(feed))
l=strlen(total);
else
l=strlen(feed);
endf=1;
endt=1;
carry=0;
for(j=0;j<l || carry!=0;j++)
{
if(total[j]==0)
endt=0;
if(feed[j]==0)
endf=0;
carry = carry + (total[j]-48)*endt + (feed[j]-48)*endf;
total[j] = (carry%10)+48;
carry=carry/10;
}
total[j]=0;
cout<<"\nTotal="<<total;
}
l=strlen(total);
for(i=0;i<l/2;i++)
{
feed[0]=total[i];
total[i]=total[l-i-1];
total[l-i-1]=feed[0];
}
cout<<"\nTotal : "<<total;
}