C # -- 2nd week experiment -- Task 3 -- compile a console application -- input x to calculate and output the value of the piecewise function F (x)

Source: Internet
Author: User

/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: compile a console application -- enter x to calculate and output the value of the piecewise function F (x ).
* Author: Lei hengxin
* Completion date: January 1, September 08, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
 
 
[Csharp]
<Span style = "font-size: 24px;"> using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication_read_string
{
Class Program
{
Static void Main (string [] args)
{
Double d; // If double is not added
Char c = 'a'; // cyclic control variable
Console. Write ("enter a number :");
String s = Console. ReadLine ();
Double x = double. Parse (s );
While (! C. Equals ('E') // end the loop when the character c is equal to e
{
If (x! = 0)
{
D = 1/x; // If double is added
}
Else
{
D = 0; // If double is not added, the system prompts that the name "d" does not exist in the current context ";
}
// Double d1 = double. Parse (d. ToString ("1F "));
// String d1 = Convert. ToDecimal ("d"). ToString ("0.00 ");
// String bb = d. ToString (). Substring (0, d. ToString (). LastIndexOf (".") + 3 );
// String bb = d. ToString (). Substring (0, d. ToString (). LastIndexOf (".") + 3 <d .()? D. Length: d. ToString (). LastIndexOf (".") + 3 );
Console. WriteLine ("\ r \ n is not rounded in, the value of the piecewise function f (x) is: {0}", Math. Round (d, 1 ));
// Console. WriteLine ("\ r \ n rounding, the value of the piecewise function f (x) is: {0}", d1 );
Console. Write ("do you want to continue the operation? Press "e" to end the operation. Press another key to continue the operation! ");
C = Console. ReadKey (). KeyChar;
Console. WriteLine ();
If (c. Equals ('E '))
{
Break;
}
Else
{
Console. Write ("enter a number :");
S = Console. ReadLine ();
X = double. Parse (s );
}
 
}
Console. ReadKey (false );
}
}
}
</Span>

Running result:

 
Experience Accumulation:
1. either of the following methods can forcibly convert a character to a double type.
[Csharp]
<Span style = "font-size: 24px;"> x = Convert. ToDouble (s );
X = double. Parse (s); </span>
 
Problem:
1. "Which function is used in C # To retain two decimal places ?"
Math. Round (d, 1) only gave up the last one without rounding it.
The methods provided on the network are not feasible, and it is a bit difficult to compile by yourself.
Please try to find out if there are any ready-made functions available.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.