convert a program from Java to C++ Need help converting this program from java t
ID: 3599007 • Letter: C
Question
convert a program from Java to C++
Need help converting this program from java to c++
// include file reading libraries
import java.io.*;
import java.util.LinkedList; // include linked list
public class Packet4 {
// main method -- similar to int main() in C++
public static void main(String[] args) throws IOException
{
// file reading code
File dataFile = new File("data.txt");
FileInputStream stream = new FileInputStream(dataFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = null;
// allocate linked list
LinkedList<String> packetsList = new LinkedList<>();
// read line by line. loop is done when end of file is reached
while ((line = reader.readLine()) != null)
{
// allocate a node, put stuff in there, then insert into the list
packetsList.add(line);
}
int nextExpected = 1; // the lowest sequence number in data.txt is 1
String previousPacketData = "";
// while nextExpected is not greater than list size
while (nextExpected <= packetsList.size())
{
// loops through all packets in list trying to find next-expected
for (int i = 0; i < packetsList.size(); i++)
{
String packet = packetsList.get(i); // get packet from list. does NOT remove it from list
String sequenceNumberStr = packet.substring(0,4); // first 4 characters
// remove spaces e.g. converts " 23" to "23"
sequenceNumberStr = sequenceNumberStr.trim();
// convert to integer
int sequenceNumberInt = Integer.parseInt(sequenceNumberStr);
// check if packet is the next expected packet
if (nextExpected == sequenceNumberInt)
{
String packetData = packet.substring(4,16); // character 5 - 16
packetData = packetData.replace("=+", " ");
// Gotcha: sometimes the = and the + are in different packets
if (previousPacketData.endsWith("=") && packetData.startsWith("+"))
{
previousPacketData = previousPacketData.replace("=", "");
packetData = packetData.replace("+", " ");
}
System.out.print(previousPacketData); // print the previous packet data
// save packet for later printing, and for the 'gotcha' situation
previousPacketData = packetData;
nextExpected = nextExpected + 1; // increment next-expected counter
}
}
}
System.out.print(previousPacketData); // print the last piece
reader.close();
}
}
//input file
----------
4rnighan - .
9is a compute.
26ls or GOTO's.
79ibraries.=+=.
20acilities=+s.
67the first=+s.
24n the page, .
2ion to C - .
57efinitions. .
63ave a main. .
6ories, Murra.
41ilities.=+=+.
40ucturing fac.
60, and in fac.
84ons is by ar.
10r language a.
5Bell Laborat.
71invoke other.
95nts, indicat.
25without labe.
82ting data be.
8 Jersey. C .
13. C lets yo.
7y Hill,=+New.
3 Brian W. Ke.
52or the proce.
18as decent co.
29e that is co.
1An Introduct.
22can be read .
80+One method .
21o your code .
44one or more .
47ilar to the=.
81of communica.
98e the statem.
86e=+parenthes.
43consists of .
68tatement of .
38 provides go.
90und the argu.
99ents=+of the.
46hich are sim.
49nd subroutin.
88 the functio.
92ere main=+is.
103d with a sem.
102tatements en.
37tion; and it.
101Individual s.
72 functions t.
96ed by ( ). T.
45functions, w.
69main. main w.
27;=+it lets y.
73o perform it.
53dures of PL/.
16learly and s.
42A C program .
51ran program .
77ogram, and o.
74s=+job, some.
94of no argume.
12many systems.
64 Execution o.
11vailable on .
93 a function .
87es following.
78thers from l.
31t being too .
28ou write cod.
35nd good prog.
91ment list; h.
75 coming from.
59h a function.
97he {} enclos.
62grams must h.
14u=+write you.
30mpact withou.
23straight dow.
17imply - it h.
76 the same pr.
65f the progra.
19ntrol flow f.
106=+free-forma.
54I, and=+perh.
61t=+all C pro.
89n name surro.
56ernal data d.
50es of a Fort.
33+encourages .
105re otherwise.
15r programs c.
66m begins at .
32cryptic; it=.
104icolon but a.
85guments. Th.
83tween functi.
39od=+data-str.
48+functions a.
100 function. .
107t.=+=+ .
58 main is suc.
55aps some ext.
70ill usually .
34modularity a.
36ram organiza.
Explanation / Answer
please find the code below for any clarifications comment below. Happy to help you.
using System;
using System.Collections.Generic;
public class Packet4
{
// main method -- similar to int main() in C++
//ORIGINAL LINE: public static void main(String[] args) throws IOException
public static void Main(string[] args)
{
// file reading code
File dataFile = new File("data.txt");
FileInputStream stream = new System.IO.FileStream(dataFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
BufferedReader reader = new System.IO.StreamReader(stream);
string line = null;
// allocate linked list
LinkedList<string> packetsList = new LinkedList<string>();
// read line by line. loop is done when end of file is reached
while (!string.ReferenceEquals((line = reader.ReadLine()), null))
{
// allocate a node, put stuff in there, then insert into the list
packetsList.AddLast(line);
}
int nextExpected = 1; // the lowest sequence number in data.txt is 1
string previousPacketData = "";
// while nextExpected is not greater than list size
while (nextExpected <= packetsList.Count)
{
// loops through all packets in list trying to find next-expected
for (int i = 0; i < packetsList.Count; i++)
{
string packet = packetsList.get(i); // get packet from list. does NOT remove it from list
string sequenceNumberStr = packet.Substring(0,4); // first 4 characters
// remove spaces e.g. converts " 23" to "23"
sequenceNumberStr = sequenceNumberStr.Trim();
// convert to integer
int sequenceNumberInt = int.Parse(sequenceNumberStr);
// check if packet is the next expected packet
if (nextExpected == sequenceNumberInt)
{
string packetData = packet.Substring(4, 12); // character 5 - 16
packetData = packetData.Replace("=+", " ");
// Gotcha: sometimes the = and the + are in different packets
if (previousPacketData.EndsWith("=", StringComparison.Ordinal) && packetData.StartsWith("+", StringComparison.Ordinal))
{
previousPacketData = previousPacketData.Replace("=", "");
packetData = packetData.Replace("+", " ");
}
Console.Write(previousPacketData); // print the previous packet data
// save packet for later printing, and for the 'gotcha' situation
previousPacketData = packetData;
nextExpected = nextExpected + 1; // increment next-expected counter
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.