Q4: a- suppose that the file (delta.txt) shown in the figure was stored in the default path, this file contains three resistors (a, b, and c separated by tabs) for unknown number of delta networks. Write a program to open this file for reading and then use a function that receives as parameter a file stream object of the file (delta.txt) and uses its information to create a new file named (star.txt); the new file is used to store the resistors (P, Q and R) after converting delta networks to star networks using the following formulas: АВ АС ВС P = A+B +C Q = A+ B+C R = A+ B +C delta.txt - Notepad File Edit Format View Help 10 20 30 20 30 40 30 10 50 20 60 50 50 10 30

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

write in c++

Q4: a- suppose that the file (delta.txt) shown in the figure was stored in the default path, this file contains
three resistors (a, b, and c separated by tabs) for unknown number of delta networks. Write a program to
open this file for reading and then use a function that receives as parameter a file stream object of the file
(delta.txt) and uses its information to create a new file named (star.txt); the new file is used to store the
resistors (P, Q and R) after converting delta networks to star networks using the following formulas:
AB
P =-
A+B+C
AC
ВС
Q =
A+ B+C
R =
A + B +C
delta.txt - Notepad
File Edit Format View Help
10
20
30
20
30
40
30
10
50
20
60
50
50
10
30
Transcribed Image Text:Q4: a- suppose that the file (delta.txt) shown in the figure was stored in the default path, this file contains three resistors (a, b, and c separated by tabs) for unknown number of delta networks. Write a program to open this file for reading and then use a function that receives as parameter a file stream object of the file (delta.txt) and uses its information to create a new file named (star.txt); the new file is used to store the resistors (P, Q and R) after converting delta networks to star networks using the following formulas: AB P =- A+B+C AC ВС Q = A+ B+C R = A + B +C delta.txt - Notepad File Edit Format View Help 10 20 30 20 30 40 30 10 50 20 60 50 50 10 30
Expert Solution
Step 1

The delta.txt file should contain value separated by spaces. Last values in each row show also have a space.

Example :

10<space>20<space>30<space><new line>

20<space>30<space>40<space><new line>

The code to implement the given requirements is as follows (comments are mentioned for better understanding of the code):

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

//Code to split the string using spaces
int *split(string resistor)
{
    int *num = new int[3];
    int k = 0;
    string temp = "";
    for (int i = 0; i < resistor.size(); i++)
    {
        if (resistor[i] != ' ')
        {
            //Adding each character till space is found
            temp += resistor[i];
        }
        else
        {
            //Converting from string type to char *
            char t[temp.size()];
            strcpy(t, temp.c_str());
            //Converting from char * to integer and storing in the array
            sscanf(t, "%d", &num[k++]);
            temp = "";
        }
    }
    //Returning the array containing the resistor values
    return num;
}

int main()
{
    fstream delta;
    //Opening the delta file to read
    delta.open("delta.txt", ios::in);
    int *num;
    int a, b, c, k = 0;
    float p, q, r;
    string temp = "";
    string output = "";
    if (delta.is_open())
    {
        string resistor;
        //Reading each line from the file
        while (getline(delta, resistor))
        {
            //Calling the split method to split the resistor values
            num = split(resistor);
            a = num[0];
            b = num[1];
            c = num[2];
            //Calculating the values of p, q and r
            p = (a * b) / (a + b + c);
            q = (a * c) / (a + b + c);
            r = (b * c) / (a + b + c);
            //Adding the result to the output string
            output += to_string(p) + " " + to_string(q) + " " + to_string(r) + "\n";
        }
    }
    delta.close();
    //Opening the star file to write
    ofstream star;
    star.open("star.txt", ios::out);
    //Printing the result to the output file
    star << output;
    star.close();
    return 0;
}

 

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY