2017-03-25 68 views
0
import java.util.Scanner; 
import java.io.*; 
import java.util.InputMismatchException; 
/*: 
NAME: Integrated Solution 
Create a C++ or Java program for accepting an integer (n) and generate following point series: 
x: n integer points from 0 to (n-1) 
y: y=x*x 

Design an effective mechanism for plotting these with minimal or nil effort from the user.*/ 

public class Integrated_Solution 
{ 
    private static final String FILENAME = "122.py"; 
    public static void main (String[] args) 
    { 
    Scanner sc = new Scanner (System.in); 
    int n = -1; 
    boolean flag = false; 
    System.out.println ("Enter value of n: "); 
    while(flag==false) 
    { 
     try 
     { 
     n = sc.nextInt();    // Inputting range from user 
     if (n >= 0) 
     flag=true; 
     else 
     System.out.println("Enter a positive value."); 
     } 
     catch (InputMismatchException e) // Exception Handling For any mismatch types (float, string etc.) 
     { 
     System.out.println ("Enter a positive integer value. \n"); 
     sc.nextLine(); 
     } 
    } 
    try 
    { 
     FileWriter fw = new FileWriter (FILENAME); // Create a new file with the given filename 
     fw.write("from pylab import linspace, plot, stem, show, title, xlabel, ylabel\n"+      
       "n = " + n + "\n" + 
       "x = linspace (0, n - 1, n)\n"+ 
       "figure (num = None, figsize = (16,8), dpi = 80, facecolor='w', edgecolor='k')\n" 
       "title ('Plot for graph $f(x) = x^2$')\n" + 
       "xlabel ('$x$')\n" + 
       "ylabel ('$x^2$')\n" + 
       "plot (x, x * x, 'b')\n"+  
       "stem (x, x * x, linefmt = 'g-', markerfont= 'bo')\n"+              
       "show()\n");    
     fw.close(); 
    } 
    catch (IOException e) 
    { 
     System.out.println("Could not write file !!" + e.getMessage()); 
    } 
    try 
    { 
     Runtime.getRuntime().exec ("python " + FILENAME); // Execute the created python file 
    } 
    catch (IOException e) 
    { 
     System.out.println("Could not execute script. Call to runtime failed. " + e.getMessage()); 
    } 
    sc.close(); 
    } 
} 

這是我的Java解決方案。不過,我希望在C++程序中實現相同的異常處理機制。什麼是C++異常處理機制相當於Java的InputMismatchException和IOException異常處理

#include <iostream> 
#include <cstdlib> 
#include <fstream> 
using namespace std; 
int main() 
{ 
    ofstream outputFile; 
    outputFile.open("122.py");  
    cout << "Enter the value of n : "; 
    int n; 
    cin >> n; 
    outputFile << "from pylab import linspace, plot, stem, show, title, xlabel, ylabel, figure\n" <<      
       "n = " << n << "\n" << 
       "x = linspace (0, n - 1, n)\n"<< 
       "figure (num = None, figsize = (16,8), dpi = 80, facecolor='w', edgecolor='k')\n" << 
       "title ('Plot for graph $f(x) = x^2$')\n" <<    
       "xlabel ('$x$')\n" << 
       "ylabel ('$x^2$')\n" << 
       "plot (x, x * x, 'b')\n"<< 
       "stem (x, x * x, linefmt = 'g-', markerfont= 'bo')\n" <<    
       "show()\n"; 
    outputFile.close(); 
    system ("python 122.py"); 
} 

以上是我的C++程序減去異常處理。爲了達到與我的Java程序相同的安全感,我應該做出什麼樣的改變?

此外,任何有關改進我的Java或C++代碼的建議(即使它與每個問題的異常處理無關)都將不勝感激。 在此先感謝。

+0

當您調用Python腳本時,Python部分與問題本身無關,所以您不應該有'python'標記。 –

+0

至於等價物,沒有。 Java和C++是兩種非常不同的語言,它們完全不同地處理例外情況。例如,在C++中,拋出的異常非常昂貴。這就是爲什麼它很少用於驗證或簡單錯誤處理等事情。 –

+0

如果你想學習如何處理來自'system'函數的錯誤,我推薦例如[這個'std :: system'參考](http://en.cppreference.com/w/cpp/utility/program/system)。或[MSDN參考](https://msdn.microsoft.com/en-us/library/277bwbdz.aspx)或[POSIX參考](http://pubs.opengroup.org/onlinepubs/9699919799/functions/ system.html)獲取特定於平臺的信息。 –

回答

0

你可以很容易地設置何時拋出異常。最常見的情況是,當std::istream::failbitistream::badbit(要誠實std::ios::badbitstd::ios::failbit,他們繼承):

int main() { 
    int n; 
    std::cin.exceptions(std::istream::failbit | std::istream::badbit); 
    try { 
    std::cin >> n; 
    } catch(istream::failure) { 
    std::cout << "failed to read value" << std::endl; 
    } 
    return 0; 
} 

同樣的道理也適用於std::ifstream因爲它也std::istream後繼承。

要檢查system()函數中的失敗,建議遵循MSDN for Windows和POSIX for Linux,Mac和其他基於Unix /靈感的系統。 MSDN,POSIX - 正如你在這裏看到的,行爲是不同的,這取決於你使用的系統。