2014-03-28 109 views
-2
#include "stdafx.h" 
#include <iostream> 
#include <string>            
#include <cstring>            
#include <cmath>             
#include <iomanip>            
#include <fstream>            
#include <cassert> 
#include <cstdlib>            
#include <ctime>             
#include <cctype>   

using namespace std; 


void printA(int i, int a, int setA[]); 
void printB(int i, int b, int setB[]); 
int printUnion(int setA[], int setB[], int setUnion[], int a, int b); 
void sort(int array[], int x); 
int printIntersection(int setA[], int setB[], int setIntersection[], int a, int b, int k); 

int main() 
{ 
ifstream infile;            //Input file 
ofstream outfile; 
infile.open("c:\\Temp\\setsA.dat");        //checks for file validity 
if (!infile) 
{ 
    cout<<"Cannot open the input file." 
     <<"This program will end."<<endl; 
    system("PAUSE"); 
    return 1; 
} 




cout<<"This program will compute the set cardinality."<<endl; 
cout<<"Select from the following option: "<<endl; 
cout<<"1 Compute and Output Set A Cardinality"<<endl; 
cout<<"2 Compute and Output Set B Cardinality"<<endl; 
cout<<"3 Compute and Output Cardinality of A's union with B"<<endl; 
cout<<"4 Quit"<<endl; 

int setA[30]; 
int setB[30]; 
int setUnion[30]; 
int setIntersection[30]; 
int a = 0; 
int b = 0; 
int i = 0; 
int k = 0; 
while (!infile.eof()) 
    {   
     infile>>setA[a]; 
     if(infile.eof()) 
      break; 
     a++;     
    } 
infile.close(); 
sort(setA, a); 

infile.open("C:\\Temp\\setsB.dat"); 
if (!infile) 
{ 
    cout<<"Cannot open the input file." 
    <<"This program will end."<<endl; 
    system("PAUSE"); 
    return 1; 
} 

while (!infile.eof()) 
    {   
     infile>>setB[b]; 
     if(infile.eof()) 
      break; 
     b++;     
    } 
sort(setB, b); 
infile.close(); 


outfile.open("c:\\Temp\\outset.dat", ios::app); 
int option; 
cin>>option; 
while(option!=4) 
{ 
    if (option == 1) 
    { 
     printA(i, a, setA); 
     outfile<<"Cardinality of Set A is "<<a<<endl; 
    } 

    else if (option == 2) 
    { 
     printB(i, b, setB); 
     outfile<<"Cardinality of Set B is "<<b<<endl; 
    } 

    else if (option == 3) 
    { 
     printUnion(setA, setB, setUnion, a, b); 
     printIntersection(setA,setB,setIntersection,a,b,k); 
     outfile<<"Cardinality of Set A's union with Set B is "<<a+b-k<<endl; 
    } 

     cin>>option; 
} 


outfile.close(); 


system("PAUSE"); 
return 0; 

} 


void printA(int i, int a, int setA[]) 
{ 
cout<<"Set A : {"; 
for(i=0;i<a;i++) 
{ 
    if(i > 0) 
     cout<<","; 
     cout<<setA[i];   
} 
cout<<"}"<<endl; 
cout<<"Cardinality of set A is "<<a<<endl; 

} 

void printB(int i, int b, int setB[]) 
{ 
cout<<"Set B : {"; 
for(i=0;i<b;i++) 
{ 
    if(i > 0) 
     cout<<","; 
     cout<<setB[i];  
} 
cout<<"}"<<endl; 
cout<<"Cardinality of set B is "<<b<<endl; 
} 

int printUnion(int setA[], int setB[], int setUnion[], int a, int b) 
{ 
int i = 0; 
int j = 0; 
int u = 0; 
while ((i < a) && (j < b)) 
{ 
    if (setA[i] < setB[j]) 
    { 
     setUnion[u] = setA[i]; 
     i++; 
     u++; 
    } 

    else if (setA[i] > setB[j]) 
    { 
     setUnion[u] = setB[j]; 
     j++; 
     u++; 
    } 

    else 
    { 
     setUnion[u] = setA[i]; 
     i++; 
     j++; 
     u++; 
    } 
} 

if (i == a) 
{ 
    while (j < b) 
    { 
     setUnion[u] = setB[j]; 
     j++; 
     u++; 
    } 
} 

else 
{ 
    while (i < a) 
    { 
     setUnion[u] = setA[i]; 
     i++; 
     u++; 
    } 
} 
cout<<"Cardinality of union set is "<<u<<endl; 
return 0; 

} 

int printIntersection(int setA[], int setB[], int setIntersection[], int a, int b, int k) { 
int i = 0; 
int j = 0; 

while(i < a && j < b) 
{ 
    if(setA[i] < setB[j]) 
    { 
     i++; 
    } 

    else if(setA[i] > setB[j]) 
    { 
     j++; 
    } 

    else if (setA[i] == setB[j]) 
    { 
     setIntersection[k] = setA[i]; 
     i++; 
     j++; 
     k++; 
    }  
} 
     cout<<"Cardinality of intersection is "<<k<<endl; 
     return 0; 
} 

void sort(int array[], int x) 

{ 
int i, j, temp, swapping; 
for (i = 1; i < x; i++) 

{ 
    swapping = 0; 
    for (j = 0; j < x-i; j++) 
    { 
     if (array[j] > array[j+1]) 
     { 
      temp = array[j]; 
      array[j] = array[j + 1]; 
      array[j + 1] = temp; 
      swapping = 1; 
     } 
    } 

    if (swapping == 0) 
    { 
     break; 
    } 
} 

} 

這是我的程序代碼。我必須打印個別集合的基數和兩個集合的聯合。在我的函數printIntersection中,我將「k」設置爲變量來保存交集的基數。當我在switch語句的情況3下主要調用k時,它仍然是0.我認爲它是從main而不是函數printIntersection中獲取k的。我如何在switch語句中設置和使用「k」,以便它能給我正確的值?C++變量問題

+0

'k'是'main()'和另一個函數中的局部變量。他們只是碰巧有相同的名字,但是完全不相關......如果你想做你所說的話,你可能想看看指針和/或引用。 – vanza

+2

這個長代碼真的與你的問題有關嗎?你能挑出只有相關的,可能是短一點的代碼? – deeiip

+0

main中的變量k只分配給零。此後從未改變。正如下面所指出的那樣,如果你想要'printIntersection'函數(其中k通過值傳遞)來更新在main中聲明的k,那麼修改printIntersection方法爲@ user3434680在答案中描述。 –

回答

1

「k」沒有被更新的原因是因爲你正在通過價值傳遞它。函數退出後,對函數中的「k」所做的任何更改都會丟失。

如果更改

int printIntersection(int setA[], int setB[], int setIntersection[], int a, int b, int k) { 

int printIntersection(int setA[], int setB[], int setIntersection[], int a, int b, int &k) { 

現在,您將通過引用傳遞「K」到printIntersection,而不是價值,你的更改將保留,即使功能後退出

+0

謝謝@ user3434680。那完美的工作。而且我也將k的聲明移到while循環中,因爲如果每次選擇相同的選項,k的值都在變化。 –