| | |
how can I show the file path instead of ref
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
I have the following code. when I run the code I have to one by one input fx,ft,apha,m,n. After I run the code:
Y
10
0.5
1
10
50
Code works with this way.
I writed to a file called "d:\\a.txt" this values(that is upper values).I tried to work the code by showing the file path but Code isn't works.
Orginal code:
Y
10
0.5
1
10
50
Code works with this way.
I writed to a file called "d:\\a.txt" this values(that is upper values).I tried to work the code by showing the file path but Code isn't works.
Orginal code:
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace hard { class Program { static void Main(string[] args) { Program app = new Program(); } Program() { bool ok = false double[] w = new double[25]; double[] l = new double[25]; double[] u = new double[25]; double[] z = new double[25]; double ft = 0.0, fx = 0.0, alpha = 0.0, h = 0.0, k = 0.0, vv = 0.0, t = 0.0, x = 0.0; int n = 0, m = 0, m1 = 0, m2 = 0, n1 = 0, flag = 0, i1 = 0, i = 0, j = 0 ; input(ref ok,ref fx,ref ft,ref alpha,ref n,ref m); if (ok) { m1 = m - 1; m2 = m - 2; // n1 = n - 1; /* step 1 */ h = fx / m; k = ft / n; vv = alpha * k / (h * h); /* step 2 */ for (i = 1; i <= m1; i++) w[i - 1] = F(i * h); /* step 3 */ l[0] = 1.0 - 2.0 * vv; u[0] = vv / l[0]; /* step 4 */ for (i = 2; i <= m2; i++) { l[i - 1] = 1.0 - 2.0 * vv - vv * u[i - 2]; u[i - 1] = vv / l[i - 1]; } /* step 5 */ l[m1 - 1] = 1.0 - 2.0 * vv - vv * u[m2 - 1]; /* step 6 */ for (j = 1; j <= n; j++) { /* step 7 */ /* current t(j) */ // t = j * k; z[0] = w[0] / l[0]; /* step 8 */ for (i = 2; i <= m1; i++) z[i - 1] = (w[i - 1] - vv * z[i - 2]) / l[i - 1]; /* step 9 */ w[m1 - 1] = z[m1 - 1]; /* step 10 */ for (i1 = 1; i1 <= m2; i1++) { i = m2 - i1 + 1; w[i - 1] = z[i - 1] - u[i - 1] * w[i]; } } /* step 11 */ output(ft, x, m1,ref w, h); } } private double F(double X) { double f; f = 0.3061 * X * X -2.1286*X+38.11; return f; } private void input(ref bool ok,ref double fx, ref double ft, ref double alpha, ref int n, ref int m ) { Console.Write("Has the function F been created immediately (Y/N) : "); string sonuc = Console.ReadLine(); if (sonuc.ToUpper() == "Y") { Console.WriteLine("The lefthand endpoint on the X-axis is 0."); ok = false; while (!ok) { Console.Write("Input the righthand endpoint on the X-axis: "); fx = Convert.ToDouble(Console.ReadLine()); if (fx < 0.0) Console.WriteLine("Must be positive number."); else ok = true; } ok = false; while (!ok) { Console.Write("Input the maximum value of the time variable T : "); ft = Convert.ToDouble(Console.ReadLine()); if (ft < 0.0) Console.WriteLine("Must be positive number."); else ok = true; } Console.Write("Input the constant alpha: "); alpha = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Input integer m = number of intervals”); Console.WriteLine("input N = number of time intervals"); Console.WriteLine("Note :must be m>3 ve n>0 "); ok = false; while (!ok) { Console.Write("m (>3) = "); m = Convert.ToInt32(Console.ReadLine()); Console.Write("n (>0) = "); n = Convert.ToInt32(Console.ReadLine()); if (m <= 2 || n <= 0) { Console.WriteLine("Numbers are not within correct range."); } else { ok = true; } } } else { Console.WriteLine("The program will end so that the function F can be created."); ok = false; } } public void output(double ft, double x, int m1, ref double[] w, double h) { System.IO.StreamWriter oup; System.IO.TextWriter tmp = Console.Out; Console.WriteLine("Choice of output method."); Console.WriteLine("1. Output to screen"); Console.WriteLine("2. Output to text file"); int flag = Convert.ToInt32(Console.ReadLine()); if (flag == 2) { Console.WriteLine("Sample: D:\\1.txt"); string name = Console.ReadLine(); try { oup = new System.IO.StreamWriter(name); Console.SetOut(oup); write(ft, x, m1, ref w, h); oup.Close(); } catch (System.IO.IOException expc) { Console.WriteLine(expc.Message + " file dont extract."); } } else { write(ft, x, m1, ref w, h); } } private void write(double ft, double x, int m1, ref double[] w, double h) { Console.WriteLine("I\tX(I)\tW(X(I),{0:#.#})", ft); for (int i = 1; i <= m1; i++) { x = i * h; Console.WriteLine("{0:#.##}\t{1:#.###}\t\t{2:#.###}", i, x, w[i - 1]); } } } }
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
don't working code:
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; namespace hard { class Program { static void Main(string[] args) { Program app = new Program(); } Program() { bool ok = false; double[] w = new double[25]; double[] l = new double[25]; double[] u = new double[25]; double[] z = new double[25]; double ft = 0.0, fx = 0.0, alpha = 0.0, h = 0.0, k = 0.0, vv = 0.0, t = 0.0, x = 0.0; int n = 0, m = 0, m1 = 0, m2 = 0, n1 = 0, flag = 0, i1 = 0, i = 0, j = 0 ; input(ref ok,ref fx,ref ft,ref alpha,ref n,ref m); if (ok) { m1 = m - 1; m2 = m - 2; // n1 = n - 1; /* step 1 */ h = fx / m; k = ft / n; vv = alpha * k / (h * h); /* step 2 */ for (i = 1; i <= m1; i++) w[i - 1] = F(i * h); /* step 3 */ l[0] = 1.0 - 2.0 * vv; u[0] = vv / l[0]; /* step 4 */ for (i = 2; i <= m2; i++) { l[i - 1] = 1.0 - 2.0 * vv - vv * u[i - 2]; u[i - 1] = vv / l[i - 1]; } /* step 5 */ l[m1 - 1] = 1.0 - 2.0 * vv - vv * u[m2 - 1]; /* step 6 */ for (j = 1; j <= n; j++) { /* step 7 */ /* current t(j) */ // t = j * k; z[0] = w[0] / l[0]; /* step 8 */ for (i = 2; i <= m1; i++) z[i - 1] = (w[i - 1] - vv * z[i - 2]) / l[i - 1]; /* step 9 */ w[m1 - 1] = z[m1 - 1]; /* step 10 */ for (i1 = 1; i1 <= m2; i1++) { i = m2 - i1 + 1; w[i - 1] = z[i - 1] - u[i - 1] * w[i]; } } /* step 11 */ output(ft, x, m1,ref w, h); } } private double F(double X) { double f; f = 0.3061 * X * X -2.1286*X+38.11; return f; } private void input(ref bool ok,ref double fx, ref double ft, ref double alpha, ref int n, ref int m ) { try { FileStream fs = new FileStream("d:\\a.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); List<string> lines = new List<string>(); String line; //add all the lines to a list while ((line = sr.ReadLine()) != null) { lines.Add(line); } //read the lines you would like to read: try { fx = double.Parse(lines[0]); ft = double.Parse(lines[1]); alpha = double.Parse(lines[2]); m = Int32.Parse(lines[3]); n = Int32.Parse(lines[4]); } catch { } } catch (Exception e) { Console.WriteLine("Exception in ShowFile: {0}", e); } } public void output(double ft, double x, int m1, ref double[] w, double h) { System.IO.StreamWriter oup; System.IO.TextWriter tmp = Console.Out; Console.WriteLine("Choice of output method."); Console.WriteLine("1. Output to screen"); Console.WriteLine("2. Output to text file"); int flag = Convert.ToInt32(Console.ReadLine()); if (flag == 2) { Console.WriteLine("Sample: D:\\1.txt"); string name = Console.ReadLine(); try { oup = new System.IO.StreamWriter(name); Console.SetOut(oup); write(ft, x, m1, ref w, h); oup.Close(); } catch (System.IO.IOException expc) { Console.WriteLine(expc.Message + " file dont extract."); } } else { write(ft, x, m1, ref w, h); } } private void write(double ft, double x, int m1, ref double[] w, double h) { Console.WriteLine("I\tX(I)\tW(X(I),{0:#.#})", ft); for (int i = 1; i <= m1; i++) { x = i * h; Console.WriteLine("{0:#.##}\t{1:#.###}\t\t{2:#.###}", i, x, w[i - 1]); } } } }
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
problem solved:
finally code:
finally code:
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Program app = new Program(); Console.ReadLine(); } Program() { bool ok = false; double[] w = new double[25]; double[] l = new double[25]; double[] u = new double[25]; double[] z = new double[25]; double ft = 0.0, fx = 0.0, alpha = 0.0, h = 0.0, k = 0.0, vv = 0.0, t = 0.0, x = 0.0; int n = 0, m = 0, m1 = 0, m2 = 0, n1 = 0, flag = 0, i1 = 0, i = 0, j = 0; input(ref ok, ref fx, ref ft, ref alpha, ref n, ref m); if (ok) { m1 = m - 1; m2 = m - 2; // n1 = n - 1; /* step 1 */ h = fx / m; k = ft / n; vv = alpha * k / (h * h); /* step 2 */ for (i = 1; i <= m1; i++) w[i - 1] = F(i * h); /* step 3 */ l[0] = 1.0 - 2.0 * vv; u[0] = vv / l[0]; /* step 4 */ for (i = 2; i <= m2; i++) { l[i - 1] = 1.0 - 2.0 * vv - vv * u[i - 2]; u[i - 1] = vv / l[i - 1]; } /* step 5 */ l[m1 - 1] = 1.0 - 2.0 * vv - vv * u[m2 - 1]; /* step 6 */ for (j = 1; j <= n; j++) { /* step 7 */ /* current t(j) */ // t = j * k; z[0] = w[0] / l[0]; /* step 8 */ for (i = 2; i <= m1; i++) z[i - 1] = (w[i - 1] - vv * z[i - 2]) / l[i - 1]; /* step 9 */ w[m1 - 1] = z[m1 - 1]; /* step 10 */ for (i1 = 1; i1 <= m2; i1++) { i = m2 - i1 + 1; w[i - 1] = z[i - 1] - u[i - 1] * w[i]; } } /* step 11 */ output(ft, x, m1, ref w, h); } } private double F(double X) { double f; f = 0.3061 * X * X - 2.1286 * X + 38.11; return f; } private void input(ref bool ok, ref double fx, ref double ft, ref double alpha, ref int n, ref int m) { try { FileStream fs = new FileStream("D:\\1.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); string[] lines = new string[10]; // you cannot use list like that string line; //add all the lines to a list int i = 0; while ((line = sr.ReadLine()) != null) { lines[i++] = (line); } //read the lines you would like to read: fx = double.Parse(lines[0]); ft = double.Parse(lines[1]); alpha = double.Parse(lines[2]); m = Int32.Parse(lines[3]); n = Int32.Parse(lines[4]); } catch (Exception e) { Console.WriteLine("Exception in ShowFile: {0}", e); } ok = true; } public void output(double ft, double x, int m1, ref double[] w, double h) { System.IO.StreamWriter oup; System.IO.TextWriter tmp = Console.Out; Console.WriteLine("Choice of output method."); Console.WriteLine("1. Output to screen"); Console.WriteLine("2. Output to text file"); int flag = Convert.ToInt32(Console.ReadLine()); if (flag == 2) { Console.WriteLine("Sample: C:\\Data\\1.txt"); string name = Console.ReadLine(); try { name = name.Replace(@"\", @"\\"); // look at the changes in the path oup = new System.IO.StreamWriter(name); Console.SetOut(oup); write(ft, x, m1, ref w, h); oup.Close(); } catch (System.IO.IOException expc) { Console.WriteLine(expc.Message + " file dont extract."); } } else { write(ft, x, m1, ref w, h); } } private void write(double ft, double x, int m1, ref double[] w, double h) { Console.WriteLine("I\tX(I)\tW(X(I),{0:#.#})", ft); for (int i = 1; i <= m1; i++) { x = i * h; Console.WriteLine("{0:#.##}\t{1:#.###}\t\t{2:#.###}", i, x, w[i - 1]); } } } }
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Thats a heck of a lot of code.
Work with something smaller, its easier for us, and easier for you as you end up with a small snippet with only whats needed in it
If you just read the files from console, its much harder because someone could write
"..\..\..\..\..\..\..\..\..\dir1\dir2\a.txt"
if you did that using the filepicker of course it would return you a full path to the file.
But, in the example above if your current directory was
c:\dir3\
then the new path would be c:\dir1\dir2\a.txt
but if the current directory was
c:\dir1\dir3
the new path would still be c:\dir1\dir2\a.txt
sounds like you should find a way of parsing the directories
Work with something smaller, its easier for us, and easier for you as you end up with a small snippet with only whats needed in it
If you just read the files from console, its much harder because someone could write
"..\..\..\..\..\..\..\..\..\dir1\dir2\a.txt"
if you did that using the filepicker of course it would return you a full path to the file.
But, in the example above if your current directory was
c:\dir3\
then the new path would be c:\dir1\dir2\a.txt
but if the current directory was
c:\dir1\dir3
the new path would still be c:\dir1\dir2\a.txt
sounds like you should find a way of parsing the directories
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- Automating Installations Using Windows Setup Manager (Windows tips 'n' tweaks)
- Problem in netbeans with mysql query. (JSP)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Help me with my myspace clone (PHP)
- Gets all information in listView into a binary file/String variables/textBox in VC++ (C++)
- Dr Scheme Tutorial (Computer Science)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Help needed removing coldfusion.112 (Viruses, Spyware and other Nasties)
Other Threads in the C# Forum
- Previous Thread: Displaying Images in a picture box
- Next Thread: how to avoid code repetition here?
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml






