當前位置:首頁 » 編程軟體 » 作業編程

作業編程

發布時間: 2022-04-20 04:44:51

c語言編程作業

#include <stdio.h>

#include<string.h>

void StrReverse(char *st)

{

char s,*p1,*p2;

p1=st;

p2=st;

while(*p2)

p2++;//將指針p2移至字元串最後一個非空元素後

p2-=1;//讓指針指向最後一個非空元素

while(p1<p2)//兩個指針一前一後向中間移動,交換數組元素

{

s=*p1;

*p1=*p2;

*p2=s;

p1++;

p2--;

}

}

int main()

{

char a[80];

gets(a);

StrReverse(a);

puts(a);

}

㈡ c語言作業編程問題

下面是我給的答案,有問題的聯系我,再討論。從12點多一直做到現在,別忘了給我選成推薦答案哈。累傻了我了都

1、 楊輝三角形的每一項數據正好是組合 (即s(n!/m!/(n-m)!)的值,其中n是行數(從0行開始);m是列數(從0列開始)。 請使用上述演算法得到楊輝三角形每一個位置的值並按下圖列印。 要求用函數f計算一個正整數的階乘(用遞歸函數來實現),通過主函數調用f完成計算。

答: 下面是我的源代碼,程序輸出的部分在最後的注釋裡面

/*
* yanghui.cc
*
* Created on: 2010-6-14
* Author: LiuFeng
* Email: [email protected]
*/

#include <cstdio>
#include <iostream>

using namespace std;

int fac(int base){
if(base==1){
return 1;
}
return fac(base-1)*base;
}

int triDisplay(int** a, int row, int col){
if(row != col){
perror("must : row=col");
return (-1);
}
int sz=row;

printf("\n");
for(int i=0;i<sz;++i){
*((int*)a+i*sz+0)=1;
*((int*)a+i*sz+i)=1;
}

for(int n=2;n<sz;++n)
for(int m=1;m<n;++m){
*((int*)a+n*sz+m) = fac(n)/fac(m)/fac(n-m);
}

for(int n=0;n<sz;++n){
for(int m=0;m<=n;++m){
printf("%5d", *((int*)a+n*sz+m) );
}
printf("\n");
}
return 0;

}

int
main(void){
int data[10][10];
int size=10;

if(triDisplay((int**)data,size,size)<0){
perror("bad mtria");
exit(-2);
}
return 0;
}

/*
[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc
[Administrator@ /<7>06/14]$ ./tri.exe

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

*/

2. 編寫一個函數,要求對n個學生的成績進行排序,要求用數組名作函數參數。在數組a中存放了10個學生某門課程的成績,調用上述函數,實現對10個學生的成績排序。

答: 程序的源代碼如下,程序的輸出在最後面的注釋裡面

/*
* DcSort.cc
*
* Created on: 2010-6-14
* Author: LiuFeng
* Email: [email protected]
*
* g++ -g -O3 DcSort.cc -Wall -o dsort
*/

#include <cstdio>
#include <cstring>
#include <cstdlib>

typedef struct
{
char _sname[30];
double _sscore;
} stu;

int
Partition(stu * stus, int l, int h)
{
stu pivot = stus[l];

while(l<h){
while(l<h && ((stus[h]._sscore) >= (pivot._sscore))) h--;
if(l<h) stus[l++]=stus[h];
while(l<h && ((stus[l]._sscore) <= (pivot._sscore))) l++;
if(l<h) stus[h--]=stus[l];
}
stus[l]=pivot;

return l;
}

void DcSort(stu* stus, int low, int high){
int pivotpos;
if(low<high){
pivotpos=Partition(stus,low,high);

DcSort(stus,low,pivotpos-1);
DcSort(stus,pivotpos+1,high);
}
}

int main(void){
stu s[10]={
{"zhao",60},
{"qian",40},
{"sun",80},
{"li",90},
{"zhou",70},
{"wu",50},
{"zheng",80},
{"jiang",90},
{"shen",100},
{"han",80},
};

for(int i=0; i<10;++i){
printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);
}

DcSort(s,0,10);

printf("\n===================after sorted ==================\n");

for(int i=0; i<10;++i){
printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);
}

return 0;
}

/*

輸出結果:
[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort
[Administrator@ ~/<1>preInterView/SortAndFind]$ clear
[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe
student: name: zhao, score: 60.000000
student: name: qian, score: 40.000000
student: name: sun, score: 80.000000
student: name: li, score: 90.000000
student: name: zhou, score: 70.000000
student: name: wu, score: 50.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: shen, score: 100.000000
student: name: han, score: 80.000000

===================after sorted ==================
student: name: qian, score: 40.000000
student: name: wu, score: 50.000000
student: name: zhao, score: 60.000000
student: name: zhou, score: 70.000000
student: name: han, score: 80.000000
student: name: sun, score: 80.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: li, score: 90.000000
student: name: shen, score: 100.000000
[Administrator@ ~/<1>preInterView/SortAndFind]$

*/

三、程序運行結果示例:

第一題:
/*
[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc
[Administrator@ /<7>06/14]$ ./tri.exe

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

*/

第二題:

/*

輸出結果:
[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort
[Administrator@ ~/<1>preInterView/SortAndFind]$ clear
[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe
student: name: zhao, score: 60.000000
student: name: qian, score: 40.000000
student: name: sun, score: 80.000000
student: name: li, score: 90.000000
student: name: zhou, score: 70.000000
student: name: wu, score: 50.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: shen, score: 100.000000
student: name: han, score: 80.000000

===================after sorted ==================
student: name: qian, score: 40.000000
student: name: wu, score: 50.000000
student: name: zhao, score: 60.000000
student: name: zhou, score: 70.000000
student: name: han, score: 80.000000
student: name: sun, score: 80.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: li, score: 90.000000
student: name: shen, score: 100.000000
[Administrator@ ~/<1>preInterView/SortAndFind]$

*/

二、實驗內容:按題目要求完成程序的改錯、調試、填空和編寫。

1、以下程序中,main函數通過調用fun()函數統計整數序列中的負數的個數以及平均值。
本題約定平均值由函數返回,負數的個數由參數返回。程序有若干錯誤,請先閱讀程序,找出其中的錯誤行,
並寫出出錯的原因,最後上機調試該程序驗證自己的預測。

#1 double aver(int a[], int n, int *p) // p所謂返回的參數之一,應該使用二級指針,或者指針的引用;
#2 { int i,sum=0 ; // 這里的*p指向了函數內部的臨時變數,且該變數在函數結束時,
// 會同時被系統從棧上釋放掉, 那麼你在main函數中讀取p指向的地址,得到的
// 就是一個不可以預期的值了,也就是「野指針」
// 使用二級指針或者指針的引用,是可以將內存在函數體內外帶入帶出的,但是
// 注意要在main中先申請好
#3 *p=0 ;
#4 for(i=0;i<n;i++)
#5 { sum=sum+a[i] ;
#6 if(a[i]<0) *p++;
#7 }
#8 return sum/n;
#9 }
#10 #include "stdio.h"
#11 main()
#12 { int count,x[]={0,12,33,-9,-5,27,80,0,54,63};
#13 double av;
#14 av=aver(x,10,count);
#15 printf("count: %d\naverage: %.2f\n",count,av);
#16 }

2、設有如下結構定義,且struct link為鏈表的結點類型,由該結構類型創建的鏈表中的a結點已被指針p指向(見下圖),請完成下面的操作:

struct link
{ int score;
struct link *next;
}*p,*q;

(1) 寫出刪除b結點(包括釋放其存儲空間)的語句序列(允許藉助於q指針),但要求鏈表的連續結構不能破壞,不能移動p指針。

答:

void
deleteNode(link* p, link* q, int b)
{
q = p;
int len=0;

while(q->next)++len;

if(len < b) {perror("You input a overflow position"); exit(-1);}

q=p;
while(--b){ // move to b
q=q->next;
}

while(q->next) { // overwrite the prenode,one by one
q->score=q->>next->score;
}

free(q) // q point to the last node;
}

