#include <stdio.h>
#include <malloc.h>
typedef struct conversion {
double data;
struct conversion *next;
}conver;
void pushit(conver *top, int data); //入堆疊
void outit(conver *top); //出堆疊
void integer_translate(int Integer, conver *top, int Base);
void decimal_translate(double Decimal, int Base);
int main() {
conver *TOP1 = NULL; //頭節點,分別對應整數和小數
TOP1 = (conver *)malloc(sizeof(TOP1));
TOP1->data = NULL;
TOP1->next = NULL;
double data, decimal;
int integer, base;
printf("請輸入數字:");
scanf("%lf", &data); //讀入資料
integer = (int)data; //整數部分
decimal = data - integer; //小數部分
printf("數字讀入完成!\n");
printf("請輸入需要轉換的進制數(小寫整數):");
scanf("%d", &base); //讀入進制數
integer_translate(integer, TOP1, base); //整數進制轉換
printf(".");
decimal_translate(decimal, base);
getchar();
return 0;
}
void pushit(conver *top, int data) { //入堆疊
conver *node = (conver *)malloc(sizeof(node));
node->next = NULL;
node->data = data;
node->next = top->next;
top->next = node;
node = NULL;
}
void outit(conver *top) { //出堆疊
conver *node = NULL;
node = top->next;
printf("%d", (int)node->data);
top->next = node->next;
}
void integer_translate(int Integer, conver *top, int Base) { //整數進制轉換
while (Integer > 0) {
pushit(top, Integer%Base);
Integer /= Base;
}
while (top->next != NULL) {
outit(top);
}
}
void decimal_translate(double Decimal, int Base) { //進制轉換
int i = 0;
while (Decimal > 0 && i <= 10) {
Decimal *= Base;
printf("%d", (int)Decimal);
if (Decimal >= 1)
Decimal -= (int)Decimal;
i++;
}
}
需要實作的功能是將十進制小數轉換成任意進制的數字,在33行的conver *node = (conver *)malloc(sizeof(node));顯示觸發了一個斷點。

請求各位大神的指導!
萬分感謝!
uj5u.com熱心網友回復:
補充:在點擊繼續后顯示——0x76FAFA1D (ntdll.dll) (資料結構實驗二.exe 中)處有未經處理的例外: 0xC0000374: 堆已損壞。 (引數: 0x76FEB960)。uj5u.com熱心網友回復:
改成covert *node = (covert*)malloc(sizeof(conver));
node是個指標,sizeof(node)只有4個位元組(64位系統是8個位元組),convert是個結構體,sizeof(convert)有8+4(或8)=12(或16)個位元組。
顯然,申請記憶體空間不足,就可能造成記憶體非法訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/183402.html
標籤:C語言
上一篇:大佬看看我,第一次寫資料結構,有輸入,沒輸出,不知道哪里錯了
下一篇:移動小游戲
