943,793 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 580
  • C# RSS
Mar 11th, 2009
0

how can I show the file path instead of ref

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5.  
  6. namespace hard
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Program app = new Program();
  13. }
  14.  
  15. Program()
  16. {
  17. bool ok = false
  18. double[] w = new double[25];
  19. double[] l = new double[25];
  20. double[] u = new double[25];
  21. double[] z = new double[25];
  22. 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;
  23. int n = 0, m = 0, m1 = 0, m2 = 0, n1 = 0, flag = 0, i1 = 0, i = 0, j = 0 ;
  24. input(ref ok,ref fx,ref ft,ref alpha,ref n,ref m);
  25.  
  26. if (ok)
  27. {
  28. m1 = m - 1;
  29. m2 = m - 2;
  30. // n1 = n - 1;
  31. /* step 1 */
  32. h = fx / m;
  33. k = ft / n;
  34. vv = alpha * k / (h * h);
  35. /* step 2 */
  36. for (i = 1; i <= m1; i++) w[i - 1] = F(i * h);
  37. /* step 3 */
  38. l[0] = 1.0 - 2.0 * vv;
  39. u[0] = vv / l[0];
  40. /* step 4 */
  41. for (i = 2; i <= m2; i++)
  42. {
  43. l[i - 1] = 1.0 - 2.0 * vv - vv * u[i - 2];
  44. u[i - 1] = vv / l[i - 1];
  45. }
  46. /* step 5 */
  47. l[m1 - 1] = 1.0 - 2.0 * vv - vv * u[m2 - 1];
  48. /* step 6 */
  49. for (j = 1; j <= n; j++)
  50. {
  51. /* step 7 */
  52. /* current t(j) */
  53. // t = j * k;
  54. z[0] = w[0] / l[0];
  55. /* step 8 */
  56.  
  57. for (i = 2; i <= m1; i++)
  58. z[i - 1] = (w[i - 1] - vv * z[i - 2]) / l[i - 1];
  59. /* step 9 */
  60. w[m1 - 1] = z[m1 - 1];
  61. /* step 10 */
  62. for (i1 = 1; i1 <= m2; i1++)
  63. {
  64. i = m2 - i1 + 1;
  65. w[i - 1] = z[i - 1] - u[i - 1] * w[i];
  66. }
  67. }
  68. /* step 11 */
  69. output(ft, x, m1,ref w, h);
  70. }
  71.  
  72. }
  73.  
  74.  
  75. private double F(double X)
  76. {
  77. double f;
  78. f = 0.3061 * X * X -2.1286*X+38.11;
  79. return f;
  80. }
  81.  
  82. private void input(ref bool ok,ref double fx, ref double ft, ref double alpha, ref int n, ref int m )
  83. {
  84. Console.Write("Has the function F been created immediately (Y/N) : ");
  85. string sonuc = Console.ReadLine();
  86.  
  87. if (sonuc.ToUpper() == "Y")
  88. {
  89. Console.WriteLine("The lefthand endpoint on the X-axis is 0.");
  90. ok = false;
  91. while (!ok)
  92. {
  93. Console.Write("Input the righthand endpoint on the X-axis: ");
  94. fx = Convert.ToDouble(Console.ReadLine());
  95. if (fx < 0.0)
  96. Console.WriteLine("Must be positive number.");
  97. else
  98. ok = true;
  99. }
  100. ok = false;
  101. while (!ok)
  102. {
  103. Console.Write("Input the maximum value of the time variable T : ");
  104. ft = Convert.ToDouble(Console.ReadLine());
  105. if (ft < 0.0)
  106. Console.WriteLine("Must be positive number.");
  107. else
  108. ok = true;
  109. }
  110. Console.Write("Input the constant alpha: ");
  111. alpha = Convert.ToDouble(Console.ReadLine());
  112. Console.WriteLine("Input integer m = number of intervals”);
  113. Console.WriteLine("input N = number of time intervals");
  114. Console.WriteLine("Note :must be m>3 ve n>0 ");
  115. ok = false;
  116. while (!ok)
  117. {
  118. Console.Write("m (>3) = ");
  119. m = Convert.ToInt32(Console.ReadLine());
  120. Console.Write("n (>0) = ");
  121. n = Convert.ToInt32(Console.ReadLine());
  122. if (m <= 2 || n <= 0)
  123. {
  124. Console.WriteLine("Numbers are not within correct range.");
  125. }
  126. else
  127. {
  128. ok = true;
  129. }
  130. }
  131. }
  132. else
  133. {
  134. Console.WriteLine("The program will end so that the function F can be created.");
  135. ok = false;
  136. }
  137. }
  138.  
  139. public void output(double ft, double x, int m1, ref double[] w, double h)
  140. {
  141. System.IO.StreamWriter oup;
  142. System.IO.TextWriter tmp = Console.Out;
  143. Console.WriteLine("Choice of output method.");
  144. Console.WriteLine("1. Output to screen");
  145. Console.WriteLine("2. Output to text file");
  146. int flag = Convert.ToInt32(Console.ReadLine());
  147. if (flag == 2)
  148. {
  149.  
  150. Console.WriteLine("Sample: D:\\1.txt");
  151. string name = Console.ReadLine();
  152. try
  153. {
  154.  
  155. oup = new System.IO.StreamWriter(name);
  156. Console.SetOut(oup);
  157. write(ft, x, m1, ref w, h);
  158. oup.Close();
  159. }
  160. catch (System.IO.IOException expc)
  161. {
  162. Console.WriteLine(expc.Message + " file dont extract.");
  163. }
  164. }
  165. else
  166. {
  167. write(ft, x, m1, ref w, h);
  168. }
  169.  
  170.  
  171. }
  172.  
  173. private void write(double ft, double x, int m1, ref double[] w, double h)
  174. {
  175.  
  176. Console.WriteLine("I\tX(I)\tW(X(I),{0:#.#})", ft);
  177. for (int i = 1; i <= m1; i++)
  178. {
  179. x = i * h;
  180. Console.WriteLine("{0:#.##}\t{1:#.###}\t\t{2:#.###}", i, x, w[i - 1]);
  181. }
  182. }
  183. }
  184. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahmedo047 is offline Offline
