VBA/Pascal to C++

Hello, I recently did programming at school and what I thought was C was actually VBA/Pascal. I tried to change the VBA/Pascal to C++ but I couldn't does anybody know how to change this:

Any 1 code into c++. Never mind VBA or Pascal

VBA:
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
Sub P41()
 Const Z = 26
 Dim P(Z) As pr, tmp As pr, s As String, j As Integer, _
 K As Integer, c As String, i As Integer
 For K = 1 To Z 'we result in a standard data type pr
  P(K).nm = Chr(Asc("a") + K - 1)
  P(K).res = 0
 Next K
 Open "c:\Data\P41.txt" For Input As #1
 Do While True
  Line Input #1, s
  If s = "*" Then Exit Do
  s = LTrim(s) + " "
  Do While Len(Trim(s)) > 0
   c = LCase(Mid(s, 1, 1))
   K = Asc(c) - Asc("a") + 1
   P(K).res = P(K).res + 1
   j = InStr(1, s, " ")
   s = LTrim(Mid(s, j + 1))
 Loop
 Loop
 For i = 1 To Z - 1
  For j = 1 To Z - i
   If P(j).res < P(j + 1).res Then
    tmp = P(j)
    P(j) = P(j + 1)
    P(j + 1) = tmp
   End If
  Next j
 Next i
 For K = 1 To Z
  If P(K).res > 0 Then
   Debug.Print P(K).nm & " " & P(K).res
  End If
 Next K
 Close #1
End Sub 


Pascal
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
program c4;
 
const delta = ord('a')-1;
 
var sum: array[1..26] of integer;
    letters: array[1..26] of char;
    s:string;
    i,j,k,tmp:integer;
    c:char;
 
begin
 for i:=1 to 26 do
 begin
     sum[i] := 0;
     letters[i] := chr(ord(i)+delta);
 end;
 s:='';
 while s<>'*' do
 begin
    s := ' '+s;
    for i:=1 to length(s)-1 do
    begin
       if (s[i]=' ') and (s[i+1]<>' ') then
          inc(sum[ord(s[i+1])-delta]);
    end;
    readln(s);
 end;
 for k:=1 to 26 do
     for j:=1 to 26-k do
     begin
        if sum[j]<sum[j+1] then
        begin
           tmp:=sum[j];
           sum[j]:=sum[j+1];
           sum[j+1]:=tmp;
           c:=letters[j];
           letters[j]:=letters[j+1];
           letters[j+1]:=c;
        end;
     end;
 i:=1;
 for i:=1 to 26  do
 begin
     if sum[i]>0 then
        writeln(letters[i],' ', sum[i]);
 end;
 readln;
end;


into C++.
Thank you in advanced.
Last edited on
You "did programming at school" with VBA / Pascal?
In other words, you know those languages (somewhat).

Therefore, you can explain what these programs actually do.
The algorithm is more important than the language details.

Once you know what the program has to do, we can start to think how does one do it in C++.
"You need to write a text analysis program. At the input of the program are given lines containing English words. In one line there can be an arbitrary number of words. All words are written in small (small) English letters. Between words in a line there can be one or more spaces, there may be spaces at the beginning and end of the line. Other characters, except for lowercase English letters and spaces, are not in the lines. The length of each line does not exceed 200 characters. The number of rows is unknown, the total number of words is not more than one million. The end of the input is indicated by a line containing a single character "*".

Write an effective, including a memory, program that will determine the number of words starting with each letter of the English alphabet and output these quantities and the corresponding letters in descending order. If the number of words starting with any letters is the same, these letters should be displayed in alphabetical order. If there is no letter for any letter, you do not need to output this letter.

The size of the memory that your program uses should not depend on the size of the source list.

Before the text of the program, briefly describe the algorithm of solving the problem used by you and indicate the programming language and version used.

Example input:

one two three four five

a quick brown fox

*

Example output for the above input example:

f 3

t 2

a 1

b 1

about 1

q 1

Note. The English alphabet is the same as Latin and contains 26 letters from a to z:

abcdefghijklmnopqrstuvwxyz"
Plain lowercase words, whitespace, and word "*".

Formatted C++ input skips prefix whitespace and stores the following word. That seems perfect. Read one word at a time and account its initial.

A table of letters and their counts could be done with std::map.

A map contains (key,value) pairs and is sorted by the key. One can copy the pairs into a std:vector and then std::sort them.


On the other hand we do know that there are 26 pairs and one could precreate such vector, like the VBA and Pascal versions do. No map required.
you can probably convert pascal directly to c++. Its not fun, but pascal is really compiled pseudocode based off procedural c++ type languages. begin and end become {}. procedure becomes void function, function becomes typed function, stuff like that. Its not that different. Null is nil.

basic is a whole nother mess. A re-write is in order.

I would bet you could even make a pascal to C translater with yacc or lex or something like those (or whatever the modern version is).

Last edited on
closed account (E0p9LyTq)
A Pascal to C translator, source written in C
https://schneider.ncifcrf.gov/p2c/
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
#include <iostream>
#include <algorithm>

struct Letter {
    static char ch;
    char letter;
    int count;
    Letter() : letter(ch++), count(0) {}
    bool operator<(Letter& t) {
        return count == t.count ? letter < t.letter : count > t.count;
    }
};

char Letter::ch = 'a';

int main() {
    Letter stats[26];
    char word[250];
    while (std::cin >> word)
        stats[word[0] - 'a'].count++;
    std::sort(stats, stats + 26);
    for (int i = 0; i < 26; i++)
        if (stats[i].count > 0)
            std::cout << stats[i].letter << ". " << stats[i].count << '\n';
}

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

using PR = pair<char,int>;
bool greaterThan( PR a, PR b ) { return ( a.second > b.second || ( a.second == b.second && a.first < b.first ) ); }

int main()
{
//   ifstream in( "data.txt" );
     stringstream in( "one two three four five\n"
                      "a quick brown fox\n"
                      "*" );
     map<char,int> freq;
     string word;
     while ( in >> word && word[0] != '*' ) freq[word[0]]++;
     vector<PR> letters( freq.begin(), freq.end() );
     sort( letters.begin(), letters.end(), greaterThan );
     for ( PR e : letters ) cout << e.first << " " << e.second << '\n';
}

f 3
t 2
a 1
b 1
o 1
q 1
Thanks for all this =D
Why not use p2c? Anyone used that recently?
Topic archived. No new replies allowed.