Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all 3021 articles
Browse latest View live

How to raise to square a number whit hundreds of thousands decimal places?

$
0
0
Hi...

I am new in the matter of programming...

I have a source code to calculate the number Phi with hundreds of thousands of decimal places, but I want to square that number and I don't know how to do that.

This is the source code:

Code:

//Phi Calculator based on square root of 5
//Tested with gcc version 4.8.4
//To compile from terminal do: "g++ -o phi phi-calculator.cpp"
#include <iostream>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <algorithm>

using namespace std;
#define MAX 1001000
//Maximum Decimal Place

struct bignum{
        int num[MAX];
        int len;
        bignum(){
                for(int i=0;i<MAX;i++)
                        num[i]=0;
                len=0;
                //this function clears the number during initialization of it
        }
};
//a bignum structure is used to carry big number's data!

bignum ans,divi,buff,buff2,phi;
//ans div buff buff2 phi are 5 big numbers!

int DecPlace;
//used to carry decimal place data which user enters
void calc();
//this function calculates square root of 5
void multiply(bignum *in,bignum *out,int coef);
//this function multiplies Big number 'in' to coef and saves it in big number 'out'
bool isbigger(bignum *a,bignum *b);
//this function checks whether a > b or not
void diff(bignum *a,bignum *b,bignum *ans);
//this function returns | a - b | in ans which is a big number
void phifinalize();
//this function adds 1 to square root of five then divides it to 2 to calculate Phi

//this function cleans whole stored data of a BigNumber
void clean(bignum *a){
        for(int i=0;i<DecPlace+2;i++){
                a->num[i]=0;
                //for every index
        }
        a->len=0;
        //length of number must be zero
}

int main(){
        ans.num[0]=2;
        ans.len=1;
        //first number of SQRT is entered manually
        divi.num[0]=1;
        divi.len=3;
        cout<<"Phi calculator\nbased on square root of 5\n";
        cout<<"Please enter maximum decimal place(s): ";
        //Details which will be shown in the beginning of the program
        cin>>DecPlace;
        //gets decimal place data
        cout<<"\nCalculating the first "<<DecPlace<<" decimal place(s):"<<endl;
        clock_t start,end;
        start=clock();
        //Clock initialization to find overall time
        reverse(divi.num,divi.num+divi.len);
        calc();
        //SQRT(5) calculation
        cout<<"First "<<DecPlace<<" Decimal Place(s) of 5's square root is calculated"<<endl;
        phifinalize();
        //Phi calculation
        cout<<"\nPhi calculation is done!"<<endl;
        end=clock();
        //Clock stops
        double t1;
        t1=end-start;
        double dif=(t1)/CLOCKS_PER_SEC;
        cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
        for(int i=0;i<phi.len;i++){
                if(i==1)
                        cout<<".";
                cout<<phi.num[phi.len-i-1];
        }
        //shows the result which is Phi
        cout<<"\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
        cout<<"Produced in "<<setprecision(3)<<fixed<<dif<<" Seconds"<<endl;
        //Shows the overall time
        cout<<"Save in a file?(y|n): ";
        char in;
        cin>>in;
        if(in=='y'){
                cout<<"Enter file name: ";
                string name;
                cin>>name;
                char buff[200];
                for(unsigned int i=0;i<name.length();i++){
                        buff[i]=name[i];
                }
                buff[name.length()]='.';
                buff[name.length()+1]='t';
                buff[name.length()+2]='x';
                buff[name.length()+3]='t';
                buff[name.length()+4]='\0';
        ofstream fout;
                fout.open(buff);
                fout<<"Phi produced by this calculator"<<endl;
                fout<<"Representing the first "<<DecPlace<<" decimal place(s) of Phi"<<endl;
        fout<<"Produced in "<<setprecision(3)<<fixed<<dif<<" second(s)"<<endl;
                fout<<"______________________________________________________"<<endl;
                for(int i=0;i<phi.len;i++){
                        if(i==1)
                                fout<<".";
                        fout<<phi.num[phi.len-i-1];
                }
                fout<<"\n______________________________________________________"<<endl;
                cout<<name<<".txt saved successfully!"<<endl;
        }
time_t s,e;
time(&s);
time(&e);
//Waits for 3 seconds before closing the app
while(difftime(e,s)<3)
        time(&e);
return 0;       
}

