【发布时间】:2018-11-24 15:30:38
【问题描述】:
我的目标是打印
program starts...
set value of num to 0
set value of res to 0
<type in> Bye!!
Input(msg): Bye!!
通过实现file
SNOL
INTO num IS 0
INTO res IS 0
BEG msg
INTO num IS 5
INTO res IS MULT num num
这是我的代码。 问题: 我想知道我的 code 是如何工作了一段时间,然后当我添加一些声明语句时停止工作。现在我删除了这些语句并尝试在 code 实际工作时更改它但是现在它只在扫描输入之前有效,然后程序停止。
其他问题:
当我继续完成我的代码时,当我尝试评估文件中的最后一条语句时出现错误,因为我不知道如何比较数字是浮点数还是整数。之后,结果应该是res = num*num@。
评估 MULT:结果为 25
bool objectFloat(const char* object){
//check if 1st character is a digit, if not then return false, otherwise return true.
if (!isdigit(object[0]))
return false;
// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwise return true
int periods = 0; //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i<strlen(object); i++){
if(object[i]=='.'){
periods++;
}
//return false if character is not a digit
else if(!isdigit(object[i])){
return false;
}
}
// return true if there is only one period.
return periods == 1;
}
//function to check if character is a keyobject.
bool isKeyobject(const char* object){
char keyobjects[11][11]= {"SNOL", "LONS", "INTO", "IS", "MULT", "BEG",
"PRINT", "ADD", "SUB", "DIV", "MOD"};
int i;
for(i=0; i<11; i++){
// Check if object is equal to keyobjects at index i
// If yes, return true
if (isLowerCase(object)){
return false;}
if(strcmp(object, keyobjects[i]) == 0){
return true;
}
}
//object is not equal to any of the keyobjects so return false
return false;
}
//Function to check if every character is an integer
// If not, return false otherwise return true
bool objectInt(const char* object){
int i;
for (i = 0; i < strlen(object); i++){
if (!isdigit(object[i])) return false;
}
return true;
}
bool objectIsVariable(const char* object){
// Check if alphanumeric character & lower case
// If not, return false
int i;
for (i = 0; i < strlen(object); i++){
if (!isalnum(object[i])&&!isLowerCase(object)) return false;
}
return true;
}
int setNum(const char* object){
if(objectInt(object)){
int number, num;
number=atoi(object);
num=number;
return num;
}
else if(objectFloat(object)){
float number, num;
number=atof(object);
num=number;
return num;
}
}
int setRes(const char* object){
if(objectInt(object)){
int number, res;
number=atoi(object);
res=number;
return res;
}
else if(objectFloat(object)){
float number, res;
number=atof(object);
res=number;
}
}
int main(){
FILE *s_path = fopen("test.snol", "r");
char object[100];
char msg;
const char* IsitIS = "IS";
const char* IsitMULT = "MULT";
const char* IsitMOD = "MOD";
const char* IsitSNOL= "SNOL";
const char* IsitLONS= "LONS";
const char* IsitINTO = "INTO";
const char* IsitADD= "ADD";
const char* IsitSUB= "SUB";
const char* IsitDIV = "DIV";
const char* IsitBEG = "BEG";
const char* IsitNum = "num";
const char* IsitRes = "res";
int number;
int flag = 0;
// Get each object from s_path until end-of-file is reached
while(fscanf(s_path, "%s", &object) != -1) {
//if it doesn't start with SNOL then end
if (isKeyobject(object)&&strcmp(object, IsitSNOL)==0){
printf ("Program starts...\n\n");
}
else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){
printf ("...Set value of ");
}
if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
setNum(object);
printf("num to %d\n", number);
}
else
{
setRes(object);
printf("res to %d\n", number);
}
}
else if(strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(strcmp(object, IsitRes) == 0) {
flag = 2;
}
else if(isKeyobject(object)&&strcmp(object, IsitBEG)==0){
printf("input msg\n");
scanf("%s",msg);
fscanf(s_path," %s", &object);
printf ("INPUT(%s): %s\n",object,msg);
}
}
fclose(s_path);
}
【问题讨论】:
-
这是实际代码吗?
objectFloat()函数中的注释似乎有错字。return true.似乎独树一帜。 -
当您的程序在看似无关的更改后突然停止工作时,通常是因为您的代码被损坏,并调用了未定义的行为。注意你的编译器警告。
-
您的代码无法阅读;您需要正确格式化它。此外,您应该生成一个minimal reproducible example。
-
int setNum()并不总是返回值。 (编译器警告非常有用) -
fscanf(s_path, "%s", &object)缺少宽度限制