姜子怡

计算与软件工程 作业二

作业二

作业要求

https://edu.cnblogs.com/campus/jssf/infor_computation17-31/homework/10402)

遇到的问题

  1. 对于年级的选择不能放在一个程序里,逻辑顺序没有办法很好的运用。
  2. 难中易程度不能很好的分开。
  3. 计算答题时间有问题,只有等程序结束后在后台查看。程序的计算时间与实际情况不符

代码

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include <string.h>
#include<cstdlib>
using namespace std;
int a;
int ri = 0;//计算回答的正确率,定义全局变量 ri 来计算回答正确的题数
//divisor (int a,int b) 函数通过辗转相除法求分子和分母的最大公约数
int divisor (int a,int b)
{
  int  temp;
  if(a<b)
  {
      temp=a;
      a=b;
      b=temp;
    }
   while(b!=0)
   {
      temp=a%b;
      a=b;
      b=temp;
    }
  return a;
}
//CheckResult(char result[]) 函数来判断输入的答案是否正确
int CheckResult(char result[])
{
    char answer[10] ;
    cin>>answer;
    if(strcmp(answer,result)==0)
    {
        cout<<"回答正确"<<endl<<endl;
        ri++;
    }
    else
    {
        cout<<"回答错误,正确答案为"<<result<<endl<<endl;
    }
	return 0;
}
//integer() 函数来生成整数的四则运算
void integer()
{
    int x,y,z,temp;
    char result[10];
    srand(time(NULL));
    x = rand() % 10;
    y = rand() % 10+1;
    z = rand()%4;
    switch(z)
    {
        case 0 :
            cout<<x<<" + "<<y<<"=";
            temp = x + y;
            sprintf(result, "%d", temp);
            break;
        case 1 :
            if(x<y)
            {
                temp=x;
                x=y;
                y=temp;
            }
            cout<<x<<" - "<<y<<"=";
            temp = x - y;
            sprintf(result, "%d", temp);
            break;
        case 2 :
            cout<<x<<" * "<<y<<"=";
            temp = x * y;
            sprintf(result, "%d", temp);
            break;
        case 3 :
            cout<<x<<" ÷"<<y<<"=";
            if(x%y==0)
            {
                temp = x / y;
                sprintf(result, "%d", temp);
            }
            else
            {
                temp = divisor(x,y);
                x = x/temp;
                y = y/temp;
                sprintf(result, "%d/%d", x, y);
            }
            break;
    }
    CheckResult(result) ;
}
// fraction() 函数来生成分数的四则运算
void fraction()
{
    int a,b,c,d,x,y,z,temp;
    char result[10];
    srand(time(NULL));
    a = rand() % 10;
    b = rand() % 10+1;
    c = rand() % 10;
    d = rand() % 10+1;
    z = rand() % 4;
    if(a>b)
    {
        temp=a;
        a=b;
        b=temp;
    }
    if(c>d)
    {
        temp=c;
        c=d;
        d=temp;
    }
    switch(z)
    {
        case 0 :
            cout<<a<<"/"<<b<<" + "<<c<<"/"<<d<<"=";
            x = a*d+b*c;
            y = b*d;
            temp = divisor(x,y);
            x = x/temp;
            y = y/temp;
            if(y==1)
            {
                sprintf(result, "%d", x);
            }
            else
            {
                sprintf(result, "%d/%d", x, y);
            }
            break;
        case 1 :
            if(a*d<b*c)
            {
                temp = a;
                a = c;
                c = temp;
                temp = b;
                b = d;
                d = temp;
            }
            cout<<a<<"/"<<b<<" - "<<c<<"/"<<d<<"=";
            x = a*d-b*c;
            y = b*d;
            temp = divisor(x,y);
            x = x/temp;
            y = y/temp;
            sprintf(result, "%d/%d", x, y);
            break;
        case 2 :
            cout<<a<<"/"<<b<<" * "<<c<<"/"<<d<<"=";
            if(a==0||c==0)
            {
                temp = 0;
                sprintf(result, "%d", temp);
            }
            else
            {
                x = a*c;
                y = b*d;
                temp = divisor(x,y);
                x = x/temp;
                y = y/temp;
                sprintf(result, "%d/%d", x, y);
            }
            break;
        case 3 :
            cout<<a<<"/"<<b<<" ÷"<<c<<"/"<<d<<"=";
            if((a*d)%(b*c)==0)
            {
                x = a*d;
                y = b*c;
                temp = x / y;
                sprintf(result, "%d", temp);
            }
            else
            {
                x = a*d;
                y = b*c;
                temp = divisor(x,y);
                x = x/temp;
                y = y/temp;
                if(y==1)
                {
                    sprintf(result, "%d", x);
                }
                else
                {
                    sprintf(result, "%d/%d", x, y);
                }
            }
            break;
    }
    CheckResult(result);
}
int main()
{
    int n,q;
    cout<<"输入要生成的题数:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int a = rand()%2;
        switch(a)
        {
            case 0: integer();break;
            case 1: fraction();break;
        }
    }
    q=(ri*100)/n;
    cout<<"回答对了"<<ri<<"题"<<endl;
    cout<<"正确率为:"<<q<<"%"<<endl<<endl;
	 clock_t start,finish;
    start=clock();
     
    cout << "花费的时间 " << endl;
 
    finish=clock();
 
    cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;
	system ("pause");
}

此程序没有混合运算,适用于低年级

//判断运算符的优先级函数 
int Precede(char a, char b) 
{ 
   switch (a) 
   { 
     case '*': 
     case '/': 
        if ((b == '+') || (b == '-')) 
        { 
           return 1; //a的优先级高于b 
        } 
        else 
        { 
           return 0; //a的优先级等于b 
        } 
     
	 case '+': 
     case '-': 
	    if ((b == '*') || (b == '/')) 
	    { 
	       return -1; //a的优先级低于b 
        }
	    else 
		{ 
		   return 0; 
		} 
   
	default: 
	     cout<<"运算符错误!"<<endl; 
	     break; 
	} 
} 
 
 
int Operate(int j, int k, char operater) 
{ 
	switch (operater) 
	{ 
		case '+': 
			return k + j; 
		break;  
		case '-': 
			return k - j; 
		break; 
		case '*': 
			return k * j; 
		break;  
		case '/':  
			return k / j; 
		break;  
		default: printf("运算错误!\n"); 
		         break; 
	} 
} 
 
int calculate(int len, char *expStr) 
{ 
	//表达式异常处理,注意len <= 0要加括号 
	if ((len <= 0 ) || (expStr == NULL)) 
	{ 
		cout<<"表达式为空!"<<endl; 
	} 
	int i = 0, j = 0, k = 0; 
	std::stack<int> date; 
	std::stack<char> operate; 
	while (i < len) 
	{ 
		if ((expStr[i] >= '0') && (expStr[i] <= '9')) 
		{ 
			date.push(expStr[i] - '0'); 
			i++; 
		} 
		else if ((expStr[i] == '+') || (expStr[i] == '-') || expStr[i] == '*' || expStr[i] == '/') 
		{ 
			if (operate.empty()) 
			{ 
				operate.push(expStr[i]);
				i++; 
			} 
			else 
			{ 
				//与栈顶运算符判断优先级 
				switch (Precede(expStr[i], operate.top())) 
				{ 
				  case 0: 
				  case -1: //栈顶运算符优先级高 
					   j = date.top(); 
					   date.pop(); 
					   k = date.top(); 
					   date.pop(); 
					   date.push(Operate(j, k , operate.top()));
					   operate.pop(); 
					   //i++;  
					   break; 
 
				  case 1: //栈运算符顶优先级低 
					   operate.push(expStr[i]); 
					   i++; 
					   break; 
				  default: 
					 cout<<"优先级判断错误!"<<endl;
					   break; 
				 } 
			} 
		} 
		else 
		{ 
			cout<<"表达式无法识别!";
			break; 
		}
	} 
	
	while (!operate.empty()) 
	{ 
		j = date.top(); 
		date.pop(); 
		k = date.top(); 
		date.pop(); 
		date.push(Operate(j, k , operate.top())); 
		operate.pop(); 
	} 
	
	return date.top(); 
} 
 
int main() 
{ 
   int len = 0; int i= 0, input_flag = 0; 
    
   while (!input_flag) 
   { 
	  cout<<"请输入表达式的长度:"; 
	  input_flag = scanf("%d", &len); 
	  if (!input_flag) 
	  { 
		printf("输入有误,仅可输入数字!\n"); 
	  } 
	  flushall(); 
    } 
	char *expStr = (char *)malloc(len * sizeof(char)); 
	input_flag = 0;
	
	while (!input_flag) 
	{ 
		printf("请输入表达式:"); 
		for (i = 0; i < len; i++) 
		{ 
			input_flag = scanf("%c", &expStr[i]); 
			//scanf("%c", &expStr[i]);  
			/*
			if ((i < len) && (expStr[i] == '\n')) 
			{
				printf("长度不够,请重新输入:"); 
				i = 0; 
			}
			*/ 
			if (!input_flag) 
			{ 
				cout<<"表达式输入有误!\n"; 
				flushall(); 
				break; 
			} 
		 } 
	 } 
	
	 cout<<"表达式的计算结果为:%d\n"<< calculate(len, expStr); 
	system("pause"); 
  return 0; 
}

此程序段有混合运算,适用于高年级。但不能自动出题。

码云地址链接

https://gitee.com/jiang_zi_yi/codes/trmbh9450nfa8cloysepw65

posted on 2020-02-27 17:39  ☆♡^O^  阅读(239)  评论(1编辑  收藏  举报

导航