8 posts
since Apr 2008
Mar 11th, 2009
0

Re: how can I show the file path instead of ref

don't working code:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.IO;
  6.  
  7. namespace hard
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Program app = new Program();
  14. }
  15.  
  16. Program()
  17. {
  18. bool ok = false;
  19. double[] w = new double[25];
  20. double[] l = new double[25];
  21. double[] u = new double[25];
  22. double[] z = new double[25];
  23. 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;
  24. int n = 0, m = 0, m1 = 0, m2 = 0, n1 = 0, flag = 0, i1 = 0, i = 0, j = 0 ;
  25. input(ref ok,ref fx,ref ft,ref alpha,ref n,ref m);
  26.  
  27. if (ok)
  28. {
  29. m1 = m - 1;
  30. m2 = m - 2;
  31. // n1 = n - 1;
  32. /* step 1 */
  33. h = fx / m;
  34. k = ft / n;
  35. vv = alpha * k / (h * h);
  36. /* step 2 */
  37. for (i = 1; i <= m1; i++) w[i - 1] = F(i * h);
  38. /* step 3 */
  39. l[0] = 1.0 - 2.0 * vv;
  40. u[0] = vv / l[0];
  41. /* step 4 */
  42. for (i = 2; i <= m2; i++)
  43. {
  44. l[i - 1] = 1.0 - 2.0 * vv - vv * u[i - 2];
  45. u[i - 1] = vv / l[i - 1];
  46. }
  47. /* step 5 */
  48. l[m1 - 1] = 1.0 - 2.0 * vv - vv * u[m2 - 1];
  49. /* step 6 */
  50. for (j = 1; j <= n; j++)
  51. {
  52. /* step 7 */
  53. /* current t(j) */
  54. // t = j * k;
  55. z[0] = w[0] / l[0];
  56. /* step 8 */
  57.  
  58. for (i = 2; i <= m1; i++)
  59. z[i - 1] = (w[i - 1] - vv * z[i - 2]) / l[i - 1];
  60. /* step 9 */
  61. w[m1 - 1] = z[m1 - 1];
  62. /* step 10 */
  63. for (i1 = 1; i1 <= m2; i1++)
  64. {
  65. i = m2 - i1 + 1;
  66. w[i - 1] = z[i - 1] - u[i - 1] * w[i];
  67. }
  68. }
  69. /* step 11 */
  70. output(ft, x, m1,ref w, h);
  71. }
  72.  
  73. }
  74.  
  75.  
  76. private double F(double X)
  77. {
  78. double f;
  79. f = 0.3061 * X * X -2.1286*X+38.11;
  80. return f;
  81. }
  82.  
  83. private void input(ref bool ok,ref double fx, ref double ft, ref double alpha, ref int n, ref int m )
  84. {
  85. try
  86. {
  87. FileStream fs = new FileStream("d:\\a.txt", FileMode.Open);
  88. StreamReader sr = new StreamReader(fs);
  89.  
  90. List<string> lines = new List<string>();
  91. String line;
  92. //add all the lines to a list
  93. while ((line = sr.ReadLine()) != null)
  94. {
  95. lines.Add(line);
  96. }
  97. //read the lines you would like to read:
  98. try
  99. {
  100. fx = double.Parse(lines[0]);
  101. ft = double.Parse(lines[1]);
  102. alpha = double.Parse(lines[2]);
  103. m = Int32.Parse(lines[3]);
  104. n = Int32.Parse(lines[4]);
  105. }
  106. catch { }
  107. }
  108. catch (Exception e)
  109. {
  110. Console.WriteLine("Exception in ShowFile: {0}", e);
  111. }
  112.  
  113. }
  114.  
  115. public void output(double ft, double x, int m1, ref double[] w, double h)
  116. {
  117. System.IO.StreamWriter oup;
  118. System.IO.TextWriter tmp = Console.Out;
  119. Console.WriteLine("Choice of output method.");
  120. Console.WriteLine("1. Output to screen");
  121. Console.WriteLine("2. Output to text file");
  122. int flag = Convert.ToInt32(Console.ReadLine());
  123. if (flag == 2)
  124. {
  125.  
  126. Console.WriteLine("Sample: D:\\1.txt");
  127. string name = Console.ReadLine();
  128. try
  129. {
  130.  
  131. oup = new System.IO.StreamWriter(name);
  132. Console.SetOut(oup);
  133. write(ft, x, m1, ref w, h);
  134. oup.Close();
  135. }
  136. catch (System.IO.IOException expc)
  137. {
  138. Console.WriteLine(expc.Message + " file dont extract.");
  139. }
  140. }
  141. else
  142. {
  143. write(ft, x, m1, ref w, h);
  144. }
  145.  
  146.  
  147. }
  148.  
  149. private void write(double ft, double x, int m1, ref double[] w, double h)
  150. {
  151.  
  152. Console.WriteLine("I\tX(I)\tW(X(I),{0:#.#})", ft);
  153. for (int i = 1; i <= m1; i++)
  154. {
  155. x = i * h;
  156. Console.WriteLine("{0:#.##}\t{1:#.###}\t\t{2:#.###}", i, x, w[i - 1]);
  157. }
  158. }
  159. }
  160. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahmedo047 is offline Offline
