運算代碼
❶ 演算法代碼怎麼寫
按照你的要求編寫的c語言程序如下:
#include
int main(){
int n=0;
while(1){
if(n%1==0 && n%2==1 && n%3==0 && n%4==1 && n%5==1 && n%6==3 && n%7==0 && n%8==1 && n%9==0){
printf("筐里回有答%d個雞蛋",n);
❷ 用C語言寫一個加法運算的代碼怎麼寫
例子如下:
知識擴展:
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
C語言屬於高級程序語言的一種,它的前身是「ALGOL」。其創始人是布朗·W·卡尼漢和丹尼斯·M·利奇。C語言問世時是帶有很大的局限性,因為它只能用於UNIX系統上。然而隨著科學技術的進步,計算機工業的發展,C語言逐漸脫離UNIX。1987年美國標准化協會制定了C語言的國際標准,簡稱「ANSI C」,從此以後它便成為一種廣泛使用的程序語言。
❸ c語言函數計算機四則運算代碼
4則運算的話。。。。最多有個除0判斷,溢出判斷吧
❹ 四則運算 源代碼
VB原代碼:
Private Function EvaluateExpr(ByVal expr As String) As Single
Const PREC_NONE = 11
Const PREC_UNARY = 10 ' Not actually used.
Const PREC_POWER = 9
Const PREC_TIMES = 8
Const PREC_DIV = 7
Const PREC_INT_DIV = 6
Const PREC_MOD = 5
Const PREC_PLUS = 4
Dim is_unary As Boolean
Dim next_unary As Boolean
Dim parens As Integer
Dim pos As Integer
Dim expr_len As Integer
Dim ch As String
Dim lexpr As String
Dim rexpr As String
Dim value As String
Dim status As Long
Dim best_pos As Integer
Dim best_prec As Integer
' Remove leading and trailing blanks.
expr = Trim$(expr)
expr_len = Len(expr)
If expr_len = 0 Then Exit Function
' If we find + or - now, it is a unary operator.
is_unary = True
' So far we have nothing.
best_prec = PREC_NONE
' Find the operator with the lowest precedence.
' Look for places where there are no open
' parentheses.
For pos = 1 To expr_len
' Examine the next character.
ch = Mid$(expr, pos, 1)
' Assume we will not find an operator. In
' that case the next operator will not
' be unary.
next_unary = False
If ch = " " Then
' Just skip spaces.
next_unary = is_unary
ElseIf ch = "(" Then
' Increase the open parentheses count.
parens = parens + 1
' An operator after "(" is unary.
next_unary = True
ElseIf ch = ")" Then
' Decrease the open parentheses count.
parens = parens - 1
' An operator after ")" is unary.
next_unary = True
' If parens < 0, too many ')'s.
If parens < 0 Then
Err.Raise vbObjectError + 1001, _
"EvaluateExpr", _
"Too many )s in '" & _
expr & "'"
End If
ElseIf parens = 0 Then
' See if this is an operator.
If ch = "^" Or ch = "*" Or _
ch = "/" Or ch = "\" Or _
ch = "%" Or ch = "+" Or _
ch = "-" _
Then
' An operator after an operator
' is unary.
next_unary = True
Select Case ch
Case "^"
If best_prec >= PREC_POWER Then
best_prec = PREC_POWER
best_pos = pos
End If
Case "*", "/"
If best_prec >= PREC_TIMES Then
best_prec = PREC_TIMES
best_pos = pos
End If
Case "\"
If best_prec >= PREC_INT_DIV Then
best_prec = PREC_INT_DIV
best_pos = pos
End If
Case "%"
If best_prec >= PREC_MOD Then
best_prec = PREC_MOD
best_pos = pos
End If
Case "+", "-"
' Ignore unary operators
' for now.
If (Not is_unary) And _
best_prec >= PREC_PLUS _
Then
best_prec = PREC_PLUS
best_pos = pos
End If
End Select
End If
End If
is_unary = next_unary
Next pos
' If the parentheses count is not zero,
' there's a ')' missing.
If parens <> 0 Then
Err.Raise vbObjectError + 1002, _
"EvaluateExpr", "Missing ) in '" & _
expr & "'"
End If
' Hopefully we have the operator.
If best_prec < PREC_NONE Then
lexpr = Left$(expr, best_pos - 1)
rexpr = Right$(expr, expr_len - best_pos)
Select Case Mid$(expr, best_pos, 1)
Case "^"
EvaluateExpr = _
EvaluateExpr(lexpr) ^ _
EvaluateExpr(rexpr)
Case "*"
EvaluateExpr = _
EvaluateExpr(lexpr) * _
EvaluateExpr(rexpr)
Case "/"
EvaluateExpr = _
EvaluateExpr(lexpr) / _
EvaluateExpr(rexpr)
Case "\"
EvaluateExpr = _
EvaluateExpr(lexpr) \ _
EvaluateExpr(rexpr)
Case "%"
EvaluateExpr = _
EvaluateExpr(lexpr) Mod _
EvaluateExpr(rexpr)
Case "+"
EvaluateExpr = _
EvaluateExpr(lexpr) + _
EvaluateExpr(rexpr)
Case "-"
EvaluateExpr = _
EvaluateExpr(lexpr) - _
EvaluateExpr(rexpr)
End Select
Exit Function
End If
' If we do not yet have an operator, there
' are several possibilities:
'
' 1. expr is (expr2) for some expr2.
' 2. expr is -expr2 or +expr2 for some expr2.
' 3. expr is Fun(expr2) for a function Fun.
' 4. expr is a primitive.
' 5. It's a literal like "3.14159".
' Look for (expr2).
If Left$(expr, 1) = "(" And Right$(expr, 1) = ")" Then
' Remove the parentheses.
EvaluateExpr = EvaluateExpr(Mid$(expr, 2, expr_len - 2))
Exit Function
End If
' Look for -expr2.
If Left$(expr, 1) = "-" Then
EvaluateExpr = -EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If
' Look for +expr2.
If Left$(expr, 1) = "+" Then
EvaluateExpr = EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If
' Look for Fun(expr2).
If expr_len > 5 And Right$(expr, 1) = ")" Then
lexpr = LCase$(Left$(expr, 4))
rexpr = Mid$(expr, 5, expr_len - 5)
Select Case lexpr
Case "sin("
EvaluateExpr = Sin(EvaluateExpr(rexpr))
Exit Function
Case "cos("
EvaluateExpr = Cos(EvaluateExpr(rexpr))
Exit Function
Case "tan("
EvaluateExpr = Tan(EvaluateExpr(rexpr))
Exit Function
Case "sqr("
EvaluateExpr = Sqr(EvaluateExpr(rexpr))
Exit Function
End Select
End If
' See if it's a primitive.
On Error Resume Next
value = Primitives.Item(expr)
status = Err.Number
On Error GoTo 0
If status = 0 Then
EvaluateExpr = CSng(value)
Exit Function
End If
' It must be a literal like "2.71828".
On Error Resume Next
EvaluateExpr = CSng(expr)
status = Err.Number
On Error GoTo 0
If status <> 0 Then
Err.Raise status, _
"EvaluateExpr", _
"Error evaluating '" & expr & _
"' as a constant."
End If
End Function
自己看吧,來自vbworld
這是一個核心函數,本人未修改
❺ 如何用C語言編寫冪運算的代碼
#include"stdio.h"
#include"math.h"
intmain()
{
floata,b;
printf("請輸入整數:");
scanf("%f",&a);
printf("請輸入冪:");
scanf("%f",&b);
printf("結果:%g ",pow(a,b));
}
❻ vb加法計算代碼
PrivateSub Command1_Click()Dim a As Integer,b As
Integera=Val(Text1.Text)b=Val(Text2.Text)Text3.Text=a+bEnd Sub在這段VB程序中,「a=Val(Text1.Text)b=Val(Text2.Text)」是加法的代碼。
代碼如下:
PrivateSub Command1_Click()
Label2.Caption = Val(Text1.Text) + Val(Text2.Text)
End Sub
PrivateSub Command2_Click()
Text1.Text = "": Text2.Text = "": Label2.Caption = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
(6)運算代碼擴展閱讀
OptionExplicit
PrivateSubCombo1_Change()
EndSub
PrivateSubCommand1_Click()
IfIsNumeric(Text1.Text)AndIsNumeric(Text2.Text)Then
SelectCaseCombo1.Text
Case"+"
Text3.Text=CLng(Text1.Text)+CLng(Text2.Text)
Case"-"
Text3.Text=CLng(Text1.Text)-CLng(Text2.Text)
Case"×"
Text3.Text=CLng(Text1.Text)*CLng(Text2.Text)
Case"÷"
IfCLng(Text2.Text)<>0ThenText3.Text=CLng(Text1.Text)/CLng(Text2.Text)ElseMsgBox"出數不能為0",vbOKOnly,"提示"
CaseElse
MsgBox"請選擇運算方式",vbOKOnly,"提示"
EndSelect
Else
MsgBox"請輸入數字",vbOKOnly,"提示"
EndIf
EndSub
PrivateSubForm_Load()
Combo1.AddItem"+"
Combo1.AddItem"-"
Combo1.AddItem"×"
Combo1.AddItem"÷"
EndSub
❼ c語言代碼運算問題
除了最後兩個printf,其它的都是%d,對浮點數以整數輸出自然是0,在輸出前應強制轉型,如printf("%d",(int)b);