作业二(1)四则运算
主要功能:整数的四则运算,可以随机生成30道整数100以内加减乘除试题
设计思想:主要是void demo(void) 定义几个数 ,a,b,k ,定义a,b选数都是100以内的随机数,k为四则加,减,乘,除,等于,四种算法,+ — * % 30道题进行循环。
1 #include "stdafx.h"
2 #include <iostream>
3 #include <time.h>
4 using namespace std;
5 void demo(void) //随机产生四则运算
6 {
7 int a,b,k; //随机数m,n,计数
8
9 a=rand()%100;//生成随机数
10 b=rand()%100;
11 k=rand()%5;
12 switch(k) //四种运算随机选择
13 {
14 case 1:cout<<a<<"+"<<b<<"="<<endl;break;
15 case 2:cout<<a<<"—"<<b<<"="<<endl;break;
16 case 3:cout<<a<<"×"<<b<<"="<<endl;break;
17 case 4:cout<<a<<"÷"<<b<<"="<<endl;break;
18 }
19 }
20 int main() //主函数用于循环次数
21 {
22 int i=1; //循环次数
23 srand((unsigned)time(NULL));//为rand()函数生成不同的随机种子
24 cout<<"30道一百以内加减乘除四则运算题:"<<endl;
25 while(i<=38)
26 {
27 demo(); //调用函数
28 i++;
29 }
30 return 0;
31 }
分数的四则运算,由自己输入两个分数,进行分数的加减乘除。
网络参考。
1 #include<iostream> 2 using namespace std; 3 4 class Point 5 { 6 private: 7 int c_point; 8 int m_point; 9 char code; 10 public: 11 Point() 12 { 13 code='/'; 14 } 15 Point(int n1,int n2) 16 { 17 c_point=n1; 18 m_point=n2; 19 code='/'; 20 } 21 void Output() 22 { 23 if(m_point==0)cout<<"error"; 24 else 25 if(m_point==1)cout<<c_point; 26 else 27 cout<<"("<<c_point<<code<<m_point<<")"; 28 } 29 int Big(int n1,int n2) //最大公约数 30 { 31 int s=1; 32 int n=n1,m=n2; 33 while(s!=0) 34 { 35 s=n1%n2; 36 n1=n2; 37 n2=s; 38 } 39 if(n1!=1&&n%n1==0&&m%n1==0) 40 return n1; 41 else 42 return 0; 43 } 44 //int Small_Multiple() 45 Point operator +(Point b) 46 { 47 Point c; 48 int n1,n2,s=1; 49 n1=m_point; 50 n2=b.m_point; 51 if(n1==n2) 52 { 53 c.c_point=c_point+b.c_point; 54 c.m_point=m_point=b.m_point; 55 } 56 else 57 { 58 while(s!=0) 59 { 60 s=n1%n2; 61 n1=n2; 62 n2=s; 63 } 64 s=m_point*b.m_point/n1; //最小公倍数 65 c.m_point=s; 66 c.c_point=c_point*(s/m_point)+b.c_point*(s/b.m_point); 67 } 68 int t; 69 t=Big(c.c_point,c.m_point); 70 while(t!=0) 71 { 72 c.c_point=c.c_point/t; 73 c.m_point=c.m_point/t; 74 t=Big(c.c_point,c.m_point); 75 } 76 return c; 77 } 78 Point operator -(Point b) 79 { 80 Point c; 81 int n1,n2,s=1; 82 n1=m_point; 83 n2=b.m_point; 84 if(n1==n2) 85 { 86 c.c_point=c_point-b.c_point; 87 c.m_point=m_point=b.m_point; 88 } 89 else 90 { 91 while(s!=0) 92 { 93 s=n1%n2; 94 n1=n2; //最大公约数 95 n2=s; 96 } 97 s=m_point*b.m_point/n1; //最小公倍数 98 c.m_point=s; 99 c.c_point=c_point*(s/m_point)-b.c_point*(s/b.m_point); 100 } 101 int t; 102 t=Big(c.c_point,c.m_point); 103 while(t!=0) 104 { 105 c.c_point=c.c_point/t; 106 c.m_point=c.m_point/t; 107 t=Big(c.c_point,c.m_point); 108 } 109 return c; 110 } 111 Point operator *(Point b) 112 { 113 Point c; 114 c.c_point=c_point*b.c_point; 115 c.m_point=m_point*b.m_point; 116 int t; 117 t=Big(c.c_point,c.m_point); 118 while(t!=0) 119 { 120 c.c_point=c.c_point/t; 121 c.m_point=c.m_point/t; 122 t=Big(c.c_point,c.m_point); 123 } 124 return c; 125 } 126 Point operator /(Point b) 127 { 128 Point c; 129 c.c_point=c_point*b.m_point; 130 c.m_point=m_point*b.c_point; 131 int t; 132 t=Big(c.c_point,c.m_point); 133 while(t!=0) 134 { 135 c.c_point=c.c_point/t; 136 c.m_point=c.m_point/t; 137 t=Big(c.c_point,c.m_point); 138 } 139 return c; 140 } 141 }; 142 int main() 143 { 144 int n1,n2,m1,m2; 145 //char a1,b1; 146 cout<<"请输入两个数的分子,分母分别为:"; 147 //cin>>n1>>a1>>n2>>m1>>b1>>m2; 148 cin>>n1>>n2>>m1>>m2; 149 Point a(n1,n2),b(m1,m2); 150 cout<<"这两个分数为:"; 151 a.Output();cout<<" "; 152 b.Output();cout<<endl; 153 //本来想在这定义一个选择变量,选择是要进行那个运算符运算 154 a.Output(); 155 cout<<"+"; 156 b.Output(); 157 cout<<"="; 158 Point c; 159 c=a+b; 160 c.Output(); 161 cout<<endl; 162 163 a.Output(); 164 cout<<"-"; 165 b.Output(); 166 cout<<"="; 167 c=a-b; 168 c.Output(); 169 cout<<endl; 170 171 a.Output(); 172 cout<<"*"; 173 b.Output(); 174 cout<<"="; 175 c=a*b; 176 c.Output(); 177 cout<<endl; 178 179 a.Output(); 180 cout<<"/"; 181 b.Output(); 182 cout<<"="; 183 c=a/b; 184 c.Output(); 185 cout<<endl; 186 return 0; 187 }
以为这次用的是C++,基础不太好,整数很简单,分数不太会,所以参考了一些大神们的分数理念,望以后努力。