8 posts
since Apr 2008
Mar 11th, 2009
0

Re: how can I show the file path instead of ref

I dont have any error message.
I want to run the code by showing file path d:\\a.txt insted of input each of values from screen.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahmedo047 is offline Offline
8 posts
since Apr 2008
Mar 11th, 2009
0

Re: how can I show the file path instead of ref

problem solved:

finally code:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.IO;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Program app = new Program();
  14. Console.ReadLine();
  15. }
  16.  
  17. Program()
  18. {
  19. bool ok = false;
  20. double[] w = new double[25];
  21. double[] l = new double[25];
  22. double[] u = new double[25];
  23. double[] z = new double[25];
  24. 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;
  25. int n = 0, m = 0, m1 = 0, m2 = 0, n1 = 0, flag = 0, i1 = 0, i = 0, j = 0;
  26. input(ref ok, ref fx, ref ft, ref alpha, ref n, ref m);
  27.  
  28. if (ok)
  29. {
  30. m1 = m - 1;
  31. m2 = m - 2;
  32. // n1 = n - 1;
  33. /* step 1 */
  34. h = fx / m;
  35. k = ft / n;
  36. vv = alpha * k / (h * h);
  37. /* step 2 */
  38. for (i = 1; i <= m1; i++) w[i - 1] = F(i * h);
  39. /* step 3 */
  40. l[0] = 1.0 - 2.0 * vv;
  41. u[0] = vv / l[0];
  42. /* step 4 */
  43. for (i = 2; i <= m2; i++)
  44. {
  45. l[i - 1] = 1.0 - 2.0 * vv - vv * u[i - 2];
  46. u[i - 1] = vv / l[i - 1];
  47. }
  48. /* step 5 */
  49. l[m1 - 1] = 1.0 - 2.0 * vv - vv * u[m2 - 1];
  50. /* step 6 */
  51. for (j = 1; j <= n; j++)
  52. {
  53. /* step 7 */
  54. /* current t(j) */
  55. // t = j * k;
  56. z[0] = w[0] / l[0];
  57. /* step 8 */
  58.  
  59. for (i = 2; i <= m1; i++)
  60. z[i - 1] = (w[i - 1] - vv * z[i - 2]) / l[i - 1];
  61. /* step 9 */
  62. w[m1 - 1] = z[m1 - 1];
  63. /* step 10 */
  64. for (i1 = 1; i1 <= m2; i1++)
  65. {
  66. i = m2 - i1 + 1;
  67. w[i - 1] = z[i - 1] - u[i - 1] * w[i];
  68. }
  69. }
  70. /* step 11 */
  71. output(ft, x, m1, ref w, h);
  72. }
  73.  
  74. }
  75.  
  76.  
  77. private double F(double X)
  78. {
  79. double f;
  80. f = 0.3061 * X * X - 2.1286 * X + 38.11;
  81. return f;
  82. }
  83.  
  84. private void input(ref bool ok, ref double fx, ref double ft, ref double alpha, ref int n, ref int m)
  85. {
  86. try
  87. {
  88. FileStream fs = new FileStream("D:\\1.txt", FileMode.Open);
  89. StreamReader sr = new StreamReader(fs);
  90.  
  91. string[] lines = new string[10]; // you cannot use list like that
  92. string line;
  93. //add all the lines to a list
  94. int i = 0;
  95. while ((line = sr.ReadLine()) != null)
  96. {
  97. lines[i++] = (line);
  98. }
  99. //read the lines you would like to read:
  100.  
  101. fx = double.Parse(lines[0]);
  102. ft = double.Parse(lines[1]);
  103. alpha = double.Parse(lines[2]);
  104. m = Int32.Parse(lines[3]);
  105. n = Int32.Parse(lines[4]);
  106.  
  107. }
  108. catch (Exception e)
  109. {
  110. Console.WriteLine("Exception in ShowFile: {0}", e);
  111. }
  112. ok = true;
  113. }
  114.  
  115. public void output(double ft, double x, int m1, ref double[] w, double h)
  116. {
  117. System.IO.StreamWriter oup;
  118. System.IO.TextWriter tmp = Console.Out;
  119. Console.WriteLine("Choice of output method.");
  120. Console.WriteLine("1. Output to screen");
  121. Console.WriteLine("2. Output to text file");
  122. int flag = Convert.ToInt32(Console.ReadLine());
  123. if (flag == 2)
  124. {
  125.  
  126. Console.WriteLine("Sample: C:\\Data\\1.txt");
  127. string name = Console.ReadLine();
  128. try
  129. {
  130. name = name.Replace(@"\", @"\\"); // look at the changes in the path
  131. oup = new System.IO.StreamWriter(name);
  132. Console.SetOut(oup);
  133. write(ft, x, m1, ref w, h);
  134. oup.Close();
  135. }
  136. catch (System.IO.IOException expc)
  137. {
  138. Console.WriteLine(expc.Message + " file dont extract.");
  139. }
  140. }
  141. else
  142. {
  143. write(ft, x, m1, ref w, h);
  144. }
  145.  
  146.  
  147. }
  148.  
  149. private void write(double ft, double x, int m1, ref double[] w, double h)
  150. {
  151.  
  152. Console.WriteLine("I\tX(I)\tW(X(I),{0:#.#})", ft);
  153. for (int i = 1; i <= m1; i++)
  154. {
  155. x = i * h;
  156. Console.WriteLine("{0:#.##}\t{1:#.###}\t\t{2:#.###}", i, x, w[i - 1]);
  157. }
  158. }
  159. }
  160. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahmedo047 is offline Offline
8 posts
since Apr 2008
Mar 11th, 2009
0

Re: how can I show the file path instead of ref

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Displaying Images in a picture box
Next Thread in C# Forum Timeline: how to avoid code repetition here?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC