الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف يكتب هذا البرنامج ( طباعة اسم النوع )

مغلق
بدأه C++er في 25 سبتمبر 2013 · 3 رد · 1,349 مشاهدة · في اختبر قداراتك
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

تحتوى لغة c++03 على 17 نوع بيانات أساسي و هم:

char
signed char
unsigned char
wchar_t

signed short int
unsigned short int
signed int
unsigned int
signed long int
unsigned long int
signed long long int
unsigned long long int

float
double
long double

bool
void*

و في إصدار c++11 تم إضافة النوعين:

char16_tchar32_t

المطلوب كتابة برنامج كالتالي:

#include <iostream>
using namespace std;

typedef type_name type;

int main()
{
    cout << write_name;
}

حيث ستقوم الدالة write_name - و التى ستقوم بكتابتها - بطباعة الاسم الخاص بالنوع type و الذى قيمته يمكن ان تكون اى نوع بيانات مع العلم إذا كان type ليس فى انواع البيانات بالأعلي يتم طباعة "**unknown type**".

 

البرنامج سيتم تمريره لمترجم c++03 و c++11 و المفترض ان يطبع قيمة تتناسب مع النوع.

 

يمنع استخدام RTTI.

 

بقي أمر واحد و هو لابد من طباعة اسم النوع بالكامل فمثلا:

typedef long type;

إذا تم طباعة "long" فهي قيمة خاطئة و المفترض طباعة "signed long int".

 

السؤال متاح لمدة 5 ايام.

 

 

و الله ولي التوفيق

تم تعديل هذه المشاركة بواسطة C++er في 25 سبتمبر 2013 في 20:39

1

مدونتي: C++ Tips and Tricks

#2

هل يمكنني السؤال دون المشاركة ؟
على حد علمي فإن "النوع" هو توجيه للمصرّف ليس إلا وليس له وجود حقيقي في البرنامج سوى حسب حجمه 1 و 2 و  4 و 8 بايت .. فهل الحل هو تعليمات خاصة بالمصرف ام غير ذلك ؟

وما هي RTTI

سؤال آخر خارج الموضوع ..

لماذا يوجد نوع char طالما أنها بالحالة الافتراضية signed char ولا يوجد int (حاف) لوجود signed int ..

تم تعديل هذه المشاركة بواسطة مصطفى 36a2 في 28 سبتمبر 2013 في 19:03

#3
اقتباس

 

يمنع استخدام RTTI.

على حد علمي RTTI تستخدم فقط على polymorphic types و ليس على الانواع العادية !

 

بما ان المهلة الزمنية انقضت , هل ستضع الحل او تتركه للمحاولة’

#4

البرنامج يسير و كنت اتمني ان يستطيع احد حله، عموما ها هو:

#include <iostream>
using namespace std;

template<typename t> struct type_id { static const int id = 0; };
template<> struct type_id<signed short int> { static const int id = 1; };
template<> struct type_id<unsigned short int> { static const int id = 2; };
template<> struct type_id<signed int> { static const int id = 3; };
template<> struct type_id<unsigned int> { static const int id = 4; };
template<> struct type_id<signed long int> { static const int id = 5; };
template<> struct type_id<unsigned long int> { static const int id = 6; };
template<> struct type_id<signed long long int> { static const int id = 7; };
template<> struct type_id<unsigned long long int> { static const int id = 8; };
template<> struct type_id<float> { static const int id = 9; };
template<> struct type_id<double> { static const int id = 10; };
template<> struct type_id<long double> { static const int id = 11; };
template<> struct type_id<bool> { static const int id = 12; };
template<> struct type_id<void*> { static const int id = 13; };
template<> struct type_id<char> { static const int id = 14; };
template<> struct type_id<signed char> { static const int id = 15; };
template<> struct type_id<unsigned char> { static const int id = 16; };
template<> struct type_id<wchar_t> { static const int id = 17; };

#if __cplusplus == 201103L
template<> struct type_id<char16_t> { static const int id = 18; };
template<> struct type_id<char32_t> { static const int id = 19; };
#endif

typedef short type;

ostream& write_name(ostream& stm)
{
    const char* names[] = {
	"**unknown type**",
	"signed short int",
	"unsigned short int",
	"signed int",
	"unsigned int",
	"signed long int",
	"unsigned long int",
	"signed long long int",
	"unsigned long long int",
	"float",
	"double",
	"long double",
	"bool",
	"void*",
	"char",
	"signed char",
	"unsigned char",
	"wchar_t"
#if __cplusplus == 201103L
	,"char16_t"
	,"char32_t"
#endif
	};

	stm << names[ type_id<type>::id ]<< endl;
	return stm;
}

int main()
{
    cout << write_name;
}
اقتباس

لماذا يوجد نوع char طالما أنها بالحالة الافتراضية signed char ولا يوجد int (حاف) لوجود signed int ..

النوع char يختلف عن النوعين signed char و unsigned char فى العديد من الأمور، راجع الـ standard فالشرح يطول.
 
 
و الله ولي التوفيق

تم تعديل هذه المشاركة بواسطة C++er في 3 أكتوبر 2013 في 17:32

2

مدونتي: C++ Tips and Tricks

هذا الموضوع مغلق.

مواضيع مشابهة