陈柏衡
(陈柏衡)
1
#include <iostream>
#include <cstdlib>
#include <string>
const int N = 100;
using namespace std;
// system check
#if defined(_WIN32)
#define _SYSTEM_ 1 // Windows
#include<windows.h>
#elif defined(__ANDROID__)
#define _SYSTEM_ 5 // Android
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#define _SYSTEM_ 6 // iOS
#else
#define _SYSTEM_ 4 // macOS
#include <termios.h>
#include <unistd.h>
#endif
#elif defined(__linux__)
#define _SYSTEM_ 2 // Linux
#include <termios.h>
#include <unistd.h>
#elif defined(__unix__)
#define _SYSTEM_ 3 // Other Unix (except Apple,Linux&Android)
#include <termios.h>
#include <unistd.h>
#endif
/*
System list
type code
Windows 1
Linux 2
Other Unix 3
MacOS 4
Android 5
IOS 6
Get code from "_SYSTEM_"
*/
void pause_anykey()
{
cout << "Press any key to continue..." << endl;
termios oldt;
tcgetattr(STDIN_FILENO, &oldt); // 保存终端设置
termios newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO); // 禁用缓冲和回显
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
getchar(); // 等待任意按键
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // 恢复终端设置
//cout << endl;
}
int check1(int i)
{
cout << "check : " << i << endl;
if(_SYSTEM_ == 1)
{
system("rand.exe > rand.in");
system("ac.exe < rand.in > a.out");
system("std.exe < rand.in > b.out");
}
else if(_SYSTEM_ == 2 or _SYSTEM_ == 3 or _SYSTEM_ == 4)
{
system("./rand > rand.in");
system("./ac < rand.in > a.out");
system("./std < rand.in > b.out");
}
int result = 2;
if(_SYSTEM_ == 1) result = system("fc a.out b.out > nul");
else if(_SYSTEM_ == 2 or _SYSTEM_ == 3 or _SYSTEM_ == 4) result = system("diff a.out b.out > nul");
if(result != 0 and result != 1) return -1; // file error
return result;
}
int check2(string a, string b)
{
string cmd1;
string cmd2;
if(_SYSTEM_ == 1)
{
cmd1 = "std.exe < " + a + " > ans.out";
cmd2 = "fc " + b + " ans.out > nul";
}
else if(_SYSTEM_ == 2 or _SYSTEM_ == 3 or _SYSTEM_ == 4)
{
cmd1 = "./std < " + a + " > ans.out";
cmd2 = "diff " + b + " ans.out > nul";
}
system(cmd1.c_str());
int result = system(cmd2.c_str());
return result;
}
int main(void) {
int flag = 1;
cout << "1.自测样例 or 2.跑样例? " << endl;
int x;
cin >> x;
if (x == 1) {
cout << "输入测试次数: ";
int num;
cin >> num;
for (int i = 1; i <= num; i++) {
if (!check1(i)) continue;
else {
cout << "WA in : " << i << endl;
if(_SYSTEM_ == 1) system("pause");
else if(_SYSTEM_ == 2 or _SYSTEM_ == 3 or _SYSTEM_ == 4) pause_anykey();
flag = 0;
break;
}
}
if (flag == 1) cout << "all test Accepted!!!" << endl;
} else if (x == 2) {
string ain, aout;
cout << "输入文件名(带后缀): ";
cin >> ain;
cout << "输出文件名(带后缀): ";
cin >> aout;
if (!check2(ain, aout)) cout << "Accepted! " << endl;
else cout << "Wrong Answer" << endl;
}
return 0;
}
用了如下话题的方法
https://discourse.xinyoudui.com/t/topic/39973?u=陈柏衡
陈柏衡
(陈柏衡)
2
添加对Android的支持
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#if defined(__ANDROID__)
#include <android/log.h>
#include <android/native_activity.h>
#include <jni.h>
#endif
const int N = 100;
using namespace std;
// 系统检测宏
#if defined(_WIN32)
#define _SYSTEM_ 1
#include<windows.h>
#elif defined(__ANDROID__)
#define _SYSTEM_ 5
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#define _SYSTEM_ 6
#else
#define _SYSTEM_ 4
#endif
#elif defined(__linux__)
#define _SYSTEM_ 2
#elif defined(__unix__)
#define _SYSTEM_ 3
#endif
#if _SYSTEM_ == 5
// Android弹窗实现
void android_alert(const char* message) {
JavaVM* vm = ANativeActivity::javaVM;
JNIEnv* env;
vm->AttachCurrentThread(&env, nullptr);
jclass activityClass = env->FindClass("android/app/NativeActivity");
jmethodID getClassLoader = env->GetMethodID(activityClass,"getClassLoader", "()Ljava/lang/ClassLoader;");
jobject cls = env->CallObjectMethod(ANativeActivity::activity->clazz, getClassLoader);
jclass classLoader = env->FindClass("java/lang/ClassLoader");
jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring str = env->NewStringUTF("android/app/AlertDialog$Builder");
jclass alertClass = (jclass)env->CallObjectMethod(cls, findClass, str);
jmethodID builderCtor = env->GetMethodID(alertClass, "<init>", "(Landroid/content/Context;)V");
jobject builder = env->NewObject(alertClass, builderCtor, ANativeActivity::activity->clazz);
jmethodID setMessage = env->GetMethodID(alertClass, "setMessage", "(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;");
jstring jmsg = env->NewStringUTF(message);
env->CallObjectMethod(builder, setMessage, jmsg);
jmethodID setTitle = env->GetMethodID(alertClass, "setTitle", "(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;");
jstring jtitle = env->NewStringUTF("提示");
env->CallObjectMethod(builder, setTitle, jtitle);
jmethodID setPositiveButton = env->GetMethodID(alertClass, "setPositiveButton", "(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;");
jstring jok = env->NewStringUTF("确定");
env->CallObjectMethod(builder, setPositiveButton, jok, nullptr);
jmethodID show = env->GetMethodID(alertClass, "show", "()Landroid/app/AlertDialog;");
env->CallObjectMethod(builder, show);
vm->DetachCurrentThread();
}
#endif
void pause_anykey(const char* message = nullptr) {
#if _SYSTEM_ == 5
if(message) android_alert(message);
else android_alert("请按确定键继续...");
sleep(2); // 防止快速连续弹窗
#else
cout << (message ? message : "Press any key to continue...") << endl;
#if _SYSTEM_ != 1
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#else
system("pause > nul");
#endif
#endif
}
编译方法
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := terminal_demo
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS := -llog -landroid
include $(BUILD_SHARED_LIBRARY)