gpt4 book ai didi

c++ - C/C++ - 在 Visual Studio C++ 中使用中断

转载 作者:行者123 更新时间:2023-11-30 20:34:55 25 4
gpt4 key购买 nike

我是一名计算机科学专业的学生,​​最近学习了如何在 C 中使用中断。经过几次网络搜索后,我想出了以下代码:

#include <stdio.h>
#include <dos.h>
#include <conio.h>

#ifdef __cplusplus

#define __CPPARGS ...

#else

#define __CPPARGS

#endif

#define INTR 0x1c
#define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))
//#define clear() printf("\033[H\033[J");
/*
//positioning
void gotoxy(int x, int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
*/

void interrupt handler(__CPPARGS);
void interrupt ( *oldhandler)(__CPPARGS);

int countS = 0;
int s = 0;
int m = 0;
int ms = 0;
int l = 0;
int flag = 0;

int main(void)
{
clrscr();
printf("%02d:%02d:%02d",m,s,ms);
oldhandler = getvect(INTR);

setvect(INTR, handler);
char c;

while(1)
{
c = getch();

switch(c){

case 'e':
goto exit_loop;
break;

case ' ':
flag = 1-flag;
break;

case 'r':
flag = s = m = ms = l = 0;
clrscr();
printf("%02d:%02d:%02d",m,s,ms);
break;

case 'l':
gotoxy(++l,0);
printf("%02d:%02d:%02d",m,s,ms);
break;
}
}

exit_loop:;
setvect(INTR, oldhandler);
return 0;
}

void interrupt handler(__CPPARGS)
{

if(flag == 1){

countS++;
ms += 55;

if(countS == 18)
{
countS = ms = 0;
s++;

if(s==60)
{
m++;
s = 0;
}
}

gotoxy(0,0);
printf("%02d:%02d:%02d",m,s,ms);
}
}

这段代码是 C 控制台应用程序中的某种秒表,它在历史上的 Turbo C++ 中工作完美。我更改了 IDE,现在使用 Visual studio 2013,当我在 Visual C++->win32ConsoleApplication 中创建一个新项目并将此代码粘贴到主文件中时。它不起作用。然后出现错误,提示我应该在文件的第一个文件中添加 #include "stdafx.h" 。这样做之后,这是我的主要错误列表:

Error   1   error C2146: syntax error : missing ';' before identifier 'handler' c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 32  1   ConsoleApplication1
Error 2 error C2182: 'interrupt' : illegal use of type 'void' c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 32 1 ConsoleApplication1
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 32 1 ConsoleApplication1
Error 4 error C2065: 'oldhandler' : undeclared identifier c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 33 1 ConsoleApplication1
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 33 1 ConsoleApplication1
Error 6 error C2086: 'int interrupt' : redefinition c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 33 1 ConsoleApplication1
Error 7 error C2143: syntax error : missing ';' before '(' c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 33 1 ConsoleApplication1
Error 8 error C2059: syntax error : ')' c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 33 1 ConsoleApplication1
Error 9 error C2059: syntax error : ';' c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 33 1 ConsoleApplication1
Error 10 error C3861: 'clrscr': identifier not found c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 44 1 ConsoleApplication1
Error 11 error C2065: 'oldhandler' : undeclared identifier c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 46 1 ConsoleApplication1
Error 12 error C3861: 'getvect': identifier not found c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 46 1 ConsoleApplication1
Error 13 error C3861: 'setvect': identifier not found c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 48 1 ConsoleApplication1
Error 14 error C3861: 'clrscr': identifier not found c:\users\mohammad\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 67 1 ConsoleApplication1

这些错误与这些行相关:

void interrupt handler(__CPPARGS);
void interrupt ( *oldhandler)(__CPPARGS);

并使用:clrscr();

我的操作系统是Windows 10-64位,这是我第一次在Visual Studio中使用C/C++进行编程。我之前在 Turbo c++ 和 devC++ 中做了一些工作,但只有 Turbo c++ 运行这个示例,甚至没有 devC++。有什么差异,我应该如何解决这个问题?谢谢

最佳答案

您正在 64 位长模式下工作,因此您无法访问实模式 BIOS 中断或 MS-DOS 服务。您的代码还有许多其他问题,但最重要的是,如果没有 16 位编译器和模拟器(例如 64 位 Windows 上不存在的 NTVDM),它就无法工作

关于c++ - C/C++ - 在 Visual Studio C++ 中使用中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40831870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com