Home > Back-end >  C language when debugging, when to the scanf this step, according to the theory of black window can
C language when debugging, when to the scanf this step, according to the theory of black window can

Time:10-20

#include
# define MAXLEN 30
Typedef int DataType.
Typedef struct
{
DataType data [MAXLEN];
Int Length;
}SeqList;
SeqList L;
Void InitList (SeqList * L)
{
L - & gt; Length=0;
}
Void CreateList (SeqList * L, int n)
{
int i;
Printf (" please input integers: % d ", n);
for(i=0; iThe scanf (" % d ", & amp; L - & gt; Data [I]);
L - & gt; Length=I;
}
Int GetElem (SeqList * L, int I, DataType * x)
{
If (i<1 | | i> L - & gt; Length)
return 0;
The else
X={* L - & gt; Data [I - 1);
return 1;
}
}
Int the Locate (SeqList * L, DataType x)
{
int i=0;
While (ii++;
If (i>=L - & gt; Length)
return 0;
The else
Return the I + 1;
}
Int InsElem (SeqList * L, int I, DataType x)
{
int j;
If (L - & gt; Length>=MAXLEN)
{
Printf (" order list is full!" );
return -1;
}
If (i<1 | | i> L - & gt; Length + 1)
{
Printf (" insert position error!" );
return 0;
}
If (I==L - & gt; Length + 1)
{
L - & gt; Data [I - 1]=x;
L - & gt; Length++;
return 1;
}
For (j=L - & gt; Length - 1. J>=I - 1; J -)
L - & gt; Data [j + 1)=L - & gt; Data [j];
L - & gt; Data [I - 1]=x;
L - & gt; Length++;
return 1;
}
Int DeIElem (SeqList * L, int I, DataType * x)
{
int j;
If (L - & gt; Length==0)
{
Printf (" order list is empty!" );
return 0;
}
If (i<1 | | i> L - & gt; Length)
{
Printf (" does not exist the ith element ");
return 0;
}
* x=L - & gt; Data [I - 1);
For (j=I; JL - & gt; Data [1]=L - & gt; Data [j];
L - & gt; Length -;
return 1;
}
Void DispList (SeqList * L)
{
int i;
for(i=0; iPrintf (" % 5 d, L - & gt; Data [I]);
}
Void Menu ()
{
Printf (" \ n the operations order list ");
Printf (" \ n=============================================");
Printf (" \ n table | | 1 -- build order ");
Printf (" \ n | | 2 -- insert element ");
Printf (" \ n | 3 | - delete elements ");
Printf (" \ n | | 4 -- according to the location to find elements ");
Printf (" \ n | 5 - find its position according to the element value | ");
Printf (" \ n long table 6 - | o | ");
Printf (" \ n return to | | 0 - ");
Printf (" \ n=============================================");
Printf (" \ n please input menu (0 to 6) : ");
}
Int main ()
{
SeqList L;
DataType x;
Int n, I, loc;
Char ch1 and ch2, a;
Ch1='y'
While (ch1=='y' | | ch1=='y');
{Menu ();
The scanf (" % c ", & amp; Ch2);
getchar();
The switch (ch2)
{
Case '1' :
InitList (& amp; L);
Printf (" please input to establish linear table number: ");
The scanf (" % d ", & amp; n);
CreateList (& amp; L, n);
Printf (" establish a linear list is: ");
DispList (& amp; L);
break;
Case '2' :
Printf (" please enter to insert position: ");
The scanf (" % d ", & amp; I);
Printf (" please enter to insert element value: ");
The scanf (" % d ", & amp; X);
If (InsElem (& amp; L, I, x))
{
Printf (" has been successfully insert in the position of the first % d % d, after the insertion of the linear table for: \ n ", I, x);
DispList (& amp; L);
}
The else
Printf (" input the parameters of the insert error!" );
break;
Case '3' :
Printf (" please enter a location to remove elements: ");
The scanf (" % d ", & amp; I);
If (DeIElem (& amp; L, I, & amp; X))
{
Printf (" has been successful in the first position to delete % d % d, after deleting the linear table for: \ n ", I, x);
DispList (& amp; L);
}
The else
Printf (" \ n input delete parameter error!" );
break;
Case '4' :
Printf (" please enter to view the location in the table (starting at 1) : ");
The scanf (" % d ", & amp; I);
If (GetElem (& amp; L, I, & amp; X))
Printf (" the current linear table the first element has a value of % d: % d ", I, x);
The else
Printf (" input position error!" );
break;
Case '5' :
Printf (" please enter to find element value is: ");
The scanf (" % d ", & amp; X);
Loc=Locate (& amp; L, x);
If (loc)
Printf (" looking for the position of the element value is % d: % d ", x, loc);
The else
Printf (" without the elements in the table!" );
break;
Case '6' :
Printf (" the length of the current linear table is: % d ", L.L ength);
break;
Case '0' :
Ch1='n'. break;
Default:
Printf (" input is wrong, please input 0 ~ 6 choice!" );
}
If (ch2!='0')
{
Printf (" \ n press the enter key to continue, press any key to return to the main menu! \n");
A=getchar ();
If (a!='\ xA')
{
getchar(); Ch1='n'.
}
}
}
}

CodePudding user response:

 
//SeqList L;//-- -- -- -- -- -- -- -- comment out the bank, because L have in the main () function is defined outside of -- -- -- -- -- -- --
DataType x;
Int n, I, loc;
Char ch1 and ch2, a;
Ch1='y'
While (ch1=='y' | | ch1=='y')//-- -- -- -- -- -- -- -- -- get rid of the semicolon -- -- -- -- -- -- -- -- -- -- --


VS2015 c + + environment

CodePudding user response:

Copy your code into my vs2017, modify the bug (e.g., the semicolon) while statement after, can run normally
But
 
Case '6' :
Printf (" the length of the current linear table is: % d ", L.L ength);
break;

This statement is clearly wrong, L is an uninitialized variable, L.L ength can't return the correct value, of course, (the host's hypothesis is carried out in menu order 1, 2... 6, if the program start choose 6 will be an error)


Also: a case statement block content too much, had better use {} wrapped up,
 
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related