#include <iostream>
using std::cin
using std::cout
using std::endl
//using namespace std
typedef struct BiTNode {
char data
struct BiTNode *Lchild, *Rchild// 左、右孩子指针
} *BiTree
void CreateBiTree(BiTree &T){
以D为根节点的左子树 B根节点 以E为根节点的右子树
以G为根节点的左子树 D根节点 以H为根节点的右子树
以K为根节点的左子树 C根节点 以F为根节点的右子树
以I为根节点的左子树 F根节点 右子树为空
左子树为空 I根节点 以J为根节点的右子树
扩展资料:
主函数的两个形参形式中的形参,允许从执行环境中传递任意的多字节字符串(它们通常被称为命令行参数),各个指针 argv[1] .. argv[argc-1] 指向每个这些字符串的第一个字符。argv[0] 是指向一个表示用于执行该程序自身的名字的空结尾多字节字符串(或者当执行环境不支持时,为空字符串 "")的开头字符的指针。
这些字符串是可以改动的,虽然对它们的改动并不会被传回给执行环境:比如可以用 std::strtok 来使用它们。由 argv 所指向的数组的大小至少为 argc+1,其最后一个元素 argv[argc] 保证为一个空指针。
参考资料来源:百度百科-main函数
花了两个多小时,终于把代码写好了。。。
在窗体上添加一个TextBox控件,名称为Text1。
在窗体代码中写入如下代码:
Const MaxLenth = 20000Private Sub Form_Click()
Form_Load
End Sub
Private Sub Form_DblClick()
Form_Load
End Sub
Private Sub Form_Load()
Dim x As String
Dim tx As String
Dim m As String
Dim isFloat As Boolean
Dim n As String
Dim tn As String
Dim i As String
Dim Result As String
Dim resultA As String
Dim resultB As String
Dim els As Long
n = "1000" '这里设定平方根保留1000位小数
x = InputBox("请输入要求平方根的数:", "输入", "0.002")
x = Trim(x)
If Not IsNumeric(x) Then
MsgBox "输入内容非数值或用户选择了取消!"
Exit Sub
Else
If CLng(x) < 0 Then
MsgBox "输入的数不能小于零!"
Exit Sub
End If
End If
tx = x
m = CStr(InStr(tx, "."))
If CLng(m) > 0 Then
tn = Mid(tx, CLng(m) + 1)
If CLng(tn) > 0 Then
isFloat = True
m = CStr(Len(tn))
End If
End If
If isFloat Then
tn = "0"
If BigCompare_IsAbiggerThanB(BigDivisionMod(m, "2", True), "0") > 0 Then tn = "1"
m = BigMultiplication(BigAddition(BigDivisionMod(m, "2", False), tn), 2)
tx = Replace(tx, ".", "")
If CLng(tn) = 1 Then tx = tx & "0"
End If
tn = n
tn = BigAddition(tn, "1")
tn = BigMultiplication(tn, 2)
If isFloat Then tn = BigAddition(tn, m)
tn = BigPower("10", tn)
Result = BigSqrt(BigMultiplication(tx, tn), False)
i = BigDivisionMod(m, "2", False)
Result = Replace(Space(CLng(i)), " ", "0") & Result
resultB = Right(Result, 1)
Result = Left(Result, Len(Result) - 1)
If CLng(resultB) >= 5 Then
resultA = Left(Result, Len(Result) - 1)
resultB = Right(Result, 1)
Result = resultA & CStr(CLng(resultB) + 1)
End If
i = CLng(n) + IIf(isFloat, CLng(m), 0)
els = Len(Result) - i
If els > 0 Then
resultA = Left(Result, els)
resultB = Right(Result, i)
Result = CStr(CLng(resultA)) & "." & resultB
End If
Text1.Text = CStr(x) & "保留" & n & "位小数的平方根为:" & Result
End Sub
Function BigNumberToText(C() As Long, Length As Long) As String
Dim i As Long
Dim Pom As String
If C(0) = -1 Then
Pom = "-"
Else
Pom = ""
End If
Pom = Pom & Format$(C(Length), "0")
For i = Length - 1 To 1 Step -1
Pom = Pom & Format$(C(i), "0000")
Next i
BigNumberToText = Pom
End Function
Function IsItBigNumber(Ulaz As String) As Boolean
Dim Pom As String
Dim Pom1 As String
Dim i As Long
Dim IsItBigNumber1 As Boolean
Pom1 = Ulaz
Pom = Left$(Pom1, 300)
If IsNumeric(Pom) Then
If InStr(1, Pom, "e", 1) > 0 Then
IsItBigNumber1 = False
Else
If InStr(1, Pom, ".", 1) > 0 Then
IsItBigNumber1 = False
Else
If InStr(1, Pom, ",", 1) > 0 Then
IsItBigNumber1 = False
Else
IsItBigNumber1 = True
End If
End If
End If
Else
IsItBigNumber1 = False
End If
If IsItBigNumber1 Then
For i = 1 To Len(Pom1) \ 300
Pom = Mid$(Pom1, 300 * i, 300)
If IsNumeric(Pom) Then
If InStr(1, Pom, "e", 1) > 0 Then
IsItBigNumber1 = False
Else
If InStr(1, Pom, ".", 1) > 0 Then
IsItBigNumber1 = False
Else
If InStr(1, Pom, ",", 1) > 0 Then
IsItBigNumber1 = False
Else
If InStr(1, Pom, "-", 1) > 0 Then
IsItBigNumber1 = False
Else
If InStr(1, Pom, "+", 1) > 0 Then
IsItBigNumber1 = False
Else
IsItBigNumber1 = True
End If
End If
End If
End If
End If
Else
IsItBigNumber1 = False
End If
Next i
End If
IsItBigNumber = IsItBigNumber1
End Function
Sub TextToBigNumber(Tekst As String, A() As Long, LengthA As Long)
Dim i As Long
Dim Prvi As String
Dim MaxLenthPrvi As Long
Dim Ostatak As Long
Prvi = Trim$(Tekst)
If IsItBigNumber(Prvi) Then
A(0) = 0
If Left$(Prvi, 1) = "+" Then
Prvi = Right$(Prvi, Len(Prvi) - 1)
End If
If Left$(Prvi, 1) = "-" Then
Prvi = Right$(Prvi, Len(Prvi) - 1)
A(0) = -1
End If
MaxLenthPrvi = Len(Prvi)
If (MaxLenthPrvi \ 4) * 4 = MaxLenthPrvi Then
LengthA = MaxLenthPrvi \ 4
For i = 1 To LengthA
A(i) = Mid$(Prvi, MaxLenthPrvi - i * 4 + 1, 4)
Next i
Else
LengthA = MaxLenthPrvi \ 4 + 1
Ostatak = MaxLenthPrvi Mod 4
For i = 1 To LengthA - 1
A(i) = Mid$(Prvi, MaxLenthPrvi - i * 4 + 1, 4)
Next i
A(LengthA) = Mid$(Prvi, 1, Ostatak)
End If
Else
A(1) = 0
LengthA = 1
End If
End Sub
Sub AddB(A() As Long, LengthA As Long, B() As Long, LengthB As Long, C() As Long, LengthC As Long)
Dim Prenos As Long
Dim i As Long
Dim J As Long
Prenos = 0
If LengthA > LengthB Then
LengthC = LengthA + 1
For i = 1 To LengthB
C(i) = A(i) + B(i) + Prenos
Prenos = C(i) \ 10000
C(i) = C(i) Mod 10000
Next i
i = LengthB + 1
Do While Prenos > 0 And i <= LengthA
C(i) = A(i) + Prenos
Prenos = C(i) \ 10000
C(i) = C(i) Mod 10000
i = i + 1
Loop
If i > LengthA Then
C(i) = Prenos
Else
For J = i To LengthA
C(J) = A(J)
Next J
C(LengthA + 1) = 0
End If
Else
LengthC = LengthB + 1
For i = 1 To LengthA
C(i) = A(i) + B(i) + Prenos
Prenos = C(i) \ 10000
C(i) = C(i) Mod 10000
Next i
i = LengthA + 1
Do While Prenos > 0 And i <= LengthB
C(i) = B(i) + Prenos
Prenos = C(i) \ 10000
C(i) = C(i) Mod 10000
i = i + 1
Loop
If i > LengthB Then
C(i) = Prenos
Else
For J = i To LengthB
C(J) = B(J)
Next J
C(LengthB + 1) = 0
End If
End If
If C(LengthC) = 0 Then LengthC = LengthC - 1
End Sub
Sub MinusB(A() As Long, LengthA As Long, B() As Long, LengthB As Long, C() As Long, LengthC As Long)
Select Case CompareB(A, LengthA, B, LengthB)
Case Is < 0
Call MinusBV(B, LengthB, A, LengthA, C, LengthC)
C(0) = -1
Case Is = 0
C(1) = 0
LengthC = 1
C(0) = 0
Case Is > 0
Call MinusBV(A, LengthA, B, LengthB, C, LengthC)
C(0) = 0
End Select
End Sub
Sub AddBSigned(A() As Long, LengthA As Long, B() As Long, LengthB As Long, C() As Long, LengthC As Long)
If A(0) = 0 And B(0) = 0 Then
Call AddB(A, LengthA, B, LengthB, C, LengthC)
End If
If A(0) < 0 And B(0) < 0 Then
Call AddB(A, LengthA, B, LengthB, C, LengthC)
C(0) = -1
End If
If A(0) = 0 And B(0) < 0 Then
Call MinusB(A, LengthA, B, LengthB, C, LengthC)
End If
If A(0) < 0 And B(0) = 0 Then
Call MinusB(B, LengthB, A, LengthA, C, LengthC)
End If
End Sub
Sub CopyB(A() As Long, LengthA As Long, B() As Long, LengthB As Long)
Dim i As Long
LengthB = LengthA
For i = 0 To LengthA
B(i) = A(i)
Next i
End Sub
Sub MultB(A() As Long, LengthA As Long, B() As Long, LengthB As Long, C() As Long, LengthC As Long)
Dim Pom0(MaxLenth) As Long
Dim LengthPom0 As Long
Dim Prenos As Long
Dim i As Long
Dim J As Long
If (LengthB = 1 And B(1) = 0) Or (LengthA = 1 And A(1) = 0) Then
C(1) = 0
LengthC = 1
C(0) = 0
Exit Sub
End If
If LengthB = 1 And B(1) = 1 Then
Call CopyB(A, LengthA, C, LengthC)
Exit Sub
End If
If LengthA = 1 And A(1) = 1 Then
Call CopyB(B, LengthB, C, LengthC)
Exit Sub
End If
Prenos = 0
For i = 1 To LengthA + LengthB
Pom0(i) = 0
Next i
For i = 1 To LengthB
For J = 1 To LengthA
Pom0(i + J - 1) = Pom0(i + J - 1) + A(J) * B(i)
Prenos = Pom0(i + J - 1) \ 10000
Pom0(i + J - 1) = Pom0(i + J - 1) Mod 10000
Pom0(i + J) = Pom0(i + J) + Prenos
Next J
Next i
LengthPom0 = LengthA + LengthB
Do Until Pom0(LengthPom0) <> 0 Or LengthPom0 = 1
LengthPom0 = LengthPom0 - 1
Loop
Call CopyB(Pom0, LengthPom0, C, LengthC)
End Sub
Sub MultBSigned(A() As Long, LengthA As Long, B() As Long, LengthB As Long, C() As Long, LengthC As Long)
Dim f As Boolean
If (A(0) = 0 And B(0) = 0) Or (A(0) < 0 And B(0) < 0) Then
f = False
Else
If LengthC = 1 And C(1) = 0 Then
f = False
Else
f = True
End If
End If
Call MultB(A, LengthA, B, LengthB, C, LengthC)
If f Then
C(0) = -1
Else
C(0) = 0
End If
End Sub
Sub PowerB(A() As Long, LengthA As Long, PowerB As Long, C() As Long, LengthC As Long)
Dim i As Long
C(0) = 0
C(1) = 1
LengthC = 1
For i = 1 To PowerB
Call MultBSigned(A, LengthA, C, LengthC, C, LengthC)
Next i
End Sub
Function CompareB(A() As Long, LengthA As Long, B() As Long, LengthB As Long) As Long
Dim i As Long
Select Case LengthA - LengthB
Case Is < 0
CompareB = -1
Case Is = 0
CompareB = 0
For i = 1 To LengthA
Select Case A(LengthA - i + 1) - B(LengthA - i + 1)
Case Is < 0
CompareB = -1
Exit For
Case Is = 0
Case Is > 0
CompareB = 1
Exit For
End Select
Next i
Case Is > 0
CompareB = 1
End Select
End Function
(这份代码是VB的,VC的还不够熟悉, 暂无法写。。)
代码太长,这里不允许发表,完整的代码我已经发表到我的QQ空间,这是文章的地址:
http://user.qzone.qq.com/495825947/blog/1397222025
#include <iostream>#include <string>
using namespace std
/**-----------------------------------------------------------------------------
* 常量声明
*------------------------------------------------------------------------------
*/
const int c_defaultYear = 2007//默认年份
const int c_defaultMonth = 10 //默认月份
const int c_defaultDay = 10 //默认日期
#define CESHI//如果要做主函数测试,将此行注释即可
/**-----------------------------------------------------------------------------
* CDate类声明段
*------------------------------------------------------------------------------
*/
class CDate
{
public:
CDate() //构造函数
CDate(int a,int b,int c)//带参数的构造函数
CDate(const CDate &p) //copy构造函数
~CDate()//析构函数
public:
int getYear() const //返回年份
int getMonth() const//返回月份
int getDay() const //返回日期
bool isLeap() const //返回是否是闰年
bool isLeap()
void print()//打印
private:
bool isittrue(int,int,int) //检测是否有误
void defaultset() //使用默认设置
private:
int m_Year //年
int m_Month //月
int m_Day //日
static int sn //静态变量
}
int CDate::sn = 1 //初始化静态变量
/**-----------------------------------------------------------------------------
* CDate类实现段
*------------------------------------------------------------------------------
*/
CDate::CDate()
{
cout<<"无参数构造函数被调用.\n"
cout<<"请输入年月日,以空格隔开:"
int t_Year = 0,t_Month = 0,t_Day = 0
cin>>t_Year>>t_Month>>t_Day
if(cin.fail())//如果输入字符串、浮点数等将退出
{
cout<<"输入错误,程序将采用默认值构造\n"
defaultset()
sn++
return
}
if(isittrue(t_Year,t_Month,t_Day))//判断输入是否正确,正确则继续进行
{
m_Year = t_Year
m_Month = t_Month
m_Day = t_Day
}
else
{
cout<<"输入有误,程序将采用默认值构造类\n"
defaultset()
}
sn++//静态变量自增,用于计数
}
CDate::CDate(int a,int b,int c)
{
cout<<"带参数的构造函数被调用\n"
if(isittrue(a,b,c))
{
m_Year = a
m_Month = b
m_Day = c
}
else
{
cout<<"参数有误,程序将采用默认值构造类\n"
defaultset()
}
sn++
}
CDate::CDate(const CDate &p)
{
cout<<"拷贝构造函数被调用\n"
m_Year = p.getYear()
m_Month = p.getMonth()
m_Day = p.getDay()
sn++
}
CDate::~CDate()
{
cout<<"析构函数被调用.\n"
}
int CDate::getYear() const
{
return m_Year
}
int CDate::getMonth() const
{
return m_Month
}
int CDate::getDay() const
{
return m_Day
}
bool CDate::isLeap() const
{
if((m_Year%4==0&&m_Year%100!=0)||(m_Year%100==0&&m_Year%400 ==0))
return true
else
return false
}
bool CDate::isLeap()
{
if((m_Year%4==0&&m_Year%100!=0)||(m_Year%100==0&&m_Year%400 ==0))
return true
else
return false
}
void CDate::print()
{
cout<<m_Year<<"年"<<m_Month<<"月"<<m_Day<<"日"<<endl
cout<<"sn="<<sn<<endl<<endl
}
bool CDate::isittrue(int a,int b,int c) //检测输入字符的逻辑正确性
{
if(a<0||b<0||c<0||b>12||c>31)
return false
else if((b==2&&isLeap()&&c>29)||b==2&&!isLeap()&&c>28)
return false
else if((b==4||b==6||b==9||b==11)&&c>30)
return false
else if((b==1||b==3||b==5||b==7||b==8||b==10||b==12)&&c>31)
return false
else
return true
}
void CDate::defaultset() //使用默认值构造类
{
m_Year = c_defaultYear
m_Month = c_defaultMonth
m_Day = c_defaultDay
}
/**-----------------------------------------------------------------------------
* 公共函数定义段
*------------------------------------------------------------------------------
*/
bool isStrReal(string dStr)
{
if(dStr.size()!=8)
return false
//以下内容中的string长度一定为8
for(int i=0i<8i++)
{
int temp = static_cast<int>(dStr[i])
/*
//如果无法编译请注释上面一行,解注释这一行
int temp = (int)(dStr[i])
*/
if(temp<48||temp>57)
{
return false
}
}
return true
}
CDate changeToCDate(string dStr)
{
//设置变量用于提取字符串中的整形变量
int year=0,month=0,day=0
if(!isStrReal(dStr))
{
cout<<"你的字符串有误,程序将返回默认值构造的类"<<endl
CDate p(c_defaultYear,c_defaultMonth,c_defaultDay)
return p
}
else
{
//注意:这里-48是将编译器自动转换的ASCII码值转化成可 *** 作整数值
year = (dStr[0]-48)*1000+(dStr[1]-48)*100+(dStr[2]-48)*10+(dStr[3]-48)
month = (dStr[4]-48)*10+(dStr[5]-48)
day = (dStr[6]-48)*10+(dStr[7]-48)
}
CDate p(year,month,day)
return p
}
/**-----------------------------------------------------------------------------
* 主函数实现段
*------------------------------------------------------------------------------
*/
int main(void)
{
cout<<"以下将开始测试普通定义类、带参数构造类、copy构造函数构造类.\n"
cout<<"-----------------------------------------------------------\n"
CDate CDate1
CDate1.print()
system("pause")
CDate CDate2(1900,3,4)
CDate2.print()
system("pause")
CDate CDate3(CDate2)
CDate3.print()
system("pause")
cout<<"\n以下将是闰年检测测试部分.\n"
cout<<"-----------------------------------------------------------\n"
CDate CDate4(2008,7,8)
if(CDate3.isLeap())
cout<<CDate3.getYear()<<"年是闰年\n"
else
cout<<CDate3.getYear()<<"年不是闰年\n"
if(CDate4.isLeap())
cout<<CDate4.getYear()<<"年是闰年\n"
else
cout<<CDate4.getYear()<<"年不是闰年\n"
system("pause")
cout<<"\n以下将测试字符串转换定义类.\n"
cout<<"-----------------------------------------------------------\n"
string ss("20080323")
cout<<"我们定义的字符串是"<<ss<<endl
CDate date5 = changeToCDate(ss)
date5.print()
system("pause")
#ifndef CESHI
cout<<"\n以下将测试错误反馈.\n"
cout<<"-----------------------------------------------------------\n"
cout<<"首先定义一个类wrongdate1,请在接下来的过程中输入错误的数据,如:<22.3 33 33>、<abd d 33>等错误数据。\n"
CDate wrongdate1
wrongdate1.print()
system("pause")
cout<<"接下来测试逻辑错误,请输入如这样的值:<2008 13 1>、<1234 2 30>、<2007 2 29>等\n"
CDate wrongdate2
wrongdate2.print()
system("pause")
cout<<"接下来测试字符串的错误检测\n"
cout<<"请输入一个不是按照格式的字符串,如:sdfkasf,2008323,20080333等\n"
string str
cout<<"输入:"
cin>>str
CDate wrongdate3 = changeToCDate(str)
wrongdate3.print()
system("pause")
cout<<"测试完毕。"<<endl
#endif
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)