用C#程序实现一个简单的数学运算类 定义一个类AimpleMath; 为类编写普通

用C#程序实现一个简单的数学运算类 定义一个类AimpleMath; 为类编写普通,第1张

见代码

using System

namespace ConsolApplication1

{

    // 简单的数学运算类 SimpleMath

    public class SimpleMath

    {

        // 加

        public double Add(double a, double b)

        {

            return a + b

        }

        //

        public double Subtract(double a, double b)

        {

            return a - b

        }

        // 乘

        public double Multiply(double a, double b)

        {

            return a * b

        }

        // 除

        public double Divide(double a, double b)

        {

            return a / b

        }

        // 平方根

        public double Sqrt(double a)

        {

            return Math.Sqrt(a)

        }

        // 幂

        public double Power(double a, int n)

        {

            double r = 1

            for(int i=1 i<=ni++)

            {    

                r *= a

            }

            return r

        }

    }

    

    class Program

    {

        static void Main(string[] args)

        {

            // 实例化简单算术类SimleMath

            SimpleMath sm = new SimpleMath()

            

            double r = sm.Add(100, 200)

            Console.WriteLine(r)

            

            r = sm.Subtract(200 , 100)

            Console.WriteLine(r)

            

            r = sm.Multiply(2.0, 3.0)

            Console.WriteLine(r)

            

            r = sm.Divide(100, 5)

            Console.WriteLine(r)

            

            r = sm.Sqrt(80)

            Console.WriteLine(r)            

            r = sm.Power(10, 3)

            Console.WriteLine(r)

       }

    }

}

参考实例源码如下:

//#include <stdlib.h>

#include <iostream>

using namespace std

int main()

{

int i,j,k,l=0,n

cout<<"小学数学测试"<<endl

cout<<"请选择想测试的题型:1.+ 2.- 3.* 4./:"

cin>>i

if(i==1)

{

cout<<"请输入测试题的个数:"

cin>>j

for(int k1=0k1<jk1++)

{

srand((unsigned)time(NULL))

int x1=rand()%100

int x2=rand()%100

cout<<x1<<"+"<<x2<<"=:"

cin>>k

if(k==x1+x2){

cout<<"回答正确!"<<endl

l++

}

else

cout<<"回答错误!正确答案为:"<<x1+x2<<endl

}

}

else if(i==2)

{

cout<<"请输入测试题的个数:"

cin>>j

for(int k1=0k1<jk1++)

{

srand((unsigned)time(NULL))

int x1=rand()%100

int x2=x1+rand()%50

cout<<x2<<"-"<<x1<<"=:"

cin>>k

if(k==x2-x1){

cout<<"回答正确!"<<endl

l++

}

else

cout<<"回答错误!正确答案为:"<<x2-x1<<endl

}

}

else if(i==3)

{

cout<<"请输入测试题的个数:"

cin>>j

for(int k1=0k1<jk1++)

{

srand((unsigned)time(NULL))

int x1=rand()%100

int x2=rand()%100

cout<<x1<<"*"<<x2<<"=:"

cin>>k

if(k==x1*x2){

cout<<"回答正确!"<<endl

l++

}

else

cout<<"回答错误!正确答案为:"<<x1*x2<<endl

}

}

else if(i==4)

{

cout<<"请输入测试题的个数:"

cin>>j

for(int k1=0k1<jk1++)

{

srand((unsigned)time(NULL))

int x1=rand()%100+1

int x2=x1*(rand()%10)

cout<<x2<<"/"<<x1<<"=:"

cin>>k

if(k==x2/x1){

cout<<"回答正确!"<<endl

l++

}

else

cout<<"回答错误!正确答案为:"<<x2/x1<<endl

}

}

cout<<"这次测试你作对的题目的个数为"<<l<<"个"<<endl

cout<<"是否继续测试:0.退出 1.继续:"

cin>>n

if(n==1){

return main()}

//system("PAUSE")

return 0

}

int main()

{

double Y, x

scanf("%d",&x)

if(x>2.5)

{

Y=9.32*x-34.2

}

if(x<=2.5)

{

Y=1.5*x+7.5

}

printf("y=%d\n",Y)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8050732.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存