void calc(){
        for(int found=0;found<DecPlace;found++){
        //For each decimal place to find
                buff.len=0;
                buff2.len=0;
                //Cleans first and second buffer
                multiply(&ans,&buff,20);
                //Multiplies ans to 2 and saves it in buff
                int best=0;
                while(best<9){
                //Findes best number to multply to(instead of writing Divide function :D)
                        buff.num[0]=best+1;
                        buff.len=buff.len;
                        multiply(&buff,&buff2,best+1);
                        if(isbigger(&divi,&buff2))
                                best++;
                        else
                                break;
                }
        buff.num[0]=best;
        multiply(&buff,&buff,best);
        diff(&divi,&buff,&divi);
        reverse(divi.num,divi.num+divi.len);
        divi.len+=2;
        reverse(divi.num,divi.num+divi.len);
        reverse(ans.num,ans.num+ans.len);
        ans.num[ans.len]=best;
        ans.len++;
        reverse(ans.num,ans.num+ans.len);
        //SQRT Algorithm
        }
}

void multiply(bignum *in,bignum *out,int coef){
        //out=in*coef
        int i=0;
        if(coef==0){
                clean(out);
                out->len=1;
                return;
        }
        int temp=0;
        int len=in->len;
        while(i<len){
                out->num[i]=(in->num[i]*coef)+temp;
                temp=out->num[i]/10;
                out->num[i++]%=10;
        }
        if(i==len && temp!=0){
                out->num[i]=(in->num[i]*coef)+temp;
                temp=out->num[i]/10;
                out->num[i++]%=10;
        }
        out->len=i;
}

bool isbigger(bignum *a,bignum *b){
//checks whether  a > b or not
        if(a->len!=b->len)
                return (a->len>b->len);
        for(int i=0;i<a->len;i++)
                if(a->num[a->len-i-1]!=b->num[b->len-i-1])
                        return a->num[a->len-i-1]>b->num[b->len-i-1];
        return false;
}

void balance(bignum *num,int i){
//It's used to make difference easier!
        if(num->num[i+1]==0){
                balance(num,i+1);
        }
        num->num[i+1]--;
        num->num[i]+=10;
        if(i==num->len-2 && num->num[num->len-1]==0)
                num->len--;
}

void diff(bignum *a,bignum *b,bignum *ans){
//ans=|a-b|
        bignum *a1,*a2;
        if(isbigger(a,b)){
                a1=a;
                a2=b;
        }else{
                a1=b;
                a2=a;
        }
        if(ans!=a1){
                for(int i=0;i<a->len;i++)
                        ans->num[i]=a1->num[i];
                ans->len=a1->len;
        }
        for(int i=0;i<a2->len;i++){
                if(ans->num[i]<a2->num[i])
                        balance(ans,i);
                ans->num[i]-=a2->num[i];
        }
        int i=0;
        while(ans->num[ans->len-i-1]==0)
                i++;
        ans->len-=i;
}

void phifinalize(){
//makes Phi
        int temp=0;
        ans.num[ans.len-1]+=1;
        phi.len=ans.len;
        for(int i=0;i<ans.len;i++){
                temp*=10;
                temp+=ans.num[ans.len-i-1];
                phi.num[phi.len-i-1]=temp/2;
                temp%=2;
        }
}

Help...

Point Image and Open through Paint Using VC++

$
0
0
Hi,

I have a view button, when i click this button, i was chosen a image file using file open dialog and like to display the image through paint software using VC++.

Is possible?

VS2012 & PC Not Responding - CFiledialog

$
0
0
Hi,

i'm using CFiledialog in my project.

The below code working good, but some times empty file dialog opened, then both VS2012 software & PC - Not Responding. Even task manager also not opened.
Code:

       
CFileDialog dlg(TRUE,NULL,NULL,NULL,"BMP Image |*.bmp",NULL);
if (dlg.DoModal()==IDOK)
{                               
        imgname = dlg.GetFileName();
        imgpath = dlg.GetPathName();
}

