2012-04-23 108 views
0

@ sarnold確定這裏是我的更新試圖按照你給我的指導我試圖消除電源移動原型的類型和僅具有彼此的一個我把範圍縮小到包含一個INT主聲明每一個人函數和使用void,而不是int,每個函數都希望這是正確的舉措。多種功能

#include <iostream> 
#include <cmath> 
#include <stdio.h> 
#include <string> 

using namespace std; 


void function1() 
{ 
    //Enter Variables 
    int number; 
    cout << "Enter Variable a: "; 
    cin >> number; 
    int a = number; 


    cout << "Enter Variable b: "; 
    cin >> number; 
    int b = number; 
    cout << "Enter Variable c: "; 
    cin >> number; 
    int c = number; 
    if (a > b && a > c); 
    return a; 
    cout << "This is your highest number(" << a << ")"; 
    if (b > a && b > c); 
    return b; 
    cout << "This is your highest number(" << b << ")"; 
    if (c > a && c > B); 
    return c; 
    cout << "This is your highest number(" << c << ")"; 

} 

void function2() 
{ 
    // Prompt User for Integer 

    int number; 
    cout << "Enter integer 1: "; 
    cin >> number; 
    int a = number; 

    cout << "Enter integer 2: "; 
    cin >> number; 
    int b = number; 

    if (a != B) 
     cout << "Continue"; 

    else 
     cout << "Number 1 or 2 is the same!"; 

    cout << "Enter integer 3: "; 
    cin >> number; 
    int c = number; 

    if ((a != B) != c) 
     cout << "Continue"; 
    else 
     cout << "Number 3 is the same as number 1 or 2!"; 
    cout << "Enter integer 4: "; 
    cin >> number; 
    int d = number; 

    if (((a != B) != c) != d) 
     cout << "numbers are all different"; 
    else 
     cout << "Number 3 is the same as number 1, 2, or 3!"; 
    cout << "Thank you enter another number?\n\n"; 
    cin >> number; 
    return 0; 
} 

std::string numerals = "VXLCDM"; 
void function3() 
{ 
    char roman_Numeral; 
    int arabic_Numeral = 0; 
    cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : "; 
    while(cin.get(roman_Numeral)) 
    { 
     if(roman_Numeral == 'M') 
      arabic_Numeral = arabic_Numeral + 1000; 
     else if(roman_Numeral == 'D') 
     { 
      roman_Numeral = cin.peek(); 
      if(numerals.find(roman_Numeral, 5) != std::string::npos) 
      { 
       arabic_Numeral = arabic_Numeral - 500; 
       continue; 
      } 
      else 
      { 
       arabic_Numeral = arabic_Numeral + 500; 
       continue; 
      } 
     } 
     else if(roman_Numeral == 'C') 
     { 
      roman_Numeral = cin.peek(); 
      if(numerals.find(roman_Numeral, 4) != std::string::npos) 
      { 
       arabic_Numeral = arabic_Numeral - 100; 
       continue; 
      } 
      else 
      { 
       arabic_Numeral = arabic_Numeral + 100; 
       continue; 
      } 
     } 
     else if(roman_Numeral == 'L') 
     { 
      roman_Numeral = cin.peek(); 
      if(numerals.find(roman_Numeral, 3) != std::string::npos) 
      { 
       arabic_Numeral = arabic_Numeral - 50; 
       continue; 
      } 
      else 
      { 
       arabic_Numeral = arabic_Numeral + 50; 
       continue; 
      } 
     } 
     else if(roman_Numeral == 'X') 
     { 
      roman_Numeral = cin.peek(); 
      if(numerals.find(roman_Numeral, 2) != std::string::npos) 
      { 
       arabic_Numeral = arabic_Numeral - 10; 
       continue; 
      } 
      else 
      { 
       arabic_Numeral = arabic_Numeral + 10; 
       continue; 
      } 
     } 
     else if(roman_Numeral == 'V') 
     { 
      roman_Numeral = cin.peek(); 
      if(numerals.find(roman_Numeral, 1) != std::string::npos) 
      { 
       arabic_Numeral = arabic_Numeral - 5; 
       continue; 
      } 
      else 
      { 
       arabic_Numeral = arabic_Numeral + 5; 
       continue; 
      } 
     } 
     else if(roman_Numeral == 'I') 
     { 
      roman_Numeral = cin.peek(); 
      if(numerals.find(roman_Numeral) != std::string::npos) 
      { 
       arabic_Numeral = arabic_Numeral - 1; 
       continue; 
      } 
      else 
      { 
       arabic_Numeral = arabic_Numeral + 1; 
       continue; 
      } 
     } 
     else 
      break; 
    } 
    cout << arabic_Numeral << endl; 
    return 0; 
} 
void input (double &); 
void convert (double radius, int & feet, double & total); 
void output (int, double, double); 
void function4() 
{ 
    double total, radius; 
    int feet; 
    char yn; 
    do 
    { 
     input (radius); 
     convert (radius, feet, total); 
     output (feet, total, radius); 
     cout << "Enter a Different radius? (y or n)\n"; 
     cin >> yn; 
     cout << "\n\n"; 
    } 
    while (yn == 'y' || yn == 'Y'); 
    system("pause"); 
    return 0; 
} 
void input(double & radius) 
{ 
    cout << "---------------------------Re Enter Radius-------------------------\n\n"; 
    cout << "Enter Radius:\n"; 
    cin >> radius; 
    cout << "\n"; 
} 
void convert(double radius, int & feet, double & total) 
{ 
    //======================================================= 
    total = 3.14 * radius * radius * radius * 4/3; 
} 
//======================================================== 
void output(int feet, double total, double radius) 
{ 
    cout << "---------------------------Re Enter Radius-------------------------\n\n"; 
    cout << "The volume is: " << total << " \n\n"; 
    cout << "--------------------------- Equation -------------------------\n\n"; 
    cout << "The Equation with (" << radius << ") being:\n\n"; 
    cout << "4/3 * 3.14 * (" << radius << ")^3 = " << total << "\n\n"; 
    cout << "---------------------------  Again? -------------------------\n\n"; 
} 
void swap(int &x, int &y) 
    int number; 

