-2

スクリプトを見てみましょう。テロップを計算して答えを出力します。ご覧のとおり、現在はプラス (+) しか計算できません。私は C コーディングをしたことがないので、乗算 (X または *)、マイナス (-)、および除算 (: または /) を計算する方法がわかりません。

基本的に、誰かが乗算、マイナス、除算を含める方法を教えてくれることを望んでいました.

#include <stdio.h>
#include <stdlib.h>

int total = 0;
void telop(char*s) {
char sum[1024];

if (s[0]==0) return;
if (s[0]=='+')

{
      strncpy(sum, &s[1],1);
      total += atoi(sum);
}
    telop(&s[2]);
}

int main()

{
    telop("+1+2+3");
    printf("%d", total);
}
4

1 に答える 1

2

「-」の「+」を変更すると計算され、「/」または「*」と一緒に使用することもできます

void telop (char*s){
    char som[1024];
    if(s[0]==0) return;

    if(s[0]=='+')
    {   strncpy (som, &s[1],1);
        total += atoi(som); }
    if(s[0]=='-')
    {   strncpy (som, &s[1],1);
        total -= atoi(som); }
    if(s[0]=='/')
    {   strncpy (som, &s[1],1);
        total /= atoi(som); }
    if(s[0]=='*')
    {   strncpy (som, &s[1],1);
        total *= atoi(som); }



    telop(&s[2]);
}
于 2013-02-27T13:18:46.637 に答える