|登录 |注册

查看: 1735|回复: 8
打印 上一主题 下一主题

calculator program 请帮忙!!

[复制链接]
狂天使
2009-12-5 01:49 AM
#include <cstdlib>
#include <iostream>

using namespace std;

各位大大,我想要input 一个file进来program然后run calculator 该如何?

例子:我有一个file input.dat 里面资料有

93
50
+
70
*

然后我要让他变成(93+50)*70

那该如何呢?请给下意见谢谢!!


int main()
{
    double num;
    double num2;
    char choice;
    for (;;){
     do {
  cout<<"Welcome to thejoshcalculator. V1.5\n";
    cout<<"Please choose an option by entering the number, press q to quit\n";
    cout<<"1 - Addition\n";
    cout<<"2 - Subtraction\n";
    cout<<"3 - Division\n";
    cout<<"4 - Multiplication\n";

    cin>>choice;
    } while ( choice < '1' || choice > '7' && choice != 'q');
    if (choice == 'q') break;
    switch (choice) {
           case '1':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another number to be added\n";
                cin>>num2;
                cout<<num + num2;
                cout<<"\n";
                break;
           case '2':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another number to be subtracted\n";
                cin>>num2;
                cout<<num - num2;
                cout<<"\n";
                break;
           case '3':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another one to be divided\n";
                cin>>num2;
                cout<<num / num2;
                cout<<"\n";
                break;
           case '4':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another one to be multiplied\n";
                cin>>num2;
                cout<<num * num2;
                cout<<"\n";
                break;
     
           default:
                    cout<<"That is not an option";
                    
                }

}
return 0;


}
Super-Tomato
2009-12-5 03:12 PM
原帖由 狂天使 于 2009-12-5 01:49 AM 发表
#include
#include

using namespace std;

各位大大,我想要input 一个file进来program然后run calculator 该如何?

例子:我有一个file input.dat 里面资料有

93
50
+
70
*

然后我要让 ...


你的 coding 只是讓使用者輸入選擇,而你上方又提到 input.dat, 不确定你到底要用哪种方式
如果是讀取檔案的話,你就必须 fgets 或 c++ 的 fstream 等來取得 input.dat 的内容,然后进行 parenthesis 分析及做演算。
狂天使
2009-12-5 10:55 PM
好的,我先测试谢谢!!
狂天使
2009-12-7 02:12 AM
如果我要运算,是不是在is_open 过后里面运算

#include <iostream>
#include <fstream>

using namespace std;

int main () {

  fstream filestr;
  filestr.open ("myfile.txt");
  if (filestr.is_open())
  {
    cout << "succesful open";
    filestr << "File successfully open";
    filestr.close();
  }
  else
  {
    cout << "Error opening file";
  }
  system("pause");
  return 0;

  }
Super-Tomato
2009-12-7 06:01 AM
原帖由 狂天使 于 2009-12-7 02:12 AM 发表
如果我要运算,是不是在is_open 过后里面运算

#include
#include

using namespace std;

int main () {

  fstream filestr;
  filestr.open ("myfile.txt");
  if (filestr.is_open())
  {
  ...



這個當然是在條件成立中才执行,不然就没意义且會發生错误
狂天使
2009-12-8 11:01 PM
那我该如何同时store 在两个variable呢?

C可以用

fscanf (myfilename, %f%f, temp1,temp2)
当读取我的file时候第一个,和第二个的资料会立刻store在两个 variable

那C++语言,我该用哪一个 function

可以做到以上的效果呢?

谢谢

[ 本帖最后由 狂天使 于 2009-12-8 11:07 PM 编辑 ]

回复 #6 狂天使 的帖子

宅男-兜着走
2009-12-8 11:04 PM
store 成两个 variable 不如store 在array 好过
Super-Tomato
2009-12-9 03:24 AM
原帖由 狂天使 于 2009-12-8 11:01 PM 发表
那我该如何同时store 在两个variable呢?

C可以用

fscanf (myfilename, %f%f, temp1,temp2)
当读取我的file时候第一个,和第二个的资料会立刻store在两个 variable

那C++语言,我该用哪一个 function ...



在 C++ 中也能使用 C 的 fscanf, 只要把 C 的 stdio.h 給 include 進來就没問題了
Super-Tomato
2009-12-9 03:25 AM
原帖由 宅男-兜着走 于 2009-12-8 11:04 PM 发表
store 成两个 variable 不如store 在array 好过



C 和 Java 的方式不一樣,如果是 Java 只要 toString() 之後储存在個 String 中寫入即可
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2025-3-10 09:47 PM , Processed in 0.119564 second(s), 25 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部