1. 用vs編寫一個簡單的計算器。 用vs編一個數據結構中的循環結構,選擇結構,順序結構!!!還清還有一
cwa9958 說的有點問題,任何程序,都需要數據結構,只不過有些特別簡單而已
編寫羨耐計算器,復雜的塵派豎數據結構倒是用不上,關鍵要掌握下面的幾點:
1.正確處理各種輸入的順序及處理和顯示(這是計算器代碼中最復雜的)
2.靈活應用控制項數組
3.正確使用數學函數
4.容錯處理技術(這個需要編程人員有很好的調試技巧)
後話:要做到Windows自帶的那個的功能,不是一件簡單的事,好好編吧,編完後,你會派大發現,你的編程水平會上一個台階
2. 如何用VS編寫一個計算器程序用到C++MFC等。不需要全部代碼,數字運算符號各提供一個參考代碼即
定義一個全局變數用於存放當前顯示的數值。
例如 輸猜雹入了11 按一下+號鍵就寫m_Calc=11;//具友兆喚體自己轉
再輸入加的數值 例如輸入了22; 就寫m_Calc+=22; 除號鍵也是 m_Calc=m_Calc/55;
減好凱號鍵m_Calc-=22;~~
3. 用VS使用C#開發一個簡易計算器,實現加減乘除等於基本功能,就和電腦自帶的那個差不多,求代碼
用數組,打比方,A1。A2
第一輸入的數字到A1寄存,第二次的用A2寄存,然後做計算。
4. 在VS2010中編寫C#程序計算器
//簡單的計算器
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
namespacewincalc
{
///
///SummarydescriptionforcalcForm.
///
publicclasscalcForm:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.Buttonbutton2;
privateSystem.Windows.Forms.Buttonbutton3;
privateSystem.Windows.Forms.Buttonbutton4;
privateSystem.Windows.Forms.Buttonbutton5;
privateSystem.Windows.Forms.Buttonbutton6;
privateSystem.Windows.Forms.Buttonbutton7;
privateSystem.Windows.Forms.Buttonbutton8;
privateSystem.Windows.Forms.Buttonbutton9;
privateSystem.Windows.Forms.Buttonbutton10;
privateSystem.Windows.Forms.ButtonbClr;
privateSystem.Windows.Forms.ButtonbDot;
privateSystem.Windows.Forms.ButtonbPlus;
privateSystem.Windows.Forms.ButtonbSub;
privateSystem.Windows.Forms.ButtonbMul;
privateSystem.Windows.Forms.ButtonbDiv;
privateSystem.Windows.Forms.ButtonbEqu;
privateSystem.Windows.Forms.TextBoxtxtCalc;
//以下是要添加的代碼
//定義變數
DoubledblAcc;
DoubledblSec;
boolblnClear,blnFrstOpen;
StringstrOper;
//以上是添加的代碼
///
///Requireddesignervariable.
///
privateSystem.ComponentModel.Containercomponents=null;
publiccalcForm()
{
//
//
//
InitializeComponent();
//
//TODO:
//
//以下是要添加的代碼
//初始化設量
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
blnClear=true;
strOper=newstring('=',1);
//以上是添加的代碼
}
///
///Cleanupanyresourcesbeingused.
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#
///
///-donotmodify
///.
///
()
{
this.bPlus=newSystem.Windows.Forms.Button();
this.bMul=newSystem.Windows.Forms.Button();
this.bDot=newSystem.Windows.Forms.Button();
this.txtCalc=newSystem.Windows.Forms.TextBox();
this.bClr=newSystem.Windows.Forms.Button();
this.bDiv=newSystem.Windows.Forms.Button();
this.bSub=newSystem.Windows.Forms.Button();
this.button8=newSystem.Windows.Forms.Button();
this.button9=newSystem.Windows.Forms.Button();
this.bEqu=newSystem.Windows.Forms.Button();
this.button10=newSystem.Windows.Forms.Button();
this.button4=newSystem.Windows.Forms.Button();
this.button5=newSystem.Windows.Forms.Button();
this.button6=newSystem.Windows.Forms.Button();
this.button7=newSystem.Windows.Forms.Button();
this.button1=newSystem.Windows.Forms.Button();
this.button2=newSystem.Windows.Forms.Button();
this.button3=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//bPlus
//
this.bPlus.BackColor=System.Drawing.SystemColors.Control;
this.bPlus.ForeColor=System.Drawing.SystemColors.ControlText;
this.bPlus.Location=newSystem.Drawing.Point(208,112);
this.bPlus.Name="bPlus";
this.bPlus.Size=newSystem.Drawing.Size(32,80);
this.bPlus.TabIndex=1;
this.bPlus.Text="+";
//以下是要添加的代碼
bPlus.Click+=newSystem.EventHandler(this.btn_Oper);
//以上是添加的代碼
//
//bMul
//
this.bMul.Location=newSystem.Drawing.Point(160,112);
this.bMul.Name="bMul";
this.bMul.Size=newSystem.Drawing.Size(32,32);
this.bMul.TabIndex=1;
this.bMul.Text="*";
//以下是要添加的代碼
bMul.Click+=newSystem.EventHandler(this.btn_Oper);
//以上是添加的代碼
//
//bDot
//
this.bDot.ForeColor=System.Drawing.Color.Black;
this.bDot.Location=newSystem.Drawing.Point(112,208);
this.bDot.Name="bDot";
this.bDot.Size=newSystem.Drawing.Size(32,32);
this.bDot.TabIndex=0;
this.bDot.Text=".";
//以下是要添加的代碼
bDot.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//txtCalc
//
this.txtCalc.Location=newSystem.Drawing.Point(16,24);
this.txtCalc.Name="txtCalc";
this.txtCalc.ReadOnly=true;
this.txtCalc.RightToLeft=System.Windows.Forms.RightToLeft.Yes;
this.txtCalc.Size=newSystem.Drawing.Size(224,21);
this.txtCalc.TabIndex=2;
this.txtCalc.Text="";
//
//bClr
//
this.bClr.BackColor=System.Drawing.SystemColors.Control;
this.bClr.ForeColor=System.Drawing.SystemColors.ControlText;
this.bClr.Location=newSystem.Drawing.Point(208,64);
this.bClr.Name="bClr";
this.bClr.Size=newSystem.Drawing.Size(32,32);
this.bClr.TabIndex=0;
this.bClr.Text="AC";
//以下是要添加的代碼
bClr.Click+=newSystem.EventHandler(this.btn_clr);
//以上是添加的代碼
//
//bDiv
//
this.bDiv.Location=newSystem.Drawing.Point(160,160);
this.bDiv.Name="bDiv";
this.bDiv.Size=newSystem.Drawing.Size(32,32);
this.bDiv.TabIndex=1;
this.bDiv.Text="/";
//以下是要添加的代碼
bDiv.Click+=newSystem.EventHandler(this.btn_Oper);
//以上是添加的代碼
//
//bSub
//
this.bSub.Location=newSystem.Drawing.Point(160,64);
this.bSub.Name="bSub";
this.bSub.Size=newSystem.Drawing.Size(32,32);
this.bSub.TabIndex=1;
this.bSub.Text="-";
//以下是要添加的代碼
bSub.Click+=newSystem.EventHandler(this.btn_Oper);
//以上是添加的代碼
//
//button8
//
this.button8.Location=newSystem.Drawing.Point(16,64);
this.button8.Name="button8";
this.button8.Size=newSystem.Drawing.Size(32,32);
this.button8.TabIndex=0;
this.button8.Text="7";
//以下是要添加的代碼
button8.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button9
//
this.button9.Location=newSystem.Drawing.Point(64,64);
this.button9.Name="button9";
this.button9.Size=newSystem.Drawing.Size(32,32);
this.button9.TabIndex=0;
this.button9.Text="8";
//以下是要添加的代碼
button9.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//bEqu
//
this.bEqu.BackColor=System.Drawing.SystemColors.Control;
this.bEqu.ForeColor=System.Drawing.SystemColors.ControlText;
this.bEqu.Location=newSystem.Drawing.Point(160,208);
this.bEqu.Name="bEqu";
this.bEqu.Size=newSystem.Drawing.Size(80,32);
this.bEqu.TabIndex=1;
this.bEqu.Text="=";
//以下是要添加的代碼
bEqu.Click+=newSystem.EventHandler(this.btn_equ);
//以上是添加的代碼
//
//button10
//
this.button10.Location=newSystem.Drawing.Point(112,64);
this.button10.Name="button10";
this.button10.Size=newSystem.Drawing.Size(32,32);
this.button10.TabIndex=0;
this.button10.Text="9";
//以下是要添加的代碼
button10.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button4
//
this.button4.Location=newSystem.Drawing.Point(112,160);
this.button4.Name="button4";
this.button4.Size=newSystem.Drawing.Size(32,32);
this.button4.TabIndex=0;
this.button4.Text="3";
//以下是要添加的代碼
button4.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button5
//
this.button5.Location=newSystem.Drawing.Point(16,112);
this.button5.Name="button5";
this.button5.Size=newSystem.Drawing.Size(32,32);
this.button5.TabIndex=0;
this.button5.Text="4";
//以下是要添加的代碼
button5.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button6
//
this.button6.Location=newSystem.Drawing.Point(64,112);
this.button6.Name="button6";
this.button6.Size=newSystem.Drawing.Size(32,32);
this.button6.TabIndex=0;
this.button6.Text="5";
//以下是要添加的代碼
button6.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button7
//
this.button7.Location=newSystem.Drawing.Point(112,112);
this.button7.Name="button7";
this.button7.Size=newSystem.Drawing.Size(32,32);
this.button7.TabIndex=0;
this.button7.Text="6";
//以下是要添加的代碼
button7.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button1
//
this.button1.BackColor=System.Drawing.SystemColors.Control;
this.button1.ForeColor=System.Drawing.Color.Black;
this.button1.Location=newSystem.Drawing.Point(16,208);
this.button1.Name="button1";
this.button1.Size=newSystem.Drawing.Size(80,32);
this.button1.TabIndex=0;
this.button1.Text="0";
//以下是要添加的代碼
button1.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button2
//
this.button2.Location=newSystem.Drawing.Point(16,160);
this.button2.Name="button2";
this.button2.Size=newSystem.Drawing.Size(32,32);
this.button2.TabIndex=0;
this.button2.Text="1";
//以下是要添加的代碼
button2.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//button3
//
this.button3.Location=newSystem.Drawing.Point(64,160);
this.button3.Name="button3";
this.button3.Size=newSystem.Drawing.Size(32,32);
this.button3.TabIndex=0;
this.button3.Text="2";
//以下是要添加的代碼
button3.Click+=newSystem.EventHandler(this.btn_clk);
//以上是添加的代碼
//
//calcForm
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(256,261);
this.Controls.AddRange(newSystem.Windows.Forms.Control[]{
this.txtCalc,
this.bEqu,
this.bDiv,
this.bMul,
this.bSub,
this.bPlus,
this.bDot,
this.bClr,
this.button10,
this.button9,
this.button8,
this.button7,
this.button6,
this.button5,
this.button4,
this.button3,
this.button2,
this.button1});
this.Name="calcForm";
this.Text="計算器";
this.ResumeLayout(false);
}
#endregion
//以下是要添加的代碼
//小數點的操作
privatevoidbtn_clk(objectobj,EventArgsea)
{
if(blnClear)
txtCalc.Text="";
Buttonb3=(Button)obj;
txtCalc.Text+=b3.Text;
if(txtCalc.Text==".")
txtCalc.Text="0.";
dblSec=Convert.ToDouble(txtCalc.Text);
blnClear=false;
}
//程序開始點
privatestaticvoidMain()
{
Application.Run(newcalcForm());
}
privatevoidbtn_Oper(objectobj,EventArgsea)
{
Buttontmp=(Button)obj;
strOper=tmp.Text;
if(blnFrstOpen)
dblAcc=dblSec;
else
calc();
blnFrstOpen=false;
blnClear=true;
}
//等號運算
privatevoidbtn_equ(objectobj,EventArgsea)
{
calc();
}
//所有運算操作
privatevoidcalc()
{
switch(strOper)
{
case"+":
dblAcc+=dblSec;//加號運算
break;
case"-":
dblAcc-=dblSec;//減號運算
break;
case"*":
dblAcc*=dblSec;//乘號運算
break;
case"/":
dblAcc/=dblSec;//除號運算
break;
}
strOper="=";//等號運算
blnFrstOpen=true;
txtCalc.Text=Convert.ToString(dblAcc);//將運算結果轉換成字元類型,並輸出
dblSec=dblAcc;//將運算數A的值放入運算數B中,以便後面運算
}
//清除按鈕
privatevoidbtn_clr(objectobj,EventArgsea)
{
clear();
}
//清除按鈕的操作
privatevoidclear()
{
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
txtCalc.Text="";
txtCalc.Focus();//設置焦點為txtCalc
}
//以上是添加的代碼
}
}
5. 怎麼用vs做一個計算器
首先,點擊VS2012,啟動VS軟體,然後建立如下圖所示的應用程序,在圖的下面輸入項目名
建立基於對話框的標准程序,如下圖所示,其餘設置採用系統設置,點擊完成
如下圖,軟體會生成下面的對話框,還有相應的庫,代碼,資源文件等
對上面的對話框進行編輯,如下圖中所示,最拿悶配後編輯的樣子如下,其中1,5,6是Edit 控制項,2,7是靜態控制項,3是buttion,4是取消按鈕改了個名字,將各個控制項按下圖排好,並改好名字,其餘部分採用軟體默認
對1,5,7控制項添加變數,對控制項右擊,選擇如下
如下圖變數添加向導,1選擇Value,2選擇Double (方便計算小數)3,是變數名,我們選擇a,相同的方式,對上圖中的1,5,7分別添加變數,分別為工a,b,sum
添加完後在對話框上雙擊計算按鈕,就會生成相應的消息映射,進入如下代碼中,見下圖
在這個函數中添加下面代碼,UpdataDate()函數中參數true表示從控制項變數中獲取數值,false相反
sum=a+b,就是我們先在控制項中添加的三個變數,此處所做的計算器只是用來做個加法
添加完上述代碼後,編譯成功如下所示
ctrl+f5運行如下圖
輸入數值進行加法計算,你可以進行其它的相關擴展,比如減消指罩羨法,乘法等,都是相同的方法
6. vs2010編譯簡單計算器窗體
先看看圖片 說明以上控制項動態生成的,初學可自己拖放控制項,以下代碼僅供參考 /*設計一個嫌宴殲簡單的四則計算器*/ string[]名稱={"+","-","*","/"}; Button[]控制項組=Array.ConvertAll(名稱,控制項=>newButton()); EventHandler[]按鈕事件=newEventHandler[]{按鈕加_Click,按鈕減_Click,按鈕乘_Click,按鈕除_Click}; foreach(Button按鈕in控芹沖件組) { 按鈕.Text=名稱祥孝[--數]; 按鈕.Name="按鈕"+數.ToString(); 按鈕.Parent=this; 按鈕.Anchor=(AnchorStyles.Top|AnchorStyles.Left); 按鈕.Location=newPoint(100*數,17); 按鈕.Click+=按鈕事件[數]; } 名稱=newstring[]{"值1","值2","結果"}; TextBox[]數字輸入框=Array.ConvertAll(名稱,控制項=>newTextBox()); 數=3; foreach(TextBox輸入框in數字輸入框) { 輸入框.Text=(--數+10).ToString(); 輸入框.Name="輸入框"+名稱[數]; 輸入框.Parent=this; 輸入框.Anchor=(AnchorStyles.Top|AnchorStyles.Left); 輸入框.Location=newPoint(100*數,0); } void按鈕加_Click(objectsender,EventArgse) { 四則運算("+"); } void按鈕減_Click(objectsender,EventArgse) { 四則運算("-"); } void按鈕乘_Click(objectsender,EventArgse) { 四則運算("*"); } void按鈕除_Click(objectsender,EventArgse) { 四則運算("/"); } void四則運算(string運算符) { Control.ControlCollection文本框=this.Controls; double值1=int.Parse(文本框[9].Text),值2=int.Parse(文本框[8].Text); switch(運算符) { case"+": {文本框[7].Text=(值1+值2).ToString();} break; case"-": {文本框[7].Text=(值1-值2).ToString();} break; case"*": {文本框[7].Text=(值1*值2).ToString();} break; case"/": {文本框[7].Text=(值1/值2).ToString();} break; default: break; } }
7. 剛剛用vs2013寫的c語言計算器不輸出結果,是不是代碼錯了
把搭沒scanf_s("%d %c %d", &a,&fh,&b);改為
scanf_s("%d %c %d"知畢納數悔, &a,&fh,1,&b);
8. vs計算器的代碼可以算小數字元串
vs計算器的代碼可以算小數字元串。根據查詢相關資料信息顯示vs計算器的代碼是一種計算器,所以可以算小數字元串。vs算器實現了基礎的數學運算,如叢槐加,減,乘,除首激等任務,主要是通過該程滲芹友序學習vs.net的編程環境。