codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define CLEARSCREEN system("cls")
int pot(int e);
int freal(int dim, char v[]);
char *nopuntdd(char s[]);
char *nopuntds(char s[]);
void restparzial(char s[50][10], char *str, int lung, int ind, int pos);
void stamp(char s[20][200], int g);
char *sinopunt(float snp,int r, int f);
void aggspazio(char v[], char *str);
int main()
{
while(1){
char divid[100]={0},divis[100]={0},cif[2]={0},*strnopundivid="",*strnopundivis="",
partedivid[100]={0},bidecimal[100]={0},strnopuntquo[25]={0},w[50][10]={0},r[20][200]={0},v[20]={0},
rpd[20]={0},rpv[20]={0},rpl[30]={0},rpr[20]={},rpr2[20]={},strs[20]={0},tasto="";
int i=0,i2=0,t=0,p=0,c=0,j=0,k=0,e,r2,fat[10]={0},intquoz=0,intnopuntdivid=0,intnopuntdivis=0,rest=0,reald,
realv,accum=0,intnopuntquo=0,z=0,sep=0,fc=0,riga=0, partint=0,distdivid=0,distrest=0;
float realdivid=0,realdivis=0,realquoz=0,puntrealdivid=0;
reald=1, realv=1;
e=0;
while(reald) {
printf("\nDigita il dividendo: ");
scanf(" %s", divid);
if(!freal(20,divid)) {
printf("\nDividendo non valido ");
reald=1;
}
else {
reald=0;
}
}
while(realv) {
printf("\nDigita il divisore: ");
scanf(" %s", divis);
if(!freal(20,divis)) {
printf("\nDivisore non valido ");
realv=1;
}
else {
realv=0;
}
}
sscanf(divid, "%f", &realdivid);// Conversione da reale a stringa del dividendo
sscanf(divis, "%f", &realdivis);// Conversione da reale a stringa del divisore
partint=(int)realdivid;//Estazione parte intera del dividendo
sprintf(partedivid, "%d", partint);// conversione a stringa di partint
strnopundivid=nopuntdd(divid);// eliminazione del punto dal dividendo reale
strnopundivis=nopuntds(divis);// eliminazione del punto dal divisore reale
sscanf(strnopundivid, "%d", &intnopuntdivid);// conversione da stringa ad intero di strnopundivid
sscanf(strnopundivis, "%d", &intnopuntdivis);// conversione da stringa ad intero di strnopundivis
realquoz=realdivid/realdivis;// calcolo quoziente reale
intquoz=(int)(realquoz);// parte intera del quoziente
intnopuntquo=(int)(intnopuntdivid/intnopuntdivis);// quoziente degli interi senza punto ottenuti dai reali
rest=intnopuntdivid%intnopuntdivis;// resto del colcolo di intnopuntquo
sprintf(strnopuntquo, "%d", intnopuntquo);// conversione di intnopuntquo a stringa
// Calcolo dei vari resti parziali
i=strlen(strnopuntquo)-1;
i2=i+1;
c=i;
accum=rest;
while(i>0){
cif[0]=strnopuntquo[i];
t=(int)(cif[0])-48;
p=t*pot(e)*intnopuntdivis;
p=p+accum;
fat[i]=((int)(p/pot(e)));
z=p;
sprintf(strs,"%d", z);
restparzial(w, strs, strlen(strnopundivid), i, e);
accum=p;
i--;
i2--;
e++;
}
printf("\n\n");
// Creazione delle righe del disegno
//PRIMA RIGA - Linea dividendo e divisore
if(realdivis-(int)realdivis!=0){
sprintf(rpv,"%.2f",realdivis);
}
else{
sprintf(rpv,"%d",(int)realdivis);
}
strcat(r[1]," ");
if(realdivid-partint==0){
strcat(r[1],partedivid);
}
else{
sprintf(bidecimal, "%.2f", realdivid);
strcat(r[1],bidecimal);
}
strcat(r[1], sinopunt(realdivid, rest, 1));
strcat(r[1],"| ");
strcat(r[1],rpv);
// SECONDA RIGA - Linea di divisione tra resti, quoziente e operandi
sprintf(rpr2,"%.3f",realquoz);
sprintf(rpr,"%.2f",realquoz);
if(strlen(rpr)>=strlen(rpv)) {
sep=strlen(rpr)+2;
}
else {
sep=strlen(rpv)+2;
}
if(rest==0 && realdivid-partint==0){
fc=strlen(partedivid) - 1;
}
else if(rest>0 && realdivid-partint==0){
fc=strlen(partedivid) + strlen(sinopunt(realdivid, rest, 1)) - 3;
}
else if(rest==0 && realdivid-partint!=0){
fc=strlen(bidecimal) + strlen(sinopunt(realdivid, 0, 1)) - 2;
}
else if(rest>0 && realdivid-partint!=0){
fc=strlen(bidecimal) + strlen(sinopunt(realdivid, rest, 1)) - 3;
}
for(i=0; i<=fc; i++) {
rpl[i]=' ';
}
rpl[fc+1]='|';
for(i=0; i<=sep; i++) {
rpl[i+fc+2]='_';
}
strcat(r[2],sinopunt(realdivid, rest, 1));
strcat(r[2]," ");
strcat(r[2],rpl);
//TERZA RIGA - Linea primo resto parziale
strcat(r[3]," ");
if(strlen(strnopuntquo)-1!=0){
strcat(r[3],w[1]);
strcat(r[3], sinopunt(realdivid, rest, 2));
restparzial(w, strs, strlen(strnopundivid)+1, 0, 0);
}
if(strlen(strnopuntquo)-1==0){
sprintf(strs,"%d", rest);
restparzial(w, strs, strlen(partedivid), 0, 0);
strcat(r[3],w[0]);
strcat(r[3], sinopunt(realdivid, rest, 3));
}
strcat(r[3],"| ");
strcat(r[3],rpr2);
riga=3;
// RIGHE SUCCESSIVE ALLA TERZA
if(strlen(strnopuntquo)-1>0){
for(i=riga+1; i<=strlen(strnopuntquo)+riga-2; i++){
strcat(r[i]," ");
strcat(r[i],w[i-riga+1]);
strcat(r[i], sinopunt(realdivid, rest, 2));
strcat(r[i],"| ");
}
riga=i;
sprintf(strs,"%d", rest);
restparzial(w, strs, strlen(strnopundivid), 0, 0);
strcat(r[riga]," ");
strcat(r[riga],w[0]);
strcat(r[riga], sinopunt(realdivid, rest, 3));
strcat(r[riga],"| ");
}
// RESTI FINALI
if(rest>0){
rest=(rest*10)%intnopuntdivis;
rest=rest*10;
for(i=1; i<=2; i++){
sprintf(strs, "%d", rest);
if(realdivid-partint==0){
restparzial(w, strs, strlen(strnopundivid)+2, 10+i, 0);
}
else{
restparzial(w, strs, strlen(strnopundivid)+1, 10+i, 0);
}
strcat(r[riga+i]," ");
strcat(r[riga+i],w[10+i]);
strcat(r[riga+i],"| ");
rest=rest%intnopuntdivis;
}
riga=riga+2;
}
printf("\n\n");
// FUNZIONE DI STAMPA DISEGNO --------------------------------
stamp(r,riga);
//------------------------------------------------------------
printf("Premi n e invio per chiudere, un qualsiasi altro tasto e invio per continuare ");
scanf(" %c", &tasto);
if(tasto=='n'){ return 0;}
// Azzeramento dati delle matrici
for(i=0; i<20; i++){
r[i][200]="";
}
for(i=0; i<50; i++){
w[i][10]="";
}
system("cls");
}
return 0;
}
int pot(int e) {// potenze di 10
int i=1;
int b=1;
while(i<=e) {
b=b*10;
i++;
}
return b;
}
int freal(int dim, char v[]) {// controllo che i dati inseriti siano cifre numeriche
int i;
int l=strlen(v);
for(i=0; i<l; i++) {
if(!isdigit(v[i]) && v[i]!='.') {
return 0;
}
}
return 1;
}
char *nopuntdd(char s[]) { //elimina punto dalla stringa dividendo inserita da tastiera
static char v1[100];
int i=0,j=0;
while(s[i]!='\0') {
if(s[i]!='.') {
v1[j]=s[i];
i++,j++;
}
else {
v1[i]=s[i+1];
i++;
}
}
return v1;
}
char *nopuntds(char s[]) { //elimina punto dalla stringa divisore inserita da tastiera
static char v2[100];
int i=0,j=0;
while(s[i]!='\0') {
if(s[i]!='.') {
v2[j]=s[i];
i++,j++;
}
else {
v2[i]=s[i+1];
i++;
}
}
return v2;
}
void restparzial(char s[50][10], char *str, int lung, int ind, int pos){ //formatta l'incollonamento dei resti parziali
int i,ln;
ln=strlen(str)-1;
i=lung-1;
while(i>=0){
if(pos>0){
s[ind][i]= 32;
ln--;
}
else if(ln>=0 && pos<=0){
s[ind][i]= str[ln];
ln--;
}
else if(ln<=0 && pos<0){//<--
s[ind][i]=32;
ln--;
}
i--;
pos--;
}
s[ind][lung]='\0';
}
char *sinopunt(float snp, int r, int f){ // determina la posizione dei punti e dei decimali
static char m[4];
int in;
in=(int)snp;
// sprintf(n,"%.2f",snp);
// l=strlen(n)-1;
if(snp-in!=0){/* sì decimali*/
if(r==0 && f==1){/* comportameto divisore con resto zero */
m[2]='\0';
m[1]='\0';
m[0]='\0';
}
if(r!=0 && f==1){/* comportameto divisore con resto non zero */
m[3]='\0';
m[2]='\0';
m[1]='\0';
m[0]=32;
}
if(r==0 && f==2){/* comportameto resti parziali con resto zero */
m[3]='\0';
m[2]='\0';
m[1]='\0';
m[0]='\0';
}
if(r!=0 && f==2){/* comportameto resti parziali con resto non zero */
m[3]='\0';
m[2]='\0';
m[1]='\0';
m[0]=32;
}
if(r!=0 && f==3){/* comportamento resto finale */
m[3]='\0';
m[2]='\0';
m[1]='\0';
m[0]=48;
}
if(r==0 && f==3){/* comportamento resto finale */
m[3]='\0';
m[2]='\0';
m[1]=32;
m[0]=32;
}
}
else{ /* no decimali*/
if(r==0 && f==1){/* comportameto divisore con resto zero */
m[3]='\0';
m[2]='\0';
m[1]='\0';
m[0]='\0';
}
if(r!=0 && f==1){/* comportameto divisore con resto non zero */
m[3]='\0';
m[2]='\0';
m[1]=32;
m[0]=32;
}
if(r==0 && f==2){/* comportameto resti parziali con resto zero */
m[3]='\0';
m[2]='\0';
m[1]='\0';
m[0]='\0';
}
if(r!=0 && f==2){/* comportameto resti parziali con resto non zero */
m[3]='\0';
m[2]='\0';
m[1]=32;
m[0]=32;
}
if(r!=0 && f==3){/* comportameto resto finale */
m[3]='\0';
m[2]='\0';
m[1]=32;
m[0]=48;
}
}
return m;
}
void stamp(char s[20][200], int g) { // Motore grafico di stampa
int i,j,l,p2,st,ri,nrig;
//char rig[6][5]= {"****","* *"," *","* "," ","____"},p; // alternativa grafica
char rig[6][5]= {{205,205,205,205},{186,32,32,186},{32,32,32,186},{186,32,32,32},{32,32,32,32},{95,95,95,95}},p;
char cod[12][7]= {"011104","222224","020304","020204","110224","030204","030104","022224","010104","010204","444444","444454"};
nrig=0;
for(ri=1; ri<=g+2; ri++) {
l=strlen(s[ri]);
for(i=0; i<=5; i++) {
nrig++;
if(nrig<6 || nrig==9 || nrig>=12) {
for(j=0; j<l; j++) {
if((int)(s[ri][j])==' ') {
printf("%s ", rig[4]);
}
else if((int)(s[ri][j])=='_') {
if (i<=1 || i>=3) {
printf(" ");
}
else {
printf("%c%c%c%c",205,205,205,205);
}
}
else if((int)(s[ri][j])=='|') {
printf(" %c%c",186,186);
}
else {
p=(char)(s[ri][j]);
p2=atoi(&p);
st=(int)(cod[p2][i])-48;
printf("%s", rig[st]);
if((int)(s[ri][j+1])=='.'){
if (i<2 || i>3) {
printf(" ");
}
else {
printf(" %c ",220);
}
j++;
}
else {
printf(" ");
}
}
}
printf("\n");
}
}
}
for(i=0; i<20; i++){
for(j=0; j<200; j++){
s[i][j]="0";
}
}
}