Goldberg
Nouveau membre
Bonjour, voilà mon programme qui me permet d'additionner deux nombres écrits sous forme binaire
comment pourrais-je faire pour que j'introduise les deux nombres (premier nombre binaire et second nombre binaire) sur la ligne de commande et non plus ds le programme même?
ex : (si le programme s'appelle addition.exe)
je tappe : addtion.exe 110 100
et il apparait :
le premier nombre binaire est : 110
le second nombre binaire est : 100
leur somme binaire est : 1010
Au revoir
[cpp]
#include <stdio.h>
#include <string.h>
int main ()
{
size_t length;
char tab1[80],tab2[80];
int d,e,tab3[80],tab4[80];
int i,a=0,b=0;
printf("\nPremier nombre binaire\n");
gets(tab1);
length = strlen(tab1);
d=length;
printf("\nSecond nombre binaire\n");
gets(tab2);
length = strlen(tab2);
e=length;
if (d>=e) {
for (i=d-1; i>=d-e; i--) {tab4=tab2[i-(d-e)]-48;};
for (i=d-e-1; i>=0; i--) {tab4=0;};
for (i=d-1; i>=0; i--) {tab3=tab4+tab1-48+a;
if (tab3==0) {tab3=0;a=0;};
if (tab3==1) {tab3=1;a=0;};
if (tab3==2) {tab3=0;a=1;};
if (tab3==3) {tab3=1;a=1;};
};
printf("\nLa somme des 2 nombres est :\n");
if (a==1) {printf("1");};
for(i=0; i<=d-1; i++) printf("%d", tab3);
};
if (d<e) {
for (i=e-1; i>=e-d; i--) {tab4=tab1[i-(e-d)]-48;};
for (i=e-d-1; i>=0; i--) {tab4=0;};
for (i=e-1; i>=0; i--) {tab3=tab4+tab2-48+b;
if (tab3==0) {tab3=0;b=0;};
if (tab3==1) {tab3=1;b=0;};
if (tab3==2) {tab3=0;b=1;};
if (tab3==3) {tab3=1;b=1;};
};
printf("\nLa somme des 2 nombres est :\n");
if (b==1) {printf("1");};
for(i=0; i<=e-1; i++) printf("%d", tab3);
};
}
[/cpp]
comment pourrais-je faire pour que j'introduise les deux nombres (premier nombre binaire et second nombre binaire) sur la ligne de commande et non plus ds le programme même?
ex : (si le programme s'appelle addition.exe)
je tappe : addtion.exe 110 100
et il apparait :
le premier nombre binaire est : 110
le second nombre binaire est : 100
leur somme binaire est : 1010
Au revoir
[cpp]
#include <stdio.h>
#include <string.h>
int main ()
{
size_t length;
char tab1[80],tab2[80];
int d,e,tab3[80],tab4[80];
int i,a=0,b=0;
printf("\nPremier nombre binaire\n");
gets(tab1);
length = strlen(tab1);
d=length;
printf("\nSecond nombre binaire\n");
gets(tab2);
length = strlen(tab2);
e=length;
if (d>=e) {
for (i=d-1; i>=d-e; i--) {tab4=tab2[i-(d-e)]-48;};
for (i=d-e-1; i>=0; i--) {tab4=0;};
for (i=d-1; i>=0; i--) {tab3=tab4+tab1-48+a;
if (tab3==0) {tab3=0;a=0;};
if (tab3==1) {tab3=1;a=0;};
if (tab3==2) {tab3=0;a=1;};
if (tab3==3) {tab3=1;a=1;};
};
printf("\nLa somme des 2 nombres est :\n");
if (a==1) {printf("1");};
for(i=0; i<=d-1; i++) printf("%d", tab3);
};
if (d<e) {
for (i=e-1; i>=e-d; i--) {tab4=tab1[i-(e-d)]-48;};
for (i=e-d-1; i>=0; i--) {tab4=0;};
for (i=e-1; i>=0; i--) {tab3=tab4+tab2-48+b;
if (tab3==0) {tab3=0;b=0;};
if (tab3==1) {tab3=1;b=0;};
if (tab3==2) {tab3=0;b=1;};
if (tab3==3) {tab3=1;b=1;};
};
printf("\nLa somme des 2 nombres est :\n");
if (b==1) {printf("1");};
for(i=0; i<=e-1; i++) printf("%d", tab3);
};
}
[/cpp]