为啥总是报运行时错误啊;

为啥总是报运行时错误啊;在我的devc++编译器上就能正常运行

img

img

img

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int MAXN = 15;
int dayOfMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isLeapYear(int year)
{
    return (year%4==0 && year%100!=0)||year%400 == 0;
}
int addOneDay(int &year,int &month,int &day)
{
    if(isLeapYear(year))
    {
        dayOfMonth[2]=29;
    }
    day++;
    if(day>dayOfMonth[month])
    {
        month++;
        day=1;
    }
    dayOfMonth[2]=28;
}
int main()
{
    char time[MAXN];
    int year,month,day,count=0;
    cin.getline(time,MAXN);
    sscanf(time,"%04d-%02d-%02d",&year,&month,&day);
    int year1=year,month1=1,day1=1;
    while(year)
    {
        if(month1 == month && day1 == day)
            break;
        addOneDay(year1,month1,day1);
        count++;
        //printf("%04d-%02d-%02d\n",year1,month1,day1);
        
    }
    printf("%d",count+1);
}

main函数最后加个 return 0; 试一下

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7512009
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:devc++中的圣诞树
  • 除此之外, 这篇博客: 首次适应算法 动态分区分配方式的模拟 C语言——课程设计实习中的 C语言源程序——建议使用Devc ++ 运行 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    #include<stdio.h>
    #include<stdlib.h>
    struct nodespace{
    	int teskid;   // 作业号 
    	int begin;    // 开始地址 
    	int size;     // 大小 
    	int status;   // 状态 0代表占用,1代表空闲 
    	struct nodespace *next;  // 后指针 
    };
    void initNode(struct nodespace *p){
    	if(p == NULL){	//如果为空则新创建一个 
    		p = (struct nodespace*)malloc(sizeof(struct nodespace));
    	}
    	p->teskid = -1;
    	p->begin = 0;
    	p->size = 640;
    	p->status = 1;
    	p->next =NULL; 
    }  
    void myMalloc1(int teskid,int size,struct nodespace *node){
    	while(node != NULL){
    		if(node->status == 1){  //空闲的空间 
    			if(node->size > size){  //当需求小于剩余空间充足的情况 
    				//分配后剩余的空间 
    				struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));
    				p->begin = node->begin + size;
    				p->size = node->size - size;
    				p->status = 1;
    				p->teskid = -1;
    				//分配的空间 
    				node->teskid = teskid; 
    				node->size = size;
    				node->status = 0;
    				//改变节点的连接 
    				p->next = node->next; 
    				node->next = p;
    				printf("==================================分配内存成功!==================================\n");
    				break; 
    			}else if(node->size == size){ //需求空间和空闲空间大小相等时 
    				node->teskid = teskid; 
    				node->size = size;
    				node->status = 0;
    				printf("==================================分配内存成功!==================================\n");
    				break;
    			}	
    		}
    		if(node->next == NULL){
    			printf("===============================分配失败,没有足够的空间!=============================\n");
    			break;
    		}
    		node = node->next;
    	}
    } 
    void myFree(int teskid,struct nodespace *node){
    	if(node->next == NULL && node->teskid == -1){
    		printf("================================您还没有分配任何作业!================================\n");
    	}
    	
    	while(node != NULL){
    		if(node->status == 1 && node->next->status ==0 && node->next->teskid == teskid){
    			
    			struct nodespace *q = node->next;
    			node->next = node->next->next;
    			free(q);
    			printf("==================================释放内存成功!==================================\n");
    			if(node->next->status == 1){ //下一个空间是空闲空间时 
    				node->size = node->size + node->next->size;
    				struct nodespace *q = node->next;
    				node->next = node->next->next;
    				free(q);
    				printf("==================================释放内存成功!==================================\n");
    			}
    			break;
    		}else if(node->status == 0 && node->teskid == teskid){  //释放空间和空闲空间不连续时  
    			node->status = 1;
    			node->teskid = -1;
    			if(node->next != NULL && node->next->status == 1){ //下一个空间是空闲空间时 
    				node->size = node->size + node->next->size;
    				struct nodespace *q = node->next;
    				node->next = node->next->next;
    				free(q);
    			}
    			printf("==================================释放内存成功!==================================\n");
    			break;
    		}else if(node->next == NULL){  //作业号不匹配时 
    			printf("==================================没有此作业!!==================================\n");
    			break;
    		}
    		node = node->next;
    	}
    	
    	 
    } 
    void printNode(struct nodespace *node){
    	printf("                        内存情况                        \n"); 
    	printf(" -------------------------------------------------------\n");
    	printf("| 起始地址\t结束地址\t大小\t状态\t作业号\t|\n");
    	while(node != NULL){
    		if(node->status==1){
    			printf("| %d\t\t%d\t\t%dKB\tfree\t 无\t|\n", node->begin + 1, node->begin+node->size, node->size);
    		}else{
    			printf("| %d\t\t%d\t\t%dKB\tbusy\t %d\t|\n", node->begin + 1, node->begin+node->size, node->size, node->teskid);
    		}
    		node = node->next;
    	}
    	printf(" -------------------------------------------------------\n");
    }
    void destory(struct nodespace *node){
    	struct nodespace *q = node;
    	while(node != NULL){
    		node = node->next;
    		free(q);
    		q = node;
    	}
    } 
    void menu(){
    	printf("\n"); 
    	printf("\t\t\t\t   ╭═════════════════════════════════○●○●═══╮\n");
    		printf("\t\t\t\t   │    首次适应算法的动态分区分配方式模拟      │\n");
    		printf("\t\t\t\t   ╰═══○●○●═════════════════════════════════╯\n");
    		printf("\t\t\t\t   ┌───────────────────────────────────────────-┐\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 1. 申请内存                │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 2. 回收内存                │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 3. 查看内存情况            │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 4. 退出                    │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   └────────────────────────────────────────────┘\n");
    		printf("\t\t\t\t\t\t  请您选择(1-4):\t");
    }
     
    int main(){
    	// node为整个空间 
    	system("color 0f");
    	//system("mode con cols=120 lines=50");
    	struct nodespace *init = (struct nodespace*)malloc(sizeof(struct nodespace));
    	struct nodespace *node = NULL;
    	initNode(init);			//初始化主链 
    	node = init; 			//指向链表头 
    	int option; 
    	int teskid;
    	int size;
    	while(1){
    		menu();		//打印想要进行的操作
    		scanf("%d",&option);
    		if(option == 1){
    			printf("请输入作业号;");
    			scanf("%d",&teskid);
    			printf("此作业申请的空间大小(KB):");
    			scanf("%d",&size);
    			myMalloc1(teskid,size,node);
    			printf("\n"); 
    			printNode(node);
    		}else if(option == 2){
    			printf("请输入作业号:");
    			scanf("%d",&teskid);
    			myFree(teskid,node);
    			printf("\n"); 
    			printNode(node);
    		}else if(option == 3){
    			printNode(node);
    		}else if(option == 4){
    			destory(node);
    			initNode(init);
    			node = init;
    			break;
    		}else{
    			printf("===========================您的输入有误,请重新输入!============================\n");
    			continue;
    		}
    	}
    return 0;
    }
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^