• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

برنامه خواندن و تبديل عدد وارد شده به شكل كلمات

sima_c

کاربر تازه وارد
تاریخ عضویت
21 می 2006
نوشته‌ها
30
لایک‌ها
0
با سلام خدمت همه دوستان
من دو تمرین از قسمت اشاره گرها و ساختمان ها داشتم . دوستان اگه میشه راهنماییم کنن خیلی ممنون میشم.
تمرین 1 :
تابعي بنويسيد که عدد صحيحي را دريافت کرده (در قالب عدد و نه رشته) و معادل متني آنرا برگرداند. عملکرد صحيح آنرا در يک برنامه نشان دهيد.
برنامه زیر عدد 3 رقمی را بصورت رشته ای میگیره و بصورت متنی چاپ میکنه ولی من برای اینکه بتونم عدد 4 رقمی صحیح رو بگیرم و چاپ کنم چکار باید بکنم :
کد:
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

void ascii2text(char* t, char* s);

int main(){

	const int n=40;
	char a, s[n+1]="", t[n+1]="";
	int i, x, y;
	gets(s);
	ascii2text(s,t);
	puts(t);
	return(0);
	}

void ascii2text(char* s, char* t){//s : ascii input string, t :output string
	char *dig1[]={
		"",
		"one ",
		"two ",
		"three ",
		"four ",
		"five ",
		"six ",
		"seven ",
		"eight ",
		"nine "
		};
	char *dig21[]={
		"eleven ",
		"twelve ",
		"thirteen ",
		"fourteen ",
		"fifteen ",
		"sixteen ",
		"seventeen ",
		"eighteen ",
		"nineteen "
		};
	char *dig2[]={
		"",
		"ten ",
		"twenty ",
		"thirty ",
		"fourty ",
		"fifty ",
		"sixty ",
		"seventy ",
		"eighty ",
		"ninety "
		};
	char hundred[]="hundred ";
	char error[]="not suitable for more than 3 digits";
	char z[10]="0000000";
	int i=0,j=0,k=0;

	while(s[k++]);
	k=(3-(k-1)%3)%3;
	while(z[k+j]=s[j])j++;

	k=0;
	j=0;
	while(t[j++]=dig1[z[i]-48][k++]);
	j--;
		if(z[i]-48){
		k=0;
		while(t[j++]=hundred[k++]);
		j--;
		}
	i++;
	k=0;
	if(z[i]-48==1 && z[i+1]-48!=0){
		i++;
		while(t[j++]=dig21[z[i]-48-1][k++]);
		}
	else{
		while(t[j++]=dig2[z[i]-48][k++]);
		j--;
		i++;
		k=0;
		while(t[j++]=dig1[z[i]-48][k++]);
		}
	}



تمرین 2 .
تابعي بنويسيد که زمان را در قالب يک ساختمان (structure) دريافت کرده (24 ساعتي) و معادل آنرا به صورت متن (12 ساعتي با تعيين روز/شب يا قبل از ظهر و بعد از ظهر) برگرداند.

از دوستان خواهش میکنم راهنماییم کنن
خیلی خیلی ممنونم
 

sima_c

کاربر تازه وارد
تاریخ عضویت
21 می 2006
نوشته‌ها
30
لایک‌ها
0
دوستان راهنمایی شما رو نیاز دارم .
در کار با اشاره گر ها
ممنون میشم راهنمایی کنید
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
قسمت 3 رقم هزارگان را چگونه مي خواهيد بخوانيد؟
مثلا 566 هزار و .....
يا جور ديگه؟
.
 

sima_c

کاربر تازه وارد
تاریخ عضویت
21 می 2006
نوشته‌ها
30
لایک‌ها
0
اگه در برنامه بالا فقط به جای 3 رقم - 4 رقم رو بخوام بگیره چکار باید بکنم . میشه راهنمایی کنید
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
برنامه زير عددي 4 رقمي را گرفته و آن را با كلمات مي خواند.

کد:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
void print_one_to_nine(int a);
void print_eleven_t0_nineteen(int a);
void print_twoenty_t0_ninety(int a) ;
main()
{
clrscr(); // clear screen
float my_number,  hezaar_gaan,	 sad_gaan,  dah_gaan , yekaan ;
cout<<"give a number "<<endl<<"smaller than 9999"
    <<", 0 for exit"<<endl;
  while(1)
	    {

 cin>>my_number;

    if (my_number>9999) {
			 cout<<"your number is greater"<<endl
			 <<" than 9999"<<endl;
			 continue;
			 }
    if (my_number==0) exit(0);

    hezaar_gaan=int(my_number/1000);
    sad_gaan   =int ( (my_number-(hezaar_gaan*1000) )/100 ) ;
    dah_gaan   =int ( (my_number-(hezaar_gaan*1000+sad_gaan*100) )/10 ) ;
    yekaan     = my_number-(hezaar_gaan*1000+sad_gaan*100+dah_gaan*10)  ;

     print_one_to_nine(hezaar_gaan);
   if (hezaar_gaan>0) cout<<" thousand ";

     print_one_to_nine(sad_gaan);
   if (sad_gaan>0) cout<<" hundred ";

 if (dah_gaan>1)   print_twoenty_t0_ninety (  (dah_gaan*10)          ) ;
 if (dah_gaan==1)  print_eleven_t0_nineteen(  (dah_gaan*10+yekaan)   ) ;
 if (dah_gaan!=1)  print_one_to_nine(yekaan);
	   cout<<endl;
	    } //end while
 return 0;
}
// ------------------------------------
void print_one_to_nine(int a)
{
switch(a)
	   {
       case 1:cout<<"one";break;
	case 2:cout<<"two";break;
	 case 3:cout<<"three";break;
	  case 4:cout<<"four";break;
	   case 5:cout<<"five";break;
	    case 6:cout<<"six";break;
	     case 7:cout<<"seven";break;
	      case 8:cout<<"eight";break;
	       case 9:cout<<"nine";break;
       default:cout<<"";
	      }
} // end function
// ------------------------------------
void print_eleven_t0_nineteen(int a)
{
switch(a)
	   {
      case 10:cout<<"ten";break;
       case 11:cout<<"eleven";break;
	case 12:cout<<"twoelve";break;
	 case 13:cout<<"thirteen";break;
	  case 14:cout<<"fourteen";break;
	   case 15:cout<<"fifteeen";break;
	    case 16:cout<<"sixteen";break;
	     case 17:cout<<"seventeen";break;
	      case 18:cout<<"eightteen";break;
	       case 19:cout<<"nineteen";break;
       default:cout<<"";
	      }
} // end function
// ------------------------------------
void print_twoenty_t0_ninety(int a)
{
 switch(a)
	   {
       case 20:cout<<"twoenty ";break;
	case 30:cout<<"thirty ";break;
	 case 40:cout<<"fourty ";break;
	  case 50:cout<<"fifty ";break;
	   case 60:cout<<"sixty ";break;
	    case 70:cout<<"seventy ";break;
	     case 80:cout<<"eigthy ";break;
	      case 90:cout<<"ninety ";break;
		      default:cout<<"";
	      }

} // end function


a3.gif

 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
اگر نمي خواهيد از توابع رياضي سي(در برنامه پست قبل ، تابع جز صحيح) استفاده كنيد، مي توانيد با عملگر باقيمانده به روش زير حل كنيد:

کد:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void print_one_to_nine(int a);
void print_eleven_t0_nineteen(int a);
void print_twoenty_t0_ninety(int a) ;
main()
{
clrscr(); // clear screen
int my_number,  hezaar_gaan,	 sad_gaan,  dah_gaan , yekaan ;
cout<<"give a number "<<endl<<"smaller than 9999"
    <<", 0 for exit"<<endl;
  while(1)
	    {

 cin>>my_number;

    if (my_number>9999) {
			 cout<<"your number is greater"<<endl
			 <<" than 9999"<<endl;
			 continue;
			 }
    if (my_number==0) exit(0);

    yekaan=my_number%10;
    dah_gaan=( (my_number%100)-               yekaan    ) /10;
    sad_gaan=( (my_number%1000)-( dah_gaan*10+yekaan )  )  /100;
    hezaar_gaan= ( my_number-( sad_gaan*100+dah_gaan*10+yekaan )  )  /1000;

     print_one_to_nine(hezaar_gaan);
   if (hezaar_gaan>0) cout<<" thousand ";

     print_one_to_nine(sad_gaan);
   if (sad_gaan>0) cout<<" hundred ";

 if (dah_gaan>1)   print_twoenty_t0_ninety (  (dah_gaan*10)          ) ;
 if (dah_gaan==1)  print_eleven_t0_nineteen(  (dah_gaan*10+yekaan)   ) ;
 if (dah_gaan!=1)  print_one_to_nine(yekaan);
	   cout<<endl;
	    } //end while
 return 0;
}
// ------------------------------------
void print_one_to_nine(int a)
{
switch(a)
	   {
       case 1:cout<<"one";break;
	case 2:cout<<"two";break;
	 case 3:cout<<"three";break;
	  case 4:cout<<"four";break;
	   case 5:cout<<"five";break;
	    case 6:cout<<"six";break;
	     case 7:cout<<"seven";break;
	      case 8:cout<<"eight";break;
	       case 9:cout<<"nine";break;
       default:cout<<"";
	      }
} // end function
// ------------------------------------
void print_eleven_t0_nineteen(int a)
{
switch(a)
	   {
      case 10:cout<<"ten";break;
       case 11:cout<<"eleven";break;
	case 12:cout<<"twoelve";break;
	 case 13:cout<<"thirteen";break;
	  case 14:cout<<"fourteen";break;
	   case 15:cout<<"fifteeen";break;
	    case 16:cout<<"sixteen";break;
	     case 17:cout<<"seventeen";break;
	      case 18:cout<<"eightteen";break;
	       case 19:cout<<"nineteen";break;
       default:cout<<"";
	      }
} // end function
// ------------------------------------
void print_twoenty_t0_ninety(int a)
{
 switch(a)
	   {
       case 20:cout<<"twoenty ";break;
	case 30:cout<<"thirty ";break;
	 case 40:cout<<"fourty ";break;
	  case 50:cout<<"fifty ";break;
	   case 60:cout<<"sixty ";break;
	    case 70:cout<<"seventy ";break;
	     case 80:cout<<"eigthy ";break;
	      case 90:cout<<"ninety ";break;
		      default:cout<<"";
	      }

} // end function

