Drugi problem to #include . Przed jego dodaniem wszystko było ok. Po jego dodaniu cały dotychczasowy kod działa "inaczej". Jako przykład mogę podać zachowanie po wybraniu opcji 'a'. Bez conio nie czeka na potwierdzenie enterem, tylko od razu wchodzi w opcję, potem czeka na wprowadzenie litery, która ma być nazwą macierzy. Z conio zaś czeka na potwierdzenie enterem, robi jakieś niepotrzebne puste linie, nie wiadomo skąd zakłada, że została jako nazwa macierzy wybrana wartość nie będąca literą, choć nie wybrano żadnej wartości.
Pozdro!
Kod: Zaznacz cały
#include <stdio.h>
#include <conio.h>
//#include <cstdlib.h> //library for cleaning the screen
struct matrices
{
int owned; //do you own this matrix
int sizea, sizeb; //what is the size of matrix, a-rows, b-columns
int values[25][25]; //every element of this matrix
} matrix[27]; //there are 27 matrices
char choose_opt()
{
char temp_opt;
system("cls");
printf("Matrix calculator \n\
(a) Create new matrix \n\
(b) Edit existing matrix \n\
(c) Show existing matrix \n\
(d) Delete existing matrix \n\
(e) Save matrices to the file \n\
(f) Load matrices from the file \n\
(g) Transposition (MxN)^T = (NxM) \n\
(h) Addition (MxN)+(MxN)=(MxN) \n\
(i) Scalar multiplication c*(MxN)=(MxN) \n\
(j) Matrix multiplication (MxN)x(NxP)=(MxP)\n\
(k) Exponentiation (MxM)^c=(MxM) \n\
(l) Determinant det[(MxM)]=c\n\
(m) Inverse (MxM)^(-1)=(MxM) if det[(MxM)]<>0\n\
(n) About the program\n\
(o) Exit the application\n");
do
{
printf("Choose your option.");
temp_opt = getch();
if ((temp_opt < 'a')||(temp_opt > 'o'))
printf("\nImproper choice. ");
else
printf("\n");
} while ((temp_opt < 'a')||(temp_opt > 'o'));
return temp_opt;
}
void Show_owned()
{
int i;
int do_you_own_any_matrix=0; //check wheter there are any owned matrices
for (i=0;i<27;i++)
if (matrix[i].owned)
{
do_you_own_any_matrix=1;
break;
}
if (do_you_own_any_matrix) //if there are, show which one
{
printf("You own these matrices: ");
for (i=0;i<27;i++)
{
if (matrix[i].owned==1)
printf("%c",'a'+i-1);
}
}
else
printf("You do not own any matrix.");
}
void Func_a()
{
char temp_opt_local;
int temp_opt_local_number;
int siza,sizb; int error;
Show_owned(); //show matrices owned by the user
printf("\nChoose the letter for a new matrix (zero for exit).");
do //choose the letter as a name for your new matrix
{
temp_opt_local = getch();
if (temp_opt_local == '0') {printf("\nYou have not created new matrix");break;}
if ((temp_opt_local < 'a')||(temp_opt_local > 'z'))
{printf("\nYou must choose the letter. ");continue;}
temp_opt_local_number = (int)temp_opt_local-(int)'a'+1; //int x; char y; x=(int)y writes number of symbol y in ascii table
if (matrix[temp_opt_local_number].owned==1)
{printf("\nYou must choose another letter. ");continue;} //continue is not needed
else
{printf("\nYour choise is proper: %c. ",temp_opt_local); break;}
} while (1==1);
//after choosing the letter, determine size of the matrix
if (temp_opt_local != '0')//((temp_opt_local >= 'a')&&(temp_opt_local <= 'z'))
{
siza=0;sizb=0;
printf("\nWrite two numbers (first for number of rows, second for columns). Use space to");
printf("\nseparate these two numbers. They must be integers between 1 and 25.");
do
{
printf("\nThe size of the matrix: ");
fflush(stdin); //very important line - it clears standard input
error = scanf("%d %d",&siza,&sizb); //error becomes number of correctly read inputs
printf("(%d)(%d) ",siza,sizb); //testing line
} while ((error!=2)||(siza<0)||(siza>25)||(sizb<0)||(sizb>25));
}
printf("[siza %d, sizb %d]",siza,sizb); //testing line
}
int main(int argc, char *argv[])
{
char chosen_opt;
int i; //give the information that there are no owned matrices
for (i=0;i<27;i++)
matrix[i].owned=0;
do //choose the operation on matrix
{
chosen_opt = choose_opt();
printf("Your choice: %c\n",chosen_opt);
switch (chosen_opt)
{
case 'a': Func_a(); break;
case 'b': printf("Now you own matrix nr7"); matrix[7].owned=1; break; //temporary option
case 'c': printf("You don't own m nr7"); matrix[7].owned=0; break; //temporary option
case 'o': printf("See you later!"); break;
default : printf("Under construction"); break;
}
printf("\n");
getch();
} while (chosen_opt!='o');
return 0;
}