抱歉小妹還是新手沒辦法回覆上一篇給我幫助的人
謝謝大家很溫暖
目前碰到一個問題是要做一個自動計算的四則運算
我看網路上都是教學如何做一個輸入的計算機
想問一下以下程式碼該如何修改才會自己跑
先說謝謝大家?
int main() {
double a, b;
char op;
double answer;
printf("Please enter your number: ");
scanf("%lf%c%lf", &a, &op, &b);
if (op == '+') {
answer = a + b;
}
else if (op == '-') {
answer = a - b;
}
else if (op == '*') {
answer = a * b;
}
else {
answer = a / b;
}
printf("Answer: %lf\n", answer);
return 0;
}
c的話你要自己做個簡化的直譯器.........
如果是c#的話,這裡有範例:
https://dotnetfiddle.net/B9ocfc
using System;
using org.matheval;
public class Program
{
public static void Main()
{
Expression expression = new Expression("3*(4+5)/6*(7+8)-9");
Decimal salary = expression.Eval<Decimal>();
Console.WriteLine(salary);
}
}
這是我很久以前寫的四則運算(含括號)的程式,OBJECT PASCAL語法,上班時間不太好花太多心思在外務上,所以沒有想現在改成C語法,參考看看吧
function formulax(calcstr:string):double;
var
x : integer;
ssign: string ;
calclist: TStringlist;
begin
result:=0;
calclist:=TStringlist.Create;
calcstr:=stringreplace(calcstr,' ','',[rfReplaceAll]);
calcstr:=stringreplace(calcstr,#13#10,'',[rfReplaceAll]);
calcstr:=stringreplace(calcstr,'*','|*'#13,[rfReplaceAll]);
calcstr:=stringreplace(calcstr,'/','|/'#13,[rfReplaceAll]);
calcstr:=stringreplace(calcstr,'+','|+'#13,[rfReplaceAll]);
calcstr:=stringreplace(calcstr,'-','|-'#13,[rfReplaceAll]);
calclist.text:=calcstr;
if calclist.Count>0 then begin
while calclist.Count>1 do begin
for x:= 0 to calclist.count - 1 do begin
if split(calclist[x],'|',1)='*' then begin
calclist[x]:=format('%0.9f',[strtofloatdef(split(calclist[x],'|',0),0)*strtofloatdef(split(calclist[x+1],'|',0),1)]);
ssign:=split(calclist[x+1],'|',1);
calclist.Delete(x+1);
if ssign<>'' then calclist[x]:=calclist[x]+'|'+ssign;
break;
end;
if (split(calclist[x],'|',1)='/') then begin
try
calclist[x]:=format('%0.9f',[strtofloatdef(split(calclist[x],'|',0),0)/strtofloatdef(split(calclist[x+1],'|',0),1)]);
except
Calclist[x]:='0';
end;
ssign:=split(calclist[x+1],'|',1);
calclist.Delete(x+1);
if ssign<>'' then calclist[x]:=calclist[x]+'|'+ssign;
break;
end;
if (split(calclist[x],'|',1)='+')and(pos('|*',calclist.text)=0)and(pos('|/',calclist.text)=0) then begin
calclist[x]:=format('%0.9f',[strtofloatdef(split(calclist[x],'|',0),0)+strtofloatdef(split(calclist[x+1],'|',0),1)]);
ssign:=split(calclist[x+1],'|',1);
calclist.Delete(x+1);
if ssign<>'' then calclist[x]:=calclist[x]+'|'+ssign;
break;
end;
if (split(calclist[x],'|',1)='-')and(pos('|*',calclist.text)=0)and(pos('|/',calclist.text)=0) then begin
calclist[x]:=format('%0.9f',[strtofloatdef(split(calclist[x],'|',0),0)-strtofloatdef(split(calclist[x+1],'|',0),1)]);
ssign:=split(calclist[x+1],'|',1);
calclist.Delete(x+1);
if ssign<>'' then calclist[x]:=calclist[x]+'|'+ssign;
break;
end;
end;
end;
result:=strtofloatdef(calclist[0],0);
end;
calclist.free;
end;
???會動啊
如果要一直跑可以參考
#include <stdio.h>
int main() {
double a, b;
char op;
double answer;
while(1){
printf("Please enter your number: ");
scanf("%lf%c%lf", &a, &op, &b);
if (op == '+') {
answer = a + b;
}else if (op == '-') {
answer = a - b;
}else if (op == '*') {
answer = a * b;
}else {
answer = a / b;
}
printf("Answer: %lf\n", answer);
if(a == 100){
break;
}
}
return 0;
}
不好意思我不知道為啥跑不動
https://imgur.com/a/A084did
首先 請你先看一看這邊Markdown 常用語法
另外 圖實在太模糊了 你可以直接上傳it幫 不用透過第三方
其次 我不確定是不是你們老師規定要用visual studio編輯
我慣用的C/C++語言編輯器是 DEV C++
條件容許的話換了吧 經典好用
vscode 跟 visual studio 有相容問題要解決 對新手相對不友善
三 你可以檢核你的debugger(下面) 他有告訴你 在visual studio中
他認為scanf有溢位風險 建議你改成scanf_s
你將錯誤資訊餵google 其實可以發現更完善的解答
不想換編輯器可以在頂部加一行
#define _CRT_SECURE_NO_WARNINGS
(無驗證)
學習新語言遇到問題是正常的
所以要學會如何找出問題跟解決它
加油!!
謝謝柳丁大
#include<stdio.h>
int main(int argc, char *argv[]) {
double a, b;
char op;
double answer;
printf("\n請輸入數字(1)):");
scanf("%lf",&a);
printf("\n請輸入 + - x ÷:");
scanf("%s",&op);
printf("\n請輸入數字(2)):");
scanf("%lf",&b);
if (op == '+') {
answer = a + b;
}
if (op == '-') {
answer = a - b;
}
if (op == '*') {
answer = a * b;
}
if (op == '/') {
answer = a / b;
}
printf("\n答案: %lf\n", answer);
return 0;
}
C語言四則運算,程式碼可以參考看看,感恩。