zoukankan      html  css  js  c++  java
  • C++实验 类的应用

    老师讲了C++实验,做一个小总结吧
    创建一个名为HugeInteger的大整数类(因为现在的int型能存放的整数的位数有局限性,可以只考虑正整数),表示方法,要求这个整数类型能存放长度为40位的整数数字,可以自由设计是数据的数据成员来存放40位的大整数。例如:创建长度为40的一个整数数组最多能存放40位的大整数(提示:数组的每个下标变量存放整数的一位)或者可以创建长度为40的char型数组,每个位置放一个数字一位。

    编写提供3个成员函数

    • setInteger函数设置数据,功能是给长度为40的数组赋值(提示:参数可以是int型数据,或者参数可以是字符串表示,能够传入的数据足够长,因为int本身就是局限了HugeInteger的发挥)

    • print函数将大整数打印出来

    • add函数大整数类的对象之间可以进行加法运算。(思考:大整数类的对象和int型变量进行加法运算)思考的部分可以不用实现。

    编写一个测试程序,在测试HugeInteger类的成员函数。

    要求:类要用接口与实现分离的方式创建,即.h和.cpp文件,测试文件另创建.cpp文件。提交时将三个文件打成压缩包提交。

    1 .h文件

    #pragma once
    class Huge
    {
    public:
    	Huge();
    	Huge(int a);
    	Huge(char *s);
    	void setInt(int a);
    	void setChar(char *s);
    	void print();
    	Huge operator+(const Huge &h1);
    	~Huge();
    private:
    	int n[40];
    };
    

    2.cpp文件

    #include "Huge.h"
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    Huge::Huge()
    {
    	for (int i = 0; i <= 39; i++)
    		n[i] = 0;
    	//n[40]={0};错的
    }
    Huge::Huge(int a)
    {
    	setInt(a);
    }
    Huge::Huge(char *s)
    {
    	setChar(s);
    }
    void Huge::setInt(int a)
    {
    //a=12345,n[39]=5,n[38]=4,....00000....12345
    	//n[0]=1,n[1]=2....12345...00000
    	//n[0]=5,n[1]=4....54321...00000
    	for (int i = 0; i <= 39; i++)
    		n[i] = 0;
    	int i = 39;
    	while (a != 0)
    	{
    		n[i] = a % 10;
    		i--;
    		a /= 10;
    	}
    }
    void Huge::setChar(char *s)//"1245457658678465675635"
    {
    	for (int i = 0; i <= 39; i++)
    		n[i] = 0;
    	int len = strlen(s);//"12a45",len=5;len=4;s[len]='5',n[39]=5;'5'-'0'=5
    	len -= 1;
    	for (int i = 39; i >= 0&&len>=0; i--,len--)
    	{
    		if(s[len]<='9'&&s[len]>='0')//
    		n[i] = s[len] - '0';//n[i] = s[len] -48;
    	}
    }
    void Huge::print()
    {
    	int i = 0;
    	for (; i <= 39; i++)
    		if (n[i] != 0)
    			break;
    	if (i == 40)
    		cout << 0;
    	else
    		for (; i <= 39; i++)
    			cout << n[i];
    	cout << endl;
    }
    Huge Huge::operator+(const Huge &h1)
    {
    	Huge temp;//12345+567
    	int c = 0;
    	for (int i = 39; i >= 0; i--)
    	{
    		temp.n[i] = n[i] + h1.n[i] + c;
    		if (temp.n[i] >=10)
    		{
    			c = 1;
    			temp.n[i] %= 10;
    		}
    		else
    			c = 0;
    	}
    	return temp;
    }
    
    Huge::~Huge()
    {
    }
    
    

    3.测试文件

    #include<iostream>
    #include "Huge.h"
    using namespace std;
    int main()
    {//explicit
    	Huge h1, h2(762), h3("12a345");
    	Huge h4 = 12345;
    	h4 = h3;
    	h1.print();
    	h2.print();
    	h3.print();
    	h1=h2+h3;
    	h1.print();
    	return 0;
    }
    
    
  • 相关阅读:
    Smart Client Architecture and Design Guide
    Duwamish密码分析篇, Part 3
    庆贺发文100篇
    .Net Distributed Application Design Guide
    New Introduction to ASP.NET 2.0 Web Parts Framework
    SPS toplevel Site Collection Administrators and Owners
    来自Ingo Rammer先生的Email关于《Advanced .Net Remoting》
    The newsletter published by Ingo Rammer
    深度探索.Net Remoting基础架构
    信道、接收器、接收链和信道接受提供程序
  • 原文地址:https://www.cnblogs.com/xiaotian66/p/13257211.html
Copyright © 2011-2022 走看看