Home > Back-end >  /C language help kindergarten calculator 2.0 (plus or minus a positive integer,) related issues
/C language help kindergarten calculator 2.0 (plus or minus a positive integer,) related issues

Time:03-24

My train of thought is a cycle of a, pick battles, about the number, the number of operations, is assigned to another array c, c to characters, then put into integer array c d, finally to addition and subtraction array d

A - & gt; N - & gt; C - & gt; D - & gt; The result

 # include & lt; stdio.h> 
# include & lt; Math. H>
# include & lt; stdlib.h>
Void main ()
{

{
Printf (" hw additional \ n ");
Int I, j=0, result, sz, TMP=0, m;
Printf (" please enter in the number of figures: \ n ");
The scanf (" % d ", & amp; Sz);
Int size=2 * sz - 1;//the size is the length of all characters, the back is used to control the loop condition

getchar();//absorb enter ~
Printf (" please enter formula: \ n ");
Char a [1000], c [1000], [1000] d, n [1000];//a [1000] of the original characters, n [1000] is used to record a digital part of the []
//c [1000] record after the arithmetic of characters in a (that is the difference between a and c is that there are no battles in c)
/[1000] for c/d into integer array

Int tmp1, tmp2;//the two temporary variables are integer

Gets (a);
//a of the record number (a - & gt; N)
for(int i=0; I{
If (a [I]! )
='\ n'{
If (a [I] <='9' & amp; & A [I] & gt;='0')
{
Sscanf (& amp; A [I], "% d", & amp; N [I]);

//printf (" n [% d]=% d ", I, n [I]);

}
}
The else break;

}

//record, and will, calculate a
for(i=0; I{
If (a [I]! )
='\ n'{
If (a [I]=='*')
{
M=I - (2 * TMP + 1);
/* m=I - (2 * TMP + 1) is generated after, the number of new array ordinal number, such as 1-2 * 3 * located at I=4, after operation into 1-6, number 6 in the I 'm=3===I - (2 * TMP + 1)=4 - (2 * 0 + 1)=3 formula of perfect confirm ~ */

//TMP is the number, the number of the previous number,

Tmp1=n * n [I - 1] [I + 1);//such as tmp1=2 * 3
//printf (" tmp1=% d \ n ", tmp1);
Sprintf (& amp; C [m], "% d", tmp1);//the integer tmp1 into string c
//printf (" [% d]=% c "c, m, c [m]);
Tmp++;//every battles, TMP since an

}
Else if (a [I]=='/')
{
M=I - (2 * TMP + 1);
Tmp2=n [I - 1)/n (I + 1),
Sprintf (& amp; C [m], "% d", tmp2);
Tmp++;
}
The else
{
M=I - 2 * TMP;//in the current is not a number, case, calculate the current character of ordinal
C=[m] a [I];//not directly assigned to, when c
}
}
The else break;
}
//output c [m], in order to observe the
for(m=0; M{
Printf (" [c] % d=% \ n "c, m, c [m]);
}
Record//c number (c - & gt; D)
for(m=0; M{
If (c [m]. )
='\ n'{
If (c [m] <='9' & amp; & C [m] & gt;='0')
{
Sscanf (& amp; C [m], "% d", & amp; D [m]);
Printf (" [% d]=% c "c, m, c [m]);
Printf (" d [% d]=% d ", m, d [m]);

}
}
The else break;

}
Result=d [0];
Printf (" result0=% d \ n ", result);

//the following part to add and subtract computing
for(m=0; M{


If (c [m]. )
='\ n'{

If (c [m]=='-')
{
Printf (" [% d] d=% d ", m + 1, d [m + 1));
The result -=d [m + 1];
}
Else if (c [m]=='+')
{
The result +=d [m + 1];
}
}
The else break;



}
Printf (" result=% d ", result);

}



Results:

CodePudding user response:

Trouble to help us see,,,

CodePudding user response:

Problem: participate in the operation of only single digits, in the formula can't LianCheng or even (such as: 1 + 2 * 3 * 4), for reference:
 # include & lt; stdio.h> 
# include & lt; Math. H>
# include & lt; stdlib.h>
Void main ()
{
Printf (" hw additional \ n ");
Int I, j=0, result, sz, TMP=0, m;
Char t [2];//add character array t
Printf (" please enter in the number of figures: \ n ");
The scanf (" % d ", & amp; Sz);
Int size=2 * sz - 1;//the size is the length of all characters, the back is used to control the loop condition

getchar();//absorb enter ~
Printf (" please enter formula: \ n ");
Char a [1000], c [1000], [1000] d, n [1000];//a [1000] of the original characters, n [1000] is used to record a digital part of the []
//c [1000] record after the arithmetic of characters in a (that is the difference between a and c is that there are no battles in c)
/[1000] for c/d into integer array
Int tmp1, tmp2;//the two temporary variables are integer
Gets (a);
//a of the record number (a - & gt; N)
for(int i=0; I{
If (a [I]! )
='\ n'{
If (a [I] <='9' & amp; & A [I] & gt;='0')
{
Sscanf (& amp; A [I], "% d", & amp; N [I]);

Printf (" n [% d]=% d ", I, n [I]);
}
}
The else break;
}
//record, and will, calculate a
for(i=0; I{
If (a [I]! )
='\ n'{
If (a [I]=='*')
{
M=I - (2 * TMP + 1);
//m=I - (2 * TMP + 1) is generated after, the number of new array ordinal number, such as 1-2 * 3 * located at I=4, after operation into 1-6,
//number 6 in the I 'm=3===I - (2 * TMP + 1)=4 - (2 * 0 + 1)=3 formula of perfect confirm ~
//TMP is the number, the number of the previous number,
Tmp1=n * n [I - 1] [I + 1);//such as tmp1=2 * 3
//printf (" tmp1=% d \ n ", tmp1);
Sprintf (t, "% d", tmp1);//the integer tmp1 into characters
C [m] [0]=t;
//printf (" [% d]=% c "c, m, c [m]);
Tmp++;//every battles, TMP since an
i++;//since I add a
}
Else if (a [I]=='/')
{
M=I - (2 * TMP + 1);
Tmp2=n [I - 1)/n (I + 1),//not consider not divisible
the condition of theSprintf (t, "% d", tmp2);
C [m] [0]=t;
Tmp++;
i++;
}
The else
{
M=I - 2 * TMP;//in the current is not a number, case, calculate the current character of ordinal
C=[m] a [I];//not directly assigned to, when c
}
}
The else break;
}
//output c [m], in order to observe the
for(i=0; I{
Printf (" [c] % d=% c \ n ", I, c [I]);
}
//system (" pause ");
for(m=0; M{
If (c [m]. )
='\ n'{
If (c [m] <='9' & amp; & C [m] & gt;='0')
{
Sscanf (& amp; C [m], "% d", & amp; D [m]);
Printf (" [% d]=% c "c, m, c [m]);
Printf (" d [% d]=% d ", m, d [m]);

}
}
The else break;

}
Result=d [0];
Printf (" result0=% d \ n ", result);

//the following part to add and subtract computing
for(m=0; M
  • Related