Hi every one i need help really quickly. I have a deadline for tonight and need some to help me do a sum in c#.
In the textbox i have to enter values for example "5,5,10,10,7,7,9,9,20,20,3,3,4,4" and it has to take the first entered and the 3rd last and last entered and plus them together
Loading
FroglegPosted Apr 3, 2011, 5:53 PM
if (buttClick == 4)
{
fileNum++;
string[] allLines = new string[firstClick.Length + secondClick.Length + thirdClick.Length];
for (int i = 0; i < firstClick.Length; i++)
{
allLines[i] = firstClick[i];
}
int oneL = firstClick.Length;
for (int i = 0; i < secondClick.Length; i++)
{
allLines[i + oneL] = secondClick[i];
}
int twoL = secondClick.Length;
twoL= twoL+oneL;
for (int i = 0; i < thirdClick.Length; i++)
{
allLines[i + twoL] = thirdClick[i];
}
System.IO.File.WriteAllLines("henry" + fileNum.ToString() + ".txt", allLines);
buttClick = 1;
button1.Enabled = false;<<<<<<<<<<<<<<<<<<
timer1.Start();<<<<<<<<<<<<<<<<<<<<<<<<<
and in timer event
private void timer1_Tick(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Stop();
}
}
FroglegPosted Apr 3, 2011, 5:48 PM
int fileNum = 0;
int buttClick = 1;
string[] firstClick, secondClick, thirdClick;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0,second = 0,third = 0;
switch (buttClick)
{
case 1:
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
}
break;
case 2:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = second - third;
}
}
break;
case 3:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = third + third;
}
}
break;
}
if (result >= 24) result %= 24;
DateTime dt = DateTime.Today.AddHours(result);
string format = "dddd hh:mm";
string[] lines = new string[61];
for (int i = 0; i < 61; i++)
{
lines[i] = dt.AddDays(i - 30).ToString(format);
}
if (buttClick == 1)
{
firstClick = lines;
buttClick = 2;
}
else if (buttClick == 2)
{
secondClick = lines;
buttClick = 3;
}
else if (buttClick == 3)
{
thirdClick = lines;
buttClick = 4;
}
if (buttClick == 4)
{
fileNum++;
string[] allLines = new string[firstClick.Length + secondClick.Length + thirdClick.Length];
for (int i = 0; i < firstClick.Length; i++)
{
allLines[i] = firstClick[i];
}
int oneL = firstClick.Length;
for (int i = 0; i < secondClick.Length; i++)
{
allLines[i + oneL] = secondClick[i];
}
int twoL = secondClick.Length;
twoL= twoL+oneL;
for (int i = 0; i < thirdClick.Length; i++)
{
allLines[i + twoL] = thirdClick[i];
}
System.IO.File.WriteAllLines("henry" + fileNum.ToString() + ".txt", allLines);
buttClick = 1;
}
}
Henry BartoschPosted Apr 3, 2011, 5:41 PM
FroglegPosted Apr 3, 2011, 5:24 PM
int fileNum = 0;
int buttClick = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0,second = 0,third = 0;
switch (buttClick)
{
case 0:
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
}
buttClick = 1;
break;
case 1:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = second - third;
}
}
buttClick = 2;
break;
case 2:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = third + third;
}
}
buttClick = 3;
break;
}
if (result >= 24) result %= 24;
DateTime dt = DateTime.Today.AddHours(result);
string format = "dddd hh:mm";
string[] lines = new string[61];
for (int i = 0; i < 61; i++)
{
lines[i] = dt.AddDays(i - 30).ToString(format);
}
if (buttClick == 3)
{
fileNum++;
System.IO.File.WriteAllLines("henry" + fileNum.ToString() + ".txt", lines);
buttClick = 0;
}
}
No point in third -third
Henry BartoschPosted Apr 3, 2011, 5:08 PM
f (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
with this it gave me an answer. but now with the same button click event i want
a result = second - third
and a result = third - third
and a result = third + third
so if you can help me with an example; coz all of this should happen on the same button click event and save to the text file.
FroglegPosted Apr 3, 2011, 5:01 PM
Henry BartoschPosted Apr 3, 2011, 4:51 PM
third - third;
second - third;
and it should give an answer and disply it in the text file
FroglegPosted Apr 3, 2011, 4:43 PM
Henry BartoschPosted Apr 3, 2011, 4:37 PM
Please.
fileNum++;
VulpesPosted Apr 3, 2011, 9:46 AM
However, if the results are all to be saved to the same file, then you'll need to use File.AppendAllLines rather than File.WriteAllLines.
Henry BartoschPosted Apr 3, 2011, 9:13 AM
I also want tit to do
first-first;
first + first
third +third;
second;
third;
first;
second - third;
second + second + third+third
and show all the diverent times with the dates in the same text file
Henry BartoschPosted Apr 3, 2011, 9:13 AM
I also want tit to do
first-first;
first + first
third +third;
second;
third;
first;
second - third;
second + second + third+third
and show all the diverent times with the dates in the same file
Henry BartoschPosted Apr 3, 2011, 8:38 AM
VulpesPosted Apr 3, 2011, 8:16 AM
fileNum++;
which will be incremented each time the button is pressed and thereby generate a new file name to save the calculations to.
Henry BartoschPosted Apr 3, 2011, 7:37 AM
but now i have the problem of the text file. it keeps on saving to the same text file. Every time i say save a new text file should be created, not replace the previous one
VulpesPosted Apr 3, 2011, 6:44 AM
private int fileNum = 0; // need this form level variable to count the files
fileNum++;
Henry BartoschPosted Apr 3, 2011, 6:25 AM
Henry BartoschPosted Apr 3, 2011, 6:21 AM
VulpesPosted Apr 3, 2011, 6:19 AM
To find it quickly, I'd use a full path. The following should do as a temporary measure:
System.IO.File.WriteAllLines(@"c:\henry.txt", lines);
Henry BartoschPosted Apr 3, 2011, 6:17 AM
Sunday 04:00
Tuesday 04:00
Friday 04:00
Henry BartoschPosted Apr 3, 2011, 6:13 AM
But it doesnt save to the text file. Searching for the text file but cant find it.
Should i create a path?
VulpesPosted Apr 3, 2011, 6:07 AM
Building on Frogleg's code, this should do it:
Henry BartoschPosted Apr 3, 2011, 5:41 AM
FroglegPosted Apr 3, 2011, 5:33 AM
DateTime d = DateTime.Today.AddDays(-30);// use minus to subtract (30) to add
to get string value use
d.ToString();
To write a text file use
using System.IO; // above namespace
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(d.ToString());// for the days prior and forward use say DateTime d2 = DateTime.Today.AddDays(30);
}
Gotta go Bye
Henry BartoschPosted Apr 3, 2011, 5:22 AM
but with todays date and time the time should be the same for time for 30 days forward and backward : example todays date: Sunday 16:00(if it goes backward its gona be saturday 16:00, vryday 16:00...etc. and forward monday 16:00, tuesday 16:00...etc) and then save all to a text file.
FroglegPosted Apr 3, 2011, 5:13 AM
1: Get a time from the textbox - will have to be less than 24hrs otherwise it will be tomorrow
2: From day of entry (today) we need the date 30 days back and 30 days forward
3: Save all to text file
I have limited time tonight so hopefully someone else will join in to help 7.15pm here
Henry BartoschPosted Apr 3, 2011, 4:54 AM
Thanks
FroglegPosted Apr 3, 2011, 4:48 AM
Summarize what you want in one question please
Henry BartoschPosted Apr 3, 2011, 4:25 AM
the last 2 entered should now be subtracted from each other : 4-4
Henry BartoschPosted Apr 3, 2011, 4:07 AM
Henry BartoschPosted Apr 3, 2011, 4:06 AM
Henry BartoschPosted Apr 3, 2011, 4:03 AM
and now if there is anything over 24 it should devide it by 24
FroglegPosted Apr 3, 2011, 3:54 AM
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0,second = 0,third = 0;
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
}
MessageBox.Show("Answer = " + Convert.ToString(result));
Henry BartoschPosted Apr 3, 2011, 3:51 AM
But if i enter 5,5,20,20,10,10,1,1
it gives me the amswer 15 and it should be 16
Henry BartoschPosted Apr 3, 2011, 3:35 AM
so in the case i stated the "5,5,...."
So it has to do 5+3+4 = 12 and then it should show in time format. 12:00
FroglegPosted Apr 3, 2011, 3:33 AM
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0;
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
result = first + Convert.ToInt32(str[str.Length - 3]);
}
}
MessageBox.Show("Answer = " + Convert.ToString(result));
}
Karthikeyan AnbarasanPosted Apr 3, 2011, 3:20 AM