المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : ســـؤال فـي الـ ++C أرجو الرد بسرعة اليوم اختباري



Silent Sniper
22-11-2006, 02:02 AM
السلام عليكم ورحمة الله وبركاته

عندي سؤال بسيط في المكتبات الجاهزة

مثل الدالة fixed الموجودة في المكتبة <iostream> ماذا تعمل؟؟

وايضا الدالة setprecision() في المكتبة <iomanip> ماهو عملها ايضا بالتفصيل؟؟

ارجو المساعدة ضروووري ..

RAAAAD
22-11-2006, 07:03 AM
بإختصار شديد .. لأنه لا يوجد لدي وقت ...


مثل الدالة fixed الموجودة في المكتبة <iostream> ماذا تعمل؟؟

تعمل على تحويل العدد من الشكل التالي وهو scientific number

1.100000e+000

إلى الشكل التالي .. Default Number

1.1

أنظر إلى الـ help الموجود في الـ .net ستري التالي :





Specifies that a floating-point number is displayed in fixed-decimal notation.
ios_base& fixed( ios_base& _Str (http://www.montada.com/));
Parameter


_Str
A reference to an object of type ios_base (http://www.montada.com/ms-help://MS.VSCC/MS.MSDNVS/vcstdlib/html/vclrfiosbase_class.htm), or to a type that inherits from ios_base.
Return Value


A reference to the object from which _Str is derived.
Remarks


fixed is the default display notation for floating-point numbers. scientific (http://www.montada.com/ms-help://MS.VSCC/MS.MSDNVS/vcstdlib/html/vclrfscientific_ios.htm) causes floating-point numbers to be displayed using scientific notation.
The manipulator effectively calls _Str.setf (http://www.montada.com/ms-help://MS.VSCC/MS.MSDNVS/vcstdlib/html/vclrfiosbasesetf.htm)(ios_base:: fixed (http://www.montada.com/ms-help://MS.VSCC/MS.MSDNVS/vcstdlib/html/vclrfiosbasefmtflags.htm#vclrfiosbasefixed), ios_base:: floatfield (http://www.montada.com/ms-help://MS.VSCC/MS.MSDNVS/vcstdlib/html/vclrfiosbasefmtflags.htm#vclrfiosbasefloatfield)), and then returns _Str.
Example


// ios_fixed.cpp// compile with: /EHsc#include <iostream>int main( ) { using namespace std; float i = 1.1; cout << i << endl; // fixed is the default cout << scientific << i << endl; cout.precision( 1 ); cout << fixed << i << endl;}
Output


1.11.100000e+0001.1




===================================



وايضا الدالة setprecision() في المكتبة <iomanip> ماهو عملها ايضا بالتفصيل؟؟

هذا المثال سيوضح كل شئ ...

#include <iostream>
#include <iomanip>
using namespace std;
const double d1 = 1.23456789;
const double d2 = 12.3456789;
const double d3 = 123.456789;
const double d4 = 1234.56789;
const double d5 = 12345.6789;
const long l1 = 16;
const long l2 = 256;
const long l3 = 1024;
const long l4 = 4096;
const long l5 = 65536;
int base = 10;
void DisplayDefault( )
{
cout << endl << "default display" << endl;
cout << "d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl;
cout << "d3 = " << d3 << endl;
cout << "d4 = " << d4 << endl;
cout << "d5 = " << d5 << endl;
}
void DisplayWidth( int n )
{
cout << endl << "fixed width display set to " << n << ".\n";
cout << "d1 = " << setw(n) << d1 << endl;
cout << "d2 = " << setw(n) << d2 << endl;
cout << "d3 = " << setw(n) << d3 << endl;
cout << "d4 = " << setw(n) << d4 << endl;
cout << "d5 = " << setw(n) << d5 << endl;
}
void DisplayLongs( )
{
cout << setbase(10);
cout << endl << "setbase(" << base << ")" << endl;
cout << setbase(base);
cout << "l1 = " << l1 << endl;
cout << "l2 = " << l2 << endl;
cout << "l3 = " << l3 << endl;
cout << "l4 = " << l4 << endl;
cout << "l5 = " << l5 << endl;
}
int main( int argc, char* argv[] )
{
DisplayDefault( );
cout << endl << "setprecision(" << 3 << ")" << setprecision(3);
DisplayDefault( );
cout << endl << "setprecision(" << 12 << ")" << setprecision(12);
DisplayDefault( );
cout << setiosflags(ios_base::scientific);
cout << endl << "setiosflags(" << ios_base::scientific << ")";
DisplayDefault( );
cout << resetiosflags(ios_base::scientific);
cout << endl << "resetiosflags(" << ios_base::scientific << ")";
DisplayDefault( );
cout << endl << "setfill('" << 'S' << "')" << setfill('S');
DisplayWidth(15);
DisplayDefault( );
cout << endl << "setfill('" << ' ' << "')" << setfill(' ');
DisplayWidth(15);
DisplayDefault( );
cout << endl << "setprecision(" << 8 << ")" << setprecision(8);
DisplayWidth(10);
DisplayDefault( );
base = 16;
DisplayLongs( );
base = 8;
DisplayLongs( );
base = 10;
DisplayLongs( );
return 0;
}

RAAAAD
22-11-2006, 07:18 AM
يعني نستطيع القول بأن الدالتان fixed و setprecision

يستخدمان من أجل الحكم بدقة الأعداد الـ decimal

بالتوفيق

Silent Sniper
23-11-2006, 12:55 PM
أشكرك اخي RAAAAD على الشرح الواضح وابشرك فهمته :>
والله يكثر من امثالك صراحة قدمت لي خدمة العمر
تحياتي لك