[英]An empty for loop fixed my code but I don't know how
我正在嘗試解決一些練習以准備即將到來的測試,我遇到了一個我偶然解決的問題,我想了解我的代碼發生了什么。
練習要求我創建一個結構類型,它表示 4 個變量(x、y、w 和 z)的單項式和兩個函數,print_term 將在屏幕上顯示單項式,read_term 接受用戶輸入並返回單項式。 該練習要求用戶輸入的格式為“CVPVPVPVP”,其中 C 是單項式的系數,V 是 4 個變量之一(以任意順序),P 是它們的冪。
//Here is the monomial struct
struct term{
int coef;
int potx;
int poty;
int potw;
int potz;
};
我的問題在於 read_term 函數:有時,如果 C 為負,則其中一個變量的冪將設置為 -48(這肯定來自我嘗試將表示數字的 char 轉換為 int 中的值):對於例如,輸入“-257 x 2 z 3 w 1”。 將返回-257(x^2)(w^-48)(z^3),而“257 x 2 z 3 w 1”。 而是返回正確的結果 275(x^2)w(z^3)。
struct term read_term(){
struct term result;
result.coef =0;
result.potx =0;
result.poty =0;
result.potw =0;
result.potz =0;
char input[255];
for(int i =0; i < sizeof(input)/sizeof(input[0]);i++){
input[i] = '\0';
}
char inputNeg[255];
int negativo = 0;
printf("Termine: \n");
fgets(input, 255, stdin);
input[strlen(input)-1] = '\0'; //should remove '\n'
//I remeve the minus sign, i'll add it back in later.
if(input[0] == '-'){
negativo = 1;
int i = 1;
do{
inputNeg[i-1] = input[i];
i++;
}while(input[i] != '\0');
printf("\n%s\n", input);
strcpy(input, inputNeg);
printf("\n%s\n", input);
}
////////////THIS MAKE OR BRAKE THE CODE
//printf("\ninput prova\n");
for(int i =0; i < sizeof(input)/sizeof(input[0]);i++){
//printf("%c", input[i]);
}
////////////////////////////
//Change struct term result based on input:
int index = 0;
int cifreCof[12];
for(int j=0; j< sizeof(cifreCof)/sizeof(cifreCof[0]);j++){
cifreCof[j] = 0;
}
int temp = 0;
int pot = 0;
do{
//Coeff is always at index 0
if(index == 0){
//Conversion from char to int
do{
cifreCof[index] = input[index] - '0';
index++;
pot++;
}while(input[index] != ' ');
//Puts all the digits in one int
for(int i=0; i < sizeof(cifreCof)/sizeof(cifreCof[0]); i++){
temp = temp + (cifreCof[i]*pow(10, pot-1));
pot--;
}
//Place the minus sign back.
if(negativo == 0){
result.coef = temp;
}
else{
result.coef = (-1)*temp;
}
}
// Variabili.
else{
//White space
if(input[index] == ' '){
index++;
}
//x char
else if(input[index] == 'x'){
index++;
index++;
//per ora mi limito ad esponenti a una cifra
if(input[index] != '.'){
result.potx = input[index] - '0';
index++;
}
else{index++;}
}
//y char
else if(input[index] == 'y'){
index++;
index++;
//per ora mi limito ad esponenti a una cifra
if(input[index] != '.'){
result.poty = input[index] - '0';
index++;
}
else{index++;}
}
// w char
else if(input[index] == 'w'){
index++;
index++;
//per ora mi limito ad esponenti a una cifra
if(input[index] != '.'){
result.potw = input[index] - '0';
index++;
}
else{index++;}
}
//z char
else if(input[index] == 'z'){
index++;
index++;
//per ora mi limito ad esponenti a una cifra
if(input[index] != '.'){
result.potz = input[index] - '0';
index++;
}
else{index++;}
}
else{index++;}
}
}while(input[index] != '\0');
return result;
}
為了確定問題,我放置了一個 for 循環來打印我用來保存輸入的數組,只是為了發現這個循環將函數的結果更改為正確的結果:我決定保留循環但清空它它的代碼,結果仍然是正確的。 我不知道它如何修復我的代碼,但確實如此。 有什么解釋嗎?
空循環不太可能與修復代碼有關。 但未定義的行為可能在暴露問題方面發揮了作用。
創建變量時,分配的內存內容是未知的。 因此,如果在初始化之前使用它,結果將是不可預知的。
一個簡單的初始化將起作用:
char inputNeg[255] = {0};//initializes all elements of array in one step.
雖然 for 循環確實在這里初始化了數組
char input[255];
for(int i =0; i < sizeof(input)/sizeof(input[0]);i++){
input[i] = '\0';
}
...聲明期間的初始化更有效:
char input[255] = {0};
對cifreCof
做同樣的事情
關於調用fgets
后刪除換行符,請考慮使用此方法:
//input[strlen(input)-1] = '\0';//unsafe for an empty string
input[strcspn(input, "\n")] = 0;//this is safe, and will remove newline
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.