Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can any one tell me how to print that upper codes in that format ???????
where i can change format ???
this is my code....

C++
#include "stdafx.h"
#include <stdio.h>
#include <math.h>

FILE *pg;
double input_status, d,r0,q0,r,q,r1,q1,total_perimeter=0;
int t=0;
double dtor = 3.14159265358979 / 180.0; //<<---- notice 
int main() 
{
     errno_t err;
     // Open for read (will fail if file "input.txt" does not exist)
     err = fopen_s(&pg, "input.txt", "r");
     if (err == 0)
        printf("The file 'input.txt' was opened\n\n");
     else
     {
        printf("The file 'input.txt' was not opened\n");
        return 0; //exit the program
     }
     //fopen_s(&pg , "input.txt" , "r");   //you really do not need this line
     input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
     r0 = r; q0 = q;  //save the first point for later to close the polygon
     if(input_status==EOF)
     {
        printf("There are no data points in the file");
        return 0; //exit the program
     }
     printf("point %d: r=%lf , q=%lf\n", t, r, q);
     do
     {
          input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
          if(input_status==EOF) 
               break; //if no more points exit the loop
          t++;
          printf("point %d: r=%lf , q=%lf\n", t, r1, q1);
          d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
          printf("distance between %d and %d = %lf \n\n",(t-1),t,d);
          r = r1; q = q1;  //make the previous point become the current point 
          total_perimeter += d; //accumulate the perimeter             
     }while(1);
     r1 = r0; q1 = q0;  //make the new point become the first point to close the polygon
     d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
     printf("distance between %d and 0 = %lf \n\n\n",t,d);
     total_perimeter += d;  //accumulate the perimeter
     printf("Total Perimiter = %lf\n",total_perimeter);     
     return 1; 
}


this code give me this output
point 1: r=3.000000 , q=180.000000
point 2: r=4.472136 , q=116.565000 
distance between 1 and 2 = 4.123108 
accum 4.123108 

 point 3: r=5.200961 , q=268.898300 
distance between 2 and 3 = 9.394148 
accum 13.517256 

 point 4: r=5.003998 , q=87.709400 
distance between 3 and 4 = 10.204410 
accum 23.721666 

 point 5: r=4.716990 , q=57.994620 
distance between 4 and 5 = 2.507988 
accum 26.229654 

 point 6: r=4.924429 , q=23.962490 
distance between 5 and 6 = 2.828427 
accum 29.058081 

 point 7: r=6.403124 , q=308.659800 
distance between 6 and 7 = 7.017835 
accum 36.075916 

 point 8: r=5.200961 , q=268.898300 
distance between 7 and 8 = 4.104874 
accum 40.180790 

 distance between 8 and 1 = 5.953990 
accum 46.134780 


 Total Perimiter = 46.134780


i want to print output like this
Point 1: r = 3.000000; q = 180.000000
Point 2: r = 4.472136; q = 116.565000
Point 3: r = 5.003998; q = 87.709400
…………..
Sides:
1 to 2: 17.000021 Accumulated perimeter: 17.000021
2 to 3: 6.201378 Accumulated perimeter: 23.201399
……..
Total perimeter: 117.234534
Posted
Updated 17-Dec-15 1:00am
v2
Comments
Andreas Gieriet 17-Dec-15 7:01am    
What exactly is the problem? Is it the algorithm that you do not know how to adapt or is it lack of C++ knowhow, or anything else?

What did you try so fat and where do you struggle?
Cheers
Andi

PS: We do not do your homework assignments.
Member 12202551 17-Dec-15 7:06am    
that hole code is written by me and i took help from codeprojects members (if u see my others ques ans )
and i think, i wrote my ques clearly....... my output are correct ..... i just want to print my output like this in this format
Point 1: r = 3.000000; q = 180.000000
Point 2: r = 4.472136; q = 116.565000
Point 3: r = 5.003998; q = 87.709400
…………..
Sides:
1 to 2: 17.000021 Accumulated perimeter: 17.000021
2 to 3: 6.201378 Accumulated perimeter: 23.201399
……..
Total perimeter: 117.234534
i want to know where i make change in codes

please read question carefully
thanks
Andreas Gieriet 17-Dec-15 7:53am    
I read your question carefully. It does not make sense to me. What is "upper codes in that format"? And "where I can change format" is too broad of a "question". A similarly broad answer was: "in the code".
Sorry for my sarcasm, but please try to ask a precise answer before telling me to read carefully...
Cheers
Andi
PS: You tagged as C++, but this is plain C code. You might consider to change the tag to C.
Member 12202551 17-Dec-15 7:21am    
don't see the numbers ...... those are just guess

You need to adjust your print statements according to your requirements. If you want to print Point rather than point, then check the format string content is correct. If you want to print each item on a separate line, then ensure you put a newline character (\n) at the end of the format string. For further information I suggest you study https://msdn.microsoft.com/en-us/library/wc7014hz.aspx[^].
 
