您的位置:首页 > 其它

作业二(1)四则运算

2016-03-15 20:39 197 查看
主要功能:整数的四则运算,可以随机生成30道整数100以内加减乘除试题

设计思想:主要是void demo(void) 定义几个数 ,a,b,k ,定义a,b选数都是100以内的随机数,k为四则加,减,乘,除,等于,四种算法,+ — * % 30道题进行循环。

#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
void demo(void)  //随机产生四则运算
{
int a,b,k;   //随机数m,n,计数

a=rand()%100;//生成随机数
b=rand()%100;
k=rand()%5;
switch(k)    //四种运算随机选择
{
case 1:cout<<a<<"+"<<b<<"="<<endl;break;
case 2:cout<<a<<"—"<<b<<"="<<endl;break;
case 3:cout<<a<<"×"<<b<<"="<<endl;break;
case 4:cout<<a<<"÷"<<b<<"="<<endl;break;
}
}
int main()    //主函数用于循环次数
{
int i=1;    //循环次数
srand((unsigned)time(NULL));//为rand()函数生成不同的随机种子
cout<<"30道一百以内加减乘除四则运算题:"<<endl;
while(i<=38)
{
demo();  //调用函数
i++;
}
return 0;
}




分数的四则运算,由自己输入两个分数,进行分数的加减乘除。

网络参考。

#include<iostream>
using namespace std;

class Point
{
private:
int c_point;
int m_point;
char code;
public:
Point()
{
code='/';
}
Point(int n1,int n2)
{
c_point=n1;
m_point=n2;
code='/';
}
void Output()
{
if(m_point==0)cout<<"error";
else
if(m_point==1)cout<<c_point;
else
cout<<"("<<c_point<<code<<m_point<<")";
}
int Big(int n1,int n2)          //最大公约数
{
int s=1;
int n=n1,m=n2;
while(s!=0)
{
s=n1%n2;
n1=n2;
n2=s;
}
if(n1!=1&&n%n1==0&&m%n1==0)
return n1;
else
return 0;
}
//int Small_Multiple()
Point operator +(Point b)
{
Point c;
int n1,n2,s=1;
n1=m_point;
n2=b.m_point;
if(n1==n2)
{
c.c_point=c_point+b.c_point;
c.m_point=m_point=b.m_point;
}
else
{
while(s!=0)
{
s=n1%n2;
n1=n2;
n2=s;
}
s=m_point*b.m_point/n1;         //最小公倍数
c.m_point=s;
c.c_point=c_point*(s/m_point)+b.c_point*(s/b.m_point);
}
int t;
t=Big(c.c_point,c.m_point);
while(t!=0)
{
c.c_point=c.c_point/t;
c.m_point=c.m_point/t;
t=Big(c.c_point,c.m_point);
}
return c;
}
Point operator -(Point b)
{
Point c;
int n1,n2,s=1;
n1=m_point;
n2=b.m_point;
if(n1==n2)
{
c.c_point=c_point-b.c_point;
c.m_point=m_point=b.m_point;
}
else
{
while(s!=0)
{
s=n1%n2;
n1=n2;               //最大公约数
n2=s;
}
s=m_point*b.m_point/n1;         //最小公倍数
c.m_point=s;
c.c_point=c_point*(s/m_point)-b.c_point*(s/b.m_point);
}
int t;
t=Big(c.c_point,c.m_point);
while(t!=0)
{
c.c_point=c.c_point/t;
c.m_point=c.m_point/t;
t=Big(c.c_point,c.m_point);
}
return c;
}
Point operator *(Point b)
{
Point c;
c.c_point=c_point*b.c_point;
c.m_point=m_point*b.m_point;
int t;
t=Big(c.c_point,c.m_point);
while(t!=0)
{
c.c_point=c.c_point/t;
c.m_point=c.m_point/t;
t=Big(c.c_point,c.m_point);
}
return c;
}
Point operator /(Point b)
{
Point c;
c.c_point=c_point*b.m_point;
c.m_point=m_point*b.c_point;
int t;
t=Big(c.c_point,c.m_point);
while(t!=0)
{
c.c_point=c.c_point/t;
c.m_point=c.m_point/t;
t=Big(c.c_point,c.m_point);
}
return c;
}
};
int main()
{
int n1,n2,m1,m2;
//char a1,b1;
cout<<"请输入两个数的分子,分母分别为:";
//cin>>n1>>a1>>n2>>m1>>b1>>m2;
cin>>n1>>n2>>m1>>m2;
Point a(n1,n2),b(m1,m2);
cout<<"这两个分数为:";
a.Output();cout<<"   ";
b.Output();cout<<endl;
//本来想在这定义一个选择变量,选择是要进行那个运算符运算
a.Output();
cout<<"+";
b.Output();
cout<<"=";
Point c;
c=a+b;
c.Output();
cout<<endl;

a.Output();
cout<<"-";
b.Output();
cout<<"=";
c=a-b;
c.Output();
cout<<endl;

a.Output();
cout<<"*";
b.Output();
cout<<"=";
c=a*b;
c.Output();
cout<<endl;

a.Output();
cout<<"/";
b.Output();
cout<<"=";
c=a/b;
c.Output();
cout<<endl;
return 0;
}




以为这次用的是C++,基础不太好,整数很简单,分数不太会,所以参考了一些大神们的分数理念,望以后努力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: