c - 读取简单数据声明并响应分配给该变量的内存量的程序

标签 c string

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

如果程序读取到空白输入行,则应退出。

我为此程序编写了以下代码。它似乎工作得很好,除了我收到以下警告:

d:\documents\documents\visual studio 2012\projects\project3\project3\source.c(108): warning C4715: 'theSizeOf' : not all control paths return a value.

顺便说一句,我想知道它是否可以改进(也许可以通过使用 strtok?)。我还想在这个程序中添加一个文件来包含输出,但我完全不确定它是如何完成的。

#define _CRT_SECURE_NO_WARNINGS

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

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

void main()
{
     char str[50];
     gets(str);

     bytesPerValue(str);

}

void bytesPerValue(char str[]) //Ex5
{
        int i = 0, j = 0;
        int temp = 1;
        int size;
        char* tempChar = (char*)malloc((strlen(str))*sizeof(tempChar));
        while (!isspace(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 normal variables and arrays
                {
                        while (str[i] != ',' && str[i] != ';') //runs until ', ' or '; '
                        {
                                if (isspace(str[i]))
                                {
                                        while (isspace(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 so I can valuate the bytes
                                }
                                printf("%c", str[i]);
                                i++;

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


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


}

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;
        if (strcmp(str, "char")==0)
                return 1;
        if (strcmp(str, "double")==0)
                return 8;
        if (strcmp(str, "short")==0)
                return 2;
}


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;
}

最佳答案

正如 Sourav Ghosh 提到的,这绝对是代码审查的帖子。 但既然你已经浪费了时间在这里发帖,我建议你阅读以下内容:

Please explain the output of sizeof()

我不太清楚为什么你定义了自己的函数,而你可以使用 sizeof 运算符来完成你的函数正在做的事情。

编辑:

Another good example

Edint 2.0:您得到的警告是编译器基本上说:“如果您的情况都不匹配,会发生什么?”。更简单地说,它要求在没有一个 if 与 case 匹配的情况下使用 else case。

关于如何将值写入文件,可以使用fprintf();

FILE *f = fopen("file.txt", "w");
 if (f == NULL)
{
  printf("Error opening file!\n");
  exit(1);
}
int i = 1;
fprintf(f,"%d", i);
fclose(f);

More information and examples for fprintf

关于c - 读取简单数据声明并响应分配给该变量的内存量的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44299803/

相关文章:

ruby - 字符串中的字母计数,Ruby

android - 由于 java 不正确,无法构建 android

c++ - 无窗口 OpenGL

javascript - 替换 html 元素的字符串

ruby - 如何在不使用 HERE-DOCUMENT 语法的情况下在 Ruby 中制作多行字符串文字?

ruby - 算法删除元音,意外结果

c - 在 C 中制作自己的 malloc 函数

c - 理解/解码晦涩的汇编代码

c++ - 图书馆如何实现不同操作系统之间的可移植性

java - 如何根据分隔符 "\"分割字符串