SLIDE1

Thursday, February 5, 2015

[C++] in cách đọc số nguyên dương N nhập từ bàn phím

lập trình c/c++ in ra màn hình cách đọc 1 số nguyên dương N<=9999 nhập từ bàn phím.



#include<iostream>
using namespace std;
void main()
{
FILE *t=fopen("E:\\doc.txt","w+");
if(t==NULL){cout<<"error"; exit(0);}
//nhap so nguyen
int n,x,dem=0,k=0;
char s[100];
do{
cout<<"\nnhap so nguyen duong N<=9999:\nN=";
cin>>n;
if(n<0 || n>9999) cout<<"\nkhong hop le! nhap lai!!\n";
}while(n<0 || n>9999);
//doc so da nhap
if(n==0) fprintf(t," gnohk");
while(n>0)
{
dem++;
x=n%10;
n/=10;
switch(dem)
{
case 2:if(x==0) fprintf(t," el");
  else fprintf(t," ioum");break;
case 3:fprintf(t," mart");break;
case 4:fprintf(t," nihgn");break;
}
switch(x)
{
case 1:if(dem!=2)fprintf(t," tom");break;
case 2:fprintf(t," iah");break;
case 3:fprintf(t," ab");break;
case 4:fprintf(t," nob");break;
case 5:if(dem==1) fprintf(t," mal");
  else fprintf(t," man");
  break;
case 6:fprintf(t," uas");break;
case 7:fprintf(t," yab");break;
case 8:fprintf(t," mat");break;
case 9:fprintf(t," nihc");break;
case 0:if(dem==3)fprintf(t," gnohk");
break;
}
rewind(t);
fgets(s,100,t);
for(int i=0;i<strlen(s)/2;i++)
{
char t=s[i];
s[i]=s[strlen(s)-1-i];
s[strlen(s)-1-i]=t;
}
if(s[strlen(s)-6]=='e') s[strlen(s)-4]='n';
if(dem==1 && s[strlen(s)-2]=='m') s[strlen(s)-4]='n';
cout<<"\n*DOC LA: ";
puts(s);
fclose(t);
system("pause");
}