请高手帮忙解决几个c++的小程序

2024-12-19 02:49:22
推荐回答(2个)
回答1:

这是我之前写的,两个写者,一个读者,用信号量PV实现的,希望对你有用
#include
#include
#include
#include
using namespace std;

#define THREADCOUNT 3

DWORD WINAPI Rand1( LPVOID lpParam);
DWORD WINAPI Rand2( LPVOID lpParam);
DWORD WINAPI GCD( LPVOID lpParam);

HANDLE mutex1;
HANDLE empty1;
HANDLE full1;

HANDLE mutex2;
HANDLE empty2;
HANDLE full2;

int var1;
int var2;

int main()
{
HANDLE aThread[THREADCOUNT];
var1 = 0;
var2 = 0;

//创建信号量
mutex1 = CreateSemaphore(NULL, 1, 1, NULL);
empty1 = CreateSemaphore(NULL, 1, 1, NULL);
full1 = CreateSemaphore(NULL, 0, 1, NULL);
mutex2 = CreateSemaphore(NULL, 1, 1, NULL);
empty2 = CreateSemaphore(NULL, 1, 1, NULL);
full2 = CreateSemaphore(NULL, 0, 1, NULL);

//创建线程
aThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Rand1, NULL, 0, NULL);
if (aThread[0] == NULL)
{
cout << "CreateThread error: " << GetLastError << endl;
}
aThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GCD, NULL, 0, NULL);
if (aThread[1] == NULL)
{
cout << "CreateThread error: " << GetLastError << endl;
}
aThread[2] = CreateThread(NULL, 0, Rand2, NULL, 0, NULL);
if (aThread[2] == NULL)
{
cout << "CreateThread error: " << GetLastError << endl;
}

WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, 100);

for(int i = 0; i < THREADCOUNT; i++ )
CloseHandle(aThread[i]);

CloseHandle(mutex1);
CloseHandle(empty1);
CloseHandle(full1);

CloseHandle(mutex2);
CloseHandle(empty2);
CloseHandle(full2);

return 0;
}

DWORD WINAPI Rand1( LPVOID lpParam)
{
BOOL bContinue=TRUE;

while (bContinue)
{
WaitForSingleObject(empty1,INFINITE);
WaitForSingleObject(mutex1, INFINITE);

srand((unsigned long)(time(NULL)));
var1 = rand();

ReleaseSemaphore(mutex1, 1, NULL);
ReleaseSemaphore(full1, 1, NULL);
}
return TRUE;
}

DWORD WINAPI Rand2( LPVOID lpParam)
{
BOOL bContinue=TRUE;

while (bContinue)
{
WaitForSingleObject(empty2,INFINITE);
WaitForSingleObject(mutex2, INFINITE);

var2 = rand();
if (var2 == 0)
{
var2++;
}

ReleaseSemaphore(mutex2, 1, NULL);
ReleaseSemaphore(full2, 1, NULL);
}
return TRUE;
}

DWORD WINAPI GCD( LPVOID lpParam)
{
BOOL bContinue=TRUE;

while (bContinue)
{
WaitForSingleObject(full1, INFINITE);
WaitForSingleObject(mutex1,INFINITE);
int a = var1;
ReleaseSemaphore(mutex1, 1, NULL);
ReleaseSemaphore(empty1, 1, NULL);

WaitForSingleObject(full2, INFINITE);
WaitForSingleObject(mutex2,INFINITE);
int b = var2;
ReleaseSemaphore(mutex2, 1, NULL);
ReleaseSemaphore(empty2, 1, NULL);

cout << a << endl;
cout << b << endl;
int remainder = 1, oldremainder = 1, temp = 0;
if (a > b)
{
remainder = a % b;
temp = b;
oldremainder = b;
}
else
{
remainder = b % a;
temp = a;
oldremainder = a;
}
while (remainder != 0)
{
oldremainder = remainder;
remainder = temp % oldremainder;
temp = oldremainder;
}
cout << oldremainder << endl;
}
return TRUE;
}

回答2:

1.以下程序的功能是将用户从键盘输入(输入‘#’时结束)的若干大写字母转换成对应的小写字母。请补充完整tocaps函数。
#include
char tocaps (char ch)
{
在此补充内容:if(ch>='A'&&ch<='Z')
ch=ch+32;
return ch;
}
main( )
{ char c ,d ;
c=getchar( );
while(c!='#')

}

2.以下程序的功能是:用户从键盘输入两个float型数据a 和 b,计算并输出a除以b的商。程序中有语法错误,请修改。

#include
float cal( float a,float b )
{
float x;
x= a/ b;
return x;
}
main( )
{ float a, b ;
scanf( " %f %f", &a, &b );
printf( " % .2f除以 %.2f 的商是:%.2f\n ", a, b, cal (a,b) );
}

3.下面程序的功能是输出结构体数组的内容。请将程序补充完整
struct
{ char name[10];
char sex;
int age ;
}p[3]=,, };
main ( )
{
在此补充内容:int i;
for(i=0;i<=2;i++)
printf("%s %c %d\n",p[i].name,p[i].sex,p[i].age);
}

4.下面程序的功能是统计8名学生中数学和英语成绩均大于等于60分的人数。请将程序补充完整。
struct st
{ int num;
float s[2];
};
int fun (struct st stu[ ] )
{

在此补充内容: int n=0,i;
for(i=0;i<=7;i++)
if(stu[i].s[0] >=60.0 && stu[i].s[1] >=60.0 ) n++;
return n;
}
main ()
,,,,,,, };
int n;
n=fun ( stu ) ;
printf ( " The number beyond 60:%-d\n " , n );
}