what is the reason?

Problem with COM_MAP

$
0
0
I have an MS Office 2013 (Actually its MS Project 2013 addin which is a part of MS Office distributed separately) addin developed using COM. Recently I am coming across an issue. This section of the code, was working fine earlier.

Code:

DECLARE_REGISTRY_RESOURCEID(IDR_ADDIN)
    DECLARE_NOT_AGGREGATABLE(CConnect)
    BEGIN_COM_MAP(CConnect)
        COM_INTERFACE_ENTRY(IDispatch)
        COM_INTERFACE_ENTRY(AddInDesignerObjects::IDTExtensibility2)        //COM_INTERFACE_ENTRY(OfficeAddIn::CConnect::_ComMapClass)
        END_COM_MAP()
    DECLARE_PROTECT_FINAL_CONSTRUCT()

But somehow my network engineer
1) Uninstalled MS Project 2013,
2) Then installed MS Project 2010
3) And the again un-installed MS Project 2010 and reinstalled MS Project 2013.

Now I am coming across this error.

Quote:

Error 5 error C2440: 'static_cast' : cannot convert from 'OfficeAddIn::CConnect::_ComMapClass *' to 'AddInDesignerObjects::IDTExtensibility2 *' d:\maverick\Project_Path\Connect.h 30

Error 6 error C2440: 'initializing' : cannot convert from 'ATL::_ATL_CREATORARGFUNC (__stdcall *)' to 'DWORD_PTR' d:\maverick\Project_Path\Connect.h 30
Can someone help me in resolving this error?

CDockBar background color?

$
0
0
I have a legacy Visual Studio 6.0 application. I need to change the color of the Dockbar (as illustrated in the attached picture). I have tried the implementation found here to no effect.

I'm not having much luck in my internet search endeavors.

Basically, can't I grab a dockbar (top, bottom, left, etc...) at paint (or in CMainFrame::OnNotify) time like: (where m_BrushDocBar is my initialized brush of my preferred color.)??

Code:

       
      CControlBar* pTopDockBar = GetControlBar(AFX_IDW_DOCKBAR_TOP);
        if (pTopDockBar != NULL)
        {
                SetClassLong(pTopDockBar->m_hWnd, GCL_HBRBACKGROUND, (LONG)m_BrushDocBar.GetSafeHandle());
        }

I'd really appreciate any pointers on how to accomplish this. Thanks.

Edit: I tried the code below in CMainFrame::OnCreate. No effect
Code:

CControlBar* pTopDockBar = GetControlBar(AFX_IDW_DOCKBAR_TOP);
        HWND hBar = pTopDockBar->GetSafeHwnd();
        ::SetClassLongPtr(
                hBar,
                GCLP_HBRBACKGROUND,
                (LONG_PTR)GetSysColorBrush(COLOR_ACTIVECAPTION));

Name:  DockBarColor.png
Views: 39
Size:  6.5 KB
Attached Images
 

Visual 2010 general overview needed plz and ty

$
0
0
I've just started exploring the realm of programming with C++ and I need a little help understanding a few basic things about the compiler I have. Would someone care to explain somethings to me? I would be grateful. if you pm me ill give you my skype and we can talk there more effectively.

[RESOLVED] efficiency issues

$
0
0
Hello guys,
This program should generate set amount of numbers (top) and store them in array (numbers) without repeats.
This is a training program for a class I'm taking. the program runs and does what it suppose to, however i noticed that I'm unable to run the program for top=40000. I think my problem is the second for loop its running through too many checks/compares. The program ran fine for top = 30000. I'm just trying to find out if there is a better way to run the loop where it would do less compares. I'm hoping if the loop runs less checks it will be able to run for bigger arrays. Full code below:


