|
Gee - that doesn't sound like homework at all. What have you written so far ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi, Christian Graus i`m realy apperciate your replay and i think that i found the code i need.
using System;
namespace Point_project
{
public class Fraction
{
private int up, down;
public Fraction()
{}
public Fraction( int m, int n)
{
Up = m;
Down = n;
}
public override string ToString()
{
return Up+""+" / "+Down;
}
public int Up
{
get
{
return up;
}
set
{
up = value;
}
}
public int Down
{
get
{
return down;
}
set
{
down = value;
}
}
public static Fraction operator + (Fraction x, Fraction y)
{
int r1 = (x.Down * y.Up) + (y.down * x.Up);
int r2 = x.Down * y.down;
return new Fraction( r1, r2);
}
public static Fraction Add( Fraction x, Fraction y)
{
return x + y;
}
public static Fraction operator - (Fraction x, Fraction y)
{
int r1 = (y.down * x.Up)-(x.Down * y.Up) ;
int r2 = x.Down * y.down;
return new Fraction( r1, r2);
}
public static Fraction Sub( Fraction x, Fraction y)
{
return x - y;
}
public static Fraction operator * (Fraction x, Fraction y)
{
return new Fraction( x.Up * y.Up, x.Down * y.Down);
}
public static Fraction Multiply( Fraction x, Fraction y)
{
return x * y;
}
public static Fraction operator / ( Fraction x, Fraction y)
{
int r1 = x.Up * y.Down;
int r2 = x.Down * y.Up;
return new Fraction( r1, r2);
}
public static Fraction Division ( Fraction x, Fraction y)
{
return x / y;
}
public static int GCD(int left, int right)
{
if (left < 0)
left = - left;
if (right < 0)
right = - right;
if (left < 2 || right < 2)
return 1;
if(left == right)return left;
do
{
if (left < right)
{
int temp = left;
left = right;
right = temp;
}
left %= right;
} while (left != 0);
return right;
}
public void simplify()
{
int g=GCD(down,up);
down/=g;
up/=g;
}
}
public class FractionTeast
{
static void Main()
{
Fraction w = new Fraction();
Fraction x = new Fraction();
Fraction y = new Fraction();
Fraction z = new Fraction();
Console.Write("Numerator1 =");
x.Up = Convert.ToInt32(Console.ReadLine());
Console.Write("denominator1 =");
x.Down = Convert.ToInt32(Console.ReadLine());
while(x.Down == 0)
{
Console.Write("Invalid value REENTER denominator1 =");
x.Down = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nNumerator2 =");
y.Up = Convert.ToInt32(Console.ReadLine());
Console.Write("denominator2 =");
y.Down = Convert.ToInt32(Console.ReadLine());
while(y.Down == 0)
{
Console.Write("Invalid value REENTER denominator1 =");
y.Down = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nSelect the calculation type (1:Addtion, 2:Subtraction, 3:Multiplication, 4:Division, Or 0:ToTerminate)==>");
sbyte e = Convert.ToSByte(Console.ReadLine());
while(e != 0)
{
if(e < 0 || e > 4)
{
Console.Write("\nInvalid number PLEASE Select the calculation type (1:Addtion, 2:Subtraction, 3:Multiplication, 4:Division, Or 0:ToTerminate)==>");
e = Convert.ToSByte(Console.ReadLine());
}
if( e == 1)
{
z = (x+y);
z.simplify();
if(z.Down == 1)
{
Console.WriteLine(x.Up+"/"+x.Down+" + "+y.Up+"/"+y.Down+" = "+z.Up);
}
else if(z.Up == 0)
{
Console.WriteLine(x.Up+"/"+x.Down+" + "+y.Up+"/"+y.Down+" = 0");
}
else
{
Console.WriteLine(x.Up+"/"+x.Down+" + "+y.Up+"/"+y.Down+" = "+z);
}
Console.Write("\nSelect the calculation type (1:Addtion, 2:Subtraction, 3:Multiplication, 4:Division, Or 0:ToTerminate)==>");
e = Convert.ToSByte(Console.ReadLine());
}
if( e == 2)
{
z = (x-y);
z.simplify();
if(z.Down == 1)
{
Console.WriteLine(x.Up+"/"+x.Down+" - "+y.Up+"/"+y.Down+" = "+z.Up);
}
else if(z.Up == 0)
{
Console.WriteLine(x.Up+"/"+x.Down+" - "+y.Up+"/"+y.Down+" = 0");
}
else
{
Console.WriteLine(x.Up+"/"+x.Down+" - "+y.Up+"/"+y.Down+" = "+z);
}
Console.Write("\nSelect the calculation type (1:Addtion, 2:Subtraction, 3:Multiplication, 4:Division, Or 0:ToTerminate)==>");
e = Convert.ToSByte(Console.ReadLine());
}
if( e == 3)
{
z = (x*y);
z.simplify();
if(z.Down == 1)
{
Console.WriteLine(x.Up+"/"+x.Down+" X "+y.Up+"/"+y.Down+" = "+z.Up);
}
else if(z.Up == 0)
{
Console.WriteLine(x.Up+"/"+x.Down+" X "+y.Up+"/"+y.Down+" = 0");
}
else
{
Console.WriteLine(x.Up+"/"+x.Down+" X "+y.Up+"/"+y.Down+" = "+z);
}
Console.Write("\nSelect the calculation type (1:Addtion, 2:Subtraction, 3:Multiplication, 4:Division, Or 0:ToTerminate)==>");
e = Convert.ToSByte(Console.ReadLine());
}
if( e == 4)
{
z = (x/y);
z.simplify();
if(z.Down == 1)
{
Console.WriteLine(x.Up+"/"+x.Down+" / "+y.Up+"/"+y.Down+" = "+z.Up);
}
else if(z.Up == 0)
{
Console.WriteLine(x.Up+"/"+x.Down+" / "+y.Up+"/"+y.Down+" = 0");
}
else
{
Console.WriteLine(x.Up+"/"+x.Down+" / "+y.Up+"/"+y.Down+" = "+z);
}
Console.Write("\nSelect the calculation type (1:Addtion, 2:Subtraction, 3:Multiplication, 4:Division, Or 0:ToTerminate)==>");
e = Convert.ToSByte(Console.ReadLine());
}
}
if( e == 0)
{
Console.WriteLine("\nPress ENTER toterminate");
Console.ReadLine();
}
}
}
}
casue i'm the man withno lose
|
|
|
|
|
Looks likely. If you 'found' this code, I recommend you learn what it does, because your professor will ask you and your next homework will assume you know how this one works.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
well, i mean i that found the simplifying methods and matched it together and with the rest of the code even it(simplifying methods) were containing many mistakes and i fix it
but the rest of the code me who made it.
so don`t be ware for me
i made the discussion with my Prof. about it and i was great
but anyway thanks a lot
casue i'm the man withno lose
|
|
|
|
|
Hi!
Currently I'm writing a very simple mail server using SMTP and POP3. My architecture is as follows:
Start Mailserver ->
-> create SMTP server thread
-> create thread for each client that connects
-> create POP3 server thread
-> create thread for each client that connects
Stop Mailserver ->
-> Shutdown SMTP and POP3 thread and their children threads
Is this a good architecture to use in a server?
If yes, how do I gracefully shut down the threads? Right now I have a method "Stop" that will set a static volatile bool stop to true which will cause some while-loops to terminate, but that doesn't work, the loops don't register that "stop" has changed to true
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I can't advise you on the arhitecture, becouse I have never programed a server of any kind (but I supose I would go the same way as you went...).
Anyways for the killing threads part: create a collection of your threads objects (I'm a little bit rusty on this part, but I do know that every thread has an object) and then when you have to kill them, just loop trough that collection and shut the threads down...
Easy.
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
CWIZO wrote: just loop trough that collection and shut the threads down...
Yeah that's okay, but what is the correct way to shut the threads down?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I think every thread has a stop thread function or something like that... so that is the correct way.
Or am I missing your point?
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
Hi,
The cost of VS 2005 standard edition is almost double in the UK
compared to the US. Is there any legal reason why I can't order VS from
the US instead of buying it locally?
Regards
Pieter
|
|
|
|
|
I can't imagine that there would be. Stuff in the UK always seems to cost more. when the UK is my only option for music I know it costs a ton.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thanks Christian. Yes, stuff are sometimes pretty expensive in the UK. I don't mind paying a few £££s more, but double - that's ridiculous!
Pieter
|
|
|
|
|
Subject: Try/Catch Variable Scoping
If I try to compile the following code in .NET 1.1, I get the error "Use of unassigned local variable 'rdr'" when I'm assigning a value to intSurveyQuestionOrdinal. I understand that the try block's brace is creating a separate scope. However, I tried to get around that by declaring rdr at the top of the method. Why doesn't this work?
Thanks.
protected int GetSurveyQuestion()
{
SqlDataReader rdr;
SqlConnection connection = new SqlConnection(AppConfig.GetDbConnectionString(Convert.ToString(Request.Url)));
connection.Open();
SqlCommand command = new SqlCommand("usp_SEL_SurveyQuestion", connection);
command.CommandType = CommandType.StoredProcedure;
try
{
rdr = command.ExecuteReader();
}
catch(Exception exc)
{
pnlQuestion.Visible = false;
pnlError.Visible = true;
lblAdminPageHeader1.Text = "Application Error";
lblAdminPrompt1.Text = "The SELECT database operation failed: " + exc.Message;
rdr.Close();
connection.Close();
}
int intSurveyQuestionOrdinal = rdr.GetOrdinal("SurveyQuestion"); // <--- ERROR HAPPENS HERE
int intSurveyIDOrdinal = rdr.GetOrdinal("SurveyID");
while (rdr.Read())
{
strSurveyQuestion = rdr.GetString(intSurveyQuestionOrdinal);
intSurveyID = rdr.GetInt32(intSurveyIDOrdinal);
}
rdr.Close();
connection.Close();
return intSurveyID;
}
|
|
|
|
|
Because, in some cases the rdr variable will not be set. If the ExecuteReader call fails, the rdr variable doesn't get a value. The compiler only lets you use a variable if it's certain that it always has a value.
You should also get the same error message on the line where you call rdr.Close().
In fact, you shouldn't call rdr.Close() at all in the catch section. If the code in the catch section is executed, the rdr variable certainly has no value.
Also, if you get an exception, you shouldn't let the code just go on and try to use the reader anyway, as there is no reader.
---
b { font-weight: normal; }
|
|
|
|
|
OK, I understand your reasoning. That still leaves the question, how do I handle this situation? I'm reading articles about this situation online and in Studio's online help but they all seem to deal with the very simplest of situations (e.g. just set a label control if an error occurs).
Thanks again.
|
|
|
|
|
Just don't let the code panic. Just because a database call failed, you don't need to drop everything and run. In the catch section just set the rdr variable to null and prepare the error message, then after the try--catch block you just check if the variable is null or not. If it's not null, loop thought the data. If it's null, just skip that part and go directly on to gracefully closing the database.
I always put the connection object in a using() block. That way, what ever happens, at least the database connection is closed correctly.
using (SqlConnection connection = new SqlConnection(...)) {<br />
...do the database stuff<br />
}
The using() block always calls the Dispose method of the connection, and as the Dispose method of the connection always calls the Close method, you don't have to do that either.
---
b { font-weight: normal; }
|
|
|
|
|
That worked! Thank you very much!!
|
|
|
|
|
I am using Visual Studio 2005. I wrote a Setup Project to install my application. After setup is complete, I need to get the destination folder that the user chose to install to (not the default destination folder, but the one the user actually chose during the install. I found an article that said in Visual Studio 2003, this was done by adding a registry entry (through the Setup project) and setting the value of the entry to [INSTALLDIR]. In Visual Studio 2005, I have no problem adding a registry entry and setting it's value, but [INSTALLDIR] is not recognized as a special tag, and is just treated as a literal string. Has the name of this tag changed in VS 2005? Anyone know how to do this in VS 2005
thanks!
|
|
|
|
|
i had the same need, what is your goal.
|
|
|
|
|
My goal is to be able to read the install dir. path from within the app at run time. So, having the Setup program write the path to the registry would be a perfectly acceptable way to accomplish this goal, only problem is I can't figure out how to do this.
|
|
|
|
|
i mean if you explain what have you to do, you may avoid to get the user setup path.
Anyway after the install you can run automatically a little .exe without form installed together your main app, that create a registry key
|
|
|
|
|
Yes, but how do I get the path to put it into the registry key?
|
|
|
|
|
What is the best way to update a database like Access, use oleDBcomand with parameters or use DataSet?
|
|
|
|
|
The dataset uses a command object to update the database in the end anyway, so you can skip the middle man and do the work yourself. Use a dataset if you find it more convenient.
---
b { font-weight: normal; }
|
|
|
|
|
but what is the faster way? if i use no dataset way i've to connect everytime at the database so it spend time.
Using dataset at the end, when i close the program i've to update the database anyway so i connect just one time to it, but i've to write SQL insert, update, delete comand as in no DataSet way.
So what is the better or faster way?
|
|
|
|
|
Neither. You'll be doing the exact same thing either way. THe only difference will be when you do it. You'll be using your own SQL statements and updating records, one at a time, based on the changes your holding in your data model. Using a DataSet object is no different. Same SQL statements, just a different object holding your data.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|