| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 588 人关注过本帖
标题:谁帮我看看sml的C语言代码。给我注释啊。求啊
只看楼主 加入收藏
微度C
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-6-11
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
谁帮我看看sml的C语言代码。给我注释啊。求啊
#ifndef _Simpletron_H_
#define _Simpletron_H_
#include <stdio.h>
#include<string.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define Read        10
#define Write       11
#define Loda        20
#define Store       21
#define Add         30
#define Subtract    31
#define Divide      32
#define Multiply    33
#define Branch      40
#define Branchneg   41
#define Branchzero  42
#define Halt        43


int        accumulator;
short    instructionCounter;
int        instructionRegister;
short    operationCode;
short    operand;
int        memory[100];

static void    init();
void    instruction()
{
    printf("\n\n\n\n                        *** Welcome to Simpletron ***");
    printf("\n\n                 It is Mrt Lin's works. Please don't copy!!\n\n\n\n ");
}
void    dump()
{
    char sign = '+';
    int start = 9;
    int i = 0;
    int j = 0;
    int skip = 7;
    printf("\n\n\n\n\n");
 
    printf("accumulator\t\t\t%+.4d\n\n", accumulator);
    printf("instructionCounter\t\t   %.2d\n\n", instructionCounter);
    printf("instructionRegister\t\t%+.4d\n\n", instructionRegister);
    printf("operationCode\t\t\t   %.2d\n\n", operationCode);
    printf("operand\t\t\t\t   %.2d\n", operand);
    printf("\n\n");
    printf("     \t\tMemory:\n\n");
    for (i=0 ;i<10 ;i++ )
    {
        if (i > 0)
            printf("%7d", i);
        else
            printf("%9d", i);
    }
    printf("\n");
    for (i=0 ;i<10 ;i++ )
    {
        printf("%.2d", i*10);
        for (j=0 ;j<10 ;j++ )
        {
            printf(" %+.4d ", memory[i*10 + j]);
        }
        printf("\n");
    }
}
void    halt()
{
    printf("\n\n        *** Simpletron execution terminated ***\n\n");
    dump();
    exit(1);
}
void    error(int errno)/*errno indicates error number*/
{
    switch (errno)
    {
    case 1:
        printf("\n\n[Instruction is negateive.]\n");break;
    case 2:
        printf("\n\n[Attempt to devide by zero.]\n");break;
    case 3:
        printf("\n\n[Result beyond -9999 ~ +9999.]\n");break;
   
    }
    printf("\n\n      *** Simpletron execution abnormally terminated. ***\n");
    dump();
    exit(-1);
}
int        read();
int        write();
int        load();
int        store();
int        add();
int        subtract();
int        divide();
int        multiiply();
int        branch();
int        branchneg();
int        branchzero();
void    start();
void    exec();
void    exechelper();

#ifdef __cplusplus
}
#endif

#endif

#include <stdio.h>
#include <math.h>


int main(int argc, char *argv[])
{
    init();
    instruction();
    start();
    exec();
return 0;
}




static void init()
{
    accumulator = 0;
    instructionCounter = 0;
    instructionRegister = 0;
    operationCode = 0;
    operand = 0;
}
void start()
{
    int resultOfScanf = 0;
    int scanfed = -1;
    while (scanfed != -99999)
    {
        printf("%.2d             <<<<             ", instructionCounter);
        resultOfScanf = scanf("%d", &scanfed);
        if (0 == resultOfScanf || (scanfed!=-99999 && abs(scanfed)>9999))
        {
            printf("wrong instruction or data word.(-9999 ~ +9999)]\n");
            continue;   
        }
        if (scanfed == -99999)
        {
            instructionCounter--;
            break;
        }
        memory[ instructionCounter++ ] = scanfed;
    }
    printf("          \n\n*** Program. Is completeing ***\n\n");
}
void exec()//判断是否超过一百个程序;
{
    int i = 0;
    init();
    printf("*** Program. execution begins ***\n\n");
    while (instructionCounter<100)
    {
        instructionRegister = memory[ instructionCounter++ ];
        if (instructionRegister <= 0)
        {
            instructionCounter--;
            error(1);
        }
        operationCode = instructionRegister/100;
        operand = instructionRegister%100;
        exechelper();
    }
}
void exechelper()
{
    switch (operationCode)
    {
    case Read:
        read();break;
    case Write:
        write();break;
    case Loda:
        load();break;
    case Store:
        store();break;
    case Add:
        add();break;
    case Subtract:
        subtract();break;
    case Divide:
        divide();break;
    case Multiply:
        multiply();break;
    case Branch:
        branch();break;
    case Branchneg:
        branchneg();break;
    case Branchzero:
        branchzero();break;
    case Halt:
        halt();
    }
}
int read()
{
    int resultOfScanf = 0;
    int scanfed = -1;
    while (1)
    {
        printf("       \n\ndo your work!!    \n");
        printf("please input your munbers: ");
        resultOfScanf = scanf("%d", &scanfed);
        if (0 == resultOfScanf || abs(scanfed)>9999)
            printf("[Please input a integer( -9999 ~ +9999 ).]\n");
        memory[ operand ] = scanfed;
        break;
    }
    return scanfed;
}
int write()
{
    printf("%d", memory[ operand ]);
    return 1;
}
int load()
{
    accumulator = memory[ operand ];
    return 1;
}
int store()
{
    memory[ operand ] = accumulator;
    return 1;
}
int add()
{
    accumulator += memory[ operand ];
    return 1;
}
int subtract()
{
    accumulator -= memory[operand ];
    return 1;
}
int divide()
{
    int div = memory[ operand ];
    if (div == 0)
    {
        error(2);
    }
    else
    {
        accumulator /= div;
    }
    return 1;
}
int multiply()
{
    accumulator *= memory[ operand ];
    if (abs(accumulator) > 9999)
        error(3);
    return 1;
}
int branch()
{
    instructionCounter = operand;
    return 1;
}
int branchneg()
{
    if (accumulator < 0)
        instructionCounter = operand;
    return 1;
}
int branchzero()
{
    if (accumulator == 0)
        instructionCounter = operand;
    return 1;
}
或者帮我来个简单的?




要求


这样就实现了Simpletron模拟器指令执行的过程;
1.    由键盘输入范例1的SML程序,执行并在屏幕上显示逐条指令执行过程中的“计算机k,hu信息
2.    将范例程序2(见附录B中)编写在文本文件fl2.txt中,Simpletron运行时从文件中读取程序再执行并最终将执行过程的“计算机转储”信息保存在另一个文本文件中。
3.    程序运行时,有键盘读入文本文件名,再打开指定的文件执行完成以下任务的SML程序:
1)    用标记控制的循环读取10个正数,计算并打印出它们的和;
2)    用计数器控制的循环读取7个数,其中既有正数,也有负数,计算并打印出它们的平均值。
3)    读取一组数,判断并打印出最大的数。读取的第一个数表示要处理的数的个数。
其它要求:
    只能使用C/C++语言,源程序要有适当的注释,使程序容易阅读

搜索更多相关主题的帖子: C语言 include 
2012-06-14 13:27
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:20 
要求好高啊
2012-06-14 14:12
微度C
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-6-11
收藏
得分:0 
回复 2楼 zklhp
老大、要不 就给个符合条件的模拟器吧
2012-06-14 14:30
快速回复:谁帮我看看sml的C语言代码。给我注释啊。求啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026542 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved