我的位置:首頁 >  3C科技▼ >  程式語言
  1. 開箱新訊
  2. 行動裝置
  3. 程式語言
  4. 電腦園區

C++求救……

G  粉腸 發表時間:
題目如下

「使用do… while 迴圈 以scanf()去讀入10筆由加(+)減(-)乘(*)除(/)運算符號相接之整數數值,且數值後之終止符號為!並於迴圈中以switch … case判斷運算符號,並依序進行浮點數運算運算與印出運算式與結果
**不用先乘除後加減 格式為精準至小數點以下2位
輸入:
12
*
34
+
56
-
78
*
90
-
98
/
76
-
54
+
32
/
10

輸出:12*34+56-78*90-98/76-54+32/10= 43.38」

然後這是目前寫出來的東西……

#include<stdio.h>
#include<stdlib.h>
int main( void ){
    int a;
    char b;
    float t;
    scanf("%d",&a);
    t = a;
    scanf("%c",&b);
    do{
        scanf("%d",&a);
        switch( b ){
                case '+':
                     t = t + a;
                     break;
                case '-':
                     t = t - a;
                     break;
                case '*':
                     t = t * a;
                     break;
                case '/':
                     t = t / a;
                     break;
                default:
                        break;
                        }
        scanf("%c",&b);
                        }while( b != '!' );
     printf("%.2f\n",t);
     system("pause");
     }
但是算出來的結果是48.38……不知道哪裡錯了囧。
然後我不知道怎麼把輸入的東西變成完整算式列出來QAQ
B  工程師IN台論之星 發表時間:2014-12-10 09:41:26 (1樓)
scanf("%d",&a);
t = a;
scanf("%c",&b);

enter算一個鍵,所以你的B會抓到enter

===========================================
#include "stdio.h"
#include "stdlib.h"
#include "stdafx.h"
#include <string>
#include <iostream> 
#include <sstream>
using namespace std;


template <class T> 
std::string ConvertToString(T);


int main( void ){
    string result = "";
    int a;
    char b;
    float t;
    scanf("%d",&a);
    result += ConvertToString(a);
    t = a;
    getchar();
    scanf("%c",&b);
    result += b;
    do{
        getchar();
        scanf("%d",&a);
        result += ConvertToString(a);
        switch( b ){
                case '+':
                     t = t + a;
                     break;
                case '-':
                     t = t - a;
                     break;
                case '*':
                     t = t * a;
                     break;
                case '/':
                     t = t / a;
                     break;
                default:
                        break;
        }
        getchar();
        scanf("%c",&b);
        result += b;
     }while( b != '!' );
    result = result.substr(0,result.length()-1);
    cout << result << "=";
    printf("%.2f\n",t);
     
    system("pause");
    return 0;
}


template <class T> 
std::string ConvertToString(T value) {
  std::stringstream ss;
  ss << value;
  return ss.str();
}



本帖最後由 B  工程師IN台論之星 於 2014-12-10 10:19:55 編輯
檢舉引用編輯刪除

G  粉腸 發表時間:2014-12-17 09:54:34 (2樓)
QUOTE:

作者:B  工程師IN台論之星 回覆日期:2014-12-10 17:41:26
scanf("%d",&a);
t = a;
scanf("%c",&b);

enter算一個鍵,所以你的B會抓到enter

===========================================
#include "stdio.h"
#include "stdlib.h"
#include "stdafx.h"
#include <string>
#include <iostream> 
#include <sstream>
using namespace std;


template <class T> 
std::string ConvertToString(T);


int main( void ){
    string result = "";
    int a;
    char b;
    float t;
    scanf("%d",&a);
    result += ConvertToString(a);
    t = a;
    getchar();
    scanf("%c",&b);
    result += b;
    do{
        getchar();
        scanf("%d",&a);
        result += ConvertToString(a);
        switch( b ){
                case '+':
                     t = t + a;
                     break;
                case '-':
                     t = t - a;
                     break;
                case '*':
                     t = t * a;
                     break;
                case '/':
                     t = t / a;
                     break;
                default:
                        break;
        }
        getchar();
        scanf("%c",&b);
        result += b;
     }while( b != '!' );
    result = result.substr(0,result.length()-1);
    cout << result << "=";
    printf("%.2f\n",t);
     
    system("pause");
    return 0;
}


template <class T> 
std::string ConvertToString(T value) {
  std::stringstream ss;
  ss << value;
  return ss.str();
}





感謝工程師大大QAQ

檢舉引用編輯刪除