#include <fortran.h>
real function f() code
return 0.0;
endfunction
program(main)
if 3 == 3 then
print(*) "Your PC is fine.";
elseif 3 == 4 then
print(*) "Your PC is broken.";
else
print(*) "Your PC is trash.";
endif
write(*, "(I)") 5);
end
第10行出现错误expected unqualified-id before 'else'
,我不知道为什么。
在第16行,程序应该在屏幕上写“ 5”,但出现运行时错误。
此外,C ++是区分大小写的语言,有没有办法使C ++区分大小写?
FORTRAN.H是:
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#define IF if(
#define THEN ){
#define ELSEIF } else if(
#define ELSE }else{
#define ENDIF }
#define if IF
#define then THEN
#define elseif ELSEIF
#define else ELSE
#define endif ENDIF
#define PROGRAM(x) main(){
#define program PROGRAM
#define END }
#define end END
#define STOP exit(0);
#define stop STOP
#define call
#define subroutine
#define recursive
#define function
#define FOR for(
#define for FOR
#define LOOP ){
#define loop LOOP
#define ENDFOR }
#define endfor ENDFOR
#define WHILE while(
#define DO ){
#define ENDDO }
#define enddo ENDDO
#define PRINT(t) std::cout<<
#define _ <<
#define READ std::cin>>
#define $ >>
#define Read READ
#define read READ
#define Print PRINT
#define print PRINT
#define selectcase(x) switch(x){
#define endselectcase }
#define endfunction }
#define code {
typedef char character;
typedef int integer;
typedef double doubleprecision;
typedef float real;
char* function __FORTRAN_WRITE_ARGS(const char* f) code
integer i;
std::__cxx11::string arg = "";
if f[0] != '(' || f[strlen(f)-1] != ')' then
return "";
endif
for i = 0; i < strlen(f); i++ loop
selectcase(f[i])
case ' ':
continue;
break;
case '(':
continue;
break;
case ')':
continue;
break;
case ',':
continue;
break;
case 'I':
if f[i+1] >= 48 && f[i+1] <= 57 then
arg += "%";
arg += f[i+1];
arg += "d";
else
arg += "%d";
endif
break;
default:
continue;
endselectcase
endfor
char retval[arg.length()] = "";
strcat(retval, arg.c_str());
return retval;
endfunction
#define write(t, f) printf(__FORTRAN_WRITE_ARGS(f),
#define endwrite )
#define WRITE write
#define precision
有什么办法可以解决此问题或错误?
答案 0 :(得分:0)
虽然我喜欢该项目的想法,但目前实施存在缺陷,我不确定您是否能够获得100%的转换。 ReactJs
使用一些不推荐使用的旧做法,并包含两个实际的编译错误。
主要问题(为什么要问这个问题)很清楚地看到了何时通过预处理程序运行代码。
fortran.h
int main(){
if( 3 == 3 ){
std::cout<< "Your PC is fine.";
} }else{ if(( 3 == 4 ){ // *** The issue you're having ***
std::cout<< "Your PC is broken.";
}else{
std::cout<< "Your PC is trash.";
}
printf(__FORTRAN_WRITE_ARGS("(I)"), 5);
}
(我消除了大多数小写字母的变体)被替换,但不是您想要的方式。 ELSEIF
进行了替换,但是您完成了ELSEIF
和#define else ELSE
。这显然与真正的C ++冲突,并且您正在进行双重替换,从而创建了损坏的代码。
下面是我修改的fortran.h和main.cpp。值得在我的fortran.h上运行差异以查看我所做的更改。现在可以编译,但是仍然坏了。在说我的电脑很好之后,它会打印垃圾。我戳了一下您的WRITE函数,但是为了保留您的语法(这可能是不正确的,我不知道Fortran,但是'()'不匹配),我无法提出任何合理的信息。它可能需要嵌套的宏调用。一种生成字符串,另一种实际上将字符串替换为#define if IF
语句。为了弄清楚这一点,仅预处理器标志printf
将成为您的朋友,以查看您尝试的替换是否有效。
fortran.h
-E
主要
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#define IF if(
#define THEN ){
#define ELSEIF } else if(
#define ELSE }else{
#define ENDIF }
#define PROGRAM(x) int main(){
#define program PROGRAM
#define END }
#define end END
#define STOP exit(0);
#define stop STOP
#define call
#define subroutine
#define recursive
#define function
#define FOR for(
#define LOOP ){
#define ENDFOR }
#define WHILE while(
#define DO ){
#define ENDDO }
#define PRINT(t) std::cout<<
#define _ <<
#define READ std::cin>>
#define $ >>
#define selectcase(x) switch(x){
#define endselectcase }
#define endfunction }
#define CODE {
typedef char character;
typedef int integer;
typedef double doubleprecision;
typedef float real;
char* function __FORTRAN_WRITE_ARGS(const char* f) CODE
integer i;
std::string arg = ""; // Removed __cx11, not needed
IF f[0] != '(' || f[strlen(f)-1] != ')' THEN
return "";
ENDIF
FOR i = 0; i < strlen(f); i++ LOOP
selectcase(f[i])
case ' ':
case '(':
case ')':
case ',':
continue;
break;
case 'I':
IF f[i+1] >= 48 && f[i+1] <= 57 THEN
arg += "%";
arg += f[i+1];
arg += "d";
ELSE
arg += "%d";
ENDIF
break;
default:
continue;
endselectcase
ENDFOR
char retval[arg.length() + 1]; // Removed pointless initialization, fixed size
for (auto& I : retail) i = '\0'; // null character initialization
strncpy(retval, arg.c_str(), arg.length());
return retval;
endfunction
#define write(t, f) printf(__FORTRAN_WRITE_ARGS(f),
#define endwrite )
#define WRITE write
#define precision
如果最终目标是使用fortran语法合法地编写C ++代码而不是新颖性,我会说要迅速放弃该想法,而只是学习C ++。