读取简单数据声明并以分配给该变量的内存量作为响应的程序 [英] program that reads simple data declarations and responds with the amount of memory that would be allocated to that variable

查看:63
本文介绍了读取简单数据声明并以分配给该变量的内存量作为响应的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序读取简单的数据声明并以将分配给该变量的内存量作为响应.

I have written a program that reads simple data declarations and responds with the amount of memory that would be allocated to that variable.

每行输入应包含-类型名称,该名称必须为以下之一:char,int,short,long,float或double. -一个或多个单独的声明规范,以逗号分隔. -分号标记行尾.

Each input line should consist of - A type name, which must be one of the following: char, int, short, long, float, or double. - One or more individual declaration specifications separated by commas. - A semicolon marking the end of line.

在我的代码中,我还添加了一个文件用法(见下文).

In my code I also add a use of file (see below).

我想问你如何使用sizeof而不是编码要分配的数据类型的大小的方式.我还要问你我在文件中的使用是否正确.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

void bytesPerValue(char str[], char* filename) ;
int theSizeOf(char *str);
 int strToNumber(char *str);

void main()
{
     char str[50];

     gets(str);

     bytesPerValue(str,"input.txt");

}

void bytesPerValue(char str[], char* filename) 
{
        int i = 0, j = 0;
        int temp = 1;
        int size;
        char* tempChar = (char*)malloc((strlen(str))*sizeof(tempChar));
        FILE *f=fopen(filename,"w");
        if (f==NULL)
            exit(1);
        while (str[i]!=' ' || str[i]=='*') //checking the type of the variables//
        {
                tempChar[j] = str[i];
                i++;
                j++;   
        }
        tempChar[j] = '\0';
        size = theSizeOf(tempChar);
        j = 0;
        i++;
        while (str[i] != ';')
        {

                if (isalpha(str[i]) || str[i]=='_') // for  variables and arrays//
                {
                        while (str[i] != ',' && str[i] != ';') //runs until ', ' or '; ' //
                        {
                                if (str[i]==' ')
                                {
                                        while (str[i]==' ')
                                                i++;
                                }

                                if (str[i] == '[') //checks if it is array//
                                {
                                        printf("%c", str[i]);
                                        i++;
                                        while (str[i] != ']')
                                        {
                                                tempChar[j] = str[i]; //copies the value in the string//
                                                i++;
                                                j++;
                                        }

                                        tempChar[j] = '\0';
                                        temp = strToNumber(tempChar); //converting to number in order to valuate the bytes//
                                }
                                printf("%c", str[i]);
                                i++;

                                if (isspace(str[i]))
                                {
                                        while (isspace(str[i]))
                                                i++;
                                }
                        }
                        fprintf(f," requires %d bytes \n", temp*(sizeof(temp)));
                }


                if (str[i] == '*') //for pointers//
                {
                        while (str[i] != ',' && str[i] != ';')
                        {
                                printf("%c", str[i]);
                                i++;
                                if (str[i]==' ')
                                {
                                        while (str[i]==' ')
                                                i++;
                                }
                        }
                        fprintf(f," requires %d bytes \n", 4);
                }
                if (str[i] != ';')
                        i++;
        }

 fclose(f);
}

int theSizeOf(char* str) // checking the size of the variable
{
        if (strcmp(str, "int")==0 || strcmp(str, "long")==0 || strcmp(str, "float")==0)
                return 4;
        else if (strcmp(str, "char")==0)
                return 1;
        else if (strcmp(str, "double")==0)
                return 8;
        else if (strcmp(str, "short")==0)
                return 2;
        else 
            return 0;
}


int strToNumber(char* str) //converting the string to number//
{
        int temp=1;
        int num=0;
        int t;
        int i;
        int length = strlen(str);
        for (i = length-1; i >= 0; i--)
        {
                t = str[i] - '0';
                num += t * temp;
                temp *= 10;
        }
        return num;
} 

推荐答案

如何使用sizeof而不是编码要分配的数据类型的大小的方式?

how can I use sizeof instead the way I coded the size of data types to be allocated?

size_t theSizeOf(const char* str) {
  if (strcmp(str, "char"  )==0) return sizeof(char);
  if (strcmp(str, "short" )==0) return sizeof(short);
  if (strcmp(str, "int"   )==0) return sizeof(int);
  if (strcmp(str, "long"  )==0) return sizeof(long);
  if (strcmp(str, "float" )==0) return sizeof(float);
  if (strcmp(str, "double")==0) return sizeof(double);
  return 0;
}

或使用数组

size_t theSizeOf(const char* str) {
  const struct {
    const char *type;
    size_t size;
  } types[] =  {
     {"char", sizeof(char) },
     {"int", sizeof(int) },
     // Add others as needed.
     {"double", sizeof(double) },
  };
  for (size_t i=0; i< sizeof types /sizeof types[0]; i++) {
     if (strcmp(str, types[i].type)==0) return types[i].size;
  }
  return 0;
}


我也想问你我在文件中的使用是否正确.

I also would like to ask you whether my use in file is right.

许多问题,例如

    // No need for cast
    // buffer allocation 1 too short
    // wrong sizeof argument
    char* tempChar = (char*)malloc((strlen(str))*sizeof(tempChar));
    // instead use
    char* tempChar = malloc(strlen(str) + 1);

    // potential infinite loop as code does not check for null character
    while (str[i]!=' ' || str[i]=='*') {
      ...

    // Does not handle negative numbers nor overflow of `temp`.
    strToNumber()

这篇关于读取简单数据声明并以分配给该变量的内存量作为响应的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