Code:

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{

        const int top=4000;
        int numbers[top];
        int i,j;
        srand(time(NULL));

        for (int i=0;i<top;i++)
{
    bool check;
    int n;
    do
    {
    n=rand()%top;
    check=true;
    for (int j=0;j<i;j++)
        {
        if (n == numbers[j])
        {
            check=false;
            break;
        }
        }
    } while (!check);
    numbers[i]=n;
}


        int lines;
               
        if (top%10==0)lines=top/10;
        else lines=top/10+1;
        for (j=0; j<lines;j++) {
                for (i=0;(i<10) && (10*j+i<top);i++) {
                        cout << setw(7)<< numbers[10*j+i];                       
                }
                cout << endl;
}
       
        system ("PAUSE");

        return 0;
}

Swipe Screen Function ..

$
0
0
Hi All

I wish to write Swipe Screen Function in MFC with Touch Screen monitor .

How to do that.

I have different screen templates in my software and wish to change them with Screen Swipe
same as we do on mobile ..

How to do this ?

Thanking you ...

Need help!!!

$
0
0
How to code
input : 10
output : 1 2 3 1 2 3 1 2 3 1

with <stdio.h>

I'm new with coding, so please anyone help me :(

When it comes to C++ and Visual C++, what are the most important topics to know?

$
0
0
First, let me thank you for using Codeguru. Your patronage is appreciated.

We are currently doing a little research, so you'll see questions like the topic of this thread ins several areas. We are trying to understand what is considered important to developers (you) in given topic areas.

In this case, what do you consider important topics, tools, features, technology around C++ and Visual C++? What is it topics would get your attention? What topics do you think are most important for developers?

Post your thoughts (or opinions) in this thread. Feel free to state your level of experience on the topic as well since those will experience likely have different opinions than newbies!

Brad!
Site Admin

Alternative to cout logging in Windows Environment.

$
0
0
I find out that Java println and many fold faster than cout in C++.
I am currently using cout as a quick visual logging tool.
I wonder how I can make the same lightning fast text output in C++?
Thanks
Jack

Circle Color Palette Pie Chart

$
0
0
Hi,

I don't know how to create Circle Color Palette Pie Chart.
I refer this project,Create-2d-pie-chart
Using this example, i tried like this,
Code:

int deta;
        for(int i=0; i<=255; i++)
        {
                for(int j=0; j<=255; j++)
                {
                        for(int k=0; k<=255; k++)
                        {                       
                                //deta = (no.of.obj / tot no of obj ) * 360;
                                deta = i*j*k;
                                deta = deta /(255*255*255);
                                deta = deta * 360;
               
                                m_pGraphObject1->Add2DPieGraphSegment(deta, RGB(i,j,k), "" );
                        }
                }
        }

Not working.

Using std::istream with an already existing file

$
0
0
Is it possible to attach an istream object to an already opened file handle or FILE* (so I could read the file contents into my object?)

I discovered recently that ifstream has a c'tor for this but it doesn't seem to be universal (it's available in some versions of MSVC but not for other compilers). The reason I need an already opened file is that I need to open it from a cross-platform file path (in UTF-8 format). So I can't rely on ifstream directly - nor open / fopen etc, as they don't accept UTF-8 paths on Windows. So for opening the files I use a 3rd party library which does understand UTF-8 (hope that makes sense).

P.S. I've just been reading about something called std::filebuf which apparently CAN connect to std::istream. I'm not sure if that would help at all? (I'd never heard of std::filebuf until a few minutes ago :) ). Is it even available in MSVC? I couldn't find much about it on MSDN... :cry:

old ActiveX control compatible with IMAGE_FILE_LARGE_ADDRESS_AWARE?

$
0
0
Hi,

I am having a problem in which a program crashes after 2 to 12 hours due to memory fragmentation. I am exploring several options for trying to solve it. I cannot convert it to a 64-bit application for a couple of reasons, one of which is the heavy use of an old third-party 32-bit ActiveX control for spreadsheet functionality in the UI.

One of the options I am exploring is setting the IMAGE_FILE_LARGE_ADDRESS_AWARE flag in the compiler. If I do this, will I risk intermittent crashes due to the ActiveX control (since presumably it was not compiled with that flag set)?

Thank you for any insight,
GeoRanger

USB Programming with MFC

$
0
0
Hi ..

I need data transfer through USB port . Pl guide me how to program USB port via MFC .
I need tutorial or sample code

Thanking you

2 quick questions - (1) enums and (2) switch

$
0
0
Hey guys, I'm new to these forums and I didn't see any dedicated "beginners" forum. So I'm asking here.

1. Are enums literally nothing more than an array of const unsigned ints that are presented as names rather than values, because names are more useful for humans than values in the given situation?

enum foo = { LOW, MEDIUM, HIGH };
vs.
int foo [] = { 3, 6, 8 };

2. Are switch statement nothing more than a cleaner way to handle larger groups of if statement. If so, what are the benefits of using if statements at all?

Thanks in advance.

Sending a message to another application

$
0
0
Hey Gurus!

I have written two pieces of software using MFC dialog based classes. One of them creates DCPs for digital cinema, and the other checks the integrity of DCPs.
I would like my first program to talk to my second program with a single one way message.
To do this I am using PostMessage, and my second program is receiving the message with no issues at all. The trouble is, I can't read the data I am sending.

Here is my code, firstly the program sending the message:

Code:

HWND dcpProbe = ::FindWindow( 0, "DCPProbe" );

if ( dcpProbe )
{
        char *message;

        message = ( char * ) malloc( sizeof( "Hello World" ) );

        strcpy( message, "Hello World" );

        if ( !::PostMessage( dcpProbe, DCPPRO_ANALYSEDCP, ( WPARAM ) NULL, ( LPARAM ) message ) ) free( message );
}

An now the program receiving the message:

Code:

ON_MESSAGE( DCPPRO_ANALYSEDCP, DCPProAnalyseDCP )

afx_msg LRESULT CDCPProbeDlg::DCPProAnalyseDCP( WPARAM param1, LPARAM param2 )
{
        char *parameters;
       
        parameters = ( char * )param2;

        // Do things with parameters.

        // free( parameters ); // Do this once I have handled the message

        return false;
}

I have printed out the pointer being sent, and when I run the receiving program in debug, the memory pointer I receive is correct!! However the debugger says:

parameters: 0x07b82870 <Error reading characters of string>
<Unable to read memory>


I am confused as to why this is the case?
The first program is not UNICODE, however the second is UNICODE, although I doubt this is a problem.

If someone could please offer me some advice, I'd be very grateful.

Thanks so much,

Steve Q.

Problem with MFC Application

$
0
0
I have an MFC Dialog based application developed in Visual Studio 2008. Everything is perfectly fine. Except, when I run that application in a fresh PC, where, there is no Visual Studio or any other thing, it crashes with this error message.

Quote:

Problem signature:
Problem Event Name: APPCRASH
Application Name: CodeGen3.0.exe
Application Version: 1.0.0.1
Application Timestamp: 56012e6d
Fault Module Name: CodeGen3.0.exe
Fault Module Version: 1.0.0.1
Fault Module Timestamp: 56012e6d
Exception Code: c0000005
Exception Offset: 00001af5
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 16393
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Read our privacy statement online:
http://go.microsoft.com/fwlink/?link...8&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt

Can someone help me in resolving this issue?

Stretch CView for ulta-high res screens

$
0
0
Hi,

I recently moved a program from an old version of Visual Studio (6.0) to a new one (VS2010).
The program is a CView.
Now, some people who have tried to run it on ultra-high res screens have reported that the window is now displayed really tiny for them.
This would indeed be correct, given that the window is 700x700 pixels, so it would be tiny on a 4000x???? screen.
However, the odd thing is that the same program in Microsoft Visual Studio 6.0 appears to have been automatically stretched for them.
Is there possibly some sort of setting or command in VS2010 where this auto-stretching is enabled?
Or is there possibly some easy way for me to stretch the screen myself?

Thank you in advance.

Saving view

$
0
0
I have an application that compound more CBitmaps into single one, and all of them I spread it into CScrollView. How can I save entire view (helped by CImage), but not just visible part in window client ? I saw on internet a lot of examples of how to save CDC, but just visible part ... in my case this is not suitable ... could you help me ?
Viewing all 3021 articles
Browse latest View live