スタックについての質問です。
Posted: 2015年5月20日(水) 19:33
私は教科書のスタックの見本を元に身体測定のデータ(氏名、体重、年齢、視力、身長)を記録するプログラムを作りたいのですが、pushが正しく働いてくれません。もしかしたら他の部分も間違っているのかもしれないです。助言をいただけるとありがたいです。それと名前を入力するところで強制終了するようになってしまいました。C言語の能力は低いです。
申し訳ないのですがソースコードが長いです。
code=c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#define line 100
typedef struct{
double weight;
double eyesight;
int age;
int height;
} Data;
typedef struct{
char *name;
Data data;
} sinntaisokutei ;
typedef struct{
int max;
int pos;
sinntaisokutei **stack;
} sinntaisokuteistack;
int Initialize(sinntaisokuteistack *s, int max){
s->pos = 0;
if ((s->stack = calloc(max, sizeof(sinntaisokutei *))) == NULL) {
s->max = 0;
return -1;
}
s->max = max;
return 0;
}
void Terminate(sinntaisokuteistack *s){
if (s->stack != NULL){
while( --s->pos >= 0)
free(s->stack[s->pos]);
free(s->stack);
}
s->max = s->pos = 0;
}
int Push(sinntaisokuteistack *s, sinntaisokutei *x){
if (s->pos >= s->max) {
return -1;
}
if ((s->stack[s->pos] = calloc(mbstrlen(x)+1, sizeof(sinntaisokutei))) == NULL) {
return -1;
}
s->stack[s->pos++]=x;
return 0;
}
int Pop(sinntaisokuteistack *s, sinntaisokutei *x){
if (s->pos <= 0){
return -1;
}
x=s->stack[--s->pos];
free(s->stack[s->pos]);
return 0;
}
int Peek(sinntaisokuteistack *s, sinntaisokutei *x){
if (s->pos <= 0) {
return -1;
}
x=s->stack[s->pos-1];
return 0;
}
int Maximumnumber(const sinntaisokuteistack *s){
return s->max;
}
int Registrationnumber(const sinntaisokuteistack *s){
return s->pos;
}
void Print2(const sinntaisokutei *x){
printf("%-18.18s%.2lf%5.4d%5.4d%.2lf\n", x->name, x->data.weight, x->data.age, x->data.height, x->data.eyesight);
}
void Print(const sinntaisokuteistack *s){
int i;
for(i = 0; i < s->pos; i++) {
Print2(s->stack);
}
putchar('\n');
}
int main(void){
sinntaisokuteistack s;
int max;
printf("何人登録しますか");
scanf("%d", &max);
if (Initialize(&s, max)==-1){
puts("登録できません.\n");
return 1;
}
while (1) {
int select;
sinntaisokutei x[line];
printf("現在のデータ数:%d/%d\n",Registrationnumber(&s), Maximumnumber(&s));
printf("(1)登録 (2)削除 (3)次に削除されるデータ (4)全データ表\示 (0)終了:");
scanf("%d", &select);
if (select == 0) break;
switch (select) {
case 1:
printf("名前: "); scanf("%s", x->name);
printf("体重: "); scanf("%lf", &(x->data.weight));
printf("年齢: "); scanf("%d", &(x->data.age));
printf("視力: "); scanf("%lf", &(x->data.eyesight));
printf("身長: "); scanf("%d", &(x->data.height));
if (Push(&s, x) == -1)
puts("\aエラー:登録できませんでした.\n");
break;
case 2:
if (Pop(&s, x) == -1)
puts("\aエラー:削除出来ませんでした.\n");
else{
printf("削除したしたデータは");
Print2(x);
}
break;
case 3:
if (Peek(&s, x) == -1)
puts("\aエラー:表\示できませんでした.\n");
else{
printf("次に削除されるデータは%s", x);
}
break;
case 4:
Print(&s);
break;
}
}
Terminate(&s);
return 0;
}
/code
よろしくお願いします。
申し訳ないのですがソースコードが長いです。
code=c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#define line 100
typedef struct{
double weight;
double eyesight;
int age;
int height;
} Data;
typedef struct{
char *name;
Data data;
} sinntaisokutei ;
typedef struct{
int max;
int pos;
sinntaisokutei **stack;
} sinntaisokuteistack;
int Initialize(sinntaisokuteistack *s, int max){
s->pos = 0;
if ((s->stack = calloc(max, sizeof(sinntaisokutei *))) == NULL) {
s->max = 0;
return -1;
}
s->max = max;
return 0;
}
void Terminate(sinntaisokuteistack *s){
if (s->stack != NULL){
while( --s->pos >= 0)
free(s->stack[s->pos]);
free(s->stack);
}
s->max = s->pos = 0;
}
int Push(sinntaisokuteistack *s, sinntaisokutei *x){
if (s->pos >= s->max) {
return -1;
}
if ((s->stack[s->pos] = calloc(mbstrlen(x)+1, sizeof(sinntaisokutei))) == NULL) {
return -1;
}
s->stack[s->pos++]=x;
return 0;
}
int Pop(sinntaisokuteistack *s, sinntaisokutei *x){
if (s->pos <= 0){
return -1;
}
x=s->stack[--s->pos];
free(s->stack[s->pos]);
return 0;
}
int Peek(sinntaisokuteistack *s, sinntaisokutei *x){
if (s->pos <= 0) {
return -1;
}
x=s->stack[s->pos-1];
return 0;
}
int Maximumnumber(const sinntaisokuteistack *s){
return s->max;
}
int Registrationnumber(const sinntaisokuteistack *s){
return s->pos;
}
void Print2(const sinntaisokutei *x){
printf("%-18.18s%.2lf%5.4d%5.4d%.2lf\n", x->name, x->data.weight, x->data.age, x->data.height, x->data.eyesight);
}
void Print(const sinntaisokuteistack *s){
int i;
for(i = 0; i < s->pos; i++) {
Print2(s->stack);
}
putchar('\n');
}
int main(void){
sinntaisokuteistack s;
int max;
printf("何人登録しますか");
scanf("%d", &max);
if (Initialize(&s, max)==-1){
puts("登録できません.\n");
return 1;
}
while (1) {
int select;
sinntaisokutei x[line];
printf("現在のデータ数:%d/%d\n",Registrationnumber(&s), Maximumnumber(&s));
printf("(1)登録 (2)削除 (3)次に削除されるデータ (4)全データ表\示 (0)終了:");
scanf("%d", &select);
if (select == 0) break;
switch (select) {
case 1:
printf("名前: "); scanf("%s", x->name);
printf("体重: "); scanf("%lf", &(x->data.weight));
printf("年齢: "); scanf("%d", &(x->data.age));
printf("視力: "); scanf("%lf", &(x->data.eyesight));
printf("身長: "); scanf("%d", &(x->data.height));
if (Push(&s, x) == -1)
puts("\aエラー:登録できませんでした.\n");
break;
case 2:
if (Pop(&s, x) == -1)
puts("\aエラー:削除出来ませんでした.\n");
else{
printf("削除したしたデータは");
Print2(x);
}
break;
case 3:
if (Peek(&s, x) == -1)
puts("\aエラー:表\示できませんでした.\n");
else{
printf("次に削除されるデータは%s", x);
}
break;
case 4:
Print(&s);
break;
}
}
Terminate(&s);
return 0;
}
/code
よろしくお願いします。