Chắc suất Đại học top - Giữ chỗ ngay!! ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.
#include <iostream>
#include <cstring>
using namespace std;
std::string tong(std::string R1,std::string R2)
{
int K1=0, K2=0;
while (K1<R1.size() && R1[K1] != '.') K1++;
while (K2<R2.size() && R2[K2] != '.') K2++;
int N1=R1.size()-K1, N2=R2.size()-K2;
if (N1>N2) {
if (N2==0)
{
R2+='.';N2++;
}
for (int i=0;i<N1-N2;i++)
{
R2+='0';
}
}
else {
if (N1==0 && N2>0)
{
R1+='.';N1++;
}
for (int i=0;i<N2-N1;i++)
{
R1+='0';
}
}
while (R1.size()<R2.size()) R1='0'+R1;
while (R2.size()<R1.size()) R2='0'+R2;
cout<<R1<<"\n";
cout<<R2<<"\n";
// tổng
int Nho=0,S;
string R3="";
for (int i=R1.size()-1;i>=0;i--)
if (R1 != '.')
{
S=(R1-'0')+(R2-'0')+Nho;
Nho=S/10;
R3=char(S%10+'0')+R3;
}
else {R3='.'+R3;}
if (Nho>0) R3=char(Nho+'0')+R3;
return R3;
}
std::string hieu(std::string R1,std::string R2)
{
int K1=0, K2=0;
while (K1<R1.size() && R1[K1] != '.') K1++;
while (K2<R2.size() && R2[K2] != '.') K2++;
int N1=R1.size()-K1, N2=R2.size()-K2;
if (N1>N2) {
if (N2==0)
{
R2+='.';N2++;
}
for (int i=0;i<N1-N2;i++)
{
R2+='0';
}
}
else {
if (N1==0 && N2>0)
{
R1+='.';N1++;
}
for (int i=0;i<N2-N1;i++){R1+='0';}}
while (R1.size()<R2.size()) R1='0'+R1;
while (R2.size()<R1.size()) R2='0'+R2;
cout<<R1<<"\n";
cout<<R2<<"\n";
// hiệu
string R3="";int Nho=0;int S;
for (int i=R1.size()-1;i>=0;i--)
if (R1 != '.')
{
S=(R1-'0')-((R2-'0') + Nho);
if (S<0)
{
S+=10;Nho=1;
}
else Nho=0; R3=char(S+'0')+R3;
}
else {R3='.'+R3;}
//cout<<"Nho="<<Nho<<"\n";
if (Nho>0) {
R3="";Nho=0;
for (int i=R1.size()-1;i>=0;i--)
if (R2 != '.')
{
S=(R2-'0')-((R1-'0') + Nho);
if (S<0)
{
S+=10;Nho=1;
}
else Nho=0;
R3=char(S+'0')+R3;
}
else {
R3='.'+R3;
}
R3='-'+R3;
}
return R3;
}
int main () {
string R1="12.923423424";
string R2="74234.382";
cout<<"\n R1 "<<R1;
cout<<"\n R2 "<<R2;
cout<<"\n tổng "<<tong(R1,R2);
cout<<"\n R1 "<<R1;
cout<<"\n R2 "<<R2;
cout<<"\n hiệu \n"<<hieu(R1,R2);
return 0;
#include <cstring>
using namespace std;
std::string tong(std::string R1,std::string R2)
{
int K1=0, K2=0;
while (K1<R1.size() && R1[K1] != '.') K1++;
while (K2<R2.size() && R2[K2] != '.') K2++;
int N1=R1.size()-K1, N2=R2.size()-K2;
if (N1>N2) {
if (N2==0)
{
R2+='.';N2++;
}
for (int i=0;i<N1-N2;i++)
{
R2+='0';
}
}
else {
if (N1==0 && N2>0)
{
R1+='.';N1++;
}
for (int i=0;i<N2-N1;i++)
{
R1+='0';
}
}
while (R1.size()<R2.size()) R1='0'+R1;
while (R2.size()<R1.size()) R2='0'+R2;
cout<<R1<<"\n";
cout<<R2<<"\n";
// tổng
int Nho=0,S;
string R3="";
for (int i=R1.size()-1;i>=0;i--)
if (R1 != '.')
{
S=(R1-'0')+(R2-'0')+Nho;
Nho=S/10;
R3=char(S%10+'0')+R3;
}
else {R3='.'+R3;}
if (Nho>0) R3=char(Nho+'0')+R3;
return R3;
}
std::string hieu(std::string R1,std::string R2)
{
int K1=0, K2=0;
while (K1<R1.size() && R1[K1] != '.') K1++;
while (K2<R2.size() && R2[K2] != '.') K2++;
int N1=R1.size()-K1, N2=R2.size()-K2;
if (N1>N2) {
if (N2==0)
{
R2+='.';N2++;
}
for (int i=0;i<N1-N2;i++)
{
R2+='0';
}
}
else {
if (N1==0 && N2>0)
{
R1+='.';N1++;
}
for (int i=0;i<N2-N1;i++){R1+='0';}}
while (R1.size()<R2.size()) R1='0'+R1;
while (R2.size()<R1.size()) R2='0'+R2;
cout<<R1<<"\n";
cout<<R2<<"\n";
// hiệu
string R3="";int Nho=0;int S;
for (int i=R1.size()-1;i>=0;i--)
if (R1 != '.')
{
S=(R1-'0')-((R2-'0') + Nho);
if (S<0)
{
S+=10;Nho=1;
}
else Nho=0; R3=char(S+'0')+R3;
}
else {R3='.'+R3;}
//cout<<"Nho="<<Nho<<"\n";
if (Nho>0) {
R3="";Nho=0;
for (int i=R1.size()-1;i>=0;i--)
if (R2 != '.')
{
S=(R2-'0')-((R1-'0') + Nho);
if (S<0)
{
S+=10;Nho=1;
}
else Nho=0;
R3=char(S+'0')+R3;
}
else {
R3='.'+R3;
}
R3='-'+R3;
}
return R3;
}
int main () {
string R1="12.923423424";
string R2="74234.382";
cout<<"\n R1 "<<R1;
cout<<"\n R2 "<<R2;
cout<<"\n tổng "<<tong(R1,R2);
cout<<"\n R1 "<<R1;
cout<<"\n R2 "<<R2;
cout<<"\n hiệu \n"<<hieu(R1,R2);
return 0;