Read file and print in specific format. C++
I have to write a program in C++ and I haven't used this laugauge before.
I know how to write it in Java or C#, but I should write it in C++!
The code should read a text file (I do this step) and then print the output in a specific format using the array (I don't now how to do this step)
For example: The file has the following:
Task distribution duration dependence
A Normal 2,10 -
B UNIF 2,7 A
The code will print the following:
The task A is a normal distribution and it is duration between 2 and 10. It doesn't depend on any task. Task B is unif distribution, etc.
I want to mention that it is not a homework or an assignment to be submitted to the teacher, but I work in a research project (something extra to learn something new before leaving the University) and they ask us to use C++ which I found to be difficult to learn (I have other courses with grades which I should consider rather than doing the research).
I write the code in C# and I searched for utilities to convert from C# to C++, but it doesn't work!
http://www.tangiblesoftwaresolutions.com/Product_Details/Instant_CPlusPlus_CSharp_Edition.html
This is my code in C# and it works correctly.
class Program
{
static List<string []> allTasks = new List<string []>();
static List<TaskInfo> Tasks = new List<TaskInfo>();
static void Main(string[] args)
{
string line;
string[] info = null;
StreamReader sr = new StreamReader("C:\\Users\\7bah Jno0ney\\Documents\\Visual Studio 2008\\Projects\\Example1_C++\\Tasks.txt");
string fLine = sr.ReadLine();
while ((line = sr.ReadLine()) != null)
{
line = line.Replace("\r", "");
info = line.Split(' ');
allTasks.Add(info);
}
for (int i = 0; i < allTasks.Count; i++)
{
for (int j = 0; j < allTasks[i].Length; j++)
{
allTasks[i][j] = allTasks[i][j].Replace("\r", "");
allTasks[i][j] = allTasks[i][j].Replace(" ", "");
}
TaskInfo task = new TaskInfo(allTasks[i][0], allTasks[i][1], allTasks[i][2], allTasks[i][3]);
Tasks.Add(task);
}
Console.WriteLine("You have " + Tasks.Count + " Tasks.");
Console.WriteLine();
for (int i = 0; i < Tasks.Count; i++)
{
if (Tasks[i].getTaskDep() == null || Tasks[i].getTaskDep() == "-")
{
Console.WriteLine("The first task in the project is the task " + Tasks[i].getTaskName() +". " +
"The task distribution is " + Tasks[i].getTaskDis() + " and its duration is " + Tasks[i].getTaskDur() + "." + " Finally, it doesn't has any dependence");
Console.WriteLine();
}
else
{
string name = Tasks[i].getTaskName();
string dep = Tasks[i].getTaskDep();
if(dep.Contains(","))
{
string[] depends = dep.Split(',');
Console.Write("Task " + Tasks[i].getTaskName() + " has " + Tasks[i].getTaskDis() +
" distribution and its duration is " + Tasks[i].getTaskDur() + ". ");
Console.Write("This task depends on " + depends.Length + " tasks.");
for (int d = 0; d < depends.Length; d++)
{
int num = d + 1;
Console.Write(" The " + num + " depended task is " + depends[d] + ". ");
}
Console.WriteLine();
Console.WriteLine();
}
else
{
foreach (TaskInfo ti in Tasks)
{
开发者_StackOverflow if((ti.getTaskName() == name) && (ti.getTaskDep() == dep)){
Console.WriteLine("This Task is the " + i +
" task and its distribution is " + Tasks[i].getTaskDis() +
". It is duration is " + Tasks[i].getTaskDur() +
" and it depends on the task " + ti.getTaskDep());
Console.WriteLine();
}
}
}
}
}
}
}
What are some utilities or programs to help to converting the code?
For C++ probably what you should be looking at is using a file stream (std::ifstream
most likely) for the reading, and the standard output stream (std::cout
) for writing. Given the lack of detail, the best I can tell you after that is to go read up on C++ file streams and stream formatting. Here's a tutorial I found online for file streams, and here's one for stream formatting.
I recommend using one of two routes, C++ streams or printf
of C fame.
For C++ streams, have a look at the I/O Manipulators library, especially these functions:
setw
setprecision
left
right
For the printf
technique, look at the format specifiers:
%d
%c
%s
I highly recommend using spaces instead of tabs. There is no standard tab size for consoles.
If you are dealing with a GUI, I recommend either using a table widget or using font metrics to get accurate alignment. You could cheat and use a fixed pitch font, but the correct and laborious method is to calculate the width of each character, then determine the spacing necessary. Or just put the text a fixed horizontal location.
精彩评论