Share this answer
 
Comments
Member 12202551 17-Dec-15 20:26pm    
thanks for your help ...
The logic of your program changes significantly if you change from logging-as-you-go to logging-by-function.

I.e. the original program reads the records one after the other and reports all actions immediately. No need to store any values.

If you now want to report in a different sequence that you read, you better load the data first into some appropriate structure and than work on these.

Since you tagged your post as C++, attached please find a possible solution in C++.

Cheers
Andi
C++
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>

using namespace std;

typedef float length_t;
typedef float angle_deg_t;
typedef float angle_rad_t;

class PointAsPolar {
private:
	length_t    _radius;
	angle_deg_t _angleInDeg;
public:
	PointAsPolar()
        : _radius(0.0), _angleInDeg(0.0)
        {}
	PointAsPolar(length_t radius, angle_deg_t angleInDeg)
        : _radius(radius), _angleInDeg(angleInDeg)
        {}
	length_t calcX() const { return length_t(_radius * cos(calcAngleInRad())); }
	length_t calcY() const { return length_t(_radius * sin(calcAngleInRad())); }
	length_t getRadius() const { return _radius; }
	angle_deg_t getAngleInDeg() const { return _angleInDeg; }
	angle_rad_t calcAngleInRad() const { 
             return angle_rad_t(_angleInDeg * 3.14159265358979323846 / 180.0);
        }
	length_t calcDistance(const PointAsPolar& other) const {
		length_t dx = other.calcX() - calcX();
		length_t dy = other.calcY() - calcY();
		return sqrt(dx*dx + dy*dy);
	}
	friend istream& operator>> (istream& input, PointAsPolar& p);
};
inline istream& operator>> (istream& input, PointAsPolar& p) {
	return input >> p._radius >> p._angleInDeg;
}
inline ostream& operator<< (ostream& output, const PointAsPolar& p) {
    return output << "r = "
                  << p.getRadius()
                  << ", q = "
                  << p.getAngleInDeg()
                  << " (x = "
                  << p.calcX()
                  << ", y = "
                  << p.calcY()
                  << ")";
}

class Polygon {
private:
	vector<PointAsPolar> _points;
public:
	Polygon(istream& input)
        { PointAsPolar p; while(input >> p) _points.push_back(p); }
	size_t size() const
        { return _points.size(); }
	const PointAsPolar& operator[](size_t pos) const
        { return _points[pos % size()]; }
};
inline ostream& operator<< (ostream &output, const Polygon &poly) {
	size_t n = poly.size();
	for(size_t i = 0; i < n; ++i) {
            output << "Point "
                   << (i+1)
                   << ": "
                   << poly[i]
                   << endl;
        }
	return output;
}


int main()
{
	Polygon poly(ifstream("input.txt"));
	cout << poly;

	cout << "Sides:" << endl;
	length_t perimeter = 0.0;
	size_t n = poly.size();
	for(size_t i = 0; i < n; ++i) {
		length_t distance = poly[i].calcDistance(poly[i+1]);
		perimeter += distance;
		cout << i+1
                     << " to "
                     << (((i+1)%n)+1)
                     << ": "
                     << distance
                     << " Accumulated perimeter "
                     << perimeter
                     << endl;
	}

	cout << "Total perimeter: " << perimeter << endl;
}
The resulting output is
Point 1: r = 3, q = 180 (x = -3, y = -2.62268e-007)
Point 2: r = 4.47214, q = 116.565 (x = -2, y = 4)
Point 3: r = 5.20096, q = 268.898 (x = -0.1, y = -5.2)
Point 4: r = 5.004, q = 87.7094 (x = 0.199999, y = 5)
Point 5: r = 4.71699, q = 57.9946 (x = 2.5, y = 4)
Point 6: r = 4.92443, q = 23.9625 (x = 4.5, y = 2)
Point 7: r = 6.40312, q = 308.66 (x = 4, y = -5)
Point 8: r = 5.20096, q = 268.898 (x = -0.1, y = -5.2)
Sides:
1 to 2: 4.12311 Accumulated perimeter 4.12311
2 to 3: 9.39415 Accumulated perimeter 13.5173
3 to 4: 10.2044 Accumulated perimeter 23.7217
4 to 5: 2.50799 Accumulated perimeter 26.2297
5 to 6: 2.82843 Accumulated perimeter 29.0581
6 to 7: 7.01783 Accumulated perimeter 36.0759
7 to 8: 4.10487 Accumulated perimeter 40.1808
8 to 1: 5.95399 Accumulated perimeter 46.1348
Total perimeter: 46.1348
 
Share this answer
 
v2
Comments
Member 12202551 17-Dec-15 20:27pm    
thanks a lot for this help .......
Andreas Gieriet 18-Dec-15 1:27am    
You are welcome.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900