PDA

View Full Version : سوال: اشکال برنامم رو کسی میتونه بگیره؟(تبدیل درجه سلسیوس به فارنهایت،حل معادله درجه دو، بخشپذیری)



h_zamani
جمعه 01 آبان 1388, 22:31 عصر
سلام ..من این برنامه هام کار نمیکنند..کسی میتونه کمکم کنه؟ البته من تو محیط ویژوال فعلا کار میکنم.هرچه زودتر جوابم رو بدین ممنون میشم :
این برنامم باید درجه حرارت و واحدش رو ( فارنهایت یا سانتی گراد) از کاربر بگیره و به واحد دیگه (سانتی گراد یا فارنهایت)تبدیل کنه ولی نمیدونم چرا هی ارور میده به f و c ؟؟؟؟
class Program
{
static void Main(string[] args)
{
double t, c, f;
char u;
Console.Write("temperature:");
t = Convert.ToInt64(Console.ReadLine());
Console.Write("press c for centigrade & other for farenheit:");
u = Convert.ToChar(Console.ReadLine());
if (u == c)
f = (1.8*c) + 32;
else
c = (f - 32)/1.8;
Console.WriteLine("degree:{0}", f, c);
}
}
---------------------------------------------------------------------------------------------------
این یکی برنامه حل معادله درجه 2 هست که باز هم کار نمیکنه:
class Program
{
static void Main(string[] args)
{
int a, b, c, d, x1, x2;
Console.Write("Enter a:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter b:");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter c:");
c = Convert.ToInt32(Console.ReadLine());
d = b*b - 4*c;
if (d <= 0)
{
if (d < 0)
Console.Write("the equation doesnt have any roots");
else
x1 = x2 = -b/(2*a);
}
else
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
Console.WriteLine("x1 = {0} \n x2={1}", x1, x2);
}
}
---------------------------------------------------------------------------------------------------

این یکی هم برنامه ایه که عدد رو میدیم و اگه بر پنج بخشپذیر بود میده 't' و در غیر اینصورت میده 'f':
class Program
{
static void Main(string[] args)
{
double a;
double answer;
Console.Write(" Enter a:");
a = Convert.ToInt32(Console.ReadLine());
if ((a % 5) = 0)
{
answer = 't';
}
else
{
answer = 'f';
}
Console.WriteLine("answer:{0}", answer);
}
}

sara.f
جمعه 01 آبان 1388, 23:48 عصر
سلام
من یکی که از کدهات سر در نیاوردم، لطفا کدهایت را در تگ کد قرار بده تا قابل خواندن باشند و در ضمن بگو چه خطایی دارند؟ متن خطا را در اینجا قرار بده.

esmartiz_red
جمعه 01 آبان 1388, 23:55 عصر
اگه خود برنامه رو اینجا یذاری که خیلی بهتره از بایت copyright هم نگران نباش دوستان رعایت می کنن :بامزه::چشمک:

khmahdi
سه شنبه 26 آبان 1388, 05:19 صبح
در مورد برنامه ی اول (فارنهایت):
شما قبل از اینکه متغیر های c و f رو مقدار دهی کنید ، از اونها استفاده کردید، واین باعث ایجاد خطای :
Use of unassigned local variable
می شه،پس اول اونارو مقدار دهی کنید؛
در مورد برنامه ی دوم(معادله درجه دو):
برای استفاده از تابع جذر(()sqrt) باید نام کلاس والد اون روهم نام ببرید:
Math.sqrt(d);
در مورد برنامه ی سوم(بخش پذیری):
باید به جای:
if ((a % 5) = 0)
این کد را بنویسید:
if ((a % 5) == 0)
چون باید یک عبارت منطقی (بولی) ایجاد بشه و "=" در عبارت منطقی معنایی نداره
این جوری که پیداست syntax شما خیلی قوی نیست ،قبل شروع برنامه نویسی روش کار کنید
اگه بازم سؤالی بود در خدمتم
یا علی

misoft.ir
سه شنبه 26 آبان 1388, 06:21 صبح
سلام
ببین این به دردت میخوره؟




20.7 Converting Between Temperature Scales
Problem
You have a temperature reading measured in one temperature scale and need to convert
it to another scale.
Solution
To convert between Celsius, Fahrenheit, and Kelvin, use the following methods:
public static double CelsiusToFahrenheit(double celsius)
{
return (1.8 * celsius) + 32;
}
public static double FahrenheitToCelsius(double fahrenheit)
{
return 1.8 * (fahrenheit - 32);
}
public static double CelsiusToKelvin(double celsius)
{
return celsius + 273;
}
public static double KelvinToCelsius(double kelvin)
{
return kelvin - 273;
}
public static double FahrenheitToKelvin(double fahrenheit)
{
return CelsiusToKelvin(FahrenheitToCelsius(fahrenheit));
}
public static double KelvinToFahrenheit(double kelvin)
{
return CelsiusToFahrenheit(KelvinToCelsius(kelvin));
}
802 | Chapter 20: Numbers and Enumerations
Discussion
There are three main temperature scales that are in use today: Celsius, Fahrenheit,
and Kelvin. The Celsius scale (˚C) is used in most of the world to measure air temperatures.
In the United States, the Fahrenheit scale (˚F) is used to measure temperaturesat
or near the surface, while the Celsius scale is used to measure upper air
temperatures. The Kelvin (K) scale is used by scientists and for astronomical
temperatures.
All three temperature scales are related to each other through the “triple
point of water.” The triple point of water isthe temperature at
which water vapor, liquid water, and ice can coexist simultaneously.
The triple point occurs at 0.01 ˚C (273.16 K or 32.02 ˚F).




از کتاب C# 3.0 Cookbook کپی کردم

اَرژنگ
سه شنبه 26 آبان 1388, 08:03 صبح
سلام
ببین این به دردت میخوره؟




20.7 Converting Between Temperature Scales
Problem
You have a temperature reading measured in one temperature scale and need to convert
it to another scale.
Solution
To convert between Celsius, Fahrenheit, and Kelvin, use the following methods:
public static double CelsiusToFahrenheit(double celsius)
{
return (1.8 * celsius) + 32;
}
public static double FahrenheitToCelsius(double fahrenheit)
{
return 1.8 * (fahrenheit - 32);
}
public static double CelsiusToKelvin(double celsius)
{
return celsius + 273;
}
public static double KelvinToCelsius(double kelvin)
{
return kelvin - 273;
}
public static double FahrenheitToKelvin(double fahrenheit)
{
return CelsiusToKelvin(FahrenheitToCelsius(fahrenheit));
}
public static double KelvinToFahrenheit(double kelvin)
{
return CelsiusToFahrenheit(KelvinToCelsius(kelvin));
}
802 | Chapter 20: Numbers and Enumerations
Discussion
There are three main temperature scales that are in use today: Celsius, Fahrenheit,
and Kelvin. The Celsius scale (˚C) is used in most of the world to measure air temperatures.
In the United States, the Fahrenheit scale (˚F) is used to measure temperaturesat
or near the surface, while the Celsius scale is used to measure upper air
temperatures. The Kelvin (K) scale is used by scientists and for astronomical
temperatures.
All three temperature scales are related to each other through the “triple
point of water.” The triple point of water isthe temperature at
which water vapor, liquid water, and ice can coexist simultaneously.
The triple point occurs at 0.01 ˚C (273.16 K or 32.02 ˚F).





از کتاب C#‎‎‎‎‎‎‎‎‎ 3.0 Cookbook کپی کردم





با تشکر از زحمتتان،
این مثال خوبی است برایه روشهایه غیره‌شیگرائی، واقعا هم اسمه کتاب برازنده روشش است (به آشپزی بیشتر فایده دارد تا برنامه‌نویسی :)
متاسفانه پیدا کردن کتابی که بنا بر اصول درست باشد خیلی سخته.
استفاده زیادی از متدهایه استاتیک بازگشتی است به برنامه‌نویسی قبل از شیگرا، و مثال خوبی است که فقط استفاده از یک زبان شیگرا نیست که برنامه‌ را شیگرا و یا غیره شیگرا میکند بلکه روشهایه استفاده هم مهمند، این مثال خوبی است برایه مبتدیان که از روشهایه غیره شیگرا که حتی ممکن است با زبان شیگرا پیاده شده باشد مطلع باشند.

روش درست :



public class Temperature
{
private double mKelvin;
public double Kelvin
{
get { return this.mKelvin; }
set { this.mKelvin = value; }
}

public double Celsius
{
get { return this.mKelvin - 273; }
set
{
mKelvin = value + 273;
}
}

public double Fahrenheit
{
get { return (5 / 9) * Celsius + 32; }
set
{
Celsius = (9 / 5) * (value - 32);
}
}
}

//Use it like this
Temperature aTemperature = new Temperature() { Celsius = 0 };
MessageBox.Show(Convert.ToString(aTemperature.Fahr enheit));
MessageBox.Show(Convert.ToString(aTemperature.Kelv in));





در ضمن این هم در کتاب اشتباه بود:



public static double FahrenheitToCelsius(double fahrenheit)
{
return 1.8 * (fahrenheit - 32);
}


درستش (از لحاظ ریاضی نه از لحاظ برنامه‌نویسی) اینه:



public static double FahrenheitToCelsius(double fahrenheit)
{
return (fahrenheit - 32) / 1.8;
}