题目要求:
1.程序可以判断用户的输入答案是否正确,如果错误,给出正确答案,如果正确,给出提示。
2.程序可以处理四种运算的混合算式。
3.要求两人合作分析,合作编程,单独撰写博客。
团队成员:张绍佳、杜文星(博客:http://www.cnblogs.com/duwenxing/p/5294508.html)
设计思路:
代码分写于不同的文件中;
① head.h:在头文件head.h中,将其他.cpp文件中所需要的头文件、全局变量的声明、函数的声明都写在此文件中。
② fraction.cpp:此文件中主要定义了一些与分数有关的函数,如分数的生成化简、加减乘除、分数的输出、分数转字符串等。
③ stack.cpp:此文件的主要功能是将所给的运算式的结果计算出来,主要思路是利用栈先将运算式转化为后缀式,然后再利用栈将后缀式的结果求出,重难点是转化后缀式时如何将真分数当做一个整体,我们的解决方法是通过识别 “(” 和 “)” 来识别;同时在计算时即使是整数我们也将其转化为分数处理,故调用了fraction中的一些方法来进行加减乘除。
④yunsuan.cpp:这个文件的主要功能是实现运算式的生成,并判断使用者输入的结果和题目的答案是否相同来判断对错,并输出正确数与错误数。
⑤main.cpp:主函数所在的头文件,主要功能是和用户进行交互。
工作照:
代码:
head.h
1 #pragma once 2 #include<iostream> 3 #include<ctime> 4 #include<strstream> 5 #include<stdlib.h> 6 #include<vector> 7 #include<string> 8 #include<cstdio> 9 #include<cmath> 10 #define random(l,h) (rand()%(h-l+1)+l)//宏定义 11 #define maxsize 1000 12 using namespace std; 13 extern int flag; 14 /*stack.cpp*/ 15 struct Fraction 16 { 17 int up, down; 18 string high, low; 19 }; 20 void trans(string exp, char postexp[]); 21 Fraction compvalue(char postexp[]);//计算后缀表达式的值 22 /*fraction.cpp*/ 23 int gcd(int a, int b);//求a,b的最大公约数 24 int max(int a, int b); 25 int min(int a, int b); 26 Fraction fraction(int up, int down);//生成分数 27 Fraction fra(int d, int u);//生成真分数; 28 Fraction reduction(Fraction result);//分数的化简 29 Fraction add(Fraction f1, Fraction f2);//分数的加法 30 Fraction minus1(Fraction f1, Fraction f2);//分数的减法 31 Fraction multi(Fraction f1, Fraction f2);//分数的乘法 32 Fraction divide(Fraction f1, Fraction f2);//分数的除法 33 void ShowResult(Fraction f);//输出分数 34 string FraToString(Fraction f);//将分数转换为string类型 35 /*yunsuan.cpp*/ 36 int suiji(int down, int up);//随机生成1至n的整数 37 bool is_unique(string str, vector <string> s);//判断生成的运算式是否重复 38 void yunsuan(int time, int low, int high, int fl1, int fl2, int fl3);
fraction.cpp
1 #include"head.h" 2 int gcd(int a, int b)//求a,b的最大公约数 3 { 4 if (b == 0) return a; 5 else return gcd(b, a%b); 6 } 7 8 int max(int a, int b)//返回两个整数中较大的整数 9 { 10 int h = a >= b ? a : b; 11 return h; 12 } 13 int min(int a, int b)//返回两个整数中较小的整数 14 { 15 int l = a <= b ? a : b; 16 return l; 17 } 18 Fraction fraction(int up, int down)//生成分数 19 { 20 Fraction result; 21 result.up = up; 22 result.down = down; 23 strstream ss, kk; 24 ss << result.up; ss >> result.high; 25 kk << result.down; kk >> result.low; 26 return result; 27 } 28 Fraction fra(int d,int u)//生成真分数 29 { 30 Fraction result; 31 int temp1 = suiji(d, u);//调用function函数随机生成两个随机整数 32 int temp2 = suiji(d, u); 33 result.up = min(temp1, temp2); 34 result.down = max(temp1, temp2); 35 strstream s1, s2; 36 s1 << result.up; s1 >> result.high; 37 s2 << result.down; s2 >> result.low; 38 return result; 39 } 40 Fraction reduction(Fraction result)//分数的化简 41 { 42 if (result.down < 0) 43 { 44 result.up = -result.up; 45 result.down = -result.down; 46 } 47 if (result.up == 0) 48 { 49 result.down = 1; 50 } 51 else 52 { 53 int d = gcd(abs(result.up), abs(result.down)); 54 result.up /= d; 55 result.down /= d; 56 } 57 strstream s3, s4; 58 s3 << result.up; s3 >> result.high; 59 s4 << result.down; s4 >> result.low; 60 return result; 61 } 62 Fraction add(Fraction f1, Fraction f2)//分数的加法 63 { 64 Fraction result; 65 result.up = f1.up*f2.down + f1.down * f2.up; 66 result.down = f1.down*f2.down; 67 return reduction(result); 68 } 69 Fraction minus1(Fraction f1, Fraction f2)//分数的减法 70 { 71 Fraction result; 72 result.up = f1.up*f2.down - f1.down*f2.up; 73 result.down = f1.down*f2.down; 74 return reduction(result); 75 } 76 Fraction multi(Fraction f1, Fraction f2)//分数的乘法 77 { 78 Fraction result; 79 result.up = f1.up*f2.up; 80 result.down = f1.down*f2.down; 81 return reduction(result); 82 } 83 Fraction divide(Fraction f1, Fraction f2)//分数的除法 84 { 85 Fraction result; 86 result.up = f1.up*f2.down; 87 result.down = f1.down*f2.up; 88 return reduction(result); 89 } 90 void ShowResult(Fraction f)//输出分数 91 { 92 f = reduction(f); 93 if (f.down == 1) cout << f.up; 94 else cout << f.up << "\" << f.down; 95 } 96 string FraToString(Fraction f)//将分数转换为string类型 97 { 98 string result; 99 if (f.down == 1) result = f.high; 100 else result = f.high + "\" + f.low; 101 return result; 102 }
stack.cpp
1 #include"head.h" 2 struct 3 { 4 char data[maxsize];//存放运算符 5 int top;//栈顶指针 6 }op;//定义运算符栈 7 void trans(string exp, char postexp[])//exp[]为算数表达式,postexp[]为后缀表达式 8 { 9 char ch; 10 int i = 0, j = 0;//i作为exp的下标,j作为postexp的小标 11 op.top = -1; 12 ch = exp[i]; 13 i++; 14 while (ch != '