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

هل تستطيع تحويل سلسله نصيه تحتوى على ارقام إلى أرقام فعليه

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

فكرة موضوع الأخ Khaled.Alshaya الخاصه بتحويل الحروف الصغيره إلى كبيره و العكس ذكرتنى بمثال كنت قد قرئته فى احد كتب الأسمبلى و هو تحويل سلسله نصيه تحتوى على ارقام إلى أرقام فعليه.

ملحوظه هامه : يمنع استخدام اى دوال خارجيه (يعنى اصنع كل الدوال بنفسك)

و للعلم يمكنك الرجوع لموضوع الأخ Khaled.Alshaya يحتوى على احد الطرق التى ستوصل للحل. الموضوع هــــنا إن لم تكن قرئته بعد

*****************************

أضافه للسؤال السابق : قم بتطوير دالتك لتتعامل مع الأرقام السالبه ايضا. :resentful:

****************************

لمحترفى الـ C++ : بالإضافه للسابق قم بتطوير الداله لتحويل النصوص التى تحتوى على الأرقام العشريه. :calc:

الفكره اتتنى منذ قليل فقررت مشاركتكم بها و انا اجهز الحل الأن.

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

تم تعديل هذه المشاركة بواسطة Game Expert في 27 مايو 2009 في 19:34

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

#2

السلام عليكم

هذه محاولتي وضعتها بعجالة، أرجو من الله التوفيق,,

#include <stdio.h>
#include <math.h>
#include <string.h> 

int check_str(char * str);	  /* Are charactares valid? */
unsigned int str_to_int(char * str); 	/* Main calc function */

int main() {
	printf("%u\n", str_to_int("12352"));
	getchar();
	return 0;
}

unsigned int str_to_int(char * str)
{
	int result = 0; 	/* Return value */
	int length = strlen(str); /* String length */
	int j;	/* Counter */

	if( !check_str(str) )
		return 0;

	for( j=0; j < length; j++ ) {
		result += (int)((str[j]-'0') * pow(10.0, (length - j - 1)));
	}
	return result;
}

int check_str(char * str)
{
/*	  IF VALID
		return 1;
	ELSE
		return 0;
*/
	return 1;
}

الدالة الأخيرة متروكة لغيري,,

والسلام عليكم

تم تعديل هذه المشاركة بواسطة MoHammaD_93 في 27 مايو 2009 في 18:51

SAFETY TIP : Always delete what you new, and free what you malloc, never mix new with free or malloc with delete.

#3

محاوله جيده MoHammaD_93 و لكن دالتك تتعامل فقط مع الأرقام الصحيحه الموجبه

نقطه اخرى و هى انك استخدمت الدوال strlen و pow

فعندما قلت

اقتباس
ملحوظه هامه : يمنع استخدام اى دوال خارجيه (يعنى اصنع كل الدوال بنفسك)

قصدت اى دوال ستستخدمها للقيام بتلك العمليه و لكن لا بأس.

ننتظر ردود الأخوه المبرمجين لتقييم الحلول (التى لم تكتب بعد :angry2: )

تم تعديل هذه المشاركة بواسطة Game Expert في 27 مايو 2009 في 19:39

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

#4

ها هو ذا الكود الخاص بى للتعامل مع الأرقام الصحيحه السالبه و الموجبه

#include <iostream>
using namespace std;


int   ctoi(const char *);
int   pow(int, int);
int   str_len(const char *);


const int DOT   = 0x2E;  // dot char (.) in ACII table
const int ZERO  = 0x30;  // Zero number (0) in ASCII table

// entry point
void main()
{
   cout << ctoi("+305") << endl;
}

// get lenght of char array
int   str_len(const char * str)
{
  int size = 0;

  while (str[size]!='\0')
	  size+=1;

  return size;
}

// return the tens power of exp
int pow10(int exp)
{
   // it useful to init res with one because in loop we start multiplcation directly.
   int res=1;

   // if exp is 0 whe won't enter the loop therfore res will remain 1. and it equal to 10^0=1
   for (int i =0;i<exp;i++) res*=10;

   // return res
   return res;
}



int ctoi(const char * str)
{
  int size = str_len(str) - 1; // we'll not include the the first item in string size
  int result = 0;

  for (int i=0; i<size; i++)
	// first i'll navigate through array from last to first
	// so the first item may be + or - sign or even a number so
	// if i include the check here it will happend each time in
	// loop and may slow it down therfore i exclude the first
	// item from loop to check it at last.
	result += ((int)str[size - i] - ZERO) * pow10(i);

  // if first char is + or - so try me
  if (str[0] == '+' || str[0] == '-')
	  result *= str[0] == '+'? 1: -1;
  else
	  result += ((int)str[0] - ZERO) * pow10(size);

  return result;
}

جارى إعداد الكود الخاص بالأرقم العشريه

منتظر باقى الأخوه المبرمجين

تم تعديل هذه المشاركة بواسطة Game Expert في 27 مايو 2009 في 21:12

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

#5

السلام عليكم

وهى دى محاولتى

#include <iostream>
using namespace std;
double stod(char*a)
{
	int n_int_digits=0;
	int n_fraction_digits=0;
	double num=0;
	double k=1;
	bool b=true;
	bool sign=false;
	// this loop for count number of integer and fraction digits
if(*a=='-') {sign=true;a++;}
if(*a=='+') a++;
	while (*a!='\0')
	{

		if(*a=='.') {b=false;a++;}
		if (b==true)
		{n_int_digits+=1;}
		else if(b==false)
		{n_fraction_digits+=1;k/=10;}
		a++;

	}
	--a;
	for (int i=-1*n_fraction_digits;i<n_int_digits;i++)
	{
		if(*a=='.') {--a;i=0;}

		num+=(int(*a)-48)*k;k*=10;
		--a;
	}
	return (sign?-1:1)*num;
}
int main ()
{
	char s[]="+155.11";
	cout<<5*stod(s)<<endl;
	char g[]="-45.655";
	cout<<3*stod(g)<<endl;
	 return 0;	

}

يتعامل مع الأعداد العشريه السالبه والموجبه

فى أمان الله

تم تعديل هذه المشاركة بواسطة amin_mu123 في 27 مايو 2009 في 21:33

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

#6

جميل أخ amin_mu123

منتظرين باقى الأخوه

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

#7
اقتباس
قصدت اى دوال ستستخدمها للقيام بتلك العمليه و لكن لا بأس.

استخدمت strlen و pow على أساس انهم دوال بسيطة ولا توجد هناك حاجة لوضع شرط من أجلهم

على كل حال بالتوفيق

السلام عليكم

SAFETY TIP : Always delete what you new, and free what you malloc, never mix new with free or malloc with delete.

#8

هذه هى الدوال بالكامل مع مثال تطبيقى

#include <iostream>
using namespace std;


int   ctoi(const char *);
float ctof(const char *);

int   pow(int, int);
int   str_len(const char *);
int   find(const char*, const char, int);
int   abs(int);

const int DOT   = 0x2E;  // dot char (.) in ACII table
const int ZERO  = 0x30;  // Zero number (0) in ASCII table

// macro to print result
#define PRINT(X) \
		cout << #X << " = " << X << endl;


// entry point
void main()
{
   PRINT(ctoi("120"));
   PRINT(ctoi("+120"));
   PRINT(ctoi("-120"));

   PRINT(ctof("120.251"));
   PRINT(ctof("+120.099"));
   PRINT(ctof("-120.361"));
}

// get lenght of char array
int str_len(const char * str)
{
  int size = 0;

  while (str[size]!='\0')
	  size+=1;

  return size;
}

// find specified char an return
int find(const char* str, const char find_what,int start_idx)
{
  // index of char to find if not found -1 is the default
  int idx=-1;
  // get size of str
  int size=str_len(str);
  // find index of the char
  for(int i=start_idx;i<size-idx;i++)
	  if (str==find_what)
		  {
			idx = i;
			break;
		  }

  // return the result
  return idx;
}
// return the tens power of exp
int pow10(int exp)
{
   // it useful to init res with one because in loop we start multiplcation directly.
   int res=1;

   // if exp is 0 whe won't enter the loop therfore res will remain 1. and it equal to 10^0=1
   for (int i =0;i<exp;i++) res*=10;

   // return res
   return res;
}

// get the absloute value of num
int abs(int num)
{ return num<0? num * -1: num; }

// convert number to integer
int ctoi(const char * str)
{
  int size = str_len(str) - 1; // we'll not include the the first item in string size
  int result = 0;

  for (int i=0; i<size; i++)
	// first i'll navigate through array from last to first
	// so the first item may be + or - sign or even a number so
	// if i include the check here it will happend each time in
    // loop and may slow it down therfore i exclude the first
    // item from loop to check it at last.
	result += ((int)str[size - i] - ZERO) * pow10(i);

  // if first char is + or - so try me
  if (str[0] == '+' || str[0] == '-')
	  result *= str[0] == '+'? 1: -1;
  else
	  result += ((int)str[0] - ZERO) * pow10(size);

  return result;
}

// convert number to float
float ctof(const char * str)
{
   // find the dot
   int i=find(str,'.',0);
   // varible to the final result
   float res=0;


   // if dot did not exist
   if (i==-1)
	   // the number is integer
	   // convert it and store in result to be returned
	   res = ctoi(str);
   // otherwise dot exist as first character but there is no integer part
   else if (i==0)
	   // calculate the fraction and store it in res
	   res = (float)ctoi(str+1) / pow10(str_len(str+1));
   // otherwise integer and fraction ar exist
   else
   {
    // get the fraction
	const char* second = str+i+1;
	// get size of fraction
	int size_second = str_len(second);

	// get size of integer
	int first_size=str_len(str)-size_second-1;
	// get the integer part
	char* first = new char[first_size+1];

	// copy the integer from source to its array
	for (int i=0; i<first_size; i++)
		first=str;

	// mark last item with \0 to terminate it
	first[first_size]='\0';

	// convert integer part
	res=ctoi(first);
	// store sign in first_size varible
	first_size = res < 0 ? -1: 1;
	// now get the absloute value of res(integer part) then add it to fraction
	res=abs(res) + (float)ctoi(second) / pow10(size_second);
	// now add the sign
	res *=first_size;
	// delete first aray
	delete first;
  }

   // return the result
   return res;
}

main.rar

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

تم تعديل هذه المشاركة بواسطة محمد علاء الدين في 2 أغسطس 2010 في 20:01

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

#9
ULONG StringToInteger(PTCHAR String)
{
	PTCHAR Sub;
	TCHAR cChar, Sign;
	ULONG Length, Result, Digit;

	Sub = String;
	Length = 0;

	while (String[Length] != TEXT('\0'))
		Length++;

	Length--;
	Sign = *Sub++;

	cChar = Sign;
	if (cChar == TEXT('-') || cChar == TEXT('+'))
	{
		if (Length)
		{
			Length--;
			cChar = *Sub++;
		}
		else
		{
			cChar = TEXT('\0');
		}
	}

	Result = 0;
	while (cChar != TEXT('\0'))
	{
		if (cChar >= TEXT('0') && cChar <= TEXT('9'))
		{
			Digit = cChar - TEXT('0');
		}

		Result = (10 * Result) + Digit;

		if (!Length)
		{
			break;
		}
		Length--;
		cChar = *Sub++;
	}

	if (Sign == TEXT('-'))
	{
		Result = (ULONG)(-(LONG)Result);
	}

	return Result;
}

mov eax, dword ptr ds:[0xffdf0308]

jmp dword ptr [eax+0xfc]

#10

مجهود جيد

و حتى الأن لدينا

MoHammaD_93 و له داله تحول لأرقام صحيحه موجبه.

amin_mu123 و له داله التحويل لأرقام عشريه موجبه و سالبه.

GameExpert و لى دالتى التحويل للأرقام الصحيحه و الكسريه الموجبه و السالبه.

GamingMasteR و له دالة التحويل لأرقام صحيحه موجبه و سالبه.

تم تعديل هذه المشاركة بواسطة Game Expert في 28 مايو 2009 في 02:04

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

#11

السلام عليكم ...

لم اعرف أن لدينا أناس تعشق البرمجة :)

شكراً للأخ Game Expert على طرحه الفكرة ....

الحقيقة أنا حالاً رأيت الموضوع و لم أكتب بعد سطر من الكود... و لكني كتبت في السابق دالة لتحويل نص إلى عدد سواء كان العدد موجباً أو سالباً, و لكن هناك نقطة للأسف لم أتجاوزها ربما لأنني لم أفكر فيها كثيراً,

كيف ستكتشف أن هناك overflow ؟

تحياتي ....

#12
اقتباس
كيف ستكتشف أن هناك overflow ؟

حقيقة لم يخطر حتى على بالى موضوع الـ overflow و لكنها فكره جيده تجعل تعديل الكود الخاص بى اخبارى لدى بعض الأفكار سأجرب و اخبركم بالنتيجه

و لكن هل الكود صعب لدرجه انه لم يشارك حتى الأن سوى 4

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

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

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