#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<math.h>
void Xoa(char *s ,int vt )
{
int k=strlen(s);
for(int i=vt+1 ;i<k;i++)
{
s[i-1]=s[i];
}
s[k-1]='\0';
}
void Them(char *s, char kytu,int vt)
{
int k=strlen(s);
for(int i=k;i>=vt;i--)
{
s[i+1]=s[i];
}
s[vt]=kytu;
s[k+1]='\0';
}
void Chuanhoadodai1chuoi(char *s)
{
int k=strlen(s);
for(int i=0;i<k;i++)
{
if((s[i]==' ')||(s[i]==' '&&s[i+1]==' ')||((s[i]==',')||(s[i]==','&&s[i+1]==','))||((s[i]=='.')||(s[i]=='.'&&s[i+1]=='.')))
{
Xoa(s,i);
i--;
k--;
}
}
}
void Chuanhoa2chuoi(char *s1, char *s2)
{
int lenths1=strlen(s1);
int lenths2=strlen(s2);
if(lenths1<lenths2)
{
int k=lenths2-lenths1;
for(int i=0;i<k;i++)
{
Them(s1,'0',i);
}
}
if( lenths1>lenths2)
{
int C=lenths1-lenths2;
for(int i=0;i<C;i++)
{
Them(s2,'0',i);
}
}
}
char* tong2chuoi(char *s1,char *s2)
{
Chuanhoa2chuoi(s1,s2);
int k=strlen(s1);
char ketqua[200];
int nho=0;
int dodaichuoiketqua=0;
for(int i=k-1;i>=0;i--)
{
int so1=(s1[i]-48);
int so2=(s2[i]-48);
int tong=(so1+so2+nho);
nho=tong/10;
tong=tong%10;
ketqua[dodaichuoiketqua++]=tong+48;
}
if(nho!=0)
{
ketqua[dodaichuoiketqua]=nho+48;
++dodaichuoiketqua;
}
ketqua[dodaichuoiketqua]='\0';
strrev(ketqua);
return ketqua;
}
char* thuong2chuoi(char *s1,char *s2)
{
Chuanhoa2chuoi(s1,s2);
int k=strlen(s1);
char ketqua[200];
int dix=0;
int nho=0;
int kq;
if(s1[0]>s2[0])
{
for(int i=k-1;i>=0;i--)
{
int thuong1 =s1[i]-48;
int thuong2 =s2[i]-48;
if(thuong1<thuong2)
{
kq=(thuong1+10)-thuong2-nho;
ketqua[dix++]=kq+48;
nho=1;
if((s1[i--]-48)==(s2[i--]-48))
{
kq=((s1[i--]-48)+10)-((s2[i--]-48)+nho);
ketqua[dix++]=kq+48;
nho=0;
}
}
else
{
kq=thuong1-thuong2-nho;
ketqua[dix++]=kq+48;
nho=0;
}
}
}
ketqua[++dix]='\0';
strrev(ketqua);
return ketqua;
}
int main()
{
printf("\t\t\t chuong trinh tinh supper\n");
char s1[30000];
printf("nhap vao chuoi so thu 1:");
gets(s1);
Chuanhoadodai1chuoi(s1);
char s2[30000];
printf("nhap vao chuoi so thu 2:");
gets(s2);
Chuanhoadodai1chuoi(s2);
char tong[100000];
strcpy(tong,tong2chuoi(s1,s2));
printf("ket qua la:\t\t%s",tong);
/*
char p[30];
strcpy(p,thuong2chuoi(s1,s2));
printf("%s",p);*/
getch();
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.