نتيجه فرقي با برنامه پست قبلي ندارد.
.
 

sima_c

کاربر تازه وارد
تاریخ عضویت
21 می 2006
نوشته‌ها
30
لایک‌ها
0
سوال 1.
کد:
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

void ascii2text(char* t, char* s);

int main(){
	const int n=40;
	char a, s[n+1]="", t[n+1]="";
	int i, x, y;
	int e,o,m;
	cin >> m;
	e=3;
	while(m > 0)
	{
	o=m % 10;
	s[e]=(char)(o+48);
	e--;
	m=m / 10;
 }
	ascii2text(s,t);
	puts(t);
	return(0);
	}

void ascii2text(char* s, char* t){//s : ascii input string, t :output string
	char *dig1[]={
		"",
		"one ",
		"two ",
		"three ",
		"four ",
		"five ",
		"six ",
		"seven ",
		"eight ",
		"nine "
		};
	char *dig21[]={
		"eleven ",
		"twelve ",
		"thirteen ",
		"fourteen ",
		"fifteen ",
		"sixteen ",
		"seventeen ",
		"eighteen ",
		"nineteen "
		};
	char *dig2[]={
		"",
		"ten ",
		"twenty ",
		"thirty ",
		"fourty ",
		"fifty ",
		"sixty ",
		"seventy ",
		"eighty ",
		"ninety "
		};
	char hundred[]="hundred ";
	char thousand[]="thousand ";
	char error[]="not suitable for more than 3 digits";
	char z[10]="0000000";
	int i=0,j=0,k=0,ww=0;

	while(s[k++]);
	k=(4-(k-1)%4)%4;
	while(z[k+j]=s[j])j++;
	k=0;
	j=0;
	while(t[j++]=dig1[z[i]-48][k++]);
	j--;
		if(z[i]-48){
		k=0;
		while(t[j++]=thousand[k++]);
		j--;
		}
i=0;
	while(t[j++]=dig1[z[i+1]-48][ww++]);
	j--;
		if(z[i]-48){
		ww=0;
		while(t[j++]=hundred[ww++]);
		j--;
		}
	i++;
	k=0;

	i++;
	k=0;
	if(z[i]-48==1 && z[i+1]-48!=0){
		i++;
		while(t[j++]=dig21[z[i]-48-1][k++]);
		}
	else{
		while(t[j++]=dig2[z[i]-48][k++]);
		j--;
		i++;
		k=0;
	while(t[j++]=dig1[z[i]-48][k++]);
		}
	}


سوال2.
کد:
#include<iostream.h>
struct Time{
int hour;
int min;
int sec;}t;
void zaman(Time s);
int main (){
cout<<"Enter The time:";
cin>>t.hour>>t.min>>t.sec;
zaman(t);
return 0;
}
void zaman(Time s){
if(s.hour>12)
cout<<"The time is: "<<s.hour-12<<":"<<s.min<<":"<<s.sec<<" \"Bad az zohr\"";
else
cout<<"The time is: "<<s.hour<<":"<<s.min<<":"<<s.sec<<" \"Ghabl az zohr\"";
}
 

sima_c

کاربر تازه وارد
تاریخ عضویت
21 می 2006
نوشته‌ها
30
لایک‌ها
0
دوست عزیز خیلی ممنونم.
 
بالا