(2) 閱讀下面程序說明,按注釋提示,在劃線處補充細節,使程序達到預期功能。 [程序說明]以下程序中,函數create的功能是創建一個結點類型為struct link的學生成績鏈表,main函數中,首先調用create函數創建一個包含N個結點的成績鏈表,然後調用問題(1)的演算法,將鏈表的第2個結點刪除掉,要求輸出結點刪除前、後鏈表的內容,以驗證問題(1)演算法的正確性。

#include <stdio.h>
#define N 5
#define L sizeof(struct link)
struct link
{ int score; /* 成績 */
(1);/* 定義結點的指針域next */
};

struct link *create(void)
{
struct link *head,*p;
int i;
head=NULL;
printf("Input %d records\n",N);
for(i=1;i<=N;i++)
{
p=(2) ; /* 創建一個動態結點 */
scanf("%d",&p->score);
(3); /* 新結點進棧 */
head=p;
}
return (4) ; /* 返回所創建鏈表的頭指針 */
}

void printlist(struct link *head) /* 輸出鏈表 */
{ struct link *p;
p=head;
while(p!=NULL)
{ printf("%d,",p->score);
(5) ; /* 使p後移一個結點 */
}
printf("\n");
}

int
main( void)
{ struct link *base,*new;
(6) ; /* 調用create函數,創建鏈表 */
printlist(base); /* 輸出原始鏈表 */
/* 藉助於new指針刪除第2個結點 */

(8) ; /* 輸出結點刪除以後的鏈表內容 */
printf("ok!\n");
}

(2)答:
【 (1) link * next; 】
【 (2) p= (link*)malloc(sizeof(struct link)); 】
【 (3) p->next = head 】
【 (4) head 】
【 (5) p=p->next 】
【 (6) base = create(); 】
【 (7) deleteNode(base, new, int b); // 利用我們上一問中,寫好的函數 】
【 (8) printlist(base);】

㈢ C語言編程作業,急!!!!

#include"stdio.h"
#include<string.h>
structtel{
charname[11],num[11];
};
intmain(intargc,char*argv[]){
structtels[50];
intn,i;
chart[]="############";
printf("Pleaseentern(int0<n<51)... ");
if(scanf("%d",&n)!=1||n<1||n>50){
printf("Inputerror,exit... ");
return0;
}
printf("Entersomenameandtel-number(by'','Enter'end)... ");
for(i=0;i<n;i++)
scanf("%10s%*[^0-9]%10s",s[i].name,s[i].num);
printf("------------------------ ");
for(i=0;i<n;i++)
printf("%.*s%s%.*s%s ",12-strlen(s[i].name),t,s[i].name,12-strlen(s[i].num),t,s[i].num);
return0;
}

運行樣例:

㈣ C++編程作業

struct STRU_SDTMSG
{
cstring sno;
cstring sname;
bool sgender;
int syear;
STRU_SDTMSG()
{
sno = "";
sname = "";
sgender = true;
syear = 0;
}
}
cstring 這種寫法可以換成 char sno[15];
構造函數不要也可以;

㈤ C語言編程作業,求解答

作業1:result(int)= c (char) * i (int) + f (float) / d (double) -(f + i);

第一步:f+i, 一個float和int相加,按精度高的float進行計算,結果為float

result = char * int + float/double - float;

第二步:char * int, 他們都是整數相加為int

result =int + float/double - float;

第三步:float/double,按精度高的double進行計算結果為double

result =int + double - float;

第四步:int + double 結果為double

result =double - float;

第五步:double - float 結果為double,result為整形,賦值給整形會強制把double轉化成int,保留整數。


作業二:

a=3, b=5


原因,swap的形參是值傳遞,實參傳值給形參,子函數無法改變實參的值;要改變得傳址。


作業三:

順序查找法適應性好,可以適用在無序和有序數組查找;

折半查找法只適用於有序數組,無序查找會失敗;但是在有序數組查找時查找效率高於順序查找。

㈥ 大一C語言作業. 編寫程序輸入兩個整數,輸出它們的商和余數。

#include<stdio.h>

int main()

{

int a,b,c,d=0;

scanf("%d %d",&a,&b);

c=a/b;

d=a%b;

printf("商是:%d ",c);

printf("余數是:%d ",d);

return 0;

}

運行可用,輸入用空格分分開兩個數

比如輸入:5 3後回車

輸出:

商是:1

余數是:2

㈦ 簡單的編程作業!急!!!

#include<stdio.h>
#include<time.h>

intmain()
{
intget_age(intbirth[]);
charid[19];
intbirth[8],age=0;
printf("輸入身份證:");
scanf("%s",id);
for(inti=0;i<8;i++){
birth[i]=id[6+i]-'0';
}
printf("年齡為:%d",get_age(birth));
return0;
}

intget_age(intbirth[]){
intage=0;
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);

age+=1900+p->tm_year-(birth[0]*1000+birth[1]*100+birth[2]*10+birth[3]);
inttmp=1+p->tm_mon-(birth[4]*10+birth[5]);
if(tmp==0){
if((p->tm_mday-(birth[6]*10+birth[7]))<0){
age--;
}
}elseif(tmp<0){
age--;
}
returnage;
}

㈧ C語言作業,編程實現兩個整數的算數運算(加減乘除余)

#include<stdio.h>
main()
{
int a,b;
printf("Please input data a,b");
scanf("%d%d",&a,&b);
printf("\na+b=%d",a+b);
printf("\na-b=%d",a-b);
printf("\na*b=%d",a*b);
printf("\na/b=%d",a/b);
printf("\na%b=%d",a%b);
getch();
}

㈨ 編程作業怎麼做!

不知道你問的是哪一題

假設是其中最清楚的那一題,如555555最大的三位數

其實這題很簡單,如果不考慮性能的話,直接從100開始遍歷,定義兩個局部變數
假設是int a與int b
弄一個循環,a從100開始,然後b從1開始

a×b如果等於55555,就記錄在一個temp變數一面,如果遇到下一個a×b等於555555的組合,就跟temp比,如果比temp記錄的數大,就用大的替換掉temp裡面小的那一個。

如果a×b大於55555,立即結束本次循環,a+1,跳入下一次循環。

其實還有更簡便的方法。。。不過用到一些復雜的數學公式就不說了。

如果要簡單粗暴的話,直接開多線程去找,一個線程負責一個區間,比方說100-200,200-300一直到5555500到555555,找到滿足a×b=555555的數,而且每次a+5,(因為555555末尾是5,其中一個乘數必定是5,所以以5作為每次循環的步長應該能提升5倍性能)

總之怎麼簡單粗暴怎麼來。

㈩ VB作業 編程


Private Sub form_Click()
Dim b, c, d, sxh As Integer
For i = 100 To 999
b = i \ 100
c = (i - b * 100) \ 10
d = i - b * 100 - c * 10
sxh = b ^ 3 + c ^ 3 + d ^ 3
If sxh = i Then
Print sxh;
End If
Next i
End Sub
二、不知道你要求的是什麼的值!

Private Sub form_Click()
Dim n, s As Long
n = 1: s = 0
For i = 1 To 10
n = n * i
s = s + n
Next i
Print s
End Sub

熱點內容
安卓耳機接頭叫什麼 發布:2025-03-28 20:18:29 瀏覽:735
uc瀏覽器緩存視頻在哪裡 發布:2025-03-28 20:17:37 瀏覽:927
挖蜂巢解壓 發布:2025-03-28 20:12:17 瀏覽:276
陌陌的自定義密碼是多少 發布:2025-03-28 20:11:27 瀏覽:692
mfc創建文件夾 發布:2025-03-28 20:09:47 瀏覽:70
活體細胞編程 發布:2025-03-28 20:09:45 瀏覽:757
sdk存儲 發布:2025-03-28 19:50:44 瀏覽:947
手機壓縮視頻怎麼壓縮 發布:2025-03-28 19:49:58 瀏覽:950
超級訪問李宇春 發布:2025-03-28 19:49:48 瀏覽:226
高訪問攻擊 發布:2025-03-28 19:47:24 瀏覽:10