|
if i use the the class stack instead of vector can i just dispose of the function reverse()???
i am not sure why i would want to use the function reverse()....
also, i have implemented some code that will do what i need now i just need to convert it to the class file of bigNumber, i think.
maybe some pointers...??
#include<iostream>
#include<iomanip>
#include<stack>
using namespace std;
void main(){
stack<int> S1,S2,R;
int result = 0;
char d;
cout<<"please enter two large integers for me to add." << endl;
//Read the first digit of the first number and push it on S1
if(cin >> d && isdigit(d))
S1.push(d - '0');
cin >> noskipws;
//Read the remaining digits of the first number and push it on S1
while(cin >> d && isdigit(d))
S1.push(d - '0');
cin >> skipws;
//read the first digit of the second number and push it on S2
if(cin >> d >> noskipws && isdigit(d))
S2.push(d - '0');
//read the remaining numbers of the second number and push them on the stack
while(cin >> d && isdigit(d))
S2.push(d - '0');
//Add the two numbers
while (!S1.empty() || !S2.empty()){
if(!S1.empty()){
result += S1.top();
S1.pop();
}
if(!S2.empty()){
result += S2.top();
S2.pop();
}
R.push(result % 10);
result /= 10;
}
if(result)
R.push(result);
//display the result
cout << "the sum is " << endl;
while(!R.empty()){
cout << R.top();
R.pop();
}
cout << endl;
}
|
|
|
|
|
Anonymous wrote:
if i use the the class stack instead of vector can i just dispose of the function reverse()???
No.
Anonymous wrote:
i am not sure why i would want to use the function reverse()....
I assume so that you can create an iterator and go over the digits in reverse order for the sake of operators such as addition and subtraction. I thought you could create a reverse direction iterator though, although it's years since I used the STL. I imagine you'd call reverse before and after a mathematical operation. To me, that sounds more expensive than using array access instead of an iterator to move backwards through a container that's meant to be optimised for random access anyhow.
The problem with the code you've implimented is that it's not wrapped in a class. You should be doing all these operations within your class, using your own iostream inserters and extractors, instead of doing it all with a stack as you are here.
The general idea of what you're doing is a good start for your operator >>.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
ok so my input operator (function) will look somewhat like this:
istream & operator >>(istream & istr, bigNumber t)
{
istr >> t.something ;
return istr;
}
now how would i go about setting the constructors to the string paramter and the vector<int> v parameters?
my test program will consist of asking for two numbers...
the user will enter the first number which will be set to the one of the constructors and then matched up with the correct constructor, right. would i have to use the (this) command?
|
|
|
|
|
I'd imagine it would iterate over the vector, and pass each item in the vector to the stream. There's other stuff you should do in setting up a stream operator, read my articles on the subject here on Code Project.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
if i use the the class stack instead of vector can i just dispose of the function reverse()???
i am not sure why i would want to use the function reverse()....
also, i have implemented some code that will do what i need now i just need to convert it to the class file of bigNumber, i think.
maybe some pointers...??
#include<iostream>
#include<iomanip>
#include<stack>
using namespace std;
void main(){
stack<int> S1,S2,R;
int result = 0;
char d;
cout<<"please enter two large integers for me to add." << endl;
//Read the first digit of the first number and push it on S1
if(cin >> d && isdigit(d))
S1.push(d - '0');
cin >> noskipws;
//Read the remaining digits of the first number and push it on S1
while(cin >> d && isdigit(d))
S1.push(d - '0');
cin >> skipws;
//read the first digit of the second number and push it on S2
if(cin >> d >> noskipws && isdigit(d))
S2.push(d - '0');
//read the remaining numbers of the second number and push them on the stack
while(cin >> d && isdigit(d))
S2.push(d - '0');
//Add the two numbers
while (!S1.empty() || !S2.empty()){
if(!S1.empty()){
result += S1.top();
S1.pop();
}
if(!S2.empty()){
result += S2.top();
S2.pop();
}
R.push(result % 10);
result /= 10;
}
if(result)
R.push(result);
//display the result
cout << "the sum is " << endl;
while(!R.empty()){
cout << R.top();
R.pop();
}
cout << endl;
}
|
|
|
|
|
you may use some class derived from CString class.
then you can perform arithmetic operations on them without using vectors, or arrays. ex:
CMyClass s1,s2;
s1="123434";
s2="654321";
//now add them like this
// 123434
// + 654321
// result : 777755
we add them simply like we do it on paper , we read symbols(from the end) from s1 and s2 , for example first we'd add 4 and 1 and get 5.next we'd add 3 and 2 and get 5 and so on...
programatically you can do it like this
CString Add(CString s1,CString s2)
{
CString result,m;
int l1,l2,l,k1,k2,k,temp,g1,g2;
k=temp=k1=k2=0;
m="";
if((s1=="0")&&(s2=="0")) return "0"; // 0+0 = 0
l1=s1.GetLength();
l2=s2.GetLength();
l=__max(l1,l2);
g1=convert(s1,l+1);
g2=convert(s2,l+1);
//convert means if you must add
//123 and 45555 it must convert 123 to 00123
//it's simple to write such function
for(int i=l;i>=0;i--)
{
k1=g1.GetAt(i)-'0';
k2=g2.GetAt(i)-'0';
k=k1+k2+temp;
if(k>=10){k=k-10;temp=1;}else temp=0;
m.Format("%d",k);
result=m+result;
}
return NoZero(result);
//NoZero means if the result is 00987 ir must return 987
}you can modify this function so that it will work with negative number also.
you'll have to do similatly with -,*,/ of course with division job will be harder.
Good luck.
m0n0
|
|
|
|
|
I am using a CRegistry to read the name of a server stored in a char variable
"char cbBuffer [250]"
What I am trying to do is ping the name stored in cbBuffer as follows:
system("ping localhost> C:\\Temp\\pingresult.txt");
how can I replace the name of the "localhost" by the name stored in cbBuffer
or a better way to do this.
thanX in advanced.
|
|
|
|
|
I'm a fan of sstream. Observe:
#include <sstream>
std::ostringstream oss;
oss << "ping " << cbBuffer << " C:\\Temp\\pingresult.txt";
system(oss.str().c_str());
|
|
|
|
|
look at [sprintf() ]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
hi ,
i have made a sever & client in java and a server & client in vc++.both are tranferring data to each other properly.
but when i run java server and connect a vc++ client to it ,client gets connected to server properly but the client does not read tthe data sent by server properly and gives absurd values,instead.
the code for receive in vc++ client:
iReturn = recv (ServerSock, szClientA, sizeof (szClientA), 0);
the code for java server is
output = new ObjectOutputStreamconnection.getOutputStream() ); output.flush();
message = (String) input.readObject();
display.append( "\nhhhhhhhhh" + message );
should i change the output stream in java?
please help me.its really urgent
|
|
|
|
|
I tried to do this—but I can’t figure out what the throw and handle portions should be—please advise—this is my last assignment for the year! As you can tell I need alot of improvement!!
Write a program that prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number or a nondigit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers.
#include <iostream>
using namespace std;
// VARIABLE DECLARATIONS:
double inches; // number of inches
double foot; //number of feet
double centimeters; // number of centimeters
int choice; // users menu choice
int main()
{
//give the user some options and begin actual program
do
{ // while choice is not equal to zero
// present menu of options
cout << endl;
cout << " conversion of inches,feet to cetimeters" << endl;
cout << endl;
cout << "1 - Convert inches and feet to centimeters" << endl;
cout << "0 - Quit" << endl;
cout << endl;
cout << "Enter choice : ";
// get users response
cin >> choice;
if (choice == 1) {
// process inches to centimeters
cout << "Enter length in feet: "<<end1;
cin="">> foot;
cout<<"Enter inches as well:"<<end1;
cin="">>inches
cout<
|
|
|
|
|
Does anybody know how to call an executable from a C++ program
|
|
|
|
|
You should browse MSDN on CreateProcess() API function
|
|
|
|
|
here is a good article, hope it helps
www.codeproject.com/system/newbiespawn.asp
|
|
|
|
|
[ShellExecute]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi all
I have managed to get windows media player (active X control) to play, pause and stop music files. But it only plays a file that is preloaded before running the program.
Is there anyway I can get the player to accept and play a file chosen by the user. I have only suceeded to get the program to show the open file dialog window but it doesnt play the selected file.
Any ideas on how i can get the file to play? Similarly is there a control i can use to load a play list?
I'd be grateful if anyone has any sugestions on this
az
|
|
|
|
|
I'm afraid I don't have the code here, but I've definately played playlists and files from C# using WMP. From memory, you actually get an object that represents the file to play by calling a static load method on the playlist, then you set the current file to be that object.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hiho@ll
anybody knows how to get the server-ip (oder domain name) if i have an HINTERNET handle?
i'm using a CHTTPClient i found at codeproject which uses WinHttp
i connect to a server, which redirects (using the Location response header) to another server
the connection handling of winHttp does everything right, but i just want to know to which server i'm really connected!
i tried HttpQueryInfo
but this uses the Host header field of the http request
and this header field isn't set because of the Location field of the server
so anybody knows a function, which tells me to which server i'm connected using a HINTERNET handle?
thx@ll
|
|
|
|
|
Hi,
is it possible to use Riche Edit Controls for non MFC dialog based apps?
When I insert such a control, I dont see anything. I googled a bit, and found out that I need to call AfxInitRichEdit2. But this one is for MFC based apps. I dont use MFC and I dont want to.
Is there any way to use them? I couldnt find a function to initialize it.
DKT
|
|
|
|
|
LoadLibrary(_T("RICHED32.DLL"));
|
|
|
|
|
Perfect! Thanks alot!
EDIT: How can I auto scroll to the end of the rich edit control?
DKT
|
|
|
|
|
look for [EM_LINESCROLL] message
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
I like to write a project about chatting , but i don't know how to begin because i don't have much exprience about it . So can anyone tell me some examples or some code about it ? thks all!!!!!!!!!
|
|
|
|
|
|
I have to read some data from the registry, and will need to use it several times in probably all the parts of my application.
So instead of reading it from the registry every time I need it, I would like to read it once and make it available to all the application parts.
How can I do that? I thought about the Document class but I have more dialogs that can't access this one.
Thanks
|
|
|
|