void function5() 

{ 

    cout << "x is "; 
    cin >> number; 
    int x = number; 

    cout << "y is "; 
    cin >> number; 
    int y = number; 
    swap(x, y); 
    cout << "Swapped the first number is now(" << x << ")and the second is now (" <<y>> ")"; 

    return 0; 
} 
int leap (int year); 
void function6 (void){ 
    int month,day,year,dm,dn,leap; 

    printf("enter the month:"); 
    scanf("%d",&month); 

    printf("enter the day:"); 
    scanf("%d",&day); 

    printf("enter the year:"); 
    scanf("%d",&year); 

    if (leap==0) 
    { if(month==1) 
     dm=0; 
     if(month==2) 
      dm=31; 
     if(month==3) 
      dm=59; 
     if(month==4) 
      dm=90; 
     if(month==5) 
      dm=120; 
     if(month==6) 
      dm=151; 
     if(month==7) 
      dm=181; 
     if(month==8) 
      dm=212; 
     if(month==9) 
      dm=243; 
     if(month==10) 
      dm=273; 
     if(month==11) 
      dm=304; 
     if(month==12) 
      dm=334;} 

    else 
    { if(month==1) 
     dm=0; 
     if(month==2) 
      dm=31; 

     if(month==3) 
      dm=60; 
     if(month==4) 
      dm=91; 
     if(month==5) 
      dm=121; 
     if(month==6) 
      dm=152; 
     if(month==7) 
      dm=182; 
     if(month==8) 
      dm=213; 
     if(month==9) 
      dm=244; 
     if(month==10) 
      dm=274; 
     if(month==11) 
      dm=304; 
     if(month==12) 
      dm=335;} 

     dn=dm+day; 

     printf("the day number is :%d",dn); 

     return 0; 
} 

int leap (int year){ 
    if((year%100== 0 && year%400==0)|| (year%4==0)) 

     return 1; 
    else 
     return 0;} 

int main(int argc, char *argv[], int year, int leap, int number, int arabic_Numeral) 

{ 
    function1(); 
    function2(); 
    function3(); 
    function4(); 
    function5(); 
    function6(); 
    return 0; 
} 
+0

究竟是什麼時候的問題你試圖將它們合併成一個主? – keety 2012-04-23 02:03:18

+0

你是什麼意思「同時運行」?我看不到這些函數在同一個文件中的原因。我會首先命名這些函數來描述它們的行爲,而不是將它們稱爲main()。 – Johnsyweb 2012-04-23 02:07:02

+1

@Johnsyweb我認爲他的意思是這些都是單獨的家庭作業,他需要將他們全部放在一起... – climbage 2012-04-23 02:11:31

回答

1

那麼對於一個你有int main()定義三次。你需要給每個人一個不同的名字。

此外,您不需要複製#includeusing語句,只需在開始時使用它們一次。

我會建議將您的原型在類的開頭那也僅僅是爲了便於閱讀。

1
if (a > b && a > c); 
return a; 
cout << "This is your highest number(" << a << ")"; 

這三行總結了許多在你的代碼中的問題:

  • if條件是無用的:

    if (foo); 
    

    只執行foo測試;分號括號的代碼年底來執行,如果foo是真實的:

    if (foo) 
        /* then code */ ; 
    
  • return a聲明防止代碼的其餘部分,包括cout線後立即執行。

如果您使用文本編輯器,如vim,可以做語法感知縮進你有些小問題會更顯而易見。編輯器並不總是和編譯器一樣好,但如果你注意他們如何佈置你的代碼,他們可以幫你節省一些時間。

此外,至少在代碼離職後return應該由你的編譯器向你報告。如果沒有,請打開警告。 (在gcc-Wall -Wextra是很好的。)


更新

爲了把每一個這些成一個單一的文件,並呼籲他們每個反過來,你需要稍微重新格式化。每個程序目前是獨立的,幾乎完全在main()程序中運行。這在玩具程序中沒有問題,但是使您在一個可執行文件中全部調用它們的工作複雜化。

目前,在僞代碼你有幾個文件是這樣的:

PROG 1:

main() { 
    /* ask for input */ 
    /* run the routine */ 
    /* maybe call functions */ 
    /* output results */ 
    /* quit */ 
} 

PROG 2:

main() { 
    /* ask for input */ 
    /* run the routine */ 
    /* maybe call functions */ 
    /* output results */ 
    /* quit */ 
} 

你需要重新 - 像這樣排列你的程序:

程序:

void run_routine_one(); /* I think C++ forbids a (void) prototype here */ 
void run_routine_two(); 

main() { 
    run_routine_one(); 
    run_routine_two(); 
    /* quit */ 
} 

void run_routine_one() { 
    /* ask for input */ 
    /* run the routine */ 
    /* maybe call functions */ 
    /* output results */ 
} 

void run_routine_two() { 
    /* ask for input */ 
    /* run the routine */ 
    /* maybe call functions */ 
    /* output results */ 
} 

給定這種類型的模板,你幾乎可以複製和粘貼東西在一起;事實上,你可能希望這樣做,儘管全局變量會使問題複雜化。 (還有一個理由是沒有充分的理由阻止全球存儲)

如果您在項目到期之前有時間了,我強烈建議您稍微退一步,重新編碼代碼 - 您的例程當前正在混合輸入,計算和輸出,如果將計算的輸入和輸出分開,通常代碼更易於測試和維護。如果您提供一些幫手函數,您的輸入和輸出將會更容易。什麼這個速寫可能看起來像:

int prompt_for_int(string) 
{ 
    int ret; 
    cout << string; 
    cin >> ret; 
    return ret; 
} 

int days_in_month(int month, int year) 
{ 
    int leap = is_leap_year(year); 
    /*     J F M A M J J A S O N D */ 
    int days[2][12] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
         {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; 
    if (month < 0 || month > 11 || year < 1753) 
     return -1; 

    return days[leap][month]; 
} 

void program_one() 
{ 
    int month, year, days; 

    month = prompt_for_int("Enter month, 1 for January, 2 for February...\n"); 
    month -= 1; /* zero-indexed month */ 

    year = prompt_for_int("Enter year, not before 1754...\n"); 

    days = days_in_month(month, year); 
    cout << "There are " << days << " in " << month + 1 
     << " " << year << nl; 
    /* I'm no C++ expert -- perhaps >> nl isn't idiomatic or even functional */ 
} 

int main(int argc, char *argv[]) 
{ 
    program_one(); 
    program_two(); 
    /* maybe given them better names */ 
    return 0; 
} 

我希望你可以從打破計劃成小塊,並在各功能做到儘量少看易讀性收益。你可能會遇到一些大功能,這些功能對於保持大型功能確實有意義,但是試圖養成將功能分解爲儘可能小的功能的習慣。任務越小,越有可能發現一些簡單的重寫可以導致更簡單的代碼。 (查看days_in_month()例程 - 它使用陣列來存儲天數並根據is_leap_year()函數的結果選擇使用哪個數組。我從The Elements of Programming Stylepage 54)得知了這一點 - 儘管它被寫入FORTRAN和PL/1,仍然是一本很好的書,無論如何,那個小例程是非常容易用一個小程序很容易測試,因爲它沒有做任何輸入和輸出本身。)

+0

好吧,我明白你來了哪裏從稍微,如果我想一起編譯這些,但我怎麼會叫他們(儘管代碼是多麼糟糕):)並再次感謝你 – 2012-04-23 21:36:59

+0

@KC:好的,我添加了一些更多的例子和一些關於合併你的程序的建議。我希望這有幫助!快樂編程。 – sarnold 2012-04-24 23:39:31

+0

確定做了一些相應的變化,希望我做了正確的 – 2012-04